设为首页 加入收藏

TOP

ACE_Proactor UDP V2.0(五)
2015-07-24 05:20:59 来源: 作者: 【 】 浏览:21
Tags:ACE_Proactor UDP V2.0
r_lack_ACE_OS int open (const ACE_TCHAR *host, u_short port); int handle_timeout(const ACE_Time_Value& , const void *act /* = 0 */);//计时器到期后执行的回调函数 protected: /// This is called when asynchronous writes from the dgram socket /// complete virtual void handle_write_dgram (const ACE_Asynch_Write_Dgram::Result &result); private: void start_timing(void); int send_to_one_server(const string&,const string&); int send_to_multi_server(void); void read_content(string&); /// Network I/O handle ACE_SOCK_Dgram sock_dgram_; /// wd (write dgram): for writing to the socket ACE_Asynch_Write_Dgram wd_; const char* completion_key_; const char* act_; long time_handle_;//在计时器队列中的ID int delay_;//启动多久开始第一次触发超时 int interval_;//循环计时的间隔 }; Sender::Sender (const int delay,const int interval) : completion_key_ ("Sender completion key"), act_ ("Sender ACT"), delay_(delay), interval_(interval) { ACE_DEBUG ((LM_DEBUG, "Sender::Sender (const int delay,const int interval)\n")); } Sender::~Sender (void) { this->sock_dgram_.close (); } int Sender::open (const ACE_TCHAR *host, u_short port) { ACE_DEBUG ((LM_DEBUG, "Sender::open(%s,%d)\n",host,port)); // Initialize stuff //初始化和socket有关的成员 if (this->sock_dgram_.open (ACE_INET_Addr::sap_any) == -1) ACE_ERROR_RETURN ((LM_ERROR, "[%D][line:%l]%p\n", "ACE_SOCK_Dgram::open"), -1); // Initialize the asynchronous read. if (this->wd_.open (*this, this->sock_dgram_.get_handle (), this->completion_key_, ACE_Proactor::instance ()) == -1) ACE_ERROR_RETURN ((LM_ERROR, "[%D][line:%l]%p\n", "ACE_Asynch_Write_Dgram::open"), -1); //init time clock //启动计时 start_timing(); return 0; } void Sender::start_timing(void) { ACE_DEBUG ((LM_DEBUG, "Sender::start_timing:delay[%d]interval[%d]\n", this->delay_,this->interval_)); this->reactor(ACE_Reactor::instance()); this->time_handle_ = this->reactor()->schedule_timer(this,//在这里注册定时器 0, ACE_Time_Value(this->delay_),//程序一开始延迟delay秒开始首次执行到期函数 ACE_Time_Value(this->interval_));//循环计时,每隔interval秒重复执行 } int Sender::handle_timeout(const ACE_Time_Value& , const void *act /* = 0 */) { cout<<"\n\n\n计时器"< interval_/60<<"分钟到期"< iter_begin(fin),iter_end; string send_str(iter_begin,iter_end); content.swap(send_str); } int Sender::send_to_one_server(const string& addr,const string& sent_content) { // create a message block for the message header ACE_Message_Block* msg = 0; ACE_NEW_RETURN (msg, ACE_Message_Block (100), -1); // Copy buf into the Message_Block and update the wr_ptr (). msg->copy (sent_content.c_str(), sent_content.size()); // do the asynch send size_t number_of_bytes_sent = 0; ACE_INET_Addr serverAddr (addr.c_str()); int res = this->wd_.send (msg, number_of_bytes_sent, 0, serverAddr, this->act_); ACE_Message_Block* p = 0; p= msg; switch (res) { case 0: // this is a good error. The proactor will call our handler when the // send has completed. break; case 1: // actually sent something, we will handle it in the handler callback ACE_DEBUG ((LM_DEBUG, "********************\n")); ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes sent immediately", number_of_bytes_sent)); while (p != NULL) { string temp; for (int i=0;i length();++i) { temp.push_back(*(p->rd_ptr()+i)); } ACE_DEBUG ((LM_DEBUG,"YOU SEND[%s]\n",temp.c_str())); p = p->cont(); } ACE_DEBUG ((LM_DEBUG, "********************\n")); res = 0; break; case -1: // Something else went wrong. ACE_ERROR ((LM_ERROR, "[%D][line:%l]%p\n", "ACE_A
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇zoj 1944 Tree Recovery (二叉树) 下一篇poj 2456 Aggressive cows

评论

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