WIP: Another Networking System
28.04.2022 -
Share

I know I did Maz- / MazSimpleNetwork but still wanted a way with automatic serialization. Here find one.

I've been working on a networking system as a side project for a while now, and I'm actually pretty proud of how it's turning out.

One thing I've always been interested in is automatic serialization over the network. I finally got quite a few things working, and most of the serialization now happens (semi-) automatically.

It's just a console application for now, nothing to do with Unity yet. Everything is still pretty much a prototype, with no proper validation or security checks in place. That stuff will come later.

Automatically synchronizing objects

For example, let's say a client changes a player's health and sends the player over the network:

var player = new Player();
player.NetworkID = 1;
player.Health = 99;

rpc.SendClient<Player>(player);

Only three pieces of information are actually sent: the network ID, the health value, and the message type indicating that this is a serializable object.

The server recognizes the message as a SerializableObject, looks up the object with the matching network ID, and updates its local copy.

Normally, you wouldn't want clients to be able to modify something like health directly, but who cares for now? It's just a test.

The screenshot shows how the data gets serialized and deserialized.

Calling functions over the network

Another thing I got working is calling methods on the server directly from a client.

Client:

// int 10, float 5f
rpc.Call("DedicatedServer.SimpleGameServer.ApplyShipDamage", 10, 5f);

Server:

[RPC]
void ApplyShipDamage(int damage, float bla)
{
    Console.WriteLine("ApplyShipDamage: " + damage + " " + bla);
}

In an older version, I had to write the serialization methods myself. That was pretty annoying, especially when adding new types or changing existing ones.

Now the system automatically figures out how everything needs to be serialized. It generates the serialization logic once at startup and reuses it for subsequent calls.

No need to manually write serialization code for every single method anymore. Pretty nice!

What's next?

I had two ideas for making synchronization even easier: using something like NetworkVar<int> mana, or turning NetworkVar into an attribute.

With the attribute approach, I could potentially get rid of explicit Serialize() and Deserialize() methods entirely.

That would be pretty cool.

Update: I ended up implementing NetworkVar<T>, and it works wonderfully!

Now, whenever the server changes something like Mana.Value, the new value automatically gets synchronized over the network.

One less thing to worry about. ♥