def _process( self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool], handle_pool: ThreadPool, c_recv: multiprocessing_connection.Connection, c_send: multiprocessing_connection.Connection, wait=multiprocessing_connection.wait): while True: try: for conn in wait(list(conn_lru_dict.keys()) + [c_recv]): if conn == c_recv: c_recv.recv_bytes() continue del conn_lru_dict[conn] if not conn.closed: handle_pool.spawn(self._handle, conn_lru_dict, conn, c_send) else: self.logger.debug("conn %s was closed" % conn) except OSError as e: if getattr(e, "winerror", 0) == 6: continue logging.exception("OSError") except: logging.exception("error")
class RawSocketHandler(socketserver.BaseRequestHandler): def setup(self): registry.append(self) def handle(self): self.conn = Connection(self.request.detach()) while self.conn._handle: try: obj = json.loads(self.conn.recv_bytes().decode('utf-8')) except EOFError: break if obj['type'] == 'message': bus.post(nt_from_dict(Message, obj['message'], None)) self.conn.send_bytes(json.dumps({'ret': True}).encode('utf-8')) elif obj['type'] == 'request': m = bus.post_sync(nt_from_dict(Message, obj['message'], None)) if m: ret = {"ret": True, "response": m._asdict()} else: ret = {"ret": False, "response": None} self.conn.send_bytes(json.dumps(ret).encode('utf-8')) def send(self, msg): if isinstance(msg, Message): ret = {"type": "message", "message": msg._asdict()} else: ret = {"type": "response", "response": msg._asdict()} self.conn.send_bytes(json.dumps(ret).encode('utf-8')) def finish(self): registry.remove(self) def close(self): self.conn.close()
def _handle(self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool], conn: multiprocessing_connection.Connection, c_send: multiprocessing_connection.Connection): try: data = conn.recv_bytes() if not data: raise EOFError self.logger.debug("parse conn %s" % conn) # self.logger.debug(data) try: result = self.handle(data) if result is not None: conn.send_bytes(result) except Exception: self.logger.exception("handle error") conn_lru_dict[conn] = True c_send.send_bytes(b'ok') except OSError: self.logger.debug("conn %s was closed" % conn) conn.close() except EOFError: self.logger.debug("conn %s was eof" % conn) conn.close() except BrokenPipeError: self.logger.debug("conn %s was broken" % conn) conn.close()
def _handle( self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool], conn: multiprocessing_connection.Connection, c_send: multiprocessing_connection.Connection): try: data = conn.recv_bytes() if not data: raise EOFError self.logger.debug("parse conn %s" % conn) # self.logger.debug(data) try: result = self.handle(data) if result is not None: conn.send_bytes(result) except Exception: self.logger.exception("handle error") conn_lru_dict[conn] = True c_send.send_bytes(b'ok') except OSError: self.logger.debug("conn %s was closed" % conn) conn.close() except EOFError: self.logger.debug("conn %s was eof" % conn) conn.close() except BrokenPipeError: self.logger.debug("conn %s was broken" % conn) conn.close()
def _receiver_thread(self, conn: Connection): with conn: while True: try: buf = conn.recv_bytes() except EOFError: break channel, data = unpack_message(buf) logger.debug("Received data from %s.", channel) self._publisher.publish(channel, data) logger.info("Source connection closed.")
def _process(self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool], handle_pool: ThreadPool, c_recv: multiprocessing_connection.Connection, c_send: multiprocessing_connection.Connection, wait=multiprocessing_connection.wait): while True: try: for conn in wait(list(conn_lru_dict.keys()) + [c_recv]): if conn == c_recv: c_recv.recv_bytes() continue del conn_lru_dict[conn] if not conn.closed: handle_pool.spawn(self._handle, conn_lru_dict, conn, c_send) else: self.logger.debug("conn %s was closed" % conn) except OSError as e: if getattr(e, "winerror", 0) == 6: continue logging.exception("OSError") except: logging.exception("error")
class RawSocketHandler(socketserver.BaseRequestHandler): def setup(self): registry.append(self) def handle(self): self.conn = Connection(self.request.detach()) while self.conn._handle: try: obj = json.loads(self.conn.recv_bytes().decode('utf-8')) except EOFError: break if obj['type'] == 'message': bus.post(nt_from_dict(Message, obj['message'], None)) self.conn.send_bytes( json.dumps({ 'ret': True }).encode('utf-8')) elif obj['type'] == 'request': m = bus.post_sync( nt_from_dict(Message, obj['message'], None)) if m: ret = {"ret": True, "response": m._asdict()} else: ret = {"ret": False, "response": None} self.conn.send_bytes(json.dumps(ret).encode('utf-8')) def send(self, msg): if isinstance(msg, Message): ret = {"type": "message", "message": msg._asdict()} else: ret = {"type": "response", "response": msg._asdict()} self.conn.send_bytes(json.dumps(ret).encode('utf-8')) def finish(self): registry.remove(self) def close(self): self.conn.close()
def recv(conn: Connection): while True: t = time.time() result = SDRPlay.bytes_to_iq(conn.recv_bytes()) print("UNPACK", time.time() - t)
def recv(conn: Connection): while True: t = time.time() result = SDRPlay.unpack_complex(conn.recv_bytes()) print("UNPACK", time.time()-t)