Functions for serialization and unserialization

If the events to be sent across address spaces have no arguments or only primitive types as arguments, such as integers or chars, you have to call the function RiDSendRemoteEvent. However, if the events to be sent include objects as arguments, you must also provide two functions - one for serializing and one for unserializing the event arguments:

Serialization function

RhpAddress evStartSerialize(struct RiCEvent* const ev, const RhpAddress 
buffer, RhpPositive bufferSize, RhpPositive* resultBufferSize);

return value - pointer to the serialized event

ev - pointer of the event to be serialized

buffer - a local buffer that can be used for storing the serialized event (the user can allocate their own buffer instead)

bufferSize - the size in bytes of the parameter buffer

resultBufferSize - pointer for storing the size of the returned serialized event

Unserialization function

RiCEvent* evStartUnserialize(RhpAddress const serializedBuffer, RhpPositive serializedBufferSize);

return value - pointer to the unserialized event

serializedBuffer - pointer to the serialized buffer

serializedBufferSize - the size of the parameter serializedBuffer

Example of serialization and unserialization functions

The example refers to the event evStart, which is defined as follows:

struct evStart {
   RiCEvent ric_event;
   /***    User explicit entries    ***/
   char* msg;
};
RhpAddress evStartSerialize(struct RiCEvent* const ev, const RhpAddress 
buffer, RhpPositive bufferSize, RhpPositive* resultBufferSize) 
{  
        evStart* castedEv = (evStart*)ev;
        RhpPositive msgLength = strlen(castedEv->msg);
        /* Testing the size of the message parameter against the size of local 
buffer */
        if (bufferSize <= msgLength)
        {
              /* buffer too small - serialization is aborted */
              return NULL; 
        }
        /* copy the message string + the null terminating */
        memcpy(buffer, castedEv->msg, msgLength + 1);
        *resultBufferSize = msgLength + 1;
        return buffer; 
}

The following function uses a local buffer called receivedBuffer to store the string of the event evStart which was passed as a parameter.

RiCEvent* evStartUnserialize(RhpAddress const serializedBuffer, 
RhpPositive serializedBufferSize) {
        /* copy the message to a local buffer */
        memcpy(receivedBuffer, serializedBuffer, serializedBufferSize);
        return (RiCEvent*)RiC_Create_evStart(receivedBuffer);
}

Feedback