markiodev / Networker

A simple to use TCP and UDP networking library for .NET. Compatible with Unity.
MIT License
478 stars 77 forks source link

Decouple packet handlers from serialization #36

Closed snarlynarwhal closed 5 years ago

snarlynarwhal commented 5 years ago

Prototype code for Networker.Formatter.MessagePack and decoupling packet handlers from serialization so that a packet handler can process multiple packets.

This code makes use of MessagePack's Typeless serialization. I added:

The library modifications include:

As is, this code works with MessagePack, but it won't for Protobuf or Zero Formatter since it relies of Typeless serialization. We can make it work for Prototbuf simply by passing in the Type. But since Zero Formatter relies strictly on generics, so I don't think it will work with it. That being said, Zero Formatter has been superseded by MessagePack so how important is it for Networker to support Zero Formatter?

Ideally, we would completely defer deserialization to the packet processor and just make PacketHandlerBase<T> an empty class that inherits IPacketHandler<T> to prevent breaking changes.

Right now the PacketHandlerBase.Handle method makes use of reflection.. not sure if there's a better alternative. We can always cache a map of packet types to process methods and even precache on startup.

What do you think?

markiodev commented 5 years ago

I handled this a slightly different way but will add documentation on how to use the way you've done it on top. Thanks a lot for doing Networker.Formatter.MessagePack, but please rename to new format Networker.Extensions.MessagePack and include in separate PR.

snarlynarwhal commented 5 years ago

@MarkioE I'll take care of that later today!