Call a boost function without callback

Refresh

February 2019

Views

41 time

1

I am using boost-beast library for a websocket connection. You can refer to this example for the understanding what is happening. I have used the same example, but changed a few things:

  • split the on_handshake(beast::error_code ec) into three functions A(beast::error_code ec),B(beast::error_code ec) and C(beast::error_code ec)

  • B() sends binary data, and A() and C() are sending text.

  • A() calls B() as callback and B() calls C() as callback.

Now I am stuck at a point where I want to do this:

void session::A(beast::error_code ec) {
    if (ec)
        return (fail(ec, "handshake"));

    ws_.async_write(net::buffer(SOMETEXT),bind(&session::B, shared_from_this(), placeholders::_1));

}
void session::B(beast::error_code ec) {
    if (ec)
        return (fail(ec, "A_FAILED"));

    if(condition1) {
        ws_.binary(true);
        ws_.async_write(net::buffer(SOMEBINARY),bind(&session::C, shared_from_this(), placeholders::_1));
    } else {
        session::on_write(ec,<WHAT SHOULD I WRITE HERE>);
    }

}
void session::C(beast::error_code ec) {
    if (ec)
        return (fail(ec, "B_FAILED"));

    ws_.binary(false);

    ws_.async_write(net::buffer(SOMETEXT),bind(&session::on_write, shared_from_this(), placeholders::_1, placeholders::_2));

}

Here is the Read function:

void
on_write(
    beast::error_code ec,
    std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "write");

    // Read a message into our buffer
    ws_.async_read(
        buffer_,
        std::bind(
            &session::on_read,
            shared_from_this(),
            std::placeholders::_1,
            std::placeholders::_2));
}

QUESTION:- Please check the function B() and my question there. Any advice or answer is appreciated.

2 answers

2

You can just put a 0 in the argument and mark it as unused

e.g. session::on_write(ec, 0 /* field unused */);

0

Или вы можете дать свои подписи функциональных параметров по умолчанию:

void session::B(
    beast::error_code ec = {},
    std::size_t bytes_transferred = 0);