Skip to content

Commit

Permalink
improvements and bug fixing in multicast registering
Browse files Browse the repository at this point in the history
  • Loading branch information
Agraj P Das authored and Agraj P Das committed Jun 18, 2024
1 parent 1619f4b commit 9236d50
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
4 changes: 2 additions & 2 deletions stmp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
stmp
Sitty Talky messaging Protocol : A primitive protocol purely written in python for tinkering with your office mates over LAN!
Sitty Talky messaging Protocol : A primitive protocol purely written in python for tinkering with my office mates over LAN!
"""

from .server import STMPServer

__version__ = "0.0.2"
__version__ = "0.0.3"
__author__ = "@bRuttaZz"
2 changes: 1 addition & 1 deletion stmp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
logger = logging.getLogger("stmp")

# some configs/consts
STMP_MADDR = "239.192.1.107"
STMP_MADDR = "224.0.0.109"
STMP_PORT = 57000 # UDP port
TCP_PORT = 57001 # certainly TCP port

Expand Down
30 changes: 11 additions & 19 deletions stmp/stmp_server/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Transport:
def _prepare_sock(self):
"""Common socket settings"""
self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
if hasattr(socket, 'SO_REUSEPORT'):
self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
else:
logger.warning(
"It seems like the system is not supporting socket.SO_REUSEPORT option!"
+ " (never mind, it makes no sense, most of the time)"
Expand Down Expand Up @@ -70,7 +70,6 @@ def __init__(self, maddr: str = STMP_MADDR, port: int = STMP_PORT) -> None:

# socket
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)

def __enter__(self, *args, **kwargs):
return self
Expand All @@ -93,22 +92,15 @@ def send(self, data: bytes, addr: str = None, port: int = None) -> bool:

def listen(self) -> "ListenSession":
self._prepare_sock()
# Set some options to make it multicast-friendly
self._sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)

# Bind to the port
self._sock.bind(("", self.port))

# Set some more multicast options
intf = socket.gethostbyname(socket.gethostname())
self._sock.setsockopt(
socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf)
)
self._sock.setsockopt(
socket.SOL_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(self.addr) + socket.inet_aton(intf),
)

self._sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 1)
self._sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, True)

#register to multicast group
mGroup = socket.inet_aton(STMP_MADDR) + socket.INADDR_ANY.to_bytes(4, byteorder='big')
self._sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mGroup)
#bind the socket to the correct port
self._sock.bind(('', STMP_PORT))

return UDPListenSession(self._sock, self.addr)

Expand Down
2 changes: 0 additions & 2 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
stp_logger.addHandler(handler)
stp_logger.setLevel(logging.DEBUG)


session = STMPServer()


if len(sys.argv) > 1:
session.send_udp(
" ".join(sys.argv[1:]),
Expand Down

0 comments on commit 9236d50

Please sign in to comment.