#boost asio not accepting packets in the wrong format

6 messages · Page 1 of 1 (latest)

fast valley
#
void Server::do_read(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{
    auto length_buffer = std::make_shared<std::array<char, 4>>();
    socket->async_read_some(
        boost::asio::buffer(*length_buffer),
        [socket, length_buffer](boost::system::error_code ec, std::size_t bytes_transferred)
        {
            if (!ec && bytes_transferred == 4)
            {
                uint32_t message_length = 0;
                std::memcpy(&message_length, length_buffer->data(), sizeof(uint32_t));
                message_length = ntohl(message_length);
                Logger::debug("message_length: " + std::to_string(message_length));
                handleMsg(socket, message_length);
            }
            else
            {
                Logger::error("Read Error (length): " + ec.message() + "Bytes Transferred: " + std::to_string(bytes_transferred));
                if (onClientDisconnected)
                {
                    onClientDisconnected(socket);
                }
                active_sockets_.erase(socket);
                socket->close();
            }
        });
}

I have no idea what Im doing Im so confused

correct client:

rl.on('line', (input) => {
    const messageBuffer = Buffer.from(input, 'utf8');
    const lengthBuffer = Buffer.alloc(4);
    lengthBuffer.writeUInt32BE(messageBuffer.length, 0);
    const packet = Buffer.concat([lengthBuffer, messageBuffer]);
    client.write(packet);
});

wrong:

rl.on('line', (input) => {
    const messageBuffer = Buffer.from(input, 'utf8');
    const lengthBuffer = Buffer.alloc(6);
    lengthBuffer.writeUInt32BE(messageBuffer.length - 2, 0);
    const packet = Buffer.concat([lengthBuffer, messageBuffer]);
    client.write(packet);
});

format: [size][msg]
I do not attempt to accept packets in the wrong format.

simple aspenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

solar nova
#

how about you ask a proper question and give more details?

#

what's "right", what's "wrong"

fast valley
#

Actually everything is written there [size 4byte][msg]
But I couldnt do the checks, it didn't matter anymore, I solved it by doing something else.

#

!solved