Excellia - Networking Part 1: Packet/Event Encoding & Decoding
by, 4 August 2011 at 11:37 AM (6854 Views)
Today I finally found the end of the tunnel of networking. Some people might already know that the entire Excellia networking system has been my biggest bother so far in the programming scene. But I can now finally say that it's approaching it's end. I think I've redone it about 5-7 times, but as I mentioned earlier, I keep improving myself until I'm satisfied.
I'll start by explaining to you how I set up the codec for the login server as well as the game server. I haven't created a file server yet, though I've kept it in mind while writing the game codec (OnDemand updating goes through the same port as the actual game). With that I mean that the file server can be integrated without a problem, once I've created it. But let's proceed with the actual networking.
Excellia uses the Netty framework to approach networking. The reason I've chosen to work with Netty is because I've heard good stories about it, and 90% of all comparisons with other frameworks prove that Netty is the framework with the best performance. It is also very well documented hence comfortable to work with. It also makes writing a custom codec very easy, which is what I definitely need here.
"The Netty project is an effort to provide an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance & high scalability protocol servers & clients."
More information about Netty can be found here:
Netty - the Java NIO Client & Server Socket Framework
Now, let me describe you how I'm approaching packets, from decoding to handling and from sending to encoding. Sounds like an easy task right? I'll be honest with you, it could be. But the way I wanted it requires a more complex structure then a simple encoder and decoder implementation.
I've made a new project specifically for networking, the same as the Engine is used for, well, being an engine. The Excellia Network project is set up specifically to provide the tools and utilities required to go far further than the usual packet handling picture. The only dependency required to use the Excellia Network is the Excellia Engine, which is required to run the servers anyway.
It comes with several classes for decoding codec into logic, and the other way around. For example, there is a MessageHandler interface and MessageEvent class. The MessageHandler comes with methods that handle the actual MessageEvent. The MessageEvent class contains several volatile variables which are therefore changeable in the case of encoding or decoding.
I've also added some classes which already provide the necessary methods to encode or decode a message. The MessageEncoder and MessageDecoder classes, for example. They both implement MessageHandler, and process the message with their own encode(MessageHandlerContext, MessageEvent) and decode(MessageHandlerContext, MessageEvent) methods.
To clear it up for you, here's a table which shows you how the message encoding/decoding context works:
The stages coloured in blue is handled in Netty's thread pool. As you may notice, this differs from other servers like Hyperion. Building, encoding and decoding is completely done in Netty's thread pool. This means it is no longer of any effect to the main update cycle. I'm expecting that this gives quite a performance boost at higher player counts.
This is about it for part 1. I hope you've enjoyed reading this. I will most likely post part 2 tomorrow or the day after. Part 2 will provide more details on how the actual Event handling takes place.
For clarification: Event is a tag interface which basically represents every single happening in the server. From packets to tasks to commands. This allows the development of a wide range of highly extensible systems.
Thanks for reading, and if you have any questions feel free to ask.