Example #1
0
    def tcpserver_loop(self):
        """
        (internal use) message loop for TCP socket

        :return:
        """
        self.logger.debug("Start tcpserver_loop")
        msg_parsers = dict()
        readfds = set()
        if self.listen_socket:
            readfds.add(self.listen_socket)
        if self.listen_socket6:
            readfds.add(self.listen_socket6)
        try:
            while True:
                rready, wready, xready = select.select(readfds, [], [])
                for sock in rready:
                    if sock is self.listen_socket:
                        conn, address = self.listen_socket.accept()
                        conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                        1)
                        #print("accept from ipv4: ", address)
                        readfds.add(conn)
                        msg_parsers[conn] = message_key_types.Message()
                    elif sock is self.listen_socket6:
                        conn, address = self.listen_socket6.accept()
                        conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                        1)
                        #print("accept from ipv6: ", address)
                        readfds.add(conn)
                        msg_parsers[conn] = message_key_types.Message()
                    else:
                        buf = sock.recv(8192)
                        if len(buf) == 0:
                            del msg_parsers[sock]
                            sock.close()
                            readfds.remove(sock)
                        else:
                            msg_parsers[sock].recv(buf)
                            self.stats.update_stats_increment(
                                "network", "message_size_received_by_tcy",
                                len(buf))
                            while True:
                                msg = msg_parsers[sock].parse()
                                if msg is None:
                                    break
                                #self.logger.debug("Recv_TCP at %s: data=%s" % (sock.getsockname(), msg))
                                if KeyType.destination_node_id not in msg or KeyType.domain_id not in msg:
                                    continue
                                self.process_message_base(
                                    msg[KeyType.domain_id], None, None, None,
                                    msg, msg_parsers[sock].payload_type)

        finally:
            for sock in readfds:
                sock.close()
            self.listen_socket = None
            self.listen_socket6 = None
Example #2
0
    def tcpserver_loop(self):
        """
        (internal use) message loop for TCP socket

        :return:
        """
        self.logger.debug("Start tcpserver_loop")
        msg_parsers = dict()
        readfds = set()
        if self.listen_socket:
            readfds.add(self.listen_socket)
        if self.listen_socket6:
            readfds.add(self.listen_socket6)
        try:
            while True:
                rready, wready, xready = select.select(readfds, [], [])
                for sock in rready:
                    if sock is self.listen_socket:
                        conn, address = self.listen_socket.accept()
                        readfds.add(conn)
                        msg_parsers[conn] = message_key_types.Message()
                    elif sock is self.listen_socket6:
                        conn, address = self.listen_socket6.accept()
                        readfds.add(conn)
                        msg_parsers[conn] = message_key_types.Message()
                    else:
                        buf = sock.recv(8192)
                        if len(buf) == 0:
                            del msg_parsers[sock]
                            sock.close()
                            readfds.remove(sock)
                        else:
                            msg_parsers[sock].recv(buf)
                            while True:
                                msg = msg_parsers[sock].parse()
                                if msg is None:
                                    break
                                #self.logger.debug("Recv_TCP at %s: data=%s" % (sock.getsockname(), msg))
                                if msg_parsers[
                                        sock].payload_type == PayloadType.Type_msgpack:
                                    if KeyType.destination_node_id not in msg or KeyType.domain_id not in msg:
                                        continue
                                self.domains[msg[
                                    KeyType.domain_id]].process_message_base(
                                        True, None, msg,
                                        msg_parsers[sock].payload_type)
        finally:
            for sock in readfds:
                sock.close()
            self.listen_socket = None
            self.listen_socket6 = None
Example #3
0
def test_make_message2():
    """
    """
    data = {
        message_key_types.KeyType.command: MsgType.MESSAGE,
        message_key_types.KeyType.asset_group_id: "asset_group_id_001",
        message_key_types.KeyType.source_user_id: "source_user_id_12345",
        message_key_types.KeyType.query_id: "query_id_67890",
        message_key_types.KeyType.status: bbc_error.ESUCCESS,
    }
    message = message_key_types.make_message(
        message_key_types.PayloadType.Type_msgpack, data, 0)
    # print(message)
    assert (message[:message_key_types.Message.HEADER_LEN] ==
            b'\x00\x02\x00\x00\x00\x00\x00S')

    msg_obj = message_key_types.Message()
    msg_obj.recv(bytes(message))
    parsed_data = msg_obj.parse()
    assert (msg_obj.payload_type == message_key_types.PayloadType.Type_msgpack)
    assert (msg_obj.format_version == 0)
    assert (msg_obj.msg_len == 83)  # 'S' == 83
    print(parsed_data)
    assert (parsed_data[message_key_types.KeyType.command] == MsgType.MESSAGE)
    assert (_bytes_to_str(parsed_data[
        message_key_types.KeyType.asset_group_id]) == "asset_group_id_001")
    assert (_bytes_to_str(parsed_data[
        message_key_types.KeyType.source_user_id]) == "source_user_id_12345")
    assert (_bytes_to_str(
        parsed_data[message_key_types.KeyType.query_id]) == "query_id_67890")
    assert (
        parsed_data[message_key_types.KeyType.status] == bbc_error.ESUCCESS)
