Exemplo n.º 1
0
    def __init__(self):
        self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
        self.head_len = struct.calcsize(self.handfrt)
        self.identifier = 0

        self.encode_ins = AesEncoder()
        self.version = 0

        self._buffer = b""  # 数据缓冲buffer
        self._head = None  # 消息头, list,   [message_length, command_id, version]
        self.transport = None
        super().__init__()
Exemplo n.º 2
0
class RpcProtocol(object):
    """消息协议,包含消息处理"""
    def __init__(self):
        self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
        self.head_len = struct.calcsize(self.handfrt)
        self.identifier = 0

        self.encode_ins = AesEncoder()
        self.version = 0

        self._buffer = b""  # 数据缓冲buffer
        self._head = None  # 消息头, list,   [message_length, command_id, version]
        self.transport = None
        super().__init__()

    def pack(self, data, command_id):
        """
        打包消息, 用於傳輸
        :param data:  傳輸數據
        :param command_id:  消息ID
        :return: bytes
        """
        print(type(data))
        data = self.encode_ins.encode(data)
        # data = "%s" % data
        length = data.__len__() + self.head_len
        head = struct.pack(self.handfrt, length, command_id, self.version)
        print("type=", type(head), type(data), [head], head[0])
        print(struct.unpack(self.handfrt, head))
        return head + data
Exemplo n.º 3
0
class RpcProtocol(object):
    """消息协议,包含消息处理"""
    def __init__(self):
        self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
        self.head_len = struct.calcsize(self.handfrt)
        self.identifier = 0

        self.encode_ins = AesEncoder()
        self.version = 0

        self._buffer = b""  # 数据缓冲buffer
        self._head = None  # 消息头, list,   [message_length, command_id, version]
        self.transport = None
        super().__init__()

    def pack(self, data, command_id):
        """
        打包消息, 用於傳輸
        :param data:  傳輸數據
        :param command_id:  消息ID
        :return: bytes
        """
        data = self.encode_ins.encode(data)
        length = data.__len__() + self.head_len
        head = struct.pack(self.handfrt, length, command_id, self.version)
        return head + data

    def unpack(self, pack_data):
        head_len = self.head_len
        if head_len > len(pack_data):
            return None

        data_head = pack_data[0:head_len]
        list_head = struct.unpack(self.handfrt, data_head)
        data = pack_data[head_len:]
        result = self.encode_ins.decode(data)
        if not result:
            result = {}

        return {'result': True, 'command': list_head[1], 'data': result}
Exemplo n.º 4
0
    def __init__(self,
                 ws_handler,
                 ws_server,
                 *,
                 origins=None,
                 extensions=None,
                 subprotocols=None,
                 extra_headers=None,
                 **kwds):
        self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
        self.head_len = struct.calcsize(self.handfrt)
        self.seq = None
        self.session_id = ""

        self.encode_ins = AesEncoder(
            GlobalObject().rpc_password,
            encode_type=GlobalObject().rpc_encode_type)
        self.version = 0

        self._buffer = b""  # 数据缓冲buffer
        self._head = None  # 消息头, list,   [message_length, command_id, version]
        self.transport = None
        super().__init__(ws_handler, ws_server, **kwds)
Exemplo n.º 5
0
class DataPack(object):
    def __init__(self, password, is_encode=0):
        self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
        self.identifier = 0
        self.aes_ins = AesEncoder()
        self.version = 0

    def get_head_len(self):
        return struct.calcsize(self.handfrt)

    def unpack(self, pack_data):
        head_len = self.get_head_len()
        if head_len > len(pack_data):
            return None

        data_head = pack_data[0:head_len]
        list_head = struct.unpack(self.handfrt, data_head)
        data = pack_data[head_len:]
        result = self.aes_ins.decode_aes(data)
        if not result:
            result = {}

        return {'result': True, 'command': list_head[1], 'data': result}

    def pack(self, data, command_id):
        """
        打包消息, 用於傳輸
        :param data:  傳輸數據
        :param command_id:  消息ID
        :return:
        """
        data = self.aes_ins.encode(data)
        data = "%s" % data
        length = data.__len__() + self.get_head_len()
        head = struct.pack(self.handfrt, length, command_id, self.version)
        return str(head + data)
