-
Notifications
You must be signed in to change notification settings - Fork 98
Description
Right now all the Messages handled by a Channel, both to Handle packets coming from the Client-Side and also to Send Packets to the Server-Side are being handled by two files:
Network/ChannelPacketHandlerNetwork/ChannelPacketSender
Those two files are already enormously big and already might impact the maintainability of the Melia Core.
The suggestion is to first move the part of the code where messages are being handled and written to a different part of the Emulator. (It is important to mention to leave all-kind of Business-Logic and Application-Logic outside the scope of composing the bodies and reading the bodies of packets.
This means that the refactored methods for handling and sending packets (The part that composes the bodies of the messages and the part that reads them) should be isolated.
Sender-Reader-Handler Approach
- A reader should only contain pieces of code to read the Packet and return the data that was read as a return value of the method
- A Sender should only contain pieces of code to write the bodies of the packets and return the Buffer ready to be sent.
- A handler should invoke a Reader function when it wants to read a packet and Invoke a Sender function when it wants to send something.
An example of this strategy would be:
private readonly CharacterSessionReader Reader = new CharacterSessionReader();
private readonly CharacterSessionSender Sender = new CharacterSessionSender();
[PacketHandler(Op.CZ_LOGOUT)]
public void CZ_LOGOUT(ChannelConnection conn, Packet packet)
{
var { extra, unkByte } = Reader.CZ_LOGOUT();
Log.Info("User '{0}' is logging out.", conn.Account.Name);
Send(Sender.ZC_SAVE_INFO(conn));
Send(Sender.ZC_LOGOUT_OK(conn));
}Domain-Driven-Design Folder Structure (Code Separation)
Regarding the DDD part of the refactoring, it would mean that the Readers and Senders and Handlers would be related to their Functionality based in the Game, rather than what they are (Senders/Readers).
Imagine the following folder structure:
- Communication/Messages
- Character/
- Session/
- Reader.cs
- Handler.cs
- Sender.cs
- Inventory
- Reader.cs
- Handler.cs
- Sender.cs
...
...
This is an example of a possible Folder structure. The naming varies based on how we would separate and group the Messages.
An example of this strategy would be:
- Messages related to the Character should reside in the bigger Group "Character"
- Messages specific to the Character to manage its Inventory should reside inside "Character/Inventory"...
- Messages related to Managing the state/effects of Monsters should reside in the bigger Group "Monsters"
- Messages related to the Character to use Skills should reside inside "Character/Skills"
This is just an illustrative example.