Exemplo n.º 1
0
def test_msg_data_parse():
    wp = WireProtocol()
    msg_bytes = b'\x00\x02' + b'\x00' * 39 + b'\x20' + b'toname|||fromname|||hello world!'
    wp.parse_incoming_bytes(msg_bytes)
    data_out = wp.parse_data()
    assert len(data_out) == 3
    assert data_out[0] == 'toname'
    assert data_out[1] == 'fromname'
    assert data_out[2] == 'hello world!'
Exemplo n.º 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()
Exemplo n.º 3
0
def test_malformed():
    wp = WireProtocol()
    msg_bytes = b'this is very wrong!'
    wp.parse_incoming_bytes(msg_bytes)
    with pytest.raises(ValueError):
        data_out = wp.parse_data()