Introduction to ace_message_block

Source: Internet
Author: User

Ace_message_block is used to represent the storage space of messages in Ace. It can be used as a message buffer in network communication and is frequently used. The following describes ace_message_block functions.

  1. Create a Message Block
  2. Release message Blocks
  3. Read and Write Data from a Message Block
  4. Data Copying
  5. Other common functions

1. Create a Message Block

You can create message blocks in the following ways:

1. Directly allocate memory space for message blocks.

Ace_message_block * MB = new ace_message_block (30 );

2. Create a shared underlying data block.

Char buffer [100];

Ace_message_block * MB = new ace_message_block (buffer, 30 );

This method shares the underlying data block. The created Message Block does not copy the data or assume that it has its own ownership. When the Message Block MB is destroyed, the associated data buffer data will not be destroyed. This makes sense: the message block does not copy data, so the memory is not allocated by it, so it should not be responsible for destroying it.

3. Create a copy from an existing message block using the duplicate () function.

Ace_message_block * MB = new ace_message_block (30 );

Ace_message_block * mb2 = Mb-> duplicate ();

In this way, mb2 and MB share the same data space and use the reference counting mechanism of ace_message_block. It returns the pointer to the Message Block to be copied and adds the internal reference count.

4. Use the clone () function to copy messages from an existing message block.

Ace_message_block * MB = new ace_message_block (30 );

Ace_message_block * mb2 = Mb-> clone ();

The clone () method actually creates a new copy of the entire message block, including its data block and additional part; that is, this is a "Deep copy ".

2. Release message Blocks

Once the message block is used up, the programmer can call its release () method to release it.

  1. If the message data memory is allocated by the Message Block, calling the release () method will also release the memory.
  2. If the message block is counted by reference, release () reduces the count until it reaches 0. Then, the Message Block and the data block associated with it are removed from the memory.
  3. If a message block is created by sharing the allocated underlying data block, the underlying data block is not released.

No matter which method the message block is created, you only need to call the release () function after use to ensure that the corresponding memory can be correctly released.

3. Read and Write Data from a Message Block

Ace_message_block provides two pointer functions for programmers to perform read and write operations. rd_ptr () points to the readable data block address, and wr_ptr () points to the writable data block address, by default, the first address of the data block is executed. The following example shows how to use it.

# Include "ACE/message_queue.h"
# Include "ACE/OS. H"

Int main (INT argc, char * argv [])

{

Ace_message_block * MB = new ace_message_block (30 );

Ace_ OS: sprintf (MB-> wr_ptr (), "% s", "hello ");

Ace_ OS: printf ("% s \ n", MB-> rd_ptr ());

MB-> release ();

Return 0;

}

Note: The positions pointed to by these two pointers are not automatically moved. In the above example, after the function is executed, the execution position is still the first 0, instead of the latest writable position 5, the programmer needs to use the wr_ptr (5) function to manually move the write pointer position.

4. Data Copying

Generally, data can be copied by using a function. Copy () also ensures the update of wr_ptr () and points it to the new end of the buffer.

The following example demonstrates the usage of the copy () function.

MB-> copy ("hello ");

MB-> copy ("123", 4 );

Note: Since C ++ uses '\ 0' as the string end sign, for the above example, the underlying data block stores "Hello \ 0123 \ 0 ", ace_ OS: printf ("% s \ n", MB-> rd_ptr
(); The printed result is "hello". Pay attention when using the copy function to connect strings.

5. Other common functions

  1. Length () returns the Current Data Length
  2. Next () gets and sets the next ace_message_block link. (It is very useful to establish a Message Queue)
  3. Space () to obtain the remaining available space
  4. Size.

Summary:


Tip: http://www.cnblogs.com/TianFang/archive/2006/12/30/607960.html

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.