gpt4 book ai didi

c++ - 使用 async_read_until boost asio 问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:14:55 28 4
gpt4 key购买 nike

我正在用 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 文件。我省略了无用的函数,但如果你想要完整的文件,它是 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();
}
}

非常感谢!我整个晚上都在研究这个。

最佳答案

当您调用 async_read_until 时,您正在尝试绑定(bind) _readHandler作为回调函数,但是这个函数没有正确的签名:asio excepts it to have boost::system:error_code作为第一个参数,但在您的情况下是 const boost::tokenizer<boost::char_separator<char> > &tokenizer .更改其类型,然后相应地修改您的代码。

关于c++ - 使用 async_read_until boost asio 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35028751/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com