Example #4
0
def dummy_server_loop(socket, address):
    msg_parser = message_key_types.Message()
    try:
        while True:
            wait_read(socket.fileno())
            buf = socket.recv(8192)
            if len(buf) == 0:
                break
            msg_parser.recv(buf)
            while True:
                msg = msg_parser.parse()
                if msg is None:
                    break
                result_queue.put(msg)
    except:
        print("## disconnected")
Example #5
0
 def receiver_loop(self):
     msg_parser = message_key_types.Message()
     try:
         while True:
             buf = self.connection.recv(8192)
             if len(buf) == 0:
                 break
             msg_parser.recv(buf)
             while True:
                 msg = msg_parser.parse()
                 if msg is None:
                     break
                 self.callback.dispatch(msg, msg_parser.payload_type)
     except Exception as e:
         self.logger.info("TCP disconnect: %s" % e)
         print(traceback.format_exc())
     self.connection.close()
Example #6
0
    def udp_message_loop(self):
        """
        (internal use) message loop for UDP socket

        :return:
        """
        self.logger.debug("Start udp_message_loop")
        msg_parser = message_key_types.Message()
        # readfds = set([self.socket_udp, self.socket_udp6])
        readfds = set()
        if self.socket_udp:
            readfds.add(self.socket_udp)
        if self.socket_udp6:
            readfds.add(self.socket_udp6)
        try:
            while True:
                rready, wready, xready = select.select(readfds, [], [])
                for sock in rready:
                    data = None
                    ip4 = True
                    if sock is self.socket_udp:
                        data, addr = self.socket_udp.recvfrom(1500)
                    elif sock is self.socket_udp6:
                        data, addr = self.socket_udp6.recvfrom(1500)
                        ip4 = False
                    if data is not None:
                        msg_parser.recv(data)
                        msg = msg_parser.parse()
                        #self.logger.debug("Recv_UDP from %s: data=%s" % (addr, msg))
                        if msg_parser.payload_type == PayloadType.Type_msgpack:
                            if KeyType.domain_ping in msg:
                                self.receive_domain_ping(ip4, addr, msg)
                                continue
                            if KeyType.destination_node_id not in msg or KeyType.domain_id not in msg:
                                continue
                            if msg[KeyType.domain_id] in self.domains:
                                self.domains[msg[
                                    KeyType.domain_id]].process_message_base(
                                        ip4, addr, msg,
                                        msg_parser.payload_type)
        finally:
            for sock in readfds:
                sock.close()
            self.socket_udp = None
            self.socket_udp6 = None
Example #7
0
    def udp_message_loop(self):
        """
        (internal use) message loop for UDP socket

        :return:
        """
        self.logger.debug("Start udp_message_loop")
        msg_parser = message_key_types.Message()
        # readfds = set([self.socket_udp, self.socket_udp6])
        readfds = set()
        if self.socket_udp:
            readfds.add(self.socket_udp)
        if self.socket_udp6:
            readfds.add(self.socket_udp6)
        try:
            while True:
                rready, wready, xready = select.select(readfds, [], [])
                for sock in rready:
                    data = None
                    ipv4 = None
                    ipv6 = None
                    if sock is self.socket_udp:
                        data, (ipv4, port) = self.socket_udp.recvfrom(1500)
                    elif sock is self.socket_udp6:
                        data, (ipv6, port) = self.socket_udp6.recvfrom(1500)
                    if data is not None:
                        self.stats.update_stats_increment(
                            "network", "packets_received_by_udp", 1)
                        msg_parser.recv(data)
                        msg = msg_parser.parse()
                        #self.logger.debug("Recv_UDP from %s: data=%s" % (addr, msg))
                        if KeyType.domain_id not in msg:
                            continue
                        if msg[KeyType.domain_id] in self.domains:
                            self.process_message_base(msg[KeyType.domain_id],
                                                      ipv4, ipv6, port, msg,
                                                      msg_parser.payload_type)
        finally:
            for sock in readfds:
                sock.close()
            self.socket_udp = None
            self.socket_udp6 = None