This commit is contained in:
yumoqing 2022-11-09 23:05:33 +08:00
parent 1318ea50ee
commit 6894c7fb3a
2 changed files with 32 additions and 25 deletions

View File

@ -2,47 +2,56 @@
import time import time
from traceback import print_exc from traceback import print_exc
from socket import * from socket import *
from select import select
import json import json
from appPublic.sockPackage import get_free_local_addr from appPublic.sockPackage import get_free_local_addr
from appPublic.background import Background from appPublic.background import Background
BUFSIZE = 1024 * 64 BUFSIZE = 1024 * 64
class UdpComm: class UdpComm:
def __init__(self, port, callback, timeout=0): def __init__(self, port, callback, timeout=0):
self.buffer = []
self.callback = callback self.callback = callback
self.timeout = timeout self.timeout = timeout
self.host = get_free_local_addr()[0] self.host = get_free_local_addr()[0]
self.port = port self.port = port
self.udpSerSock = socket(AF_INET, SOCK_DGRAM) self.udpSerSock = socket(AF_INET, SOCK_DGRAM)
# 设置阻塞 # 设置阻塞
self.udpSerSock.setblocking(1 if timeout > 0 else 0) # self.udpSerSock.setblocking(1 if timeout > 0 else 0)
# 设置超时时间 1s # 设置超时时间 1s
self.udpSerSock.settimeout(timeout) # self.udpSerSock.settimeout(timeout)
self.udpSerSock.bind(('' ,port)) self.udpSerSock.bind(('' ,port))
self.run_flg = True self.run_flg = True
self.thread = Background(self.run) self.thread = Background(self.run)
self.thread.start() self.thread.start()
def run(self): def run(self):
sock = self.udpSerSock
while self.run_flg: while self.run_flg:
try: in_s, out_s, exc_s = select([sock], [sock], [])
b, addr = self.udpSerSock.recvfrom(BUFSIZE) if sock in in_s:
if addr[0] != self.host: b, addr = sock.recvfrom(BUFSIZE)
t = b[0]
b = b[1:]
if t == 'b':
self.callback(b, addr) self.callback(b, addr)
if timeout == 0: else:
time.sleep(0.1) txt = b.decode('utf-8')
except Exception as e: d = json.loads(txt)
print('exception happened:',e) self.callback(d, addr)
print_exc() if sock in out_s:
pass while len(self.buffer) > 0:
d,addr = self.buffer.pop(0)
sock.sendto(d, addr)
self.udpSerSock.close()
def stop(self): def stop(self):
self.run_flg = False self.run_flg = False
self.udpSerSock.close()
def broadcast(self, data): def broadcast(self, data):
broadcast_host = '.'.join(self.host.split('.')[:-1]) + '.255' broadcast_host = '.'.join(self.host.split('.')[:-1]) + '.255'
udpCliSock = socket(AF_INET, SOCK_DGRAM) udpCliSock = socket(AF_INET, SOCK_DGRAM)
udpCliSock.settimeout(self.timeout) # udpCliSock.settimeout(1)
udpCliSock.bind(('', 0)) udpCliSock.bind(('', 0))
udpCliSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) udpCliSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
b = data b = data
@ -53,24 +62,21 @@ class UdpComm:
def send(self,data,addr): def send(self,data,addr):
b = data b = data
if not isinstance(data, bytes): if not isinstance(data, bytes):
b = json.dumps(data).encode('utf-8') b = b'j' + json.dumps(data).encode('utf-8')
else:
b = b'b' + data
if isinstance(addr,list): if isinstance(addr,list):
addr = tuple(addr) addr = tuple(addr)
self.udpSerSock.sendto(b,addr) self.buffer.append((b, addr))
def sends(self,data, addrs): def sends(self,data, addrs):
b = data for a in addrs:
if not isinstance(data, bytes): self.send(data, a)
b = json.dumps(data).encode('utf-8')
for addr in addrs:
if isinstance(addr,list):
addr = tuple(addr)
self.udpSerSock.sendto(b,addr)
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
def msg_handle(data, addr): def msg_handle(data, addr):
print('addr:', addr, 'data=', data) print('addr:', addr, 'data=', data, len(data))
port = 50000 port = 50000
if len(sys.argv)>1: if len(sys.argv)>1:
@ -78,6 +84,7 @@ if __name__ == '__main__':
d = UdpComm(port, msg_handle) d = UdpComm(port, msg_handle)
x = input() x = input()
while x: while x:
d.broadcast(x) port, data = x.split(':')
d.send(data, ('', int(port)))
x = input() x = input()

View File

@ -1 +1 @@
__version__ = '5.1.23' __version__ = '5.1.24'