def connection_handler(): while True: try: frame = connection.receive() if frame: message = frame.payload async_adapter.spawn( self.message_handler.handle_message, message, self.client ) except ( SystemExit, KeyboardInterrupt, ConnectionError, WampProtocolError, ): break
def _listen(self, connection): def connection_handler(): while True: try: frame = connection.receive() if frame: message = frame.payload # spawn a new green thread to process the Message to # ensure that we don't block, for example, an # Invocation may be expensive. # Messages make their way to the Client via the # `_message_queue`, else the handler can simply respond # to Messages such as Challenge without bothering the # Client Peer. async_adapter.spawn( self.message_handler.handle_message, message, ) except ( SystemExit, KeyboardInterrupt, ConnectionError, WampProtocolError, ): break gthread = async_adapter.spawn(connection_handler) self._managed_thread = gthread
def receive(self, bufsize=1): frame = None received_bytes = bytearray() while True: try: bytes_ = self.socket.recv(bufsize) except socket.timeout as e: message = str(e) raise ConnectionError('timeout: "{}"'.format(message)) except Exception as exc: raise ConnectionError('Connection lost: "{}"'.format(exc)) if not bytes_: break received_bytes.extend(bytes_) try: frame = FrameFactory.from_bytes(received_bytes) except IncompleteFrameError as exc: bufsize = exc.required_bytes else: if frame.opcode == frame.OPCODE_PING: # Opcode 0x9 marks a ping frame. It does not contain wamp # data, so the frame is not returned. # Still it must be handled or the server will close the # connection. async_adapter.spawn(self.handle_ping(ping_frame=frame)) received_bytes = bytearray() continue if frame.opcode == frame.OPCODE_PONG: self.handle_pong(pong_frame=frame) received_bytes = bytearray() continue if frame.opcode == frame.OPCODE_BINARY: break if frame.opcode == frame.OPCODE_CLOSE: self.handle_close(close_frame=frame) break if frame is None: raise WampProtocolError("No frame returned") return frame
def _listen(self, connection, message_queue): def connection_handler(): while True: try: frame = connection.receive() if frame: message = frame.payload async_adapter.spawn( self.message_handler.handle_message, message, self.client ) except ( SystemExit, KeyboardInterrupt, ConnectionError, WampProtocolError, ): break gthread = async_adapter.spawn(connection_handler) self._managed_thread = gthread
def pinger(sc): # do i need to spawn here??? async_adapter.spawn(send_ping_and_expect_pong) s.enter(heartbeat, 1, pinger, (sc, ))
def start_pinging(self): def websocket_ping_thread(socket): s = sched.scheduler(time, async_adapter.sleep) def send_ping_and_expect_pong(): payload = 'wampy::' + str(uuid.uuid4()) # we send a Ping with a unique payload, and we expect # a Pong back echoing the same payload - but within the # deadline of ``heartbeat_timeout_seconds``. ping = Ping(payload=payload, mask_payload=True) try: socket.sendall(bytes(ping.frame)) except OSError: # connection closed by parent thread, or wampy # has been disconnected from server... # either way, this gthread will be killed as # soon if the Close message is received else # schedule another Ping logger.info('ping failed') pass except BrokenPipeError: logger.info('server ripped out from under us!') pass pong = None with async_adapter.Timeout(heartbeat_timeout, raise_after=False): while pong is None: # required for Hub to implelent TimeOut async_adapter.sleep() try: maybe_my_pong = self.pongs.get(block=False) except async_adapter.QueueEmpty: continue if maybe_my_pong: if maybe_my_pong.payload == payload: pong = maybe_my_pong else: # possibly Pinging faster than the server is # Ponging, else Hub hasn't scheduled the right # gthread yet. logger.error('Pongs out of order?') self.pongs.put(maybe_my_pong) if pong is None: logger.info('missed a Pong from the server') self.missed_pongs += 1 def pinger(sc): # do i need to spawn here??? async_adapter.spawn(send_ping_and_expect_pong) s.enter(heartbeat, 1, pinger, (sc, )) s.enter(heartbeat, 1, pinger, (s, )) # the first Ping will be emitted after the first "hearbeat" # seconds, i.e. *not* immediatly s.run() self.pinger_thread = async_adapter.spawn(websocket_ping_thread, self.socket) self.is_pinging = True
def pinger(sc): # do i need to spawn here??? async_adapter.spawn(send_ping_and_expect_pong) s.enter(heartbeat, 1, pinger, (sc,))
def start_pinging(self): def websocket_ping_thread(socket): s = sched.scheduler(time, async_adapter.sleep) def send_ping_and_expect_pong(): payload = 'wampy::' + str(uuid.uuid4()) # we send a Ping with a unique payload, and we expect # a Pong back echoing the same payload - but within the # deadline of ``heartbeat_timeout_seconds``. ping = Ping(payload=payload, mask_payload=True) try: socket.sendall(bytes(ping.frame)) except OSError: # connection closed by parent thread, or wampy # has been disconnected from server... # either way, this gthread will be killed as # soon if the Close message is received else # schedule another Ping logger.info('ping failed') pass except BrokenPipeError: logger.info('server ripped out from under us!') pass pong = None with async_adapter.Timeout( heartbeat_timeout, raise_after=False ): while pong is None: # required for Hub to implelent TimeOut async_adapter.sleep() try: maybe_my_pong = self.pongs.get(block=False) except async_adapter.QueueEmpty: continue if maybe_my_pong: if maybe_my_pong.payload == payload: pong = maybe_my_pong else: # possibly Pinging faster than the server is # Ponging, else Hub hasn't scheduled the right # gthread yet. logger.error('Pongs out of order?') self.pongs.put(maybe_my_pong) if pong is None: logger.info('missed a Pong from the server') self.missed_pongs += 1 def pinger(sc): # do i need to spawn here??? async_adapter.spawn(send_ping_and_expect_pong) s.enter(heartbeat, 1, pinger, (sc,)) s.enter(heartbeat, 1, pinger, (s,)) # the first Ping will be emitted after the first "hearbeat" # seconds, i.e. *not* immediatly s.run() self.pinger_thread = async_adapter.spawn( websocket_ping_thread, self.socket ) self.is_pinging = True