|
楼主 |
发表于 2008-7-13 23:14:16
|
显示全部楼层
ACE_Service_Handler实例里面的部分代码及处理方式
定义了一个结构作为包头放在数据包前面,通过ACE_Message_Block连接起来
struct PacketHeader
{
CReceiveProcess *handler;
int packet_length;
};
开始接收前的处理,先往ACE_Message_Block中压入一个PacketHeader,并把ACE_Service_Handler的实例句柄放进去,注释掉的那句是我原来的写法
ACE_NEW_NORETURN (recv_data_, ACE_Message_Block (sizeof(PacketHeader), MB_NORMAL_PACKET));
this->recv_data_->copy (ACE_reinterpret_cast(char* ,(CReceiveProcess *)this), sizeof(this));
//this->recv_data_->copy ((const char *)(this), sizeof(this));
this->reader_.read (*recv_data_, recv_data_->space ());
用于ACE_Task调用的回包函数,异常就是发生在异步写的这一行
void CReceiveProcess ::send(ACE_Message_Block &mb)
{
if (this->write_.write (mb , mb.length() ) == -1)
{
mb.release ();
//ACE_ERROR (LM_ERROR, "%p\n", "ACE_Asynch_Write_File::write"), -1);
}
}
Task里面的处理,先得到PacketHeader,然后是具体的数据包,然后调用send发放进行回包
PacketHeader * hdr = reinterpret_cast<PacketHeader *> (message->rd_ptr ());
ACE_Message_Block * data_mb = message->cont();
CReceiveProcess * msg_handler = ACE_reinterpret_cast(CReceiveProcess *,hdr->handler);
msg_handler->send(*mbt);
mbt是我测试的数据,系统时间而已,代码如下
//向发送端发回系统时间
ACE_OS::sleep(1);
time_t now = ACE_OS::gettimeofday().sec();
char *time = ctime(&now); //获取当前时间的字符串格式
ACE_Message_Block *mbt = new ACE_Message_Block(100);
mbt->copy(time); |
|