corelink.resources.Proto

 1import asyncio
 2from .variables import log
 3from .reqs import receiver_callback
 4
 5class Proto(asyncio.Protocol):
 6    """An asyncio Protocol class used to process incoming messages for a receiver.
 7    Implements TCP or UDP protocol."""
 8    def __init__(self, message: bytearray, type, port) -> None:
 9        self.header = bytes(message)
10        self.type = type
11        self.transport = None
12        self.port = port
13
14    def connection_made(self, transport) -> None:
15        self.transport = transport
16        if self.type == 'tcp':
17            self.transport.write(self.header)
18        else:
19            print(self.port)
20            self.transport.sendto(self.header)
21        log(f"[{self.type}] Receiver connected. Waiting for data.")
22            
23    def connection_lost(self, exc):
24        log('The server closed the connection')
25    
26    #TCP
27    def data_received(self, data: bytes) -> None:
28        log("[tcp] data received")
29        asyncio.create_task(receiver_callback(data))
30
31    #UDP
32    def datagram_received(self, data, addr):
33        log("[udp] data received")
34        asyncio.create_task(receiver_callback(data))
35
36    #UDP
37    def error_received(self, exc):
38        print('Error received:', exc)
class Proto(asyncio.protocols.Protocol):
 6class Proto(asyncio.Protocol):
 7    """An asyncio Protocol class used to process incoming messages for a receiver.
 8    Implements TCP or UDP protocol."""
 9    def __init__(self, message: bytearray, type, port) -> None:
10        self.header = bytes(message)
11        self.type = type
12        self.transport = None
13        self.port = port
14
15    def connection_made(self, transport) -> None:
16        self.transport = transport
17        if self.type == 'tcp':
18            self.transport.write(self.header)
19        else:
20            print(self.port)
21            self.transport.sendto(self.header)
22        log(f"[{self.type}] Receiver connected. Waiting for data.")
23            
24    def connection_lost(self, exc):
25        log('The server closed the connection')
26    
27    #TCP
28    def data_received(self, data: bytes) -> None:
29        log("[tcp] data received")
30        asyncio.create_task(receiver_callback(data))
31
32    #UDP
33    def datagram_received(self, data, addr):
34        log("[udp] data received")
35        asyncio.create_task(receiver_callback(data))
36
37    #UDP
38    def error_received(self, exc):
39        print('Error received:', exc)

An asyncio Protocol class used to process incoming messages for a receiver. Implements TCP or UDP protocol.

Proto(message: bytearray, type, port)
 9    def __init__(self, message: bytearray, type, port) -> None:
10        self.header = bytes(message)
11        self.type = type
12        self.transport = None
13        self.port = port
header
type
transport
port
def connection_made(self, transport) -> None:
15    def connection_made(self, transport) -> None:
16        self.transport = transport
17        if self.type == 'tcp':
18            self.transport.write(self.header)
19        else:
20            print(self.port)
21            self.transport.sendto(self.header)
22        log(f"[{self.type}] Receiver connected. Waiting for data.")

Called when a connection is made.

The argument is the transport representing the pipe connection. To receive data, wait for data_received() calls. When the connection is closed, connection_lost() is called.

def connection_lost(self, exc):
24    def connection_lost(self, exc):
25        log('The server closed the connection')

Called when the connection is lost or closed.

The argument is an exception object or None (the latter meaning a regular EOF is received or the connection was aborted or closed).

def data_received(self, data: bytes) -> None:
28    def data_received(self, data: bytes) -> None:
29        log("[tcp] data received")
30        asyncio.create_task(receiver_callback(data))

Called when some data is received.

The argument is a bytes object.

def datagram_received(self, data, addr):
33    def datagram_received(self, data, addr):
34        log("[udp] data received")
35        asyncio.create_task(receiver_callback(data))
def error_received(self, exc):
38    def error_received(self, exc):
39        print('Error received:', exc)