handle_read_stream的问题
void Receiver::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
if (!result.success()||result.bytes_transferred()==0)
{
cout<<"delete ..."<<endl;
delete this;
return ;
}
//ProactorTPSingleton::instance()->putq(&result.message_block());
//ACE_OS::printf("%d:putq\n",global++);
else if (mblk_->length() == SIZE_TO_READ)//获得需要接收的字节数
{
cout<<"sizetoread == "<<SIZE_TO_READ<<endl;
cout<<"data is..."<<mblk_->rd_ptr()<<endl;
cout<<"trasfered "<<result.bytes_transferred()<<endl;
/* ACE_InputCDR cdr (mblk_); 参照C++NP2上的例子没得到正确结果
ACE_CDR::Boolean byte_order;
cdr >> ACE_InputCDR::to_boolean (byte_order);
cdr.reset_byte_order (byte_order);
ACE_CDR::ULong length;
cdr >> length;
cout<<"need read :"<<length<<endl;*/
char *len = mblk_->rd_ptr();
u_long length = atoi(len);
cout<<"need read :"<<length<<endl;
mblk_->size(length+SIZE_TO_READ);//扩大容量,接收数据
rs_.read(*mblk_,length);//投递读
}
else//问题出在这,执行上面的else if时,这个else语句块也执行!!!不解
{
cout<<"recv data..."<<mblk_->rd_ptr()<< endl;
ProactorTPSingleton::instance()->putq(mblk_);
ACE_NEW(mblk_,ACE_Message_Block(SIZE_TO_READ));
//ACE_CDR::mb_align (mblk_);
rs_.read(*mblk_,SIZE_TO_READ);
cout<<" another read ..."<<endl;
}
};
客户端发测试数据:
char buf[]="1111";
peer.send(buf,sizeof buf)
可以发现,else if语句块,和最后的else语句块都执行了,感觉很奇怪,只发了一次数据,读取完后,应该只执行一次,handle_read_stream ,但结果好像时执行了2次,为什么? 你这里没有处理“短读”的问题,就是实际收取的数据长度,小于你要求的数据长度。
}else if(result.bytes_transferred() < result.bytes_to_read())
{
} 基本上就是你说的问题
页:
[1]