Boost asio trouble with async_read_until

bl4ckb0ne

I'm making an Irc bot with boost asio, and I got some problem to compiles it, and the error is not really readable.

It seems that one of the async_read_until is not going on very well, but I don't know why.

Here's the error with the g++ compiler

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

And here's the .hpp and .cpp file. I omitted the uninteressant functions, but if you want the full file, it's here.

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();
    }
}

Thanks a lot! I've been working on this all evening.

yuyoyuppe

When you call async_read_until, you're trying to bind _readHandler as a callback function, however this function doesn't have the right signature: asio excepts it to have boost::system:error_code as a first argument, but in your case it's const boost::tokenizer<boost::char_separator<char> > &tokenizer. Change its type and then modify your code accordingly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Boost asio async_read_until followed by async_read

From Dev

Boost::asio::async_read_until never calls handler

From Dev

boost::asio read/write trouble

From Dev

How should I use async_read_until and async_write simultaneously in the client app in boost::asio?

From Dev

C++ boost asio Windows file handle async_read_until infinite loop - no eof

From Dev

C++ boost asio Windows file handle async_read_until infinite loop - no eof

From Dev

Boost ASIO - What is async

From Dev

Boost::Asio - read or read_until?

From Dev

Boost::Asio - read or read_until?

From Dev

asio::async_read_until: robust and graceful way of handling multiple lines

From Dev

Boost::Asio Async write failed

From Dev

boost::asio::read() blocks forever

From Dev

Boost asio bind read handler

From Dev

Boost asio asynchronous read and then write

From Dev

Concurrent read and async_read_some in boost asio

From Dev

Boost asio TCP async server not async?

From Dev

boost::asio::async_read 100% CPU usage on simple example

From Dev

Boost asio async_read_some returning small amount of data

From Dev

"two-step" async_read with boost asio

From Dev

boost::asio::async_read return end of file error on newline

From Dev

Boost ASIO, async_read_some callback not called

From Dev

boost::asio::async_read return end of file error on newline

From Dev

"two-step" async_read with boost asio

From Dev

boost asio async udp server - poor performance

From Dev

Generic way to timeout async operations in boost::asio

From Dev

Boost.Asio: Async operations timeout

From Dev

Boost asio async operation bad file descriptor

From Dev

boost asio async udp server - poor performance

From Dev

Boost.Asio: Async operations timeout

Related Related

HotTag

Archive