Sorry for the bad title, but I wasn't sure on how to name it. I need to find a way to handle the following:
I have a class
public class Event
{
public ushort Id;
}
This class consumer of my framework have to inherit their custom event of. So let's say if they want to create the following:
public class OnConnect : Event
{
...
}
The ID is used to identify in a server/client enviroment, when the serialized byte[] data is transfered, to which event you have to deserialize the byte data. So basically in the byte[] data, the first 2 bytes contain the ushort id. Then I can read this, get the associacted type and call the correct deserializer.
But as the time showed, one potential issue or wrong usage, is, that the consumer forgets to assign a unique ushort id in their custom events or use the same id for multiple events as they don't check for that. Teaching them to use an enum as an example, tended to cause some additional problems. Anyways even if it seem easy to manage, our reports claim that this is a common issue.
So I want now to automate this to remove this possible issue. But I have really hard times, finding a possible way to solve this, as I have the following structure:
- A server/client enviroment, where the process I choose, has to deliver the same result on both ends
- The events itself cannot be used to initialize this number as it isn't always the same order when event x is first used.
First I thought about dependency injection with an interface like IRequireId
or so and on my retrieve medthod like x.componentes.Where(c=>c is IRequireId)
I assign a unique id to the event and store it somewhere else and when the event is initialized I can get the id from there. The problem with this is, that I cannot ensure, that the x.components.Where is retrieving the components in the same order as on the other peers on the network. Thus different id's could be assigned.
So I think DI is off the road. Has someone else any idea, or an idea on how to make sure, that my retrievement method does save any order, so that it is the same on each peer on the network?
As of writing this I had the idea of maybe using a HashSet
and write my own GetHashcode
that does reproduce the same hashcode on each peer. And then iterating over the hashset and doe the DI on the IRequireId
interface. But how could I write a hashcode generation that is the same on each peer on the network? That would need and id again. Damn it.
Please help x) I'm a bit lost currently. tia Synergi
EDIT
I forgot to note, as someone already asked this, why not simply add the type as a string into the byte array?
As this is a network enviroment, we do care about the size of the byte array. And if we just want to send let's say 4 bytes of data in an event and the byte[] itself is 20 bytes long because of 16 bytes of a string containing the type, than this is inacceptable.
EDIT
Thx to the hint with the class name I'm now able to solve it.
- I use the class name to generate a hashcode
- I can then use this to ensure order in my get method
- And finally I can do DI with creating an ushort id
awesome thx for the help