Make commUDP more reliable in case of bad Ethernet connection

This commit is contained in:
rprinz08 2020-11-07 11:32:50 +01:00
parent 1c039389f2
commit 09ecd9abc9
1 changed files with 28 additions and 13 deletions

View File

@ -22,6 +22,7 @@ class CommUDP:
return return
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind(("", self.port)) self.socket.bind(("", self.port))
self.socket.settimeout(1)
def close(self): def close(self):
if not hasattr(self, "socket"): if not hasattr(self, "socket"):
@ -39,16 +40,23 @@ class CommUDP:
packet = EtherbonePacket() packet = EtherbonePacket()
packet.records = [record] packet.records = [record]
packet.encode() packet.encode()
self.socket.sendto(bytes(packet), (self.server, self.port))
datas, dummy = self.socket.recvfrom(8192) try:
packet = EtherbonePacket(datas) self.socket.sendto(bytes(packet), (self.server, self.port))
packet.decode()
datas = packet.records.pop().writes.get_datas() datas, dummy = self.socket.recvfrom(8192)
if self.debug: packet = EtherbonePacket(datas)
for i, value in enumerate(datas): packet.decode()
print("read {:08x} @ {:08x}".format(value, addr + 4*i)) datas = packet.records.pop().writes.get_datas()
return datas[0] if length is None else datas if self.debug:
for i, value in enumerate(datas):
print("read {:08x} @ {:08x}".format(value, addr + 4*i))
return datas[0] if length is None else datas
except Exception as ex:
print("commUDP read: exception ({})".format(ex))
self.close()
self.open()
return []
def write(self, addr, datas): def write(self, addr, datas):
datas = datas if isinstance(datas, list) else [datas] datas = datas if isinstance(datas, list) else [datas]
@ -60,8 +68,15 @@ class CommUDP:
packet = EtherbonePacket() packet = EtherbonePacket()
packet.records = [record] packet.records = [record]
packet.encode() packet.encode()
self.socket.sendto(bytes(packet), (self.server, self.port))
if self.debug: try:
for i, value in enumerate(datas): self.socket.sendto(bytes(packet), (self.server, self.port))
print("write {:08x} @ {:08x}".format(value, addr + 4*i))
if self.debug:
for i, value in enumerate(datas):
print("write {:08x} @ {:08x}".format(value, addr + 4*i))
except Exception as ex:
print("commUDP write: exception ({})".format(ex))
self.close()
self.open()