def read_from(self, src_addr, size=65535, timeout=None): ''' Receive at most SIZE bytes of data, that were sent from host with address and port SRC_ADDR and SRC_PORT. Any incoming data that are not from the specified address will be discarded! If TIMEOUT is specified, socket will be set to non-blocking mode. If no data is received until timeout, the function returns None. Otherwise, if TIMEOUT is None, the socket will be in blocking mode and will wait for any incoming data. Specifying TIMEOUT to 0 will make the function return immediately, if there is no incoming data. @type size: int @param size: Size of the buffer used to receive data. @type timeout: int @param timeout: Socket timeout. @type src_addr: (str, int) @param src_addr: Tuple containing source address and port, that we want to receive data from. @return: Received data, or None if no data is received until timeout. ''' try: self.s.settimeout(timeout) sender = None while sender != src_addr: (data, sender) = self.s.recvfrom(size) return packet.parse(data) except socket.timeout: return None
def read(self): images = packet.parse(self._receive) logger.debug('received {} images'.format(len(images))) if len(images) == 2: (color, cloud) = images depth_uv = None color_uv = None elif len(images) == 3: (color, cloud, depth_uv) = images color_uv = None elif len(images) == 4: (color, cloud, depth_uv, color_uv) = images else: logger.warn('unrecognized number of images from camera') raise RuntimeError('unrecognized number of images from camera') if cloud is not None: cloud[:,:,1] *= -1 # flip y axis if depth_uv is not None: depth_uv = depth_uv[:,:,::-1] depth_uv = unnormalize_uv_map(depth_uv, color.shape) if color_uv is not None: color_uv = color_uv[:,:,::-1] color_uv = unnormalize_uv_map(color_uv, (cloud.shape[0], cloud.shape[1])) return (color, cloud, depth_uv, color_uv)
def rxLoop(self): while 1: pkt = packet.parse(self.socket.recv(self.config.getInt("NetworkBuffer"))) if self.onRecievePacketFunc!=None: self.onRecievePacketFunc(pkt) else: self.packets.append(pkt)
def __init__(self, parent, connection, addr, logger, taskmanager, config): self.parent = parent self.connection = connection self.address = addr self.logger = logger self.taskmanager = taskmanager self.config = config self.name = "" self.password = "" self.packets = [] self.running = True while 1: data = self.connection.recv(self.config.getInt("NetworkBuffer")) try: pkt = packet.parse(data) if pkt["ID"]==packet.PacketID.LOGIN: self.name = pkt["VALUE"][0] self.password = pkt["VALUE"][1] break except: self.logger.error("Invalid Packet Recieved from "+self.getAddress().getStringAddress()+": "+str(data)) self.logger.debug("Client Connected with the username '"+self.getName()+"' and the password '"+self.password+"'") self.eventhandler = eventhandler.EventHandler(parent, self.name+"-Client", self.logger, self.taskmanager) self.taskmanager.addThread(self.getName(), self.rxLoop)
def poll(self): if self.serial_port.in_waiting > 0: self.pkt_buffer.extend( self.serial_port.read(self.serial_port.in_waiting)) while True: if 0 in self.pkt_buffer: index = self.pkt_buffer.index(0) data = cobs.decode(self.pkt_buffer[0:index]) del self.pkt_buffer[0:index + 1] if data is None or len(data) < 6: self.rx_errs += 1 continue pkt = packet.parse(data) if pkt is not None: if self.raw_log_callback: self.raw_log_callback(0, data) self.rx_pkts += 1 return pkt else: self.rx_errs += 1 else: return None
def read(self): images = packet.parse(self._receive) logger.debug('received {} images'.format(len(images))) if len(images) == 2: (color, cloud) = images depth_uv = None elif len(images) == 3: (color, cloud, depth_uv) = images else: logger.warn('unrecognized number of images from camera') raise RuntimeError('unrecognized number of images from camera') return (color, cloud, depth_uv)
def receive(session): data = None try: recvpacket = session.socket.recvfrom(4096) data = recvpacket[0].decode('utf-8') addr = recvpacket[1] log.debug("Packet Received:\n"+data) except socket.timeout: log.error("Receive Timeout") data = None if data != None: return packet.parse(data, addr, session)
def read(self): images = packet.parse(self._receive) logger.debug('received {} images'.format(len(images))) if len(images) == 2: (color, cloud) = images depth_uv = None elif len(images) == 3: (color, cloud, depth_uv) = images elif len(images) == 4: (color, cloud, depth_uv, inverse_uv) = images else: logger.warn('unrecognized number of images from camera') raise RuntimeError('unrecognized number of images from camera') return (color, cloud, depth_uv, inverse_uv)
def receive(self): xbpkt = self.xbif.receive() if xbpkt is None or not isinstance(xbpkt, xbee.RxPacket): return None self.rssi = -xbpkt.rssi pkt = packet.parse(xbpkt.data) if pkt is not None: self.rx_pkts += 1 return pkt else: self.rx_errs += 1 return None
def poll(self): while True: xbpkt = self.xbif.poll() if xbpkt is None: return None if not isinstance(xbpkt, xbee.RxPacket): continue self.rssi = -xbpkt.rssi pkt = packet.parse(xbpkt.data) if pkt is not None: self.rx_pkts += 1 return pkt else: self.rx_errs += 1
def rxLoop(self): while self.running: pkt = {"ID": None, "VALUE": None} data = "" try: try: #data = self.connection.recv(self.config.getInt("NetworkBuffer")) while data != "[": data = self.connection.recv(1) rx = "" while rx != "]": rx = self.connection.recv(1) data = data + rx if self.config.getBoolean("DebugMode"): self.logger.debug("Received Packet from the client "+self.getName()+": "+data) try: pkt = packet.parse(data) except: self.logger.error("Packet Parsing Failure.") self.send(packet.PacketID.INVALID_PACKET_SYNTAX, None) except: if not self.send(packet.PacketID.PING, None): self.logger.error("Packet Reception Failure.") self.logger.debug("'"+self.getName()+"' Disconnected.") self.parent.disconnectClient(self.getName()) break if pkt["ID"]==packet.PacketID.DISCONNECT: self.logger.debug("'"+self.getName()+"' Disconnected.") self.parent.disconnectClient(self.getName()) break elif pkt["ID"]==packet.PacketID.PING: self.send(packet.PacketID.PING, None) else: event = events.OnPacketRecieved(pkt) self.eventhandler.callEvents(events.EventType.ON_PACKET_RECEIVED, event) except: self.logger.printErrorStack()
def read(self, size=65535, timeout=None): ''' Receive data from the network socket. If TIMEOUT is specified, socket will be set to non-blocking mode. If no data is received until timeout, the function returns None. Otherwise, if TIMEOUT is None, the socket will be in blocking mode and will wait for any incoming data. Specifying TIMEOUT to 0 will make the function return immediately, if there is no incoming data. @type size: int @param size: Size of the buffer used to receive data. @type timeout: int @param timeout: Socket timeout. @return: Tuple containing received data and sender address, or None if no data is received until timeout. ''' try: self.s.settimeout(timeout) (data, sender) = self.s.recvfrom(size) p = packet.parse(data) return (p, sender) except socket.timeout: return None
def receive(self): data = self.socket.recvfrom(1500)[0] if self.raw_log_callback: self.raw_log_callback(0, data) return packet.parse(data)