C++实现服务器压力测试框架(二)

2015-07-20 17:11:33 来源: 作者: 浏览: 8
transferred)); m_sock.async_receive(boost::asio::buffer(read_buffer), boost::bind(&CSession::receive_handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } else { stop(); } } void CSession::heartbeat() { unsigned char c[256]; UINT nLen = process_hartbeat(c);//生成心跳数据 m_sock.async_write_some(boost::asio::buffer(c,nLen),boost::bind(&CSession::heartbeat_handler,shared_from_this(),boost::asio::placeholders::error)); } void CSession::heartbeat_handler(const boost::system::error_code &ec) { if (!ec) { heartbeat_timer.expires_from_now(boost::posix_time::minutes(m_heartbeat_timer_minutes)); heartbeat_timer.async_wait(boost::bind(&CSession::heartbeat,shared_from_this())); } else { stop(); } } void CSession::login()//登录 { //连接完成 发送登录帧 //******************************************************************************************* unsigned char c[256]; UINT nLen = process_login(c);//生成登录数据 例如 c中存储了用户名称和密码等 m_sock.async_write_some(boost::asio::buffer(c,nLen),boost::bind(&CSession::login_handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void CSession::login_handler(const boost::system::error_code &ec,std::size_t bytes_transferred) { if (!ec) { start_receive();//启动接收 heartbeat(); } else { stop(); } } void CSession::connect_handler(const boost::system::error_code &ec) { if (!ec) { boost::this_thread::sleep(boost::posix_time::microseconds(500)); login();//连接成功之后开始登录 } else { boost::this_thread::sleep(boost::posix_time::microseconds(60000)); start();//连接失败 需要再次连接 } } void CSession::start_send() { //从线程安全的任务队列中获取一个任务开始发送 std::tuple ret=m_task_queue.get_nonblock(); if (std::get<0>(ret)) { unsigned char* c=std::get<1>(ret).data; int nLen=std::get<1>(ret).len; /**************************************************************************************************/ m_sock.async_write_some(boost::asio::buffer(c,nLen),boost::bind(&CSession::send_handler, shared_from_this(), boost::asio::placeholders::error)); } } void CSession::send_handler(const boost::system::error_code &ec) { if (!ec) { start_send();//任务队列不为空时,开始发送下一个任务 } }
在start中调用async_connect,当连接完成时,connect_handler就会被调用,通过 ec查看有没有连接成功,
每当有一个或多个字节被接收并保存至缓冲区时,receive_handler() 函数就会被调用。
通过 ec查看有没有接收成功, bytes_transferred 表示具体接收的字节数。

为了接受缓冲区 staying alive,所以使用了enable_shared_from_this
shared_from_this不能在构造函数中使用,所以连接函数放在start中




使用方式


class client
{
public:
	boost::shared_ptr
  
    m_pSocket;//创建继承于enable_shared_from_this类的对象时必须使用智能指针
};
std::vector
   
     client_queue;//存储生成的客户端实例 boost::asio::io_service m_io_service; boost::asio::io_service::work m_work(m_io_service);//即使io任务完成,也不退出 std::string IP="192.168.1.1"; unsigned short port=8000; int heartbeat=10; for (int i=0;i
    
     p (new CSession(IP,port,heartbeat,(m_io_service))); client_queue.at(i)->m_pSocket->start(); } boost::thread t(boost::bind(&boost::asio::io_service::run,boost::ref(m_io_service)));
    
   
  


多线程调用run方式

int
                
-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: