发现未发送完成才注册write事件,如果send完毕再反注册,下面是我现在使用的代码,你可以参考下。- int
- Connection_Handler::handle_output(ACE_HANDLE fd)
- {
- ACE_Message_Block *mb = 0;
- ACE_Time_Value nowait(ACE_OS::gettimeofday());
- while (-1 != this->getq(mb, &nowait))
- {
- ssize_t send_cnt =
- this->peer().send(mb->rd_ptr(), mb->length());
- if (send_cnt == -1)
- ACE_ERROR((LM_ERROR,
- ACE_TEXT("(%P|%t) %p\n"),
- ACE_TEXT("send")));
- else
- mb->rd_ptr(static_cast<size_t>(send_cnt));
- if (mb->length() > 0)
- {
- this->ungetq(mb);
- break;
- }
- mb->release();
- }
- return (this->msg_queue()->is_empty()) ? -1 : 0;
- }
- int
- Connection_Handler::send(ACE_Message_Block *mb)
- {
- int empty_queue = this->msg_queue()->is_empty();
- ssize_t sent = 0;
- ssize_t len = mb->length();
- if (empty_queue)
- {
- ssize_t sent =
- this->peer().send(mb->rd_ptr(), len);
- if (sent == len)
- {
- mb->release();
- return 0;
- }
- if (sent == -1 && errno != EWOULDBLOCK)
- {
- mb->release();
- ACE_ERROR_RETURN((LM_ERROR,
- ACE_TEXT("(%P|%t) %p\n"),
- ACE_TEXT("send")),
- -1);
- }
- if (sent == -1)
- sent = 0;
- mb->rd_ptr(sent);
- }
- ACE_Time_Value nowait(ACE_OS::gettimeofday());
- if (this->putq(mb, &nowait) == -1)
- {
- ACE_ERROR((LM_ERROR,
- ACE_TEXT("(%P|%t) %p\n"),
- ACE_TEXT("enqueue failed")));
- mb->release();
- return -1;
- }
- if (empty_queue)
- return this->reactor()->register_handler
- (this, ACE_Event_Handler::WRITE_MASK);
- return 0;
- }
- int
- Connection_Handler::handle_close(ACE_HANDLE h , ACE_Reactor_Mask mask)
- {
- if ( mask == ACE_Event_Handler::WRITE_MASK )
- return 0;
- return super::handle_close(h,mask);
- }
复制代码 |