Rework object sending logic
This commit is contained in:
parent
eb0f6bca22
commit
d4f5d370d4
|
@ -22,6 +22,7 @@ class Connection(threading.Thread):
|
||||||
self.send_queue = queue.Queue()
|
self.send_queue = queue.Queue()
|
||||||
|
|
||||||
self.vectors_to_get = set()
|
self.vectors_to_get = set()
|
||||||
|
self.vectors_to_send = set()
|
||||||
|
|
||||||
self.status = 'ready'
|
self.status = 'ready'
|
||||||
|
|
||||||
|
@ -76,11 +77,13 @@ class Connection(threading.Thread):
|
||||||
except ssl.SSLWantReadError:
|
except ssl.SSLWantReadError:
|
||||||
if self.status == 'fully_established':
|
if self.status == 'fully_established':
|
||||||
self._request_objects()
|
self._request_objects()
|
||||||
|
self._send_objects()
|
||||||
except socket.error as e:
|
except socket.error as e:
|
||||||
err = e.args[0]
|
err = e.args[0]
|
||||||
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
|
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
|
||||||
if self.status == 'fully_established':
|
if self.status == 'fully_established':
|
||||||
self._request_objects()
|
self._request_objects()
|
||||||
|
self._send_objects()
|
||||||
else:
|
else:
|
||||||
logging.debug('Disconnecting from {}:{}. Reason: {}'.format(self.host, self.port, e))
|
logging.debug('Disconnecting from {}:{}. Reason: {}'.format(self.host, self.port, e))
|
||||||
data = None
|
data = None
|
||||||
|
@ -270,9 +273,7 @@ class Connection(threading.Thread):
|
||||||
elif m.command == b'getdata':
|
elif m.command == b'getdata':
|
||||||
getdata = message.GetData.from_message(m)
|
getdata = message.GetData.from_message(m)
|
||||||
logging.debug('{}:{} -> {}'.format(self.host, self.port, getdata))
|
logging.debug('{}:{} -> {}'.format(self.host, self.port, getdata))
|
||||||
for vector in getdata.vectors:
|
self.vectors_to_send.update(getdata.vectors)
|
||||||
if vector in shared.objects:
|
|
||||||
self.send_queue.put(message.Message(b'object', shared.objects[vector].to_bytes()))
|
|
||||||
elif m.command == b'addr':
|
elif m.command == b'addr':
|
||||||
addr = message.Addr.from_message(m)
|
addr = message.Addr.from_message(m)
|
||||||
logging.debug('{}:{} -> {}'.format(self.host, self.port, addr))
|
logging.debug('{}:{} -> {}'.format(self.host, self.port, addr))
|
||||||
|
@ -295,3 +296,17 @@ class Connection(threading.Thread):
|
||||||
else:
|
else:
|
||||||
self.send_queue.put(message.GetData(self.vectors_to_get))
|
self.send_queue.put(message.GetData(self.vectors_to_get))
|
||||||
self.vectors_to_get.clear()
|
self.vectors_to_get.clear()
|
||||||
|
|
||||||
|
def _send_objects(self):
|
||||||
|
if self.vectors_to_send:
|
||||||
|
if len(self.vectors_to_send) > 16:
|
||||||
|
to_send = random.sample(self.vectors_to_send, 16)
|
||||||
|
self.vectors_to_send.difference_update(to_send)
|
||||||
|
else:
|
||||||
|
to_send = self.vectors_to_send.copy()
|
||||||
|
self.vectors_to_send.clear()
|
||||||
|
with shared.objects_lock:
|
||||||
|
for vector in to_send:
|
||||||
|
obj = shared.objects.get(vector, None)
|
||||||
|
if obj:
|
||||||
|
self.send_queue.put(message.Message(b'object', obj.to_bytes()))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user