The general question: how can I write a flexible, but not property heavy, messaging structure for sending TCP messages to clients and server?
Example:
I have a TCP Server for a game that sends some messages through TCP sockets to clients:
Chat messages, including chat text, chat room, and speaker.
Game messages, including game actions and events
Client messages, including clients connecting, logging in, and disconnecting
To achieve this, when one of these actions occurs within the program, an event handler is called generating some specific event type.
ChatEvent with ChatEventArgs
GameEvent with GameEventArgs
ClientEvent with ClientEventArgs
These all extend the EventArgs
class.
Now to my question. As things get more complex, I begin to cram more pieces of info into these EventArgs
. My GameEventArgs start as
GameEventArgs gameArgs = new GameEventArgs(GameAction.Start);
gameArgs.gameId = "gameId123";
gameArgs.turn = "thisPlayersTurn";
JSON json = GameEventArgs.ToJson(gameArgs);
SendMessageToClient(json);
From this you can gather that GameEventArgs has some public properties such as gameId
and turn
. But a game is more than IDs and Turns. The client will need to receive game actions, which can be very complex. Suddenly the GameEventArgs needs more and more properties. This is fine, except, that only a few of these properties will be sent at a time. There could be 20-40 unused properties of GameEventArgs. Should I make more classes that inherit GameEventArgs, and only contain certain properties? I will be sending messages often, and I want to take the GC into consideration, as these messages are one time uses. (Should they then be IDisposable?).
Is there a better method to go about creating a messaging structure, where only a few of the defined properties of the message will be used at one time?