Skip to content

What is the recommended approach for copying of a message? #174

@yoloroy

Description

@yoloroy

I'm developing a multiplayer game where the server needs to relay incoming command messages to all other clients in the session. I'm separating commands (reliable messages for input, abilities, etc.) from updates (unreliable messages for game state).

My goal is to avoid deserializing the message on the server and then re-serializing it into a new message, as this adds significant boilerplate.

What I have tried

I attempted to use AddBytes and AddMessage to copy the original message's payload into a new one. The resulting message had the expected size, but the bits within the copy were corrupted and did not correspond to the original data.

var copy = Message.AddMessage(message, 8 * known_constant_for_this_message_type, 0) // returns corrupted data
             // or AddMessage(message) when dealing with incoming message
    .AddInt(senderId) // or other appendage

Workaround: Manual parsing and reconstruction

My current workaround is to manually deserialize the message on the server and then create new Message object, reserializing the data on the next line. This works correctly:

// receiving the data
var player = src.Players[sender];
player.Position = message.GetVector2();
player.Vector = message.GetVector2();
var timestamp = message.GetDouble(); // todo [inter,extra]polation, maybe some simulation and all of that

// reserialization
copy = Message.Create()
    .AddVector2(player.Position)
    .AddVector2(player.Vector)
    .AddDouble(timestamp)
    .AddInt(sender);

While this works, doing it for every command will be tedious, especially for commands that don't affect the game state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionThis issue is a question or a request for help/clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions