使用async_read_until增强asio问题

时间:2016-01-27 04:10:58

标签: c++ boost boost-asio

我正在使用boost asio创建一个Irc机器人,我编译时遇到了一些问题,而且错误实际上并不可读。

似乎其中一个async_read_until进展不顺利,但我不知道为什么。

这是g ++编译器的错误

mkdir -p build
g++ -std=c++11 -pthread -Wall -Wextra -lboost_system -o build/main.o -c main.cpp
g++ -std=c++11 -pthread -Wall -Wextra -lboost_system -o build/irc.o -c irc.cpp
In file included from irc.hpp:6:0,
             from irc.cpp:1:
/usr/include/boost/tokenizer.hpp: In instantiation of ‘boost::tokenizer<TokenizerFunc, Iterator, Type>::tokenizer(const Container&) [with Container = boost::system::error_code; TokenizerFunc = boost::char_separator<char>; Iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; Type = std::__cxx11::basic_string<char>]’:
/usr/include/boost/bind/bind.hpp:319:35:   required from ‘void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = boost::_mfi::mf1<void, Irc, const boost::tokenizer<boost::char_separator<char> >&>; A = boost::_bi::rrlist2<const boost::system::error_code&, const long unsigned int&>; A1 = boost::_bi::value<Irc*>; A2 = boost::arg<1> (*)()]’
/usr/include/boost/bind/bind.hpp:1246:50:   required from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()(A1&&, A2&&) [with A1 = const boost::system::error_code&; A2 = const long unsigned int&; R = void; F = boost::_mfi::mf1<void, Irc, const boost::tokenizer<boost::char_separator<char> >&>; L = boost::_bi::list2<boost::_bi::value<Irc*>, boost::arg<1> (*)()>; boost::_bi::bind_t<R, F, L>::result_type = void]’
/usr/include/boost/asio/impl/read_until.hpp:636:9:   required from ‘void boost::asio::detail::read_until_delim_string_op<AsyncReadStream, Allocator, ReadHandler>::operator()(const boost::system::error_code&, std::size_t, int) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; Allocator = std::allocator<char>; ReadHandler = boost::_bi::bind_t<void, boost::_mfi::mf1<void, Irc, const boost::tokenizer<boost::char_separator<char> >&>, boost::_bi::list2<boost::_bi::value<Irc*>, boost::arg<1> (*)()> >; std::size_t = long unsigned int]’
/usr/include/boost/asio/impl/read_until.hpp:716:35:   required from ‘typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const string&, ReadHandler&&) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; Allocator = std::allocator<char>; ReadHandler = boost::_bi::bind_t<void, boost::_mfi::mf1<void, Irc, const boost::tokenizer<boost::char_separator<char> >&>, boost::_bi::list2<boost::_bi::value<Irc*>, boost::arg<1> (*)()> >; typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type = void; std::__cxx11::string = std::__cxx11::basic_string<char>]’
irc.cpp:118:2:   required from here
/usr/include/boost/tokenizer.hpp:59:47: error: ‘const class boost::system::error_code’ has no member named ‘begin’
   : first_(c.begin()), last_(c.end()), f_() { }
                                           ^
/usr/include/boost/tokenizer.hpp:59:47: error: ‘const class boost::system::error_code’ has no member named ‘end’
Makefile:17: recipe for target 'irc.o' failed
make: *** [irc.o] Error 1

这是.hpp和.cpp文件。我省略了uninteressant函数,但是如果你想要完整的文件,那就是this pull request

irc.hpp

#ifndef H_IRC
#define H_IRC

#include <vector>
#include <boost/asio.hpp>
#include <boost/tokenizer.hpp>

class Irc
{
    public:
        Irc(const std::string &server, const std::string &port, const std::function<void()> onConnect);

        void connect();
        void close();

        void user(const std::string &username);
        void user(const std::string &username, const std::string &hostname, const std::string &server, const std::string &realname);
        void nick(std::string &nickname);
        void join(const std::string &chan);
        void part(const std::string &chan);
        void privmsg(const std::string &to, const std::string &msg);
        void command(const std::string &cmd, const std::string &msg);
        void command(const std::string &cmd, const std::string &to, const std::string &msg);

        void run();

    private:
        void _read(const boost::system::error_code &error);
        void _send(std::string &message);
        void _readHandler(const boost::tokenizer<boost::char_separator<char> > &tokenizer);
        void _connectHandler(const boost::system::error_code &error);

