c++ - boost的read函数参数和bind函数参数看不懂
PHP中文网
PHP中文网 2017-04-17 13:07:24
0
2
628
/* 这是一个同步的echo客户端 
 * 在sync_echo()函数中的read函数的第三个参数中,bind有两个占位符号,
 * 调用read_complete()函数,可是在read_complete()函数中,需要用到bytes这个参数
 * 我想请问这个bytes是怎么传进去的?
*/
#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace boost::asio;
using boost::system::error_code;

io_service service;

size_t read_complete(char *buf, const error_code &err, size_t bytes)
{
    if (err)
        return 0;
    bool found = std::find(buf, buf+bytes, '\n') < buf + bytes;
    // we read one-by-one until we get to enter, no buffering
    return found? 0: 1;
}

ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);

void sync_echo(std::string msg)
{
    msg += "\n";
    ip::tcp::socket sock(service);
    sock.connect(ep);
    sock.write_some(buffer(msg));
    char buf[1024];
    int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));
    std::string copy(buf, bytes - 1);
    msg = msg.substr(0, msg.size() - 1);
    std::cout << "server echoed our " << msg << ": "
        << (copy == msg? "OK": "FAIL") << std::endl;
    sock.close();
}

int main(int argc, char *argv[])
{
    // connect several clients
    char *messages[] = {"John says hi", 
    "so does James", "Lucy just not home",
    "Boost.Asio is fun!", 0 };
    boost::thread_group threads;
    for (char **message = messages; *message; ++ message)
    {
        threads.create_thread(boost::bind(sync_echo, *message));
        boost::this_thread::sleep(boost::posix_time::millisec(100));
    }
    threads.join_all();
}
PHP中文网
PHP中文网

认证0级讲师

reply all(2)
Peter_Zhu

The first time I saw it, I was really surprised.
Moving, link: http://zh.highscore.de/cpp/boost/functionobjects.html
Key excerpt: _1 is called a placeholder and is defined in Boost.Bind. In addition to _1, Boost.Bind also defines _2 and _3. By using these placeholders, boost::bind() can be turned into a unary, binary, or ternary function.
Personal understanding:
That is to say, the third parameter required by the read function itself is a function with 2 parameters. Then for your question, for the three parameters of read_complete: the const error_code &err, size_t bytes passed in by the read function is buf passed in by you.
If possible, you can verify it by looking at the execution flow of the read function source code.

阿神

Ask:

I would like to ask how these bytes are transferred in?

Answer: Here: int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));

read_complete has three parameters. Bind means that the first parameter is buf, the second parameter is the first parameter of the returned function, and the third parameter is the second parameter of the returned function. If you replace _1 and _2, then the parameters must also be replaced when you call the returned function.

Using modern C++, it is written as follows:
Old:

boost::bind(read_complete,b uf, _1, _2)

New:

[&buf](auto a, auto b)
{
    return read_complete(buf, a, b);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template