Network behaviour tweaks

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

View File

@ -61,11 +61,19 @@ 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:
data = self.s.recv(self.next_message_size - len(self.buffer_receive)) if self.status == 'fully_established':
self.buffer_receive += data 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))
logging.debug('Received {} bytes from {}:{}'.format(len(data), self.host, self.port))
self.buffer_receive += data
except ssl.SSLWantReadError: except ssl.SSLWantReadError:
pass pass
except socket.error as e: except socket.error as e:
@ -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,11 +124,13 @@ class Connection(threading.Thread):
self.status = 'failed' self.status = 'failed'
def _send_data(self): def _send_data(self):
try: if self.buffer_send:
amount = self.s.send(self.buffer_send[:1000]) try:
self.buffer_send = self.buffer_send[amount:] amount = self.s.send(self.buffer_send)
except (BlockingIOError, ssl.SSLWantWriteError): self.buffer_send = self.buffer_send[amount:]
pass logging.debug('Sent {} bytes to {}:{}'.format(amount, self.host, self.port))
except (BlockingIOError, ssl.SSLWantWriteError):
pass
def _do_tls_handshake(self): def _do_tls_handshake(self):
logging.debug('Initializing TLS connection with {}:{}'.format(self.host, self.port)) logging.debug('Initializing TLS connection with {}:{}'.format(self.host, self.port))