#πŸ”’ Fix asio::async_read_until lambda not called on \n-terminated string

1 messages Β· Page 1 of 1 (latest)

rain birch
#

I'm working on a P2P Client with a particular protocol spec (https://p2pquake.github.io/epsp-specifications/epsp-specifications.html, might want to translate). It is very incomplete tho, but I'm currently on implementing the peer connection part.

I’m stuck because my async_read_until handler never runs even though the remote peer is sending \n‑terminated messages. The socket successfully connects to the peer, and writes just fine to the peer, but reading from the peer somehow breaks. I don't know whether its because of ncat or sth but it is not working. Using a debugger is fruitless.

Its expected to at least show a log saying "Received: <string> ..." or an error but nothing appears.

#

Here's the code


void ConnectionPeer::Peer::read() {
    auto self(shared_from_this());
    asio::async_read_until(
        self->socket, self->buffer, '\n',
        [self](asio::error_code ecode, std::size_t) -> void {
            if (ecode) {
                if (auto shared_parent = self->parent.lock()) {
                    shared_parent->peer_logger_->error(
                        "Read error: {}, from: {}", ecode.message(),
                        self->endpoint.address().to_string() + ":" +
                            std::to_string(self->endpoint.port()));
                }
                return;
            }

            std::istream input(&self->buffer);
            std::string line;
            std::getline(input, line);

            if (auto shared_parent = self->parent.lock()) {
                shared_parent->peer_logger_->info(
                    "Received: {}, from: {}", line,
                    self->endpoint.address().to_string() + ":" +
                        std::to_string(self->endpoint.port()));
            }

            if (line.size() < 5) {
                if (auto shared_parent = self->parent.lock()) {
                    shared_parent->peer_logger_->error(
                        "Invalid message: {}, from: {}", line,
                        self->endpoint.address().to_string() + ":" +
                            std::to_string(self->endpoint.port()));
                    shared_parent->stop(self->peer_id);
                }

                return;
            }

            self->handle_message(line);
            self->read();
        });
}

(Full repo: https://github.com/minhanhiscool/epsp-linux)

#

also any code review is appreciated πŸ™ πŸ™πŸ™

crude kernel
#

Are you sending just \n or a full \r\n?

rain birch
#

a full \r\n

#

there is case handling for \n only though for debugging purpose

crude kernel
#

Is your lambda ever called at all?

rain birch
#

no

carmine bridgeBOT
#

@rain birch has reached level 1. GG!

rain birch
#

walked thru the debugger it just hung at a weird asio function

crude kernel
#

You init the server after you init the peer connection?

#

What does server mean in this context?

rain birch
#

alr init the server

rain birch
#

after that the client must disconnect from the server

crude kernel
#

Shouldn't the server run before the client?

#

Also, don't you need two peers for this protocol?

rain birch
#

for me i use ncat since i dont want to code the server

#

peers too

crude kernel
#

Do you see your program's commands in ncat?

crude kernel
#

And your ::read() function is called right?

rain birch
#

it is called

crude kernel
#

Can you show the callstack for that call

rain birch
crude kernel
#

I think my first step would be to simply every thing so that you basically just have a write followed by a read and get that working

rain birch
#

ok then ill try tmr

#

its getting late so im going to sleep

crude kernel
carmine bridgeBOT
#

@rain birch has reached level 2. GG!

rain birch
#

ok fixed bug. apparently had to add a work guard since the peer thread immediately exited after startup since no work is given to it. now the read() function works as expected

wispy pagodaBOT
#

πŸ”’ Fix asio::async_read_until lambda not called on \n-terminated string