C++ 为什么我的io_service::run_one()实现会导致不确定的块并触发错误#125?
我使用BOOST与串行端口进行异步通信。我无法确定我所面临的错误的原因,希望能得到一些指导C++ 为什么我的io_service::run_one()实现会导致不确定的块并触发错误#125?,c++,boost-asio,placeholder,nonblocking,boost-bind,C++,Boost Asio,Placeholder,Nonblocking,Boost Bind,我使用BOOST与串行端口进行异步通信。我无法确定我所面临的错误的原因,希望能得到一些指导 std::string myclass::readStringUntil(const std::string& delim) { setupParameters=ReadSetupParameters(delim); performReadSetup(setupParameters); if(timeout!=posix_time::seconds(0)) timer.expir
std::string myclass::readStringUntil(const std::string& delim)
{
setupParameters=ReadSetupParameters(delim);
performReadSetup(setupParameters);
if(timeout!=posix_time::seconds(0)) timer.expires_from_now(timeout);
else timer.expires_from_now(posix_time::hours(100000));
timer.async_wait(boost::bind(&myclass::timeoutExpired,this,
asio::placeholders::error));
result=resultInProgress;
bytesTransferred=0;
for(;;)
{
io.run_one();
switch(result)
{
case resultSuccess:
{
timer.cancel();
bytesTransferred-=delim.size();//Don't count delim
istream is(&readData);
string result(bytesTransferred,'\0');//Alloc string
is.read(&result[0],bytesTransferred);//Fill values
is.ignore(delim.size());//Remove delimiter from stream
return result;
}
case resultTimeoutExpired:
port.cancel();
throw(timeout_exception("Timeout expired"));
cout<<"timeout on readuntill"<<endl;
case resultError:
timer.cancel();
port.cancel();
throw(boost::system::system_error(boost::system::error_code(),
"Error while reading"));
}
}
/////////////////////////////////////////////////////////////////////////////
void myclass::performReadSetup(const ReadSetupParameters& param)
{
if(param.fixedSize)
{
asio::async_read(port,asio::buffer(param.data,param.size),boost::bind(
&myclass::readCompleted,this,asio::placeholders::error,
asio::placeholders::bytes_transferred));
} else {
asio::async_read_until(port,readData,param.delim,boost::bind(
&myclass::readCompleted,this,asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
}
/////////////////////////////////////////////////////////////////////////////
void myclass::timeoutExpired(const boost::system::error_code& error)
{
if(!error && result==resultInProgress) result=resultTimeoutExpired;
}
/////////////////////////////////////////////////////////////////////////////
void myclass::readCompleted(const boost::system::error_code& error,
const size_t bytesTransferred)
{
if(!error)
{
result=resultSuccess;
this->bytesTransferred=bytesTransferred;
return;
}
#ifdef _WIN32
if(error.value()==995) return; //Windows spits out error 995
#elif defined(__APPLE__)
if(error.value()==45)
{
//Bug on OS X, it might be necessary to repeat the setup
//http://osdir.com/ml/lib.boost.asio.user/2008-08/msg00004.html
performReadSetup(setupParameters);
return;
}
#else //Linux
if(error.value()==125) return; //Linux outputs error 125
#endif
result=resultError;
}
std::string myclass::readStringUntil(const std::string&delim)
{
setupParameters=ReadSetupParameters(delim);
performReadSetup(设置参数);
if(timeout!=posix_time::seconds(0))timer.expires_from_now(超时);
else timer.expires_from_now(posix_时间::小时(100000));
timer.async_wait(boost::bind(&myclass::timeoutExpired),这个,
asio::占位符::错误);
结果=结果进度;
ByTestTransferred=0;
对于(;;)
{
io.run_one();
开关(结果)
{
案例结果成功:
{
timer.cancel();
ByTestTransferred-=delim.size();//不计算delim
istream is(&readData);
字符串结果(ByTestTransfered,'\0');//Alloc字符串
is.read(&result[0],ByTestTransfered);//填充值
is.ignore(delim.size());//从流中删除分隔符
返回结果;
}
case ResultItemOutExpired:
port.cancel();
抛出(超时\异常(“超时已过期”);
cout首先,错误125是操作中止:因此这可能意味着(可能)一个cancel()调用(或者导致取消的io对象的析构函数)
这很正常
我痛苦地完成了您不完整的代码,不容易发现您的问题:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
struct myclass {
struct timeout_exception : std::runtime_error {
timeout_exception(std::string const &msg) : std::runtime_error(msg) {}
};
enum {
resultInProgress,
resultTimeoutExpired,
resultSuccess,
resultError,
} result = resultInProgress;
std::string readStringUntil(std::string const &);
struct ReadSetupParameters {
ReadSetupParameters(std::string const &d = "") : delim{ d } {}
std::string delim;
bool fixedSize = false;
char mutable data[1024];
size_t size = sizeof(data);
};
void performReadSetup(const ReadSetupParameters ¶m);
ReadSetupParameters setupParameters;
boost::posix_time::time_duration timeout{ boost::posix_time::seconds(3) };
boost::asio::io_service io;
boost::asio::deadline_timer timer{ io };
// more likely a serial port, but I'm not gonna bother mocking that:
boost::asio::ip::tcp::socket port{ io };
boost::asio::streambuf readData;
size_t bytesTransferred;
myclass() { port.connect({ {}, 6767 }); }
void timeoutExpired(boost::system::error_code const &ec);
void readCompleted(boost::system::error_code const &ec, size_t bytesTransferred);
};
std::string myclass::readStringUntil(const std::string &delim) {
using namespace boost;
setupParameters = ReadSetupParameters(delim);
performReadSetup(setupParameters);
if (timeout != posix_time::seconds(0))
timer.expires_from_now(timeout);
else
timer.expires_from_now(posix_time::hours(100000));
timer.async_wait(boost::bind(&myclass::timeoutExpired, this, asio::placeholders::error));
result = resultInProgress;
for (;;) {
io.run_one();
switch (result) {
case resultSuccess: {
timer.cancel();
bytesTransferred -= delim.size(); // Don't count delim
std::istream is(&readData);
std::string result(bytesTransferred, '\0'); // Alloc string
is.read(&result[0], bytesTransferred); // Fill values
is.ignore(delim.size()); // Remove delimiter from stream
return result;
} break;
case resultTimeoutExpired:
port.cancel();
std::cout << "timeout on readuntill" << std::endl;
throw(timeout_exception("Timeout expired"));
break;
case resultError:
timer.cancel();
port.cancel();
throw(boost::system::system_error(boost::system::error_code(), "Error while reading"));
}
}
}
/////////////////////////////////////////////////////////////////////////////
void myclass::performReadSetup(const ReadSetupParameters ¶m) {
using namespace boost;
if (param.fixedSize) {
asio::async_read(port, asio::buffer(param.data, param.size),
boost::bind(&myclass::readCompleted, this, asio::placeholders::error,
asio::placeholders::bytes_transferred));
} else {
asio::async_read_until(port, readData, param.delim,
boost::bind(&myclass::readCompleted, this, asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
}
/////////////////////////////////////////////////////////////////////////////
void myclass::timeoutExpired(const boost::system::error_code &error) {
if (!error && result == resultInProgress)
result = resultTimeoutExpired;
}
/////////////////////////////////////////////////////////////////////////////
void myclass::readCompleted(const boost::system::error_code &error, const size_t bytesTransferred) {
if (!error) {
result = resultSuccess;
this->bytesTransferred = bytesTransferred;
return;
}
#ifdef _WIN32
if (error.value() == 995)
return; // Windows spits out error 995
#elif defined(__APPLE__)
if (error.value() == 45) {
// Bug on OS X, it might be necessary to repeat the setup
// http://osdir.com/ml/lib.boost.asio.user/2008-08/msg00004.html
performReadSetup(setupParameters);
return;
}
#else // Linux
if (error.value() == 125)
return; // Linux outputs error 125
#endif
result = resultError;
}
int main() {
myclass absent;
std::cout << "Ok: '" << absent.readStringUntil("Transferred") << "'\n";
}
#包括
#包括
#包括
结构myclass{
结构超时\u异常:std::runtime\u错误{
超时_异常(std::string const&msg):std::runtime_错误(msg){}
};
枚举{
结果进展,
resultTimeoutExpired,
结果成功,
结果是恐怖,,
}结果=结果进度;
std::string readStringUntil(std::string const&);
结构ReadSetupParameters{
ReadSetupParameters(std::string const&d=”“):delim{d}{
std::字符串delim;
bool fixedSize=false;
字符可变数据[1024];
size\u t size=sizeof(数据);
};
作废performReadSetup(常量读取设置参数和参数);
读取设置参数设置参数;
boost::posix_time::time_持续时间超时{boost::posix_time::seconds(3)};
boost::asio::io_服务io;
boost::asio::deadline_timer{io};
//更像是一个串行端口,但我不想去嘲笑它:
boost::asio::ip::tcp::套接字端口{io};
boost::asio::streambuf readData;
通过测试转移的尺寸;
myclass(){port.connect({{},6767});}
void timeoutExpired(boost::system::error\u code const&ec);
void readCompleted(boost::system::error\u code const&ec,size\u t bytesttransfered);
};
std::string myclass::readStringUntil(const std::string&delim){
使用名称空间boost;
setupParameters=ReadSetupParameters(delim);
performReadSetup(设置参数);
如果(超时!=posix_时间::秒(0))
timer.expires\u from\u now(超时);
其他的
timer.expires_from_now(posix_时间::小时(100000));
异步等待(boost::bind(&myclass::timeoutExpired,this,asio::placeholders::error));
结果=结果进度;
对于(;;){
io.run_one();
开关(结果){
案例结果成功:{
timer.cancel();
ByTestTransferred-=delim.size();//不计算delim
std::istream is(&readData);
std::string result(bytesttransfered,'\0');//Alloc string
is.read(&result[0],ByTestTransfered);//填充值
is.ignore(delim.size());//从流中删除分隔符
返回结果;
}中断;
case ResultItemOutExpired:
port.cancel();
std::cout首先,错误125是操作中止:因此这可能意味着(可能)一个cancel()调用(或导致取消的io对象的析构函数)
这很正常
我痛苦地完成了您不完整的代码,不容易发现您的问题:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
struct myclass {
struct timeout_exception : std::runtime_error {
timeout_exception(std::string const &msg) : std::runtime_error(msg) {}
};
enum {
resultInProgress,
resultTimeoutExpired,
resultSuccess,
resultError,
} result = resultInProgress;
std::string readStringUntil(std::string const &);
struct ReadSetupParameters {
ReadSetupParameters(std::string const &d = "") : delim{ d } {}
std::string delim;
bool fixedSize = false;
char mutable data[1024];
size_t size = sizeof(data);
};
void performReadSetup(const ReadSetupParameters ¶m);
ReadSetupParameters setupParameters;
boost::posix_time::time_duration timeout{ boost::posix_time::seconds(3) };
boost::asio::io_service io;
boost::asio::deadline_timer timer{ io };
// more likely a serial port, but I'm not gonna bother mocking that:
boost::asio::ip::tcp::socket port{ io };
boost::asio::streambuf readData;
size_t bytesTransferred;
myclass() { port.connect({ {}, 6767 }); }
void timeoutExpired(boost::system::error_code const &ec);
void readCompleted(boost::system::error_code const &ec, size_t bytesTransferred);
};
std::string myclass::readStringUntil(const std::string &delim) {
using namespace boost;
setupParameters = ReadSetupParameters(delim);
performReadSetup(setupParameters);
if (timeout != posix_time::seconds(0))
timer.expires_from_now(timeout);
else
timer.expires_from_now(posix_time::hours(100000));
timer.async_wait(boost::bind(&myclass::timeoutExpired, this, asio::placeholders::error));
result = resultInProgress;
for (;;) {
io.run_one();
switch (result) {
case resultSuccess: {
timer.cancel();
bytesTransferred -= delim.size(); // Don't count delim
std::istream is(&readData);
std::string result(bytesTransferred, '\0'); // Alloc string
is.read(&result[0], bytesTransferred); // Fill values
is.ignore(delim.size()); // Remove delimiter from stream
return result;
} break;
case resultTimeoutExpired:
port.cancel();
std::cout << "timeout on readuntill" << std::endl;
throw(timeout_exception("Timeout expired"));
break;
case resultError:
timer.cancel();
port.cancel();
throw(boost::system::system_error(boost::system::error_code(), "Error while reading"));
}
}
}
/////////////////////////////////////////////////////////////////////////////
void myclass::performReadSetup(const ReadSetupParameters ¶m) {
using namespace boost;
if (param.fixedSize) {
asio::async_read(port, asio::buffer(param.data, param.size),
boost::bind(&myclass::readCompleted, this, asio::placeholders::error,
asio::placeholders::bytes_transferred));
} else {
asio::async_read_until(port, readData, param.delim,
boost::bind(&myclass::readCompleted, this, asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
}
/////////////////////////////////////////////////////////////////////////////
void myclass::timeoutExpired(const boost::system::error_code &error) {
if (!error && result == resultInProgress)
result = resultTimeoutExpired;
}
/////////////////////////////////////////////////////////////////////////////
void myclass::readCompleted(const boost::system::error_code &error, const size_t bytesTransferred) {
if (!error) {
result = resultSuccess;
this->bytesTransferred = bytesTransferred;
return;
}
#ifdef _WIN32
if (error.value() == 995)
return; // Windows spits out error 995
#elif defined(__APPLE__)
if (error.value() == 45) {
// Bug on OS X, it might be necessary to repeat the setup
// http://osdir.com/ml/lib.boost.asio.user/2008-08/msg00004.html
performReadSetup(setupParameters);
return;
}
#else // Linux
if (error.value() == 125)
return; // Linux outputs error 125
#endif
result = resultError;
}
int main() {
myclass absent;
std::cout << "Ok: '" << absent.readStringUntil("Transferred") << "'\n";
}
#包括
#包括
#包括
结构myclass{
结构超时\u异常:std::runtime\u错误{
超时_异常(std::string const&msg):std::runtime_错误(msg){}
};
枚举{
结果进展,
resultTimeoutExpired,
结果成功,
结果是恐怖,,
}结果=结果进度;
std::string readStringUntil(std::string const&);
结构ReadSetupParameters{
ReadSetupParameters(std::string const&d=”“):delim{d}{
std::字符串delim;
bool fixedSize=false;
字符可变数据[1024];
size\u t size=sizeof(数据);
};
作废performReadSetup(常量读取设置参数和参数);
读取设置参数设置参数;
boost::posix_time::time_持续时间超时{boost::posix_time::seconds(3)};
boost::asio::io_服务io;
boost::asio::deadline_timer{io};
//更像是一个串行端口,但我不想去嘲笑它:
boost::asio::ip::tcp::套接字端口{io};
boost::asio::streambuf readData;
通过测试转移的尺寸;
myclass(){port.connect({{},6767});}
void timeoutExpired(boost::system::error\u code const&ec);
void readCompleted(boost::system::error\u code const&ec,size\u t bytesttransfered);
};
std::string myclass::readStringUntil(const std::string&delim){
使用名称空间boost;
setupParameters=ReadSetupParameters(delim);
performReadSetup(设置参数);
如果(超时!=posix_时间::秒(0))
timer.expires\u from\u now(超时);
其他的
timer.expires_from_now(posix_时间::小时(100000));
异步等待(boost::bind(&myclass::timeoutExpired,this,asio::placeholders::error));
结果=结果进度;
对于(;;){
io.run_one();