Improved the close of the proxy thread (and the socket) in the xep_0065 plugin.
This commit is contained in:
parent
2f38857681
commit
2cd936318d
@ -184,33 +184,39 @@ class xep_0065(base_plugin):
|
|||||||
# Send the IQ.
|
# Send the IQ.
|
||||||
act_iq.send()
|
act_iq.send()
|
||||||
|
|
||||||
def send(self, sid, msg):
|
def deactivate(self, sid):
|
||||||
""" Sends the msg to the socket.
|
""" Closes the Proxy thread associated to this SID.
|
||||||
|
|
||||||
sid : The SID to retrieve the good proxy stored in the
|
|
||||||
proxy_threads dict
|
|
||||||
msg : The message data.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proxy = self.proxy_threads.get(sid)
|
proxy = self.proxy_threads.get(sid)
|
||||||
if proxy:
|
if proxy:
|
||||||
proxy.send(msg)
|
proxy.s.close()
|
||||||
else:
|
del self.proxy_threads[sid]
|
||||||
# TODO: raise an exception.
|
|
||||||
pass
|
|
||||||
|
|
||||||
def on_recv(self, sid, data):
|
def close(self):
|
||||||
""" Called when receiving data
|
""" Closes all Proxy threads.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not data:
|
for sid, proxy in self.proxy_threads.items():
|
||||||
try:
|
proxy.s.close()
|
||||||
del self.proxy_threads[sid]
|
del self.proxy_threads[sid]
|
||||||
except KeyError:
|
|
||||||
# TODO: internal error, raise an exception ?
|
def send(self, sid, data):
|
||||||
pass
|
""" Sends the data over the Proxy socket associated to the
|
||||||
else:
|
SID.
|
||||||
log.debug('Received data: %s' % data)
|
"""
|
||||||
|
|
||||||
|
proxy = self.proxy_threads.get(sid)
|
||||||
|
if proxy:
|
||||||
|
proxy.s.sendall(data)
|
||||||
|
|
||||||
|
def on_recv(self, sid, data):
|
||||||
|
""" Calls when data is recv from the Proxy socket associated
|
||||||
|
to the SID.
|
||||||
|
"""
|
||||||
|
|
||||||
|
proxy = self.proxy_threads.get(sid)
|
||||||
|
if proxy:
|
||||||
self.xmpp.event('socks_recv', data)
|
self.xmpp.event('socks_recv', data)
|
||||||
|
|
||||||
|
|
||||||
@ -275,10 +281,10 @@ class Proxy(Thread):
|
|||||||
log.info('Socket connected.')
|
log.info('Socket connected.')
|
||||||
self.connected.set()
|
self.connected.set()
|
||||||
|
|
||||||
# Listen for data on the socket
|
# Blocks until the socket need to be closed.
|
||||||
self.listen()
|
self.listen()
|
||||||
|
|
||||||
# Listen returns when the socket must be closed.
|
# Closes the socket.
|
||||||
self.s.close()
|
self.s.close()
|
||||||
log.info('Socket closed.')
|
log.info('Socket closed.')
|
||||||
|
|
||||||
@ -289,7 +295,16 @@ class Proxy(Thread):
|
|||||||
|
|
||||||
socket_open = True
|
socket_open = True
|
||||||
while socket_open:
|
while socket_open:
|
||||||
ins, out, err = select([self.s, ], [], [])
|
ins = []
|
||||||
|
try:
|
||||||
|
# Wait any read available data on socket. Timeout
|
||||||
|
# after 5 secs.
|
||||||
|
ins, out, err = select([self.s, ], [], [], 5)
|
||||||
|
except Exception, e:
|
||||||
|
# There's an error with the socket (maybe the socket
|
||||||
|
# has been closed and the file descriptor is bad).
|
||||||
|
log.debug('Socket error: %s' % e)
|
||||||
|
break
|
||||||
|
|
||||||
for s in ins:
|
for s in ins:
|
||||||
data = self.recv_size(self.s)
|
data = self.recv_size(self.s)
|
||||||
@ -324,9 +339,3 @@ class Proxy(Thread):
|
|||||||
total_data.append(sock_data)
|
total_data.append(sock_data)
|
||||||
total_len = sum([len(i) for i in total_data])
|
total_len = sum([len(i) for i in total_data])
|
||||||
return ''.join(total_data)
|
return ''.join(total_data)
|
||||||
|
|
||||||
def send(self, msg):
|
|
||||||
""" Sends the data over the socket.
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.s.sendall(msg)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user