From 35eceb867683e7a9adb9b482d8092064537d89ff Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 17 Jun 2021 15:44:10 +0800 Subject: [PATCH] bugfix --- appPublic/udp_comm.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 appPublic/udp_comm.py diff --git a/appPublic/udp_comm.py b/appPublic/udp_comm.py new file mode 100644 index 0000000..d2ef3c7 --- /dev/null +++ b/appPublic/udp_comm.py @@ -0,0 +1,55 @@ +# -*- coding:UTF-8 -*- +from socket import * +import json +from appPublic.sockPackage import get_free_local_addr +from appPublic.background import Background +BUFSIZE = 1024 +class UdpComm: + def __init__(self, port, callback, timeout=1): + self.callback = callback + self.host = get_free_local_addr()[0] + self.port = port + self.udpSerSock = socket(AF_INET, SOCK_DGRAM) + # 设置阻塞 + self.udpSerSock.setblocking(1) + # 设置超时时间 1s + # self.udpSerSock.settimeout(timeout) + self.udpSerSock.bind(('' ,port)) + self.run_flg = True + self.thread = Background(self.run) + self.thread.start() + + def run(self): + while self.run_flg: + try: + b, addr = self.udpSerSock.recvfrom(BUFSIZE) + data = json.loads(b) + if addr[0] != self.host: + self.callback(data, addr) + # ret = json.dumps(self.info).encode('utf-8') + # self.udpSerSock.sendto(ret, addr) + except Exception as e: + print('exception happened:',e) + pass + + def stop(self): + self.run_flg = False + self.udpSerSock.close() + + def broadcast(self, data): + broadcast_host = '.'.join(self.host.split('.')[:-1]) + '.255' + udpCliSock = socket(AF_INET, SOCK_DGRAM) + udpCliSock.settimeout(self.timeout) + udpCliSock.bind(('', 0)) + udpCliSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) + b = json.dumps(data).encode('utf-8') + udpCliSock.sendto(b, (broadcast_host,self.port)) + + def send(self,data,addr): + b = json.dumps(data).encode('utf-8') + self.udpSerSock.sendto(b,addr) + + def sends(self,data, addrs): + b = json.dumps(data).encode('utf-8') + for addr in addrs: + self.udpSerSock.sendto(b,addr)