[Switch: STL extended Technical Manual Volume 1] ace_message_queue adaptation

Source: Internet
Author: User

Ace_message_queue

I recently participated in some commercial network projects on the middleware routing service. To meet the project requirements, I spent some effort to adapt the Self-Adaptive Communication Environment (ACE) the memory queue is adapted to the STL set concept. The results will be covered in this chapter. Before using the ace reactor framework, you need to derive the event processor class from ace_event_handler (overwrite the relevant event processing method), and then register its instanceProgramIn the singleton instance. Once the ace_reactor instance receives an I/o Event and the processor instance corresponding to the event has been registered, it calls the appropriate callback method of the latter. When used together with TCP-stream-Oriented Internet transmission protocol, Ace is used to save the received data with an ace_message_block instance, and then in the class template ace_message_queue (a special) queuing In the instance, as shown in listing 31.5 (for simplicity, ignore error handling ).

Listing 31.5. A simple event processor in the ace reactor framework

 class simpletcpreceiver 
: Public ace_event_handler
{< br>...
virtual int handle_input (ace_handle h)
{< br> const size_t block_size = 1024;
ace_message_block * MB = new ace_message_block (block_size );
ssize_t n = m_peer.recv (MB-> base (), MB-> size ();
MB-> wr_ptr (N );
m_mq.enqueue_tail (MB);
return 0;
}< br>...
PRIVATE: // member variables
ace_sock_stream m_peer; // connection socket
ace_message_queue m_mq; // Message Queue
};

The ace_message_queue class acts as a repository where all blocks are stored in order, so it faithfully expresses the original data stream. However, ace_message_queue is a strictly block container. It does not provide any abstract means to access the block content. Instead, it traverses the content using the associated ace_message_queue_iterator class template, as shown in listing 31.6. If the next block in the container is available, the ace_message_queue_iterator: Next () method returns a non-0 result and sets the pointer to be passed in. Otherwise, 0 is returned directly. The advance () method moves the current traversal position to the next block (if any ).

Listing 31.6. Example of using ace_message_queue_iteratorCode

Void simpletcpreceiver: processqueue ()
{
Ace_message_queue_iterator <ace_null_synch> mqi (m_mq );
Ace_message_block * MB;
For (; mqi. Next (MB); mqi. Advance ())
{
{For (size_t I = 0; I <MB-> length (); ++ I)
{
Printf ("% C", I [MB-> rd_ptr ()];
}}
MB-> rd_ptr (MB-> length (); // advance read PTR to "Exhaust" Block
}
}

Obviously, if you want to treat multiple blocks as a logical continuous single block, the situation will become a little messy. We need a sequence to "flat" the stream and make it suitable for STL manipulation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.