标签:使用 os io 数据 ar amp 时间 line size
class client
{
public:
/*client(const udp::endpoint& listen_endpoint)
: socket_(io_service_, listen_endpoint),
deadline_(io_service_)
{
deadline_.expires_at(boost::posix_time::pos_infin);
check_deadline();
}
std::size_t receive(const boost::asio::mutable_buffer& buffer,
boost::posix_time::time_duration timeout, boost::system::error_code& ec)
{
deadline_.expires_from_now(timeout);
ec = boost::asio::error::would_block;
std::size_t length = 0;
socket_.async_receive(boost::asio::buffer(buffer),
boost::bind(&client::handle_receive, _1, _2, &ec, &length));
do io_service_.run_one(); while (ec == boost::asio::error::would_block);
return length;
}*/
//此处是我修改的,设置传入的io操作。
client(boost::asio::io_service *io_service_input,udp::socket *socket_input)
:socket_(socket_input),io_service_(io_service_input),deadline_(*io_service_input)
{
deadline_.expires_at(boost::posix_time::pos_infin);
check_deadline();
}
std::size_t receive(const boost::asio::mutable_buffer& buffer,
boost::posix_time::time_duration timeout, boost::system::error_code& ec)
{
deadline_.expires_from_now(timeout);
ec = boost::asio::error::would_block;
std::size_t length = 0;
socket_->async_receive(boost::asio::buffer(buffer),
boost::bind(&client::handle_receive, _1, _2, &ec, &length));
do io_service_->run_one(); while (ec == boost::asio::error::would_block);
return length;
}
private:
void check_deadline()
{
if (deadline_.expires_at() <= deadline_timer::traits_type::now())
{
socket_->cancel();
deadline_.expires_at(boost::posix_time::pos_infin);
}
deadline_.async_wait(boost::bind(&client::check_deadline, this));
}
static void handle_receive(
const boost::system::error_code& ec, std::size_t length,
boost::system::error_code* out_ec, std::size_t* out_length)
{
*out_ec = ec;
*out_length = length;
}
private:
boost::asio::io_service *io_service_;
udp::socket *socket_;
deadline_timer deadline_;
};
使用方法:
google::protobuf::Message * CBoost_Udp::Send(const google::protobuf::Message& message, struct endUdpadress clientIpPort, std::string &sErrorMsg)
{
int nSendlenth = 0;
string str_sendmsg = "";
char recvBuf[1024*4];
memset(recvBuf,‘\0‘,1024*4);
boost::asio::io_service io_service;
udp::resolver resolver(io_service);
char chPort[64] = {‘\0‘};
sprintf(chPort,"%d",clientIpPort.intPort);
udp::resolver::query query(udp::v4(), clientIpPort.strip, chPort);
udp::endpoint sendandrecv_endpoint = *resolver.resolve(query);
udp::socket udp_socket(io_service);
udp_socket.open(udp::v4());
google::protobuf::Message * ret = NULL;
str_sendmsg = encode(message);
try
{
nSendlenth = udp_socket.send_to(boost::asio::buffer(str_sendmsg.c_str(), str_sendmsg.size()), sendandrecv_endpoint);
client c(&io_service,&udp_socket);//接收数据的时候等待5秒
boost::system::error_code ec;
std::size_t nrecv = c.receive(boost::asio::buffer(recvBuf),boost::posix_time::seconds(5), ec);//设置等待时间。
if (ec)
{
std::cout << "Receive error: " << ec.message() << "\n";
return NULL;
}
//int nrecv = udp_socket.receive_from(boost::asio::buffer(recvBuf,1024),sendandrecv_endpoint);
std::string data_rec(recvBuf,nrecv);
std::string srecv = data_rec.substr(sizeof(int));
ret = dynamic_cast<google::protobuf::Message*>(decode(srecv));
if(ret!= NULL)
{
cout<<"recv message after send success"<<endl;
}
else
{
cout<<"recv message after send error";
}
}
catch (boost::system::system_error &e)
{
cout << "process failed:" << e.what() << endl;
}
if(nSendlenth != str_sendmsg.size())
{
std::cout<<"send error:"<<nSendlenth<<std::endl;
}
if(udp_socket.is_open())udp_socket.close();
return ret;
}
标签:使用 os io 数据 ar amp 时间 line size
原文地址:http://www.cnblogs.com/lcfwzq/p/3929928.html