mirror of
https://github.com/titanscouting/tra-superscript.git
synced 2024-11-10 06:54:45 +00:00
possible fix: UDP broadcast,
not connecing from outside LAN
Signed-off-by: Arthur Lu <learthurgo@gmail.com>
Former-commit-id: 531c8f80f3
This commit is contained in:
parent
a4a13c7cb5
commit
5d0fbc06c6
19
src/cli/client.py
Normal file
19
src/cli/client.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
|
||||||
|
|
||||||
|
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
|
||||||
|
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
|
||||||
|
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
|
||||||
|
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
|
||||||
|
# Thanks to @stevenreddie
|
||||||
|
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||||
|
|
||||||
|
# Enable broadcasting mode
|
||||||
|
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
|
|
||||||
|
client.connect(("", 5678))
|
||||||
|
while True:
|
||||||
|
# Thanks @seym45 for a fix
|
||||||
|
data, addr = client.recvfrom(1024)
|
||||||
|
print("received message: %s"%data)
|
@ -151,18 +151,16 @@ __all__ = [
|
|||||||
|
|
||||||
# imports:
|
# imports:
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
from multiprocessing import Pool, freeze_support
|
from multiprocessing import Pool, freeze_support
|
||||||
import os
|
import os
|
||||||
import pymongo
|
import pymongo
|
||||||
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import warnings
|
import warnings
|
||||||
import websockets
|
|
||||||
|
|
||||||
from interface import splash, log, ERR, INF, stdout, stderr
|
from interface import splash, log, ERR, INF, stdout, stderr
|
||||||
from data import get_previous_time, pull_new_tba_matches, set_current_time, load_match, push_match, load_pit, push_pit, get_database_config, set_database_config, check_new_database_matches
|
from data import get_previous_time, pull_new_tba_matches, set_current_time, load_match, push_match, load_pit, push_pit, get_database_config, set_database_config, check_new_database_matches
|
||||||
@ -544,41 +542,23 @@ def start(pid_path, verbose = False, profile = False, debug = False):
|
|||||||
|
|
||||||
f = open('errorlog.log', 'w+')
|
f = open('errorlog.log', 'w+')
|
||||||
with daemon.DaemonContext(
|
with daemon.DaemonContext(
|
||||||
working_directory=os.getcwd(),
|
working_directory = os.getcwd(),
|
||||||
pidfile=pidfile.TimeoutPIDLockFile(pid_path),
|
pidfile = pidfile.TimeoutPIDLockFile(pid_path),
|
||||||
stderr=f
|
stderr = f
|
||||||
):
|
):
|
||||||
|
|
||||||
async def handler(client, path):
|
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
clients.append(client)
|
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||||
while True:
|
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
try:
|
server.settimeout(0.2)
|
||||||
pong_waiter = await client.ping()
|
|
||||||
await pong_waiter
|
|
||||||
time.sleep(3)
|
|
||||||
except Exception as e:
|
|
||||||
clients.remove(client)
|
|
||||||
break
|
|
||||||
|
|
||||||
async def send_one(client, data):
|
|
||||||
await client.send(data)
|
|
||||||
|
|
||||||
def send(target, level, message, code = 0):
|
def send(target, level, message, code = 0):
|
||||||
message_clients = clients.copy()
|
server.sendto(bytes(message, 'utf-8'), ('<broadcast>', 5678))
|
||||||
for client in message_clients:
|
|
||||||
try:
|
|
||||||
asyncio.run(send_one(client, message))
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
clients = []
|
|
||||||
start_server = websockets.serve(handler, "0.0.0.0", 5678)
|
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(start_server)
|
|
||||||
threading.Thread(target = asyncio.get_event_loop().run_forever).start()
|
|
||||||
|
|
||||||
exit_code = main(send)
|
exit_code = main(send)
|
||||||
sys.exit(exit_code)
|
server.close()
|
||||||
|
f.close()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
def stop(pid_path):
|
def stop(pid_path):
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user