InputCDR -<Message_Block>- OutputCDR 的疑惑
我尝试写了如下程序#include "ace/OS.h"
#include "ace/Message_Block.h"
#include "ace/ACE.h"
#include "ace/CDR_Stream.h"
#include <iostream>
using std::cout;
using std::endl;
struct A
{
int id;
char name;
};
int main ()
{
ACE_Message_Block *head = new ACE_Message_Block (sizeof (A) );
ACE_Message_Block *mblk = head;
ACE_InputCDR cdr(mblk);
A a;
a.id = 1097;
ACE_OS::strcpy(a.name, "Chinese Guy");
{
char buf;
memset(buf, 0x00, sizeof a);
memcpy(buf, &a, sizeof a);
cdr.read_char_array((char*)&a, sizeof a);
cout << "Message Block Size = " << mblk->size() << endl;
mblk = head;
ACE_OutputCDR ocr(mblk);
A a2;
bzero(&a2, sizeof a2);
ocr.write_char_array((char*)&a2, sizeof a2);
cout << a2.id << endl;
cout << a2.name << endl;}
head->release ();
return 0;
}
程序结果是:
Message Block Size = 24
0
(空)
似乎值没有被传递.请问是没有传到mblk里还是mblk没有传递给结构体实例a2呢? ACE_OutputCDR ocr(mblk);
和你想象的有差别,这里没有写入mblk,你跟踪一下代码就知道了。
默认是写入另外的地方,做了内部连接。需要复制出来。
int
ACE_OutputCDR::grow_and_adjust (size_t size,
size_t align,
char*& buf)
{
if (!this->current_is_writable_
|| this->current_->cont () == 0
|| this->current_->cont ()->size () < size + ACE_CDR::MAX_ALIGNMENT)
{
// Calculate the new buffer's length; if growing for encode, we
// don't grow in "small" chunks because of the cost.
size_t cursize = this->current_->size ();
if (this->current_->cont () != 0)
cursize = this->current_->cont ()->size ();
size_t minsize = size;
#if !defined (ACE_LACKS_CDR_ALIGNMENT)
minsize += ACE_CDR::MAX_ALIGNMENT;
#endif /* ACE_LACKS_CDR_ALIGNMENT */
// Make sure that there is enough room for <minsize> bytes, but
// also make it bigger than whatever our current size is.
if (minsize < cursize)
minsize = cursize;
const size_t newsize = ACE_CDR::next_size (minsize);
this->good_bit_ = false;
ACE_Message_Block* tmp;
ACE_NEW_RETURN (tmp,
ACE_Message_Block (newsize,
ACE_Message_Block::MB_DATA,
0,
0,
this->current_->data_block ()->allocator_strategy (),
0,
0,
ACE_Time_Value::zero,
ACE_Time_Value::max_time,
this->current_->data_block ()->data_block_allocator ()),
-1);
this->good_bit_ = true;
#if !defined (ACE_LACKS_CDR_ALIGNMENT)
// The new block must start with the same alignment as the
// previous block finished.
ptrdiff_t tmpalign =
ptrdiff_t(tmp->rd_ptr ()) % ACE_CDR::MAX_ALIGNMENT;
ptrdiff_t curalign =
ptrdiff_t(this->current_alignment_) % ACE_CDR::MAX_ALIGNMENT;
ptrdiff_t offset = curalign - tmpalign;
if (offset < 0)
offset += ACE_CDR::MAX_ALIGNMENT;
tmp->rd_ptr (static_cast<size_t> (offset));
tmp->wr_ptr (tmp->rd_ptr ());
#endif /* ACE_LACKS_CDR_ALIGNMENT */
// grow the chain and set the current block.
tmp->cont (this->current_->cont ());
this->current_->cont (tmp);
}
this->current_ = this->current_->cont ();
this->current_is_writable_ = true;
return this->adjust (size, align, buf);
}
页:
[1]