Module mudproto.connection
¶
MUD connection.
Class ConnectionInterface(ABC)
¶
Input and output to a MUD client or server.
Source code in mudproto/connection.py
class ConnectionInterface(ABC):
"""Input and output to a MUD client or server."""
def __init__(
self,
writer: ConnectionWriterType,
receiver: ConnectionReceiverType,
*,
is_client: bool,
**kwargs: Any,
) -> None:
"""
Defines the constructor.
Args:
writer: The object where output is written.
receiver: The object where input is received.
is_client: True if acting as a client, False if acting as a server.
**kwargs: Key-word only arguments (currently unused).
"""
self._writer: ConnectionWriterType = writer
self._receiver: ConnectionReceiverType = receiver
self._is_client: bool = is_client
@property
def is_client(self) -> bool:
"""True if acting as a client, False otherwise."""
return self._is_client
@property
def is_server(self) -> bool:
"""True if acting as a server, False otherwise."""
return not self._is_client
def write(self, data: bytes) -> None:
"""
Writes data to peer.
Args:
data: The bytes to be written.
"""
self._writer(data)
@abstractmethod
def on_connection_made(self) -> None:
"""Called by `connect` when a connection to peer has been established."""
@abstractmethod
def on_connection_lost(self) -> None:
"""Called by `disconnect` when a connection to peer has been lost."""
@abstractmethod
def on_data_received(self, data: bytes) -> None:
"""
Called by `parse` when data is received.
Args:
data: The received data.
"""
self._receiver(data)
Attribute is_client: bool
property
readonly
¶
True if acting as a client, False otherwise.
Attribute is_server: bool
property
readonly
¶
True if acting as a server, False otherwise.
Method __init__(self, writer, receiver, *, is_client, **kwargs)
special
¶
Defines the constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
writer |
ConnectionWriterType |
The object where output is written. |
required |
receiver |
ConnectionReceiverType |
The object where input is received. |
required |
is_client |
bool |
True if acting as a client, False if acting as a server. |
required |
**kwargs |
Any |
Key-word only arguments (currently unused). |
{} |
Source code in mudproto/connection.py
def __init__(
self,
writer: ConnectionWriterType,
receiver: ConnectionReceiverType,
*,
is_client: bool,
**kwargs: Any,
) -> None:
"""
Defines the constructor.
Args:
writer: The object where output is written.
receiver: The object where input is received.
is_client: True if acting as a client, False if acting as a server.
**kwargs: Key-word only arguments (currently unused).
"""
self._writer: ConnectionWriterType = writer
self._receiver: ConnectionReceiverType = receiver
self._is_client: bool = is_client
Method on_connection_lost(self)
¶
Called by disconnect when a connection to peer has been lost.
Source code in mudproto/connection.py
@abstractmethod
def on_connection_lost(self) -> None:
"""Called by `disconnect` when a connection to peer has been lost."""
Method on_connection_made(self)
¶
Called by connect when a connection to peer has been established.
Source code in mudproto/connection.py
@abstractmethod
def on_connection_made(self) -> None:
"""Called by `connect` when a connection to peer has been established."""
Method on_data_received(self, data)
¶
Called by parse when data is received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
bytes |
The received data. |
required |
Source code in mudproto/connection.py
@abstractmethod
def on_data_received(self, data: bytes) -> None:
"""
Called by `parse` when data is received.
Args:
data: The received data.
"""
self._receiver(data)
Method write(self, data)
¶
Writes data to peer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
bytes |
The bytes to be written. |
required |
Source code in mudproto/connection.py
def write(self, data: bytes) -> None:
"""
Writes data to peer.
Args:
data: The bytes to be written.
"""
self._writer(data)