CmResult COFP10PacketOutMsg::StreamFrom(ACE_InputCDR &is) { CmResult lRet = CM_ERROR_FAILURE; lRet = COFPMessage::StreamFrom(is); if (CM_FAILED(lRet)) { ACE_ERROR((LM_ERROR, ACE_TEXT("COFP10PacketOutMsg::StreamFrom, COFPMessage::StreamFrom fail\n"))); return lRet; } is>>m_tMember.buffer_id; is>>m_tMember.in_port; is>>m_tMember.actions_len; bool bGood = is.good_bit(); CM_ASSERT_RETURN(bGood, CM_ERROR_FAILURE); if (m_tMember.actions_len > 0) { ACE_INT16 swListLen = m_tMember.actions_len; ACE_UINT16 i = 0; while (swListLen > 0) { COFP10Action *action = nullptr; lRet = COFP10Action::DecodeAction(is, action); if (CM_SUCCEEDED(lRet)) { m_action_list.push_back(action); ACE_DEBUG((LM_DEBUG, ACE_TEXT("action[%u], len=%u\n"), i++, action->GetActionLen())); swListLen -= action->GetActionLen(); } else { ACE_ERROR((LM_ERROR, ACE_TEXT("COFP10PacketOutMsg::StreamFrom, COFP10Action::DecodeAction failed\n"))); return CM_ERROR_FAILURE; } } } ACE_INT16 swDataLen = m_wLength - COFPMessage::GetStreamLen() - sizeof(m_tMember) - m_tMember.actions_len; if (swDataLen < 0) { ACE_ERROR((LM_ERROR, ACE_TEXT("COFP10PacketOutMsg::StreamFrom, packet data len(%d) is invalid\n"), swDataLen)); ACE_ASSERT(0); return CM_ERROR_FAILURE; } if (swDataLen == 0) { m_wDataLen = 0; return CM_OK; } ACE_Message_Block Data(swDataLen); ACE_DEBUG((LM_DEBUG, ACE_TEXT("COFP10PacketOutMsg::StreamFrom, wr_ptr = %p, space = %u\n"), Data.wr_ptr(), Data.space())); bGood = is.read_char_array(Data.wr_ptr(), swDataLen); CM_ASSERT_RETURN(bGood, CM_ERROR_FAILURE); Data.wr_ptr((size_t)swDataLen); if (m_PacketData) { m_PacketData->append(Data.duplicate()); } else { m_PacketData = Data.duplicate(); } m_wDataLen += swDataLen; return CM_OK; }
CmResult COFP13PacketOutMsg::StreamFrom(ACE_InputCDR &is) { ACE_INT32 remainActLen = 0; ACE_INT32 packet_length = 0; ACE_CDR::Octet pad[6]; CmResult lRet = CM_ERROR_FAILURE; lRet = COFPMessage::StreamFrom(is); if (CM_FAILED(lRet)) { ACE_ERROR((LM_ERROR, ACE_TEXT("COFP13PacketOutMsg::StreamFrom(), COFPMessage::StreamFrom fail\n"))); return lRet; } ACE_DEBUG((LM_DEBUG, ACE_TEXT("COFP13PacketOutMsg::StreamFrom(): \n"))); is>>m_buffer_id; is>>m_in_port; is>>m_actions_len; is.read_octet_array(pad, sizeof(pad)); bool bGood = is.good_bit(); CM_ASSERT_RETURN(bGood, CM_ERROR_FAILURE); ACE_DEBUG((LM_DEBUG, ACE_TEXT("m_actions_len=%u\n"), m_actions_len)); if (m_actions_len>0) { remainActLen = m_actions_len; ACE_UINT16 i = 0; while (remainActLen > 0) { COFP13Action *action = nullptr; CmResult result = COFP13Action::DecodeAction(is, action); if (CM_SUCCEEDED(result)) { m_action_list.push_back(action); ACE_DEBUG((LM_DEBUG, ACE_TEXT("action[%u], len=%u\n"), i++, action->GetStreamLen())); remainActLen = remainActLen - (action->GetStreamLen()); } else { ACE_ERROR((LM_ERROR, ACE_TEXT("COFP13Action::DecodeAction failed.\n"))); return CM_ERROR_FAILURE; } } } ACE_UINT32 length = sizeof(m_buffer_id)+sizeof(m_in_port)+sizeof(m_actions_len)+sizeof(m_pad); ACE_DEBUG((LM_DEBUG, ACE_TEXT("length=%u\n"), length)); packet_length = m_wLength-COFPMessage::GetStreamLen()-length-m_actions_len; ACE_DEBUG((LM_DEBUG, ACE_TEXT("packet_length=%d, GetStreamLen=%u\n"), packet_length, GetStreamLen())); if (packet_length < 0) { ACE_ERROR((LM_ERROR, ACE_TEXT("COFP13PacketOutMsg::StreamFrom, error, length=%u, actionlength=%u\n"), length, m_actions_len)); return CM_ERROR_FAILURE; } ACE_Message_Block data(packet_length); ACE_DEBUG((LM_DEBUG, ACE_TEXT("COFP13PacketOutMsg::StreamFrom, wr_ptr=%p, space=%u\n"), data.wr_ptr(), data.space())); bGood = is.read_char_array(data.wr_ptr(), packet_length); CM_ASSERT_RETURN(bGood, CM_ERROR_FAILURE); data.wr_ptr((size_t)packet_length); if (m_PacketData) { m_PacketData->append(data.duplicate()); } else { m_PacketData = data.duplicate(); } return CM_OK; }
virtual int svc () { const size_t FileReadSize = 8 * 1024; ACE_Message_Block mblk (FileReadSize); for (;; mblk.crunch ()) { // Read as much as will fit in the message block. ssize_t bytes_read = logfile_.recv (mblk.wr_ptr (), mblk.space ()); if (bytes_read <= 0) break; mblk.wr_ptr (static_cast<size_t> (bytes_read)); // We have a bunch of data from the log file. The data is // arranged like so: // hostname\0 // CDR-encoded log record // So, first we scan for the end of the host name, then // initialize another ACE_Message_Block aligned for CDR // demarshaling and copy the remainder of the block into it. We // can't use duplicate() because we need to be sure the data // pointer is aligned properly for CDR demarshaling. If at any // point, there's not enough data left in the message block to // extract what's needed, crunch the block to move all remaining // data to the beginning and read more from the file. for (;;) { size_t name_len = ACE_OS::strnlen (mblk.rd_ptr (), mblk.length ()); if (name_len == mblk.length ()) break; char *name_p = mblk.rd_ptr (); ACE_Message_Block *rec, *head, *temp; ACE_NEW_RETURN (head, ACE_Message_Block (name_len, MB_CLIENT), 0); head->copy (name_p, name_len); mblk.rd_ptr (name_len + 1); // Skip nul also size_t need = mblk.length () + ACE_CDR::MAX_ALIGNMENT; ACE_NEW_RETURN (rec, ACE_Message_Block (need), 0); ACE_CDR::mb_align (rec); rec->copy (mblk.rd_ptr (), mblk.length ()); // Now rec contains the remaining data we've read so far from // the file. Create an ACE_InputCDR to start demarshaling the // log record, header first to find the length, then the data. // Since the ACE_InputCDR constructor increases the reference count // on rec, we release it upon return to prevent leaks. // The cdr 'read' methods return 0 on failure, 1 on success. ACE_InputCDR cdr (rec); rec->release (); ACE_CDR::Boolean byte_order; if (!cdr.read_boolean (byte_order)) { head->release (); rec->release (); break; } cdr.reset_byte_order (byte_order); // Now read the length of the record. From there, we'll know // if rec contains the complete record or not. ACE_CDR::ULong length; if (!cdr.read_ulong (length)) { head->release (); mblk.rd_ptr (name_p); break; } if (length > cdr.length ()) { head->release (); mblk.rd_ptr (name_p); break; } // The complete record is in rec... grab all the fields into // separate, chained message blocks. ACE_NEW_RETURN (temp, ACE_Message_Block (length, MB_TEXT), 0); ACE_NEW_RETURN (temp, ACE_Message_Block (2 * sizeof (ACE_CDR::Long), MB_TIME, temp), 0); ACE_NEW_RETURN (temp, ACE_Message_Block (sizeof (ACE_CDR::Long), MB_PID, temp), 0); ACE_NEW_RETURN (temp, ACE_Message_Block (sizeof (ACE_CDR::Long), MB_TYPE, temp), 0); head->cont (temp); // Extract the type ACE_CDR::Long *lp; lp = reinterpret_cast<ACE_CDR::Long*> (temp->wr_ptr ()); cdr >> *lp; temp->wr_ptr (sizeof (ACE_CDR::Long)); temp = temp->cont (); // Extract the pid lp = reinterpret_cast<ACE_CDR::Long*> (temp->wr_ptr ()); cdr >> *lp; temp->wr_ptr (sizeof (ACE_CDR::Long)); temp = temp->cont (); // Extract the timestamp (2 Longs) lp = reinterpret_cast<ACE_CDR::Long*> (temp->wr_ptr ()); cdr >> *lp; ++lp; cdr >> *lp; temp->wr_ptr (2 * sizeof (ACE_CDR::Long)); temp = temp->cont (); // Demarshal the length of the message text, then demarshal // the text into the block. ACE_CDR::ULong text_len; cdr >> text_len; cdr.read_char_array (temp->wr_ptr (), text_len); temp->wr_ptr (text_len); // Forward the whole lot to the next module. if (put_next (head) == -1) break; // Move the file-content block's read pointer up past whatever // was just processed. Although the mblk's rd_ptr has not been // moved, cdr's has. Therefore, use its length() to determine // how much is left. mblk.rd_ptr (mblk.length () - cdr.length ()); } } // Now that the file is done, send a block down the stream to tell // the other modules to stop. ACE_Message_Block *stop; ACE_NEW_RETURN (stop, ACE_Message_Block (0, ACE_Message_Block::MB_STOP), 0); put_next (stop); return 0; }