diff --git a/pypong/networking/client.py b/pypong/networking/client.py index b882a95..0f8e422 100644 --- a/pypong/networking/client.py +++ b/pypong/networking/client.py @@ -39,6 +39,9 @@ def request_server_info(server_ip, server_port): request = 'PYPONGREQ;SVRINFO' udp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + # Timeout is set to SCAN_TIMEOUT plus a random number between 0 and SCAN_TIMEOUT so that threads can + # start asynchronously to avoid a lot of requests being sent to machines at once + # Average time is SCAN_TIMEOUT*1.5 udp_client_socket.settimeout(SCAN_TIMEOUT + (SCAN_TIMEOUT * random.random())) try: udp_client_socket.sendto(str.encode(request, 'UTF-8'), (server_ip, server_port)) diff --git a/pypong/networking/gui.py b/pypong/networking/gui.py index 1f8884a..563773c 100644 --- a/pypong/networking/gui.py +++ b/pypong/networking/gui.py @@ -7,6 +7,9 @@ import pypong.networking.client as client global running +# Don't allow any more than this number of threads when looking for games +# Can cripple a network at high numbers +MAX_SCAN_REQUESTS = 64 def main(): """ @@ -14,7 +17,7 @@ def main(): """ # Executor used for scanning local network for servers - executor = concurrent.futures.ThreadPoolExecutor(max_workers=512) + executor = concurrent.futures.ThreadPoolExecutor(max_workers=MAX_SCAN_REQUESTS) main_window = tk.Tk() main_window.title('PyPong Launcher')