Skip to content

Instantly share code, notes, and snippets.

@acdimalev
Created October 15, 2021 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdimalev/912b7a839234168173c483a584798df0 to your computer and use it in GitHub Desktop.
Save acdimalev/912b7a839234168173c483a584798df0 to your computer and use it in GitHub Desktop.
from dataclasses import field
@dataclass
class InMessage:
line: str
prefix: str
command: str
params: list[str]
@dataclass
class Connection:
reference: int
socket: int = None
in_buffer: str = ""
in_messages: list[InMessage] = field(default_factory=list)
next_connection_reference = 0
connections = {}
def live_connections():
return connections.values()
def connection_by_reference(reference):
return connections.get(reference)
def next_free_connection():
reference = next_connection_reference
connection = Connection(reference)
connections[reference] = connection
next_connection_reference = 1 + reference
return connection
def drop_connections(references):
for reference in references:
if reference in connection:
del connection[reference]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment