Question

I am developing a multiplayer online game. I have the following issue:

When the user breaks his/her connection with the server, he needs to reconnect. At the first connection, during registration, the registration module produces a special ResponseDispatcher which holds the reference to the connection Channel. But if the user logs out, this Channel becomes invalid. Even though I can detect the problem and clean up resources, I have to store the reference to the registration module and Connection module to the game module, in order to renew the Channel when the user authorises and reconnects again. This creates a lot of interdependencies among modules and it gets really hard to maintain.

What I need is something like an HttpSession in Servlet Container, so that I can get the references to my channel and session resources from all modules of my server.

How is HttpSession implemented in Servlet? Is it a global hashmap storing all JSESSIONID, from which the container determines which attribute map to return? If it is a global sysmbol table, will it hit the performance (even though the time is O(1) for hashMap, there might be session modifications so it probably has to be synchronized)?

PS. Maybe some recommendations of design patterns for this case would also do.

Was it helpful?

Solution

I would recommend trying Shiro

Shiro can handle Session Management outside of a servlet container.

You may want to back Shiro with EhCache to provide proper caching and, if required, session persistence (and load balancing, etc...)

OTHER TIPS

Have a look at the Facade Pattern

enter image description here

I'm not exactly sure what the question is. Why do you "have to store the reference to the registration module and connection module"?

Anyway there are two immediately sensible solutions to your problem.

1) Make the Registration module and Connection module Singletons. Whether this is useful depends entirely on what functionality these modules provide.

2) Make the Registration module and Connection module Persistent Entities, save them to a DataStore, complete with necessary references, and retrieve and rebuild them on reconnect.

I'm not quite sure why rolling your own session implementation would be something you want to do, what happens when the session times out?

Your design seems somewhat flawed. The player should not "still be online" if his connection goes down (this is a contradiction in terms, you cannot by definition be online if you are not connected to a network), regardless of whether this was intentional or not (you cannot know whether it is intentional or not), you have no idea whether the player is able to reconnect within a timely fashion or not, so you should assume the worst. More importantly, from a purely design aspect, getting killed by the game, because your internet connection is rubbish, is probably not something you want to deal with. If persisting data is such a costly affair you should reexamine your datastore options. Also, what happens in the scenario where the server crashes while he's offline?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top