Add sock paramater to Handler callbacks

As Handler objects can have multiple sockets, we need to know which one
was involved in the incoming event.
This commit is contained in:
Brian May 2015-11-24 12:19:31 +11:00
parent c1083e983f
commit 71d46d77bf
3 changed files with 19 additions and 19 deletions

View File

@ -106,16 +106,18 @@ class MultiListener:
self.v4.setsockopt(level, optname, value) self.v4.setsockopt(level, optname, value)
def add_handler(self, handlers, callback, method, mux): def add_handler(self, handlers, callback, method, mux):
socks = []
if self.v6: if self.v6:
handlers.append( socks.append(self.v6)
Handler(
[self.v6],
lambda: callback(self.v6, method, mux, handlers)))
if self.v4: if self.v4:
handlers.append( socks.append(self.v4)
Handler(
[self.v4], handlers.append(
lambda: callback(self.v4, method, mux, handlers))) Handler(
socks,
lambda sock: callback(sock, method, mux, handlers)
)
)
def listen(self, backlog): def listen(self, backlog):
if self.v6: if self.v6:
@ -480,7 +482,6 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
ssnet.runonce(handlers, mux) ssnet.runonce(handlers, mux)
if latency_control: if latency_control:
mux.check_fullness() mux.check_fullness()
mux.callback()
def main(listenip_v6, listenip_v4, def main(listenip_v6, listenip_v4,

View File

@ -156,9 +156,9 @@ class DnsProxy(Handler):
log('DNS send to %r: %s\n' % (self.peer, e)) log('DNS send to %r: %s\n' % (self.peer, e))
return return
def callback(self): def callback(self, sock):
try: try:
data = self.sock.recv(4096) data = sock.recv(4096)
except socket.error as e: except socket.error as e:
if e.args[0] in ssnet.NET_ERRS: if e.args[0] in ssnet.NET_ERRS:
# might have been spurious; try again. # might have been spurious; try again.
@ -195,9 +195,9 @@ class UdpProxy(Handler):
log('UDP send to %r port %d: %s\n' % (dstip[0], dstip[1], e)) log('UDP send to %r port %d: %s\n' % (dstip[0], dstip[1], e))
return return
def callback(self): def callback(self, sock):
try: try:
data, peer = self.sock.recvfrom(4096) data, peer = sock.recvfrom(4096)
except socket.error as e: except socket.error as e:
log('UDP recv from %r port %d: %s\n' % (peer[0], peer[1], e)) log('UDP recv from %r port %d: %s\n' % (peer[0], peer[1], e))
return return
@ -236,7 +236,7 @@ def main(latency_control):
hw = Hostwatch() hw = Hostwatch()
hw.leftover = '' hw.leftover = ''
def hostwatch_ready(): def hostwatch_ready(sock):
assert(hw.pid) assert(hw.pid)
content = hw.sock.recv(4096) content = hw.sock.recv(4096)
if content: if content:
@ -314,7 +314,6 @@ def main(latency_control):
ssnet.runonce(handlers, mux) ssnet.runonce(handlers, mux)
if latency_control: if latency_control:
mux.check_fullness() mux.check_fullness()
mux.callback()
if dnshandlers: if dnshandlers:
now = time.time() now = time.time()

View File

@ -260,7 +260,7 @@ class Handler:
for i in self.socks: for i in self.socks:
_add(r, i) _add(r, i)
def callback(self): def callback(self, sock):
log('--no callback defined-- %r\n' % self) log('--no callback defined-- %r\n' % self)
(r, w, x) = select.select(self.socks, [], [], 0) (r, w, x) = select.select(self.socks, [], [], 0)
for s in r: for s in r:
@ -301,7 +301,7 @@ class Proxy(Handler):
elif not self.wrap2.shut_read: elif not self.wrap2.shut_read:
_add(r, self.wrap2.rsock) _add(r, self.wrap2.rsock)
def callback(self): def callback(self, sock):
self.wrap1.try_connect() self.wrap1.try_connect()
self.wrap2.try_connect() self.wrap2.try_connect()
self.wrap1.fill() self.wrap1.fill()
@ -467,7 +467,7 @@ class Mux(Handler):
if self.outbuf: if self.outbuf:
_add(w, self.wsock) _add(w, self.wsock)
def callback(self): def callback(self, sock):
(r, w, x) = select.select([self.rsock], [self.wsock], [], 0) (r, w, x) = select.select([self.rsock], [self.wsock], [], 0)
if self.rsock in r: if self.rsock in r:
self.handle() self.handle()
@ -572,7 +572,7 @@ def runonce(handlers, mux):
for h in handlers: for h in handlers:
for s in h.socks: for s in h.socks:
if s in ready: if s in ready:
h.callback() h.callback(s)
did[s] = 1 did[s] = 1
for s in ready: for s in ready:
if s not in did: if s not in did: