From 4735f3dec5b5ce72166fc80c89a2dded129ca4a1 Mon Sep 17 00:00:00 2001 From: Sebastian Majewski Date: Mon, 9 Sep 2024 21:52:46 -0500 Subject: [PATCH] Code cleanup --- pytcp/lib/socket.py | 28 +++++++++++---------------- pytcp/protocols/tcp/tcp__socket.py | 31 ++++++++++++++++++------------ pytcp/protocols/udp/udp__socket.py | 18 ++++++++--------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/pytcp/lib/socket.py b/pytcp/lib/socket.py index 1658902a..d7a72d6d 100755 --- a/pytcp/lib/socket.py +++ b/pytcp/lib/socket.py @@ -144,23 +144,17 @@ class Socket(ABC): Base class for other socket classes. """ - def __init__(self) -> None: - """ - Class constructor. - """ - - if TYPE_CHECKING: - self._family: AddressFamily - self._type: SocketType - self._local_ip_address: IpAddress - self._remote_ip_address: IpAddress - self._local_port: int - self._remote_port: int - self._parent_socket: Socket - self._tcp_session: TcpSession | None - self._tcp_accept: list[Socket] - self._event_tcp_session_established: Semaphore - self._unreachable: bool + _family: AddressFamily + _type: SocketType + _local_ip_address: IpAddress + _remote_ip_address: IpAddress + _local_port: int + _remote_port: int + _parent_socket: Socket + _tcp_session: TcpSession | None + _tcp_accept: list[Socket] + _event_tcp_session_established: Semaphore + _unreachable: bool def __str__(self) -> str: """ diff --git a/pytcp/protocols/tcp/tcp__socket.py b/pytcp/protocols/tcp/tcp__socket.py index 27993375..5b704b22 100755 --- a/pytcp/protocols/tcp/tcp__socket.py +++ b/pytcp/protocols/tcp/tcp__socket.py @@ -48,7 +48,14 @@ Ip6Address, Ip6AddressFormatError, ) -from pytcp.lib.socket import AF_INET4, AF_INET6, SOCK_STREAM, Socket, gaierror +from pytcp.lib.socket import ( + AF_INET4, + AF_INET6, + SOCK_STREAM, + AddressFamily, + Socket, + gaierror, +) from pytcp.protocols.tcp.tcp__session import ( FsmState, TcpSession, @@ -58,9 +65,9 @@ if TYPE_CHECKING: from threading import Semaphore - from pytcp.lib.socket import AddressFamily, IpAddress, SocketType - - from .tcp__metadata import TcpMetadata + from pytcp.lib.net_addr import IpAddress + from pytcp.lib.socket import SocketType + from pytcp.protocols.tcp.tcp__metadata import TcpMetadata class TcpSocket(Socket): @@ -75,8 +82,6 @@ def __init__( Class constructor. """ - super().__init__() - self._family: AddressFamily = family self._type: SocketType = SOCK_STREAM self._event_tcp_session_established: Semaphore = threading.Semaphore(0) @@ -101,12 +106,14 @@ def __init__( # Fresh socket initialization else: - if self._family is AF_INET6: - self._local_ip_address = Ip6Address() - self._remote_ip_address = Ip6Address() - if self._family is AF_INET4: - self._local_ip_address = Ip4Address() - self._remote_ip_address = Ip4Address() + match self._family: + case AddressFamily.AF_INET6: + self._local_ip_address = Ip6Address() + self._remote_ip_address = Ip6Address() + case AddressFamily.AF_INET4: + self._local_ip_address = Ip4Address() + self._remote_ip_address = Ip4Address() + self._local_port = 0 self._remote_port = 0 self._tcp_session = None diff --git a/pytcp/protocols/udp/udp__socket.py b/pytcp/protocols/udp/udp__socket.py index 7b1e50d6..3b97509c 100755 --- a/pytcp/protocols/udp/udp__socket.py +++ b/pytcp/protocols/udp/udp__socket.py @@ -53,8 +53,10 @@ AF_INET4, AF_INET6, SOCK_DGRAM, + AddressFamily, ReceiveTimeout, Socket, + SocketType, gaierror, ) from pytcp.lib.tx_status import TxStatus @@ -63,7 +65,6 @@ from threading import Semaphore from pytcp.lib.net_addr import IpAddress - from pytcp.lib.socket import AddressFamily, SocketType from pytcp.protocols.udp.udp__metadata import UdpMetadata @@ -77,8 +78,6 @@ def __init__(self, family: AddressFamily) -> None: Class constructor. """ - super().__init__() - self._family: AddressFamily = family self._type: SocketType = SOCK_DGRAM self._local_port: int = 0 @@ -89,12 +88,13 @@ def __init__(self, family: AddressFamily) -> None: self._local_ip_address: IpAddress self._remote_ip_address: IpAddress - if self._family is AF_INET6: - self._local_ip_address = Ip6Address() - self._remote_ip_address = Ip6Address() - if self._family is AF_INET4: - self._local_ip_address = Ip4Address() - self._remote_ip_address = Ip4Address() + match self._family: + case AddressFamily.AF_INET6: + self._local_ip_address = Ip6Address() + self._remote_ip_address = Ip6Address() + case AddressFamily.AF_INET4: + self._local_ip_address = Ip4Address() + self._remote_ip_address = Ip4Address() __debug__ and log("socket", f"[{self}] - Created socket")