Current code does this:
protected void Decode<T>(Action<BinaryReader> action, byte[] buffer) {...}
Creates a MemoryStream. Creates a BinaryReader reader in a stream. And then performs the action (reader).
Now suppose we have HelloMessage.cs, which has only two properties: a byte identifier and an int descriptor. The Decode method of the HelloMessageCodec.cs decoder class is as follows:
public HelloMessage Decode(byte[] buffer) { var message = new HelloMessage(); Decode<HelloMessage>(reader => { message.Id = reader.ReadByte(); message.Descriptor = reader.ReadInt32(); }, buffer); return message; }
This is really beautifully done, but there are about 15 types of messages, and each type has its own xxxMessageCodec.cs with decoding and encoding implementations, each of which has properties passed manually to the Action delegate.
Now I need to do encryption, which means if I have to follow this pattern, I would have to build 15 different message ciphers.
I have already reworked the entire codec code to solution No. 2 (with reflection), and instead of 15 functions, I have only 2 functions + without basic levels, only two functions that process messages by their properties and call ReadByte, ReadInt32, ... depending on typeof property.
So, I have done this work, but I do not have enough time to test the performance, since I need to continue working on encryption. I have to choose one of two solutions to continue, and I have a sleep problem, will there be a smaller solution (reflection of one) to hit me in the face again :)