Fixed ssl transmission

This commit is contained in:
Davte 2020-04-10 10:18:24 +02:00
parent f7a9f76aad
commit 685b4e6756
2 changed files with 19 additions and 22 deletions

View File

@ -72,17 +72,14 @@ class Client:
output_data = file_to_send.read(self.buffer_chunk_size)
if not output_data:
break
writer.write(output_data)
try:
writer.write(output_data)
await writer.drain()
except ConnectionResetError:
logging.info('Server closed the connection.')
self.stop()
break
else:
# If transmission has succeeded, write end of file
writer.write_eof()
await writer.drain()
writer.close()
return
async def run_receiving_client(self, file_path='~/input.txt'):
@ -100,10 +97,8 @@ class Client:
with open(self.file_path, 'wb') as file_to_receive:
while not self.stopping:
input_data = await reader.read(self.buffer_chunk_size)
if reader.at_eof():
break
if not input_data:
continue
break
file_to_receive.write(input_data)
def stop(self, *_):

View File

@ -15,7 +15,6 @@ class Server:
self._buffer_chunk_size = buffer_chunk_size # How many bytes per chunk
self._buffer_length_limit = buffer_length_limit # How many chunks in buffer
self._working = False
self.at_eof = False
self._server = None
self._ssl_context = None
@ -62,32 +61,37 @@ class Server:
await asyncio.sleep(1)
continue
input_data = await reader.read(self.buffer_chunk_size)
if reader.at_eof():
self.at_eof = True
self.buffer.append(input_data)
except Exception as e:
logging.error(e)
async def run_writer(self, writer):
consecutive_interruptions = 0
errors = 0
while not self.stopping:
try:
# Slow down if buffer is short
if len(self.buffer) < 3:
await asyncio.sleep(.1)
try:
input_data = self.buffer.popleft()
except IndexError:
if not self.at_eof:
continue
else:
writer.write_eof()
await writer.drain()
self.at_eof = False
# Slow down if buffer is short
consecutive_interruptions += 1
if consecutive_interruptions > 3:
break
await asyncio.sleep(.5)
continue
else:
consecutive_interruptions = 0
if not input_data:
break
writer.write(input_data)
await writer.drain()
except Exception as e:
logging.error(e)
errors += 1
if errors > 3:
break
await asyncio.sleep(0.5)
writer.close()
async def connect(self,
reader: asyncio.StreamReader,
@ -217,5 +221,3 @@ if __name__ == '__main__':
logging.info("Please consider using SSL.")
certificate, key = None, None
server.run()
# TODO: https://pymotw.com/3/asyncio/ssl.html (replace eof with zero-byte)