Example #1
0
 async def read_one_message(self) -> Message:
     size = await self.reader.readexactly(LENGTH_BYTES)
     full_message_length = int.from_bytes(size, "big")
     full_message: bytes = await self.reader.readexactly(full_message_length)
     full_message_loaded: Any = cbor.loads(full_message)
     self.bytes_read += LENGTH_BYTES + full_message_length
     self.last_message_time = time.time()
     return Message(full_message_loaded["f"], full_message_loaded["d"])
    async def _read_one_message(self) -> Optional[Payload]:
        try:
            message: WSMessage = await self.ws.receive(30)
        except asyncio.TimeoutError:
            # self.ws._closed if we didn't receive a ping / pong
            if self.ws._closed:
                asyncio.create_task(self.close())
                await asyncio.sleep(3)
                return None
            return None

        if self.connection_type is not None:
            connection_type_str = NodeType(self.connection_type).name.lower()
        else:
            connection_type_str = ""
        if message.type == WSMsgType.CLOSING:
            self.log.info(
                f"Closing connection to {connection_type_str} {self.peer_host}:"
                f"{self.peer_server_port}/"
                f"{self.peer_port}")
        elif message.type == WSMsgType.CLOSE:
            self.log.info(
                f"Peer closed connection {connection_type_str} {self.peer_host}:"
                f"{self.peer_server_port}/"
                f"{self.peer_port}")
            asyncio.create_task(self.close())
            await asyncio.sleep(3)
        elif message.type == WSMsgType.CLOSED:
            if not self.closed:
                asyncio.create_task(self.close())
                await asyncio.sleep(3)
                return None
        elif message.type == WSMsgType.BINARY:
            data = message.data
            full_message_loaded: Any = cbor.loads(data)
            self.bytes_read += len(data)
            self.last_message_time = time.time()
            msg = Message(full_message_loaded["f"], full_message_loaded["d"])
            payload_id = full_message_loaded["i"]
            payload = Payload(msg, payload_id)
            return payload
        else:
            self.log.error(f"Unexpected WebSocket message type: {message}")
            asyncio.create_task(self.close())
            await asyncio.sleep(3)
        return None
Example #3
0
    async def read_one_message(self) -> Message:
        size: bytes = b""
        try:
            # Need timeout here in case connection is closed, this allows GC to clean up
            size = await asyncio.wait_for(
                self.reader.readexactly(LENGTH_BYTES), timeout=10 * 60)
        except asyncio.TimeoutError:
            raise TimeoutError("self.reader.readexactly(LENGTH_BYTES)")

        full_message_length = int.from_bytes(size, "big")

        full_message: bytes = b""
        try:
            # Need timeout here in case connection is closed, this allows GC to clean up
            full_message = await asyncio.wait_for(
                self.reader.readexactly(full_message_length), timeout=10 * 60)
        except asyncio.TimeoutError:
            raise TimeoutError("self.reader.readexactly(full_message_length)")

        full_message_loaded: Any = cbor.loads(full_message)
        self.bytes_read += LENGTH_BYTES + full_message_length
        self.last_message_time = time.time()
        return Message(full_message_loaded["f"], full_message_loaded["d"])
Example #4
0
 def test_recursive_types(self):
     coin: Optional[Coin] = None
     l1 = [(bytes32([2] * 32), coin)]
     rr = RespondRemovals(uint32(1), bytes32([1] * 32), l1, None)
     c = cbor.loads(cbor.dumps(rr))
     RespondRemovals(c["height"], c["header_hash"], c["coins"], c["proofs"])