-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.py
34 lines (27 loc) · 937 Bytes
/
transport.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
import asyncio
from contextlib import asynccontextmanager, closing
from protocol import AwesomeProtocol
from worker import Worker
class UdpTransport():
"""Class to initialize UDP server and starts the worker"""
def __init__(self, host, port) -> None:
self.host = host
self.port = port
@property
def bind_host(self) -> str:
bind_host = self.host
return bind_host
@property
def bind_port(self) -> int:
bind_port = self.port
return bind_port
@asynccontextmanager
async def enter(self):
loop = asyncio.get_running_loop()
transport, protocol = await loop.create_datagram_endpoint(
lambda: AwesomeProtocol(),
reuse_port=True, local_addr=(self.bind_host, self.bind_port))
assert isinstance(protocol, AwesomeProtocol)
worker = Worker(protocol)
with closing(transport):
yield worker