コード例 #1
0
def test_deliver_order():
    server = Server(host=None)
    setup_server(server)
    server.users[2].logout()

    # first send from u2 to u3
    conn = server.connections[1]
    server._process_send(
        conn,
        [server.users[1].username, server.users[2].username, 'test message!'])
    server._process_send(conn, [
        server.users[1].username, server.users[2].username,
        'another test message'
    ])

    # then login as u3 and get all the messages
    conn = server.connections[2]
    server.users[2].login(conn)

    assert len(server.users[2].undelivered_messages) == 2

    server._process_deliver(conn, None)

    wp = WireProtocol()
    wp.parse_incoming_bytes(conn.send_buffer)
    assert wp.command == CMD.SEND
    assert wp.data_buffer == b'u2|||u3|||test message!'

    remaining_bytes = wp.tmp_buffer
    wp.reset_buffers()
    wp.parse_incoming_bytes(remaining_bytes)
    assert wp.command == CMD.SEND
    assert wp.data_buffer == b'u2|||u3|||another test message'
コード例 #2
0
def listen_for_messages(q, socket, buffer_size):
    # sets up
    wp = WireProtocol()

    while True:
        # indicates that the socket has been closed from the main process
        if socket.fileno() == -1:
            break

        data = socket.recv(buffer_size)
        if not data:
            q.put(
                [None, None]
            )  # None indicates server cut connection (uncontrolled failure mode)
            break

        # if parse_incoming_bytes is True, an entire message has been received
        while wp.parse_incoming_bytes(data):
            # print('received full message, command: %d, args: %s' % (wp.command, str(wp.parse_data())))
            q.put([wp.command, wp.parse_data()])
            data = wp.tmp_buffer  # Save any leftover bytes
            wp.reset_buffers()