5.Message Object(Proud.CMessage)

ProudNet uses Proud.CMessage object type to handle a message object.In normal cases of using 4. Remote Method Invocation, you won't even need to bother yourself using Proud.CMessage.

Proud.CMessage is a high performance data stream object.Through data stream, user can save various data into one data block or call them at a finger - tip.

Proud.CMessage refers to memory buffer.The memory buffer can turn into an internal memory block(called internal buffer) owned by Proud.CMessage object and can refer to another memory block(called external buffer) since it is not directly owned by Proud.CMessage. To handle Proud.CMessage object, you need to assign which memory buffer the Proud.CMessage object will use through Proud.CMessage.UseInternalBuffer or Proud.CMessage.UseExternalBuffer. When a memory buffer is assigned and you want it to refer to another memory buffer, you must initialize the memory buffer in use first.Initialization can be done by Proud.CMessage.UninitBuffer.The sample for assigning a memory buffer can be found at 5.3 Selecting a message buffer.

In normal circumstances, you can use an internal buffer but it is much effective and fast if you set to read messages as directly referring to the memory block storing those messages rather then read after Proud.CMessage object separately copies them over.This is the case where you can utilize an external buffer.

In case of Proud.CMessage using internal buffer, the maximum size of message is Proud.CNetConfig.MessageMaxLength. In another hand of using external buffer, its maximum size is limited to the size of buffer itself.You can assign the length limit of message by Proud.CMessage.SetCount. You can also write or read data on message object via Proud.CMessage.Write or Proud.CMessage.Read.Because a message object is a data stream, its size stretches out as more data is written.Inside Proud.CMessage, there is a read offset which its value gets increased when data is being read.You can override this offset with Proud.CMessage.SetReadOffset.

그림 5-1Message Object Type

5.1The basic usage of message object

Here is the example for handling Proud.CMessage.

int a = 1;
Proud::String b = L"abc";
double c = 3.3;
char d[100];
Proud::CMessage msg; // Create a message object.
msg.UseInternalBuffer(); // Assign and use an internal buffer.

msg.Write(a); // Insert integer type parameter into the message object. The size of message gets determined by sizeof(int).
msg.WriteString(b); // Insert sting type parameter to the message object. The size of message gets increased to the length of string.
msg.Write(c); // Same
msg.Write(d, 100); // Insert 100 bytes memory block to the message object.

msg.SetReadOffset(0); // Move the read offset to the top.

msg.Read(a); // Read again
msg.ReadString(b); // Same
msg.Read(c); // Same
msg.Read(d,100); // Read 100 bytes memory block

// The read offset and the length of message should be identical since what are written and read are the same.
assert(msg.GetReadOffset() == msg.GetCount());

Proud::CMessage msg2; // Another message object
msg2 = msg; // Copy the original

5.2The basic usage of Message Obejct (<<,>>operators)

You can mix Proud.CMessage and <<, >> operators together.

<< operator can be used when adding a data on a message object while >> operator is for extracting a data from it.

int a = 1;
Proud::String b = L"abc";
double c = 3.3;

Proud::CMessage msg;
msg.UseInternalBuffer();

msg << a << b << c; // Save to a message object in order of a,b,c
msg >> a >> b >> c; // Extract from a message object in order of a,b,c

5.3Selecting a message buffer

Selecting a message buffer

char a[100]; // External buffer
Proud::CMessage msg; // Message object
msg.UseExternalBuffer(a,100); // Use an external buffer but notify the maximum size as well.

msg.SetCount(3); // Use an external buffer but set as there is 3 bytes of data already stored inside of a message object.
msg.SetCount(101); // Exception occurs if it exceeds the size of external buffer

Proud::CMessage msg2; // Another message object
msg2 = msg; // Copy the original. msg and msg2 should refer to the same external buffer.

Below is an example for using a memory buffer in several ways.

char a[100]; // External buffer
Proud::CMessage msg; // Message object
msg.UseExternalBuffer(a,100); // Use an external buffer but notify its maximum size as well.

msg.SetCount(3); // Use an external buffer but set as there is 3 bytes of data already stored inside of a message object.

msg.UninitBuffer(); // Cancel the use of memory buffer and initialize its status.
msg.UseInternalBuffer(); // Assign to use an internal memory buffer.