Exemplo n.º 1
0
 def read_packet(self):
     if self.socket != None:
         while self.socket.bytesAvailable() > 0 :
             if self.incoming_packet == None:
                 self.incoming_packet = packets.create_packet(self.socket.peek(1))
             if self.socket.bytesAvailable() >= self.incoming_packet.MAX_SIZE:
                 buf = self.socket.read(self.incoming_packet.MAX_SIZE)
                 packet = self.incoming_packet
                 self.incoming_packet = None
                 packet.deserialize(buf)
                 packet.dispatch(self)
                 packet.dispatch(self.robot_layer)
                 packet.dispatch(self.graph_routing_layer)
Exemplo n.º 2
0
 def read_packet(self):
     while True:
         if self.packet is None:
             try:
                 b = self.bytes_available()
                 if b == 0:
                     return
                 received_data = self.recv(1)
                 if len(received_data) == 0:
                     return
                 self.buffer += received_data
                 try:
                     self.packet = packets.create_packet(self.buffer)
                 except packets.PacketNotFoundException as e:
                     logger.error("Packet not found (type={})".format(
                         e.packet_type))
                     logger.error("Last good packet : {} size={}".format(
                         self.last_good_packet,
                         self.last_good_packet.get_size()))
             except socket.error as err:
                 if err.errno in [errno.EAGAIN, errno.EINTR]:
                     return
                 logger.log_exception(err)
                 return
         else:
             try:
                 packet_size = self.packet.get_size()
                 try:
                     if self.bytes_available() < packet_size - 1:
                         return
                     received_data = self.recv(packet_size -
                                               len(self.buffer))
                     if len(received_data) == 0:
                         return
                     self.buffer += received_data
                 except socket.error as err:
                     if err.errno in [errno.EAGAIN, errno.EINTR]:
                         return
                     logger.log_exception(err)
                     return
                 if len(self.buffer) == packet_size:
                     # A complete packet has been received, notify the state machine
                     self.packet.deserialize(self.buffer)
                     self.last_good_packet = self.packet
                     self.event_loop.process(self, self.packet)
                     self.buffer = bytes()
                     self.packet = None
             except Exception as e:
                 self.packet = None
                 self.buffer = bytes()
                 logger.log_exception(e)
Exemplo n.º 3
0
 def handle_read(self):
     if not self.synchronized:
         while self.bytes_available():
             data = self.recv(1)
             (packet_type,) = struct.unpack("<B", data)
             if packet_type in [ packets.TurretDetect.TYPE,
                                 packets.TurretInit.TYPE,
                                 packets.TurretDistances.TYPE,
                                 packets.TurretBoot.TYPE ]:
                 self.synchronized = True
                 self.buffer = data
                 self.packet = packets.create_packet(self.buffer)
                 break
     try:
         self.read_packet()
     except Exception as e:
         logger.log("Turret channel is desynchronized. Resynchronizing. Unknown packet type: {}".format(e.args[0]))
         self.synchronized = False
Exemplo n.º 4
0
 def read_packet(self):
     while True:
         if self.packet is None:
             try:
                 b = self.bytes_available()
                 if b == 0:
                     return
                 received_data = self.recv(1)
                 if len(received_data) == 0:
                     return
                 self.buffer += received_data
                 self.packet = packets.create_packet(self.buffer)
             except socket.error as err:
                 if err.errno in [errno.EAGAIN, errno.EINTR]:
                     return
                 logger.log_exception(err)
                 return
         else:
             try:
                 try:
                     if self.bytes_available() < self.packet.MAX_SIZE - 1:
                         return
                     received_data = self.recv(self.packet.MAX_SIZE -
                                               len(self.buffer))
                     if len(received_data) == 0:
                         return
                     self.buffer += received_data
                 except socket.error as err:
                     if err.errno in [errno.EAGAIN, errno.EINTR]:
                         return
                     logger.log_exception(err)
                     return
                 if len(self.buffer) == self.packet.MAX_SIZE:
                     # A complete packet has been received, notify the state machine
                     self.packet.deserialize(self.buffer)
                     self.event_loop.process(self, self.packet)
                     self.buffer = bytes()
                     self.packet = None
             except Exception as e:
                 self.packet = None
                 self.buffer = bytes()
                 logger.log_exception(e)
Exemplo n.º 5
0
 def handle_read(self):
     if not self.synchronized:
         while self.bytes_available():
             data = self.recv(1)
             (packet_type, ) = struct.unpack("<B", data)
             if packet_type in [
                     packets.TurretDetect.TYPE, packets.TurretInit.TYPE,
                     packets.TurretDistances.TYPE, packets.TurretBoot.TYPE
             ]:
                 self.synchronized = True
                 self.buffer = data
                 self.packet = packets.create_packet(self.buffer)
                 break
     try:
         self.read_packet()
     except Exception as e:
         logger.log(
             "Turret channel is desynchronized. Resynchronizing. Unknown packet type: {}"
             .format(e.args[0]))
         self.synchronized = False
Exemplo n.º 6
0
 def read_packet(self):
     while True :
         if self.packet is None:
             try:
                 b = self.bytes_available()
                 if b == 0:
                     return
                 received_data = self.recv(1)
                 if len(received_data) == 0:
                     return
                 self.buffer += received_data
                 self.packet = packets.create_packet(self.buffer)
             except socket.error as err:
                 if err.errno in [errno.EAGAIN, errno.EINTR]:
                     return
                 logger.log_exception(err)
                 return
         else:
             try:
                 try:
                     if self.bytes_available() < self.packet.MAX_SIZE - 1:
                         return
                     received_data = self.recv(self.packet.MAX_SIZE - len(self.buffer))
                     if len(received_data) == 0:
                         return
                     self.buffer += received_data
                 except socket.error as err:
                     if err.errno in [errno.EAGAIN, errno.EINTR]:
                         return
                     logger.log_exception(err)
                     return
                 if len(self.buffer) == self.packet.MAX_SIZE:
                     # A complete packet has been received, notify the state machine
                     self.packet.deserialize(self.buffer)
                     self.event_loop.process(self, self.packet)
                     self.buffer = bytes()
                     self.packet = None
             except Exception as e:
                 self.packet = None
                 self.buffer = bytes()
                 logger.log_exception(e)
Exemplo n.º 7
0
 def send_packet(self, packet: Packet):
     packet_id: int = packet.id
     data: bytes = packet.encode()
     packet_bytes = packets.create_packet(packet_id, data)
     self.send(packet_bytes)