-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
75 lines (58 loc) · 2.37 KB
/
protocol.py
File metadata and controls
75 lines (58 loc) · 2.37 KB
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
import socket
from collections.abc import Callable
from loguru import logger
from src.shared import Request
HOST = ("localhost", 24827)
NUMBER_OF_LENGTH_BYTES = 4
MAX_DATA_LOG_LENGTH = 500
CERT = "server.crt"
class SocketClosedException(Exception):
pass
class RequestWrapper():
def __init__(self, request: Request, request_id: int, callback: Callable[[Request], None] | None, subscribed: bool = False):
self.request = request
self.id = request_id
self.callback = callback
self.subscribed = subscribed
def send(wrapped: RequestWrapper, socket: socket.socket):
"""
Send a request to the given socket.
The request is wrapped in a RequestWrapper object.
"""
request_id = wrapped.id
req = wrapped.request
subbed = wrapped.subscribed
data = f"{request_id}\n{subbed}\n{req.serialize()}"
encoded = data.encode()
length = len(encoded).to_bytes(NUMBER_OF_LENGTH_BYTES, "big")
logger.info(
f"Sending {len(encoded)} bytes>>>>>>({socket.getpeername()})\n{data if len(data) < MAX_DATA_LOG_LENGTH else data[:MAX_DATA_LOG_LENGTH] + '...[TRUNCATED]'}")
socket.sendall(length + encoded)
def receive(socket: socket.socket) -> tuple[Request, int, bool]:
"""
Receive a request from the given socket.
Returns a tuple of the request, the request id, and whether the request is for a subscribed request.
"""
length = int.from_bytes(_recvall(socket, NUMBER_OF_LENGTH_BYTES), "big")
if length == 0:
raise SocketClosedException(f"Socket was closed: {socket}")
data = _recvall(socket, length).decode()
logger.info(
f"<<<<<<Received {length} bytes ({socket.getpeername()})\n{data if len(data) < MAX_DATA_LOG_LENGTH else data[:MAX_DATA_LOG_LENGTH] + '...[TRUNCATED]'}")
id, subbed, req = data.split("\n", maxsplit=2)
subbed = subbed == "True"
return Request.deserialize(req), int(id), subbed
def _recvall(socket: socket.socket, length: int) -> bytes:
"""
Receive `length` bytes from the given socket.
Raises a SocketClosedException if the socket is closed.
"""
batches: list[bytes] = []
total = 0
while total < length:
batch = socket.recv(length - total)
if not batch:
raise SocketClosedException(f"Socket was closed: {socket}")
total += len(batch)
batches.append(batch)
return b"".join(batches)