Network behaviour tweaks

This commit is contained in:
TheKysek 2016-08-17 17:51:04 +02:00
parent a815fb2f7c
commit dc75f93e1f

View File

@ -61,10 +61,18 @@ class Connection(threading.Thread):
if not self.server: if not self.server:
self.send_queue.put(message.Version(self.host, self.port)) self.send_queue.put(message.Version(self.host, self.port))
while True: while True:
time.sleep(0.3) if self.on_connection_fully_established_scheduled and not (self.buffer_send or self.buffer_receive):
self._on_connection_fully_established()
data = True data = True
try: try:
if self.status == 'fully_established':
data = self.s.recv(4096)
self.buffer_receive += data
logging.debug('Received {} bytes from {}:{}'.format(len(data), self.host, self.port))
continue
else:
data = self.s.recv(self.next_message_size - len(self.buffer_receive)) data = self.s.recv(self.next_message_size - len(self.buffer_receive))
logging.debug('Received {} bytes from {}:{}'.format(len(data), self.host, self.port))
self.buffer_receive += data self.buffer_receive += data
except ssl.SSLWantReadError: except ssl.SSLWantReadError:
pass pass
@ -93,8 +101,6 @@ class Connection(threading.Thread):
data = None data = None
if time.time() - self.last_message_sent > 300 and self.status == 'fully_established': if time.time() - self.last_message_sent > 300 and self.status == 'fully_established':
self.send_queue.put(message.Message(b'pong', b'')) self.send_queue.put(message.Message(b'pong', b''))
if self.on_connection_fully_established_scheduled and not (self.buffer_send or self.buffer_receive):
self._on_connection_fully_established()
if self.status == 'disconnecting': if self.status == 'disconnecting':
data = None data = None
if not data: if not data:
@ -102,6 +108,7 @@ class Connection(threading.Thread):
self.s.close() self.s.close()
logging.info('Disconnected from {}:{}'.format(self.host, self.port)) logging.info('Disconnected from {}:{}'.format(self.host, self.port))
break break
time.sleep(0.2)
def _connect(self): def _connect(self):
logging.info('Connecting to {}:{}'.format(self.host, self.port)) logging.info('Connecting to {}:{}'.format(self.host, self.port))
@ -117,9 +124,11 @@ class Connection(threading.Thread):
self.status = 'failed' self.status = 'failed'
def _send_data(self): def _send_data(self):
if self.buffer_send:
try: try:
amount = self.s.send(self.buffer_send[:1000]) amount = self.s.send(self.buffer_send)
self.buffer_send = self.buffer_send[amount:] self.buffer_send = self.buffer_send[amount:]
logging.debug('Sent {} bytes to {}:{}'.format(amount, self.host, self.port))
except (BlockingIOError, ssl.SSLWantWriteError): except (BlockingIOError, ssl.SSLWantWriteError):
pass pass