Exemplo n.º 6
0
 def __init__(self, password, is_encode=0):
     self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
     self.identifier = 0
     self.aes_ins = AesEncoder()
     self.version = 0
Exemplo n.º 7
0
class WebSocketProtocol(WebSocketServerProtocol):
    """消息协议,包含消息处理"""

    cur_seq = 0
    lock = threading.RLock()

    @classmethod
    def gen_new_seq(cls):
        cls.lock.acquire()
        try:
            cls.cur_seq += 1
            return cls.cur_seq
        finally:
            cls.lock.release()

    def __init__(self,
                 ws_handler,
                 ws_server,
                 *,
                 origins=None,
                 extensions=None,
                 subprotocols=None,
                 extra_headers=None,
                 **kwds):
        self.handfrt = "iii"  # (int, int, int)  -> (message_length, command_id, version)
        self.head_len = struct.calcsize(self.handfrt)
        self.seq = None
        self.session_id = ""

        self.encode_ins = AesEncoder(
            GlobalObject().rpc_password,
            encode_type=GlobalObject().rpc_encode_type)
        self.version = 0

        self._buffer = b""  # 数据缓冲buffer
        self._head = None  # 消息头, list,   [message_length, command_id, version]
        self.transport = None
        super().__init__(ws_handler, ws_server, **kwds)

    def connection_open(self):
        super().connection_open()
        self.seq = WebSocketProtocol.gen_new_seq()
        self.session_id = "{}_{}".format(GlobalObject().id, self.seq)
        WebsocketConnectionManager().store_connection(self.seq, self)

    def connection_made(self, transport):
        super().connection_made(transport)
        self.transport = transport

    def connection_lost(self, exc):
        super().connection_lost(exc)
        WebsocketConnectionManager().remove_connection(self)
        SessionCache().del_cache(self.session_id)
        # client连接断开
        asyncio.ensure_future(websocket_route.call_target(
            CLIENT_OFFLINE, {}, session_id=self.session_id),
                              loop=GlobalObject().loop)
        # GlobalObject().loop.run_until_complete(
        #     websocket_route.call_target(CLIENT_OFFLINE, {}, session_id=self.session_id))

    async def send_message(self, result, command_id):
        data = self.pack(result, command_id)
        logger.debug("send_message:{}".format([data, type(self), self]))
        await self.send(data)

    def pack(self, data, command_id):
        """
        打包消息, 用於傳輸
        :param data:  傳輸數據
        :param command_id:  消息ID
        :return: bytes
        """
        data = self.encode_ins.encode(data)
        length = data.__len__() + self.head_len
        head = struct.pack(self.handfrt, length, command_id, self.version)
        return head + data

    async def process_data(self, data, websocket):
        if isinstance(data, str):
            data = bytes(data, encoding='utf8')
        self._buffer += data
        _buffer = None
        if self._head is None:
            if len(self._buffer) < self.head_len:
                return

            self._head = struct.unpack(self.handfrt,
                                       self._buffer[:self.head_len])  # 包头
            self._buffer = self._buffer[self.head_len:]
        content_len = self._head[0] - self.head_len
        if len(self._buffer) >= content_len:
            data = self.encode_ins.decode(self._buffer[:content_len])  # 解密
            if not data:
                raise DataException()
            await self.message_handle(self._head[1], self._head[2], data,
                                      websocket)

            _buffer = self._buffer[content_len:]
            self._buffer = b""
            self._head = None
        return _buffer

    async def message_handle(self, command_id, version, data, websocket):
        """
        实际处理消息
        :param command_id:
        :param version:
        :param data:
        :return:
        """
        logger.debug("message_handle:{}".format(
            [data, websocket, type(websocket)]))
        result = await websocket_route.call_target(command_id,
                                                   data,
                                                   session_id=self.session_id)
        if result:
            websocket.send(self.pack(result, command_id).decode("utf8"))