关于Message_Block的一些问题
写了如下程序,#include "ace/OS.h"
#include "ace/ACE.h"
#include "ace/Message_Block.h"
#include "ace/Log_Msg.h"
#pragma pack(1)
struct Head
{
int status;
short seq;
char name;
};
#pragma pack()
int
main(void)
{
ACE_Message_Block *mblk = new ACE_Message_Block (sizeof (Head) * 4);
Head First_Head;
First_Head.status = 0;
First_Head.seq = 12456;
ACE_OS::strcpy(First_Head.name, "Lien.C");
mblk->copy((char*)&First_Head, sizeof (Head));
ACE_DEBUG((LM_DEBUG, "Message Block Size = %d\n", mblk->size()));
Head Second_Head;
mblk->copy((char*)&First_Head, sizeof (Head));
ACE_OS::memcpy(&Second_Head, mblk->rd_ptr(), sizeof Second_Head);
Head Third_Head;
mblk->rd_ptr(sizeof (Head) * 2);
ACE_OS::strcpy(First_Head.name, "中华人民共和国");
mblk->copy((char*)&First_Head, sizeof (Head));
ACE_OS::memcpy(&Third_Head, mblk->rd_ptr(), sizeof Second_Head);
ACE_DEBUG((LM_DEBUG, "Message Block Size = %d\n", mblk->space()));
ACE_DEBUG((LM_DEBUG, "Second_Head Infomation: name - %s\n", Second_Head.name));
ACE_DEBUG((LM_DEBUG, "Third_Head Infomation: name - %s\n", Third_Head.name));
ACE_Message_Block *mblk_2 = mblk->clone();
mblk_2->duplicate(mblk);
ACE_DEBUG((LM_DEBUG, "Message Block Cloned Size = %d\n", mblk_2->size()));
mblk_2->reset();
mblk->cont(mblk_2);
mblk->rd_ptr(sizeof (Head) * 3);
Head Fourth_Head;
ACE_OS::memcpy(&Fourth_Head, mblk->rd_ptr(), sizeof (Head));
ACE_DEBUG((LM_DEBUG, "Third_Head Infomation: name - %s\n", Fourth_Head.name));
mblk->release();
mblk = 0;
mblk_2->release();
mblk_2 = 0;
return 0;
}
运行结果:
Message Block Size = 104
Message Block Size = 26
Second_Head Infomation: name - Lien.C
Third_Head Infomation: name - 中华人民共和国
Message Block Cloned Size = 104
Third_Head Infomation: name - œß@(乱码)
Illegal instruction (不知道为什么会输出这个, 可能地址越界)
在做mblk->cont以后发现信息似乎没有被写进来. 请问是在mblk->clone()的时候出了问题, 还是我的读指针rd_ptr不对呢? 似乎解决了呢.
将mblk->cont(mblk_2)改成:
mblk->wr_ptr ( mblk->size() );
mblk->size ( mblk->size() + mblk_2->size() );
mblk_2->reset();
mblk->copy ( mblk_2->rd_ptr(), mblk_2->size() );
在ace的例子Logging_Handler.cpp中对ACE_Message_Block的数据块连接处理似乎简洁, 不知道为什么我的不太好用,只好用这个笨一点的办法了. 希望有人指点直接连接多个ACE_Message_Block块的方法.
注: mblk->clone()是深拷贝, mblk->duplicate()是浅拷贝
mblk->copy()会将写指针wr_ptr向后移动新拷贝数据块大小
mblk->reset()会根据align方式移动到原点(默认原点是数据块头)
还有一个ACE_Message_Queue似乎是用来管理以ACE_Message_Block块为数据结点的链表, 正在研究怎么用. C++网络编程卷1里面有详细的解释。
你对这个类的认识不清,所以上面的code有很多问题,看着都累人呐。加油。
页:
[1]