Using user-defined class type in RMI

The act of converting RMI call to message or extracting necessary value from message to call RMI is called marshaling.

ProudNet offers a marshaling function for the basic type such as int or float.

Of course, you can also use user-defined class type besides from the basic one. In order to use user-defined class type, you need to create overloading of following functions.

// MyType.h
namespace Proud
{
// Ouput the contents within called RMI after converting them to strings.
// This is convenient in making a log.
void AppendTextOut(String &a,const MyType &b);
// Read the custom type content from message buffer.
CMessage& operator>>(CMessage &a, MyType &b);
// Insert the custom type content in message buffer.
CMessage& operator<<(CMessage &a, const MyType &b);
}

ProudNet's RMI function uses Proud.CMessage internally. And through overloading of above functions, RMI parameter gets marshaling.

Here is the case of using Proud.CMessage. Proud.CMessage holds message data that is used for converting RMI to message or reading parameter from message and also can be used as stream object.

How to stream marshaling function is shown in below.

// MyType.cpp
namespace Proud
{
CMessage& operator>>(CMessage &a, MyType &b)
{
a>>b.x,b.y>>b.z>>b.w;
return a;
}
CMessage& operator<<(CMessage &a, const MyType &b)
{
// Do not use a.UseInternalBuffer()!
a<<b.x,b.y<<b.z<<b.w;
return a;
}
void AppendTextOut(String &a,const MyType &b)
{
f.Format(L"{x=%f,y=%f,z=%f,w=%f}",b.x,b.y,b.z,b.w);
a+=f;
}
}

In last, the header file that declares above overloading methods must be #include first before #include proxy and stub files generated by PIDL compiler.

// Example 1
#include "MyType.h"
#include "MyPIDL_proxy.h"
// Example 2
#include "MyType.h"
#include "MyPIDL_stub.h"

The practical use of marshaling is guided in Sample code of marshaling custom type object or <Sample/CasualGame/GCServer/FarmCommon.h>.

Also you can check whether it works or not with Proud.TestMarshal()