reopen UDP socket periodically
This commit is contained in:
@@ -22,6 +22,7 @@ import syslog
|
|||||||
|
|
||||||
PORT = 50003
|
PORT = 50003
|
||||||
INTERVAL = 10
|
INTERVAL = 10
|
||||||
|
REOPENC = 6
|
||||||
PIDFILE = '/tmp/hbc.pid'
|
PIDFILE = '/tmp/hbc.pid'
|
||||||
VER = 4
|
VER = 4
|
||||||
MAXRECV = 32767
|
MAXRECV = 32767
|
||||||
@@ -54,13 +55,22 @@ class Conn:
|
|||||||
self.send = 0
|
self.send = 0
|
||||||
self.lastsend = 0 # time() last msg was sent
|
self.lastsend = 0 # time() last msg was sent
|
||||||
self.rtts = [0]
|
self.rtts = [0]
|
||||||
self.sock=socket.socket(af, socket.SOCK_DGRAM)
|
self.sock = None
|
||||||
|
|
||||||
|
|
||||||
|
def open(self):
|
||||||
|
self.sock=socket.socket(self.af, socket.SOCK_DGRAM)
|
||||||
self.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
|
self.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, \
|
||||||
self.sock.getsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR) | 1)
|
self.sock.getsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR) | 1)
|
||||||
|
|
||||||
|
|
||||||
def sendto(self, msg, ID = 'HTB'): # default ID is HearTBeat
|
def sendto(self, msg, ID = 'HTB'): # default ID is HearTBeat
|
||||||
global warned1
|
global warned1
|
||||||
|
|
||||||
|
if self.send % REOPENC == 0:
|
||||||
|
self.close()
|
||||||
|
if not self.sock:
|
||||||
|
self.open()
|
||||||
msg['name'] = shortname(iam)
|
msg['name'] = shortname(iam)
|
||||||
msg['id'] = self.conId
|
msg['id'] = self.conId
|
||||||
msg['ver'] = VER
|
msg['ver'] = VER
|
||||||
@@ -73,6 +83,7 @@ class Conn:
|
|||||||
except socket.error as e:
|
except socket.error as e:
|
||||||
if not warned1: log("socket error: %s %s:%s" % (e, self.addr, self.port))
|
if not warned1: log("socket error: %s %s:%s" % (e, self.addr, self.port))
|
||||||
warned1 = True
|
warned1 = True
|
||||||
|
self.close()
|
||||||
return
|
return
|
||||||
self.send += 1
|
self.send += 1
|
||||||
self.lastsend = time.time()
|
self.lastsend = time.time()
|
||||||
@@ -252,15 +263,16 @@ def restart():
|
|||||||
def process():
|
def process():
|
||||||
global running, dorestart
|
global running, dorestart
|
||||||
|
|
||||||
ifiles = {}
|
|
||||||
conIds = {}
|
|
||||||
for conn in conns:
|
|
||||||
ifiles[conns[conn].sock.fileno()] = conns[conn].sock
|
|
||||||
conIds[conns[conn].sock.fileno()] = conn
|
|
||||||
nextReport = time.time()
|
nextReport = time.time()
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
while time.time() < nextReport:
|
while time.time() < nextReport:
|
||||||
|
ifiles = {}
|
||||||
|
conIds = {}
|
||||||
|
for conn in conns:
|
||||||
|
ifiles[conns[conn].sock.fileno()] = conns[conn].sock
|
||||||
|
conIds[conns[conn].sock.fileno()] = conn
|
||||||
|
|
||||||
sleep=nextReport - time.time()
|
sleep=nextReport - time.time()
|
||||||
if sleep <= 0:
|
if sleep <= 0:
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in New Issue
Block a user