Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 10, 2024
1 parent a3a305e commit 4735f3d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
28 changes: 11 additions & 17 deletions pytcp/lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
31 changes: 19 additions & 12 deletions pytcp/protocols/tcp/tcp__socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions pytcp/protocols/udp/udp__socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
AF_INET4,
AF_INET6,
SOCK_DGRAM,
AddressFamily,
ReceiveTimeout,
Socket,
SocketType,
gaierror,
)
from pytcp.lib.tx_status import TxStatus
Expand All @@ -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


Expand All @@ -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
Expand All @@ -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"<g>[{self}]</> - Created socket")

Expand Down

0 comments on commit 4735f3d

Please sign in to comment.