mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2026-01-03 15:19:57 +01:00
some improvements
This commit is contained in:
parent
9c091fb1f7
commit
16c5388d3d
|
|
@ -17,6 +17,7 @@
|
|||
import logging
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
|
@ -36,22 +37,25 @@ class BroadcastClient:
|
|||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
self._socket.settimeout(5)
|
||||
self._socket.settimeout(3)
|
||||
|
||||
def sendBroadcast(self):
|
||||
def getConnInfo(self, retry=0):
|
||||
"""!Send broadcastpackets
|
||||
|
||||
This function will block until the connection Info
|
||||
from server will be received.
|
||||
This function will send broadcast package(s)
|
||||
to get connection info from the server.
|
||||
|
||||
- send the magic packet <BW-Request> on broadcast address.
|
||||
- wait for a <BW-Result> magic packet.
|
||||
- extract the connection data from the magic packet and return
|
||||
|
||||
@param retry: Count of retry - 0 is infinite (0)
|
||||
|
||||
@return True or False"""
|
||||
while True:
|
||||
sendPackages = 1
|
||||
while sendPackages <= retry or retry == 0:
|
||||
try:
|
||||
logging.debug("send magic <BW3-Request> as broadcast")
|
||||
logging.debug("send magic <BW3-Request> as broadcast - #%d", sendPackages)
|
||||
self._socket.sendto("<BW3-Request>".encode(), ('255.255.255.255', self._broadcastPort))
|
||||
payload, address = self._socket.recvfrom(1024)
|
||||
payload = str(payload, "UTF-8")
|
||||
|
|
@ -60,14 +64,16 @@ class BroadcastClient:
|
|||
logging.debug("received magic <BW3-Result> from: %s", address[0])
|
||||
self._serverIP = address[0]
|
||||
self._serverPort = int(payload.split(";")[1])
|
||||
logging.info("got connection info: %s:%d", self._serverIP, self._serverPort)
|
||||
logging.info("got connection info from server: %s:%d", self._serverIP, self._serverPort)
|
||||
return True
|
||||
except socket.timeout:
|
||||
logging.warning("no server found - retry sending magic")
|
||||
continue
|
||||
except socket.timeout: # nothing received - retry
|
||||
logging.debug("no magic packet received")
|
||||
sendPackages += 1
|
||||
except:
|
||||
logging.exception("error on getting connection info")
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@property
|
||||
def serverIP(self):
|
||||
|
|
@ -90,6 +96,7 @@ class BroadcastServer:
|
|||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
self._socket.settimeout(2)
|
||||
self._socket.bind(('', listenPort))
|
||||
self._serverThread = None
|
||||
self._serverIsRunning = False
|
||||
|
|
@ -111,14 +118,17 @@ class BroadcastServer:
|
|||
logging.exception("cannot start udp broadcast server thread")
|
||||
return False
|
||||
|
||||
def stop(self):
|
||||
def stopp(self):
|
||||
"""!Stop the broadcast server
|
||||
|
||||
Due to the timeout of the socket,
|
||||
stopping the thread can be delayed by two seconds
|
||||
|
||||
@return True or False"""
|
||||
try:
|
||||
logging.debug("stop udp broadcast server")
|
||||
self._serverIsRunning = False
|
||||
self._serverThread.join()
|
||||
# self._serverThread.join()
|
||||
return True
|
||||
except:
|
||||
logging.exception("cannot stop udp broadcast server thread")
|
||||
|
|
@ -132,14 +142,17 @@ class BroadcastServer:
|
|||
|
||||
- listen for the magic packet <BW-Request>
|
||||
- send connection info in an <BW-Result> macig packet"""
|
||||
try:
|
||||
logging.debug("start listening for magic")
|
||||
while self._serverIsRunning:
|
||||
payload, address = self._socket.recvfrom(1024) # fixme recv is blocking, evtl we can use to wait for readable data
|
||||
logging.debug("start listening for magic")
|
||||
while self._serverIsRunning:
|
||||
try:
|
||||
payload, address = self._socket.recvfrom(1024)
|
||||
payload = str(payload, "UTF-8")
|
||||
if payload == "<BW3-Request>":
|
||||
logging.debug("received magic <BW3-Request> from: %s", address[0])
|
||||
logging.info("send connection info in magic <BW3-Result> to: %s", address[0])
|
||||
self._socket.sendto("<BW3-Result>;".encode() + str(self._servePort).encode(), address)
|
||||
except:
|
||||
logging.exception("error while listening for clients")
|
||||
except socket.timeout:
|
||||
continue # timeout is accepted (not block at recvfrom())
|
||||
except:
|
||||
logging.exception("error while listening for clients")
|
||||
logging.debug("udp broadcast server stopped")
|
||||
|
|
|
|||
10
bw_server.py
10
bw_server.py
|
|
@ -51,12 +51,22 @@ try:
|
|||
from boswatch.descriptor.descriptor import Descriptor
|
||||
from boswatch.filter.doubeFilter import DoubleFilter
|
||||
from boswatch.utils import header
|
||||
from boswatch.network.broadcast import BroadcastClient
|
||||
from boswatch.network.broadcast import BroadcastServer
|
||||
except Exception as e: # pragma: no cover
|
||||
logging.exception("cannot import modules")
|
||||
print("cannot import modules")
|
||||
print(e)
|
||||
exit(1)
|
||||
|
||||
# Test for the broadcast connection info function
|
||||
server = BroadcastServer()
|
||||
client = BroadcastClient()
|
||||
server.start()
|
||||
client.getConnInfo()
|
||||
print(client.serverIP, client.serverPort)
|
||||
server.stop()
|
||||
|
||||
try:
|
||||
header.logoToLog()
|
||||
header.infoToLog()
|
||||
|
|
|
|||
Loading…
Reference in a new issue