This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_server.py
78 lines (69 loc) · 2.31 KB
/
socket_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import asyncio
class SocketServer:
def __init__(self, port=8001):
"""
One client socket server
Sends message from inbound_queue to socket and
from socket to outbound_queues
"""
self.reader: asyncio.StreamReader = None
self.writer: asyncio.StreamWriter = None
self.inbound_queue = asyncio.Queue()
self.outbound_queues: list[asyncio.Queue] = []
self.server: asyncio.Server = None
self.port = port
async def start_server(self):
self.server = await asyncio.start_server(
self.new_connecion_callback,
port=self.port
)
asyncio.create_task(self.server.serve_forever())
async def new_connecion_callback(self, reader, writer):
"""
Callback that is passed to asyncio.start_server
"""
if self.reader:
await self.close_connection(
'Allready connected. Closing...',
writer
)
return
self.reader = reader
self.writer = writer
asyncio.create_task(self.reader_worker())
asyncio.create_task(self.writer_worker())
async def close_connection(self, mesage: str, writer: asyncio.StreamWriter):
"""
Writes goodbye message and closes connection
"""
writer.write('Already connected. Closing...')
await writer.drain()
writer.close()
await writer.wait_closed()
async def reader_worker(self):
"""
Passes data from reader to outbound_queues
"""
try:
while True:
data = await self.reader.readline()
print('SOCKET <', data)
for queue in self.outbound_queues:
await queue.put(data.decode())
finally:
self.reader = None
async def writer_worker(self):
"""
Passes data from inbound_queue to writer
"""
try:
while True:
data = await self.inbound_queue.get()
print('SOCKET > ', data)
self.writer.write(data.encode())
await self.writer.drain()
finally:
self.writer = None
async def shut_down(self):
self.server.close()
await self.server.wait_closed()