This commit is contained in:
yumoqing 2022-11-09 21:37:46 +08:00
parent cbe9ef259e
commit 1318ea50ee

View File

@ -1,4 +1,5 @@
# -*- coding:UTF-8 -*- # -*- coding:UTF-8 -*-
import time
from traceback import print_exc from traceback import print_exc
from socket import * from socket import *
import json import json
@ -6,16 +7,16 @@ 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=1): def __init__(self, port, callback, timeout=0):
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) 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)
@ -27,6 +28,8 @@ class UdpComm:
b, addr = self.udpSerSock.recvfrom(BUFSIZE) b, addr = self.udpSerSock.recvfrom(BUFSIZE)
if addr[0] != self.host: if addr[0] != self.host:
self.callback(b, addr) self.callback(b, addr)
if timeout == 0:
time.sleep(0.1)
except Exception as e: except Exception as e:
print('exception happened:',e) print('exception happened:',e)
print_exc() print_exc()