如何用ace_message_block发送和接收struct结构的数据
2010-01-14 22:23
597 查看
主要是拼数据,一次读一个STRUCT内容,读几次,然后拼在一块,在然后进行后面的处理,看看ACE的例子
比如有这样的结构体
typedef struct Mystruct
{
unsigned int length;
int ID;
}MyStr;
存储可以采用
ACE_Message_Block* mb;
ACE_NEW_RETURN(mb,ACE_Message_Block(sizeof a);
ACE_OutputCDR cdr(sizeof u_long + sizeof long);
cdr << ACE_CDR::ULong(xxx);
cdr << ACE_CDR::Long(xxxx);
mb = cdr->begin()->clone();
void AIO_Input_Handler::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result) { if (!result.success () || result.bytes_transferred () == 0) delete this; else if (result.bytes_transferred () < result.bytes_to_read ()) reader_.read (*mblk_, result.bytes_to_read () - result.bytes_transferred ()); else if (mblk_->length () == LOG_HEADER_SIZE) { ACE_InputCDR cdr (mblk_); ACE_CDR::Boolean byte_order; cdr >> ACE_InputCDR::to_boolean (byte_order); cdr.reset_byte_order (byte_order); ACE_CDR::ULong length; cdr >> length; mblk_->size (length + LOG_HEADER_SIZE); reader_.read (*mblk_, length); } else { if (OUTPUT_HANDLER::instance ()->put (mblk_) == -1) mblk_->release (); ACE_NEW_NORETURN (mblk_, ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE)); ACE_CDR::mb_align (mblk_); reader_.read (*mblk_, LOG_HEADER_SIZE); } } 其他的也有: int Logging_Handler::recv_log_record (ACE_Message_Block *&mblk) { // Put <logging_peer>'s hostname in new message block. ACE_INET_Addr peer_addr; logging_peer_.get_remote_addr (peer_addr); mblk = new ACE_Message_Block (MAXHOSTNAMELEN + 1); peer_addr.get_host_name (mblk->wr_ptr (), MAXHOSTNAMELEN); mblk->wr_ptr (ACE_OS::strlen (mblk->wr_ptr ()) + 1); // Go past name // Allocate a message block for the payload; initially at least // large enough to hold the header, but needs some room for // alignment. ACE_Message_Block *payload = new ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE); // Align the Message Block for a CDR stream ACE_CDR::mb_align (payload); if (logging_peer_.recv_n (payload->wr_ptr (), 8) == 8) { payload->wr_ptr (8); // Reflect addition of 8 bytes // Create a CDR stream to parse the 8-byte header. ACE_InputCDR cdr (payload); // Extract the byte-order and use helper methods to // disambiguate octet, booleans, and chars. ACE_CDR::Boolean byte_order; cdr >> ACE_InputCDR::to_boolean (byte_order); // Set the byte-order on the stream... cdr.reset_byte_order (byte_order); // Extract the length ACE_CDR::ULong length; cdr >> length; // Ensure there's sufficient room for log record payload. ACE_CDR::grow (payload, 8 + ACE_CDR::MAX_ALIGNMENT + length); // Use <recv_n> to obtain the contents. if (logging_peer_.recv_n (payload->wr_ptr (), length) > 0) { payload->wr_ptr (length); // Reflect additional bytes // Chain the payload to mblk via the contination field. mblk->cont (payload); return length; } } // Error cases end up here, so we need to release the memory to // prevent a leak. payload->release (); payload = 0; mblk->release (); mblk = 0; return -1; } int Logging_Handler::write_log_record (ACE_Message_Block *mblk) { // Peer hostname is in the <mblk> and the log record data // is in its continuation. if (log_file_->send_n (mblk) == -1) return -1; if (ACE::debug ()) { // Build a CDR stream from the log record data. ACE_InputCDR cdr (mblk->cont ()); ACE_CDR::Boolean byte_order; ACE_CDR::ULong length; // Extract the byte-order and length, ending up at the start // of the log record itself. Use the byte order to properly // set the CDR stream for extracting the contents. cdr >> ACE_InputCDR::to_boolean (byte_order); cdr.reset_byte_order (byte_order); cdr >> length; ACE_Log_Record log_record; cdr >> log_record; // Finally extract the <ACE_log_record>. log_record.print (mblk->rd_ptr (), 1, cerr); } return mblk->total_length (); } int Logging_Handler::log_record () { ACE_Message_Block *mblk = 0; if (recv_log_record (mblk) == -1) return -1; else { int result = write_log_record (mblk); mblk->release (); // Free up the contents. return result == -1 ? -1 : 0; }
比如有这样的结构体
typedef struct Mystruct
{
unsigned int length;
int ID;
}MyStr;
存储可以采用
ACE_Message_Block* mb;
ACE_NEW_RETURN(mb,ACE_Message_Block(sizeof a);
ACE_OutputCDR cdr(sizeof u_long + sizeof long);
cdr << ACE_CDR::ULong(xxx);
cdr << ACE_CDR::Long(xxxx);
mb = cdr->begin()->clone();
相关文章推荐
- 通过消息跨进程发送与接收 TCopyDataStruct 数据 - 回复 "skymesh"
- android中如何发送及接收数据(两种方法)?
- 网卡如何发送和接收数据包
- springMVC如何接收和发送json数据对象
- android 蓝牙编程重点---如何发送和接收16进制数据
- 通过SOCKET发送、接收结构体数据
- android 蓝牙编程重点---如何发送和接收16进制数据
- android 蓝牙编程重点---如何发送和接收16进制数据
- 通过消息跨进程发送与接收 TCopyDataStruct 数据 - 回复 "skymesh"
- ACE_Message_Block消息数据类
- ACE-Task结构介绍(二)——消息块ACE_Message_Block结构的分析
- 前端如何接收 websocket 发送过来的实时数据
- android中如何发送及接收数据(两种方法)
- 如何使用JRtpLib3.5.0库中的函数和类对象和让JRTPLIB 发送/接收数据时使用同一个端口收藏
- 如何使用TCP向远端SERVER传输数据 (发送与接收)
- udp 发送和接收结构体数据
- 服务器接收和发送数据时 如何进行编码
- 16.3 socket数据如何在内核中接收和发送
- C Socket 发送/接收数据结构
- ios客户端向服务器端发送数据以及接收数据要如何实现?