        void _pong(const std::string &ping);

        std::string _server;
        std::string _port;
        std::string _chan;
        std::vector<std::function<void (const boost::tokenizer<boost::char_separator<char> >&)>> _readHandlers;
        std::function<void()> _onConnect;
        boost::asio::streambuf _buffer;
        boost::asio::io_service _ios;
        boost::asio::ip::tcp::socket _socket;
};

#endif

irc.cpp

#include "irc.hpp"
#include <iostream>
#include <boost/bind.hpp>
#include <boost/tokenizer.hpp>

Irc::Irc(const std::string &server, const std::string &port, const std::function<void()> onConnect)
    : _server(server), _port(port), _onConnect(onConnect), _socket(_ios)
{
    // Ping back handler
    _readHandlers.push_back([this](const boost::tokenizer<boost::char_separator<char> > &tokenizer) {
        std::vector<std::string> tokens(begin(tokenizer), end(tokenizer)); 

        if(tokens[0].compare("PING") == 0)
            _pong(tokens[1]);   
    });

    // 451 handler
    _readHandlers.push_back([this](const boost::tokenizer<boost::char_separator<char> > &tokenizer) {
        std::vector<std::string> tokens(begin(tokenizer), end(tokenizer));

        //for(auto &it : tokens)
        //  if (it.compare("451") == 0) join(_chan);

    });
}

void Irc::connect()
{
    boost::asio::ip::tcp::resolver resolver(_ios);
    boost::asio::ip::tcp::resolver::query query(_server, _port);
    boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
    boost::asio::ip::tcp::resolver::iterator end;
    boost::system::error_code error = boost::asio::error::host_not_found;

    while(it != end)
    {
        if(!error)
            break;

        std::cout << "Connecting to " << _server << " " << _port << std::endl;

        boost::asio::async_connect(_socket, it,
            boost::bind(&Irc::_connectHandler, this, error)
        );

        it++;

        if(error)
            std::cout << "Error : " << error.message() << std::endl;

    }

    if(error)
        std::cout << "Error connectinf to " << _server << " " << error.message() << std::endl;
    else
        std::cout << "Connection success" << std::endl;

}

void Irc::close()
{
    _socket.close();
    _ios.stop();
}

void Irc::command(const std::string &cmd, const std::string &msg)
{
    std::string message(cmd + " " + msg + "\r\n");
    _send(message);
}

void Irc::command(const std::string &cmd, const std::string &to, const std::string &msg)
{
    std::string message(cmd + " " + to + " " + msg + "\r\n");
    _send(message);
}

void Irc::run()
{
    boost::asio::async_read_until(_socket, _buffer, "\r\n",
        boost::bind(&Irc::_readHandler, this,
            boost::asio::placeholders::error
        )
    );

    _ios.run();
}

/*
 * Private
 */

void Irc::_read(const boost::system::error_code &error)
{
    if(error)
    {
        std::cerr << error.message() << std::endl;
        close();
    }
    else
    {
        std::string data(buffers_begin(_buffer.data()), buffers_begin(_buffer.data()) + _buffer.size());
        std::cout << data << std::endl;     

        boost::char_separator<char> sep("!@:; ");
        boost::tokenizer<boost::char_separator<char> > tokenizer(data, sep);

        _readHandler(tokenizer);
        boost::asio::async_read_until(_socket, _buffer, "\r\n",
            boost::bind(&Irc::_readHandler, this,
                boost::asio::placeholders::error
            )
        );

    }
}

inline void Irc::_send(std::string &message)
{
    boost::asio::write(_socket, boost::asio::buffer(message + "\r\n"));
}

void Irc::_readHandler(const boost::tokenizer<boost::char_separator<char> > &tokenizer)
{
    for(auto it : _readHandlers)
        it(tokenizer);
}

void Irc::_connectHandler(const boost::system::error_code &error)
{
    if(!error)
    {
        _onConnect();
    }
}

非常感谢!我整个晚上一直在努力。

1 个答案:

答案 0 :(得分:1)

当您调用async_read_until时,您尝试将_readHandler绑定为回调函数,但此函数没有正确的签名:asio除了它以使boost::system:error_code为{第一个参数,但在你的情况下它是const boost::tokenizer<boost::char_separator<char> > &tokenizer。 更改其类型,然后相应地修改您的代码。