The ACE_Message_Block class enables efficient manipulation of fixed- and variable-sized messages. ACE_Message_Block implements
the Composite pattern [GHJV95] and provides the following capabilities:
-
Each ACE_Message_Block contains a pointer to a reference-counted ACE_Data_Block,
which in turn points to the actual data associated with a message. This design allows flexible and efficient sharing of data and minimizes excessive memory copying overhead.
-
It allows multiple messages to be chained together into a singly linked list to support composite messages, which can be used for polymorphic lists and for layered protocol stacks that require headers/trailers to be inserted/removed efficiently.
-
It allows multiple messages to be joined together in a doubly linked list that forms the basis of the ACE_Message_Queue class outlined
on page 228 and described in [SH].
-
It treats synchronization and memory management properties as aspects [Kic97, CE00]
that applications can vary without changing the underlying ACE_Message_Block implementation.
-
The ACE_Message_Block Class Diagram
-
照片名稱:Two Kinds of ACE_Message_Block
-
-
ACE_Message_Block
一般和 ACE_InputCdr,ACE_outputCdr 結合使用,以下是使用的一些方法:
-
//mb = new ACE_Message_Block();
//ACE_CDR::mb_align(mb);
/*ACE_OutputCDR cdr(mb->wr_ptr(),ACE_CDR::MAX_ALIGNMENT+8);*/ //這種方法不對
ACE_OutputCDR cdr(ACE_CDR::MAX_ALIGNMENT+8);
size_t length1 = cdr.length(); -
ACE_Message_Block * mb = NULL;
mb = new ACE_Message_Block(cdr.begin()->rd_ptr(),cdr.begin()->total_length());
ACE_ASSERT(cdr.begin()->rd_ptr() == cdr.begin()->wr_ptr() );
cdr << ACE_CDR::Long(22);
cdr << ACE_CDR::Long(22); -
size_t length2 = cdr.length();
-
/*mb->wr_ptr((cdr.begin())->total_length());*/
-
mb->wr_ptr(length2 - length1); // wr_ptr指針要往前移動 length2 - length1個字節,ACE_OutputCDR 不會去更新wr_ptr,只能手工更新
ACE_Message_Block * payload = NULL;
payload = new ACE_Message_Block("11111"); //申請另外一個 ACE_Message_Block
payload->rd_ptr(5); -
payload->wr_ptr(10);
-
mb->cont(payload); //形成ACE_Message_Block鏈表
ACE_InputCDR 的使用
-
ACE_Message_Block * mb = new ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE);
if(sock_.recv_n(mb->wr_ptr(),8) == 8)
{
mb->wr_ptr(8);
ACE_InputCDR cdr(mb); -
ACE_CDR::Long Type,Type1;
cdr >> Type; -
cdr >>Type1;
-
if(sock_.recv_n(mb->wr_ptr(),3) > 0)
{
mb->wr_ptr(3);
}
關於ACE_Message_block ,ACE_Inputcdr,ACE_OutputCdr的使用的注意事項還有很多,需要一步一步發掘。