Skip to content

Server setup

DataSecs edited this page Feb 15, 2018 · 8 revisions

In case you might be wondering why I start out with the explanation of the server setup. In my opinion, first setting up the server makes more sense than starting off with the client setup.

Step One:

Take a look at the raw base, that is used to set up the server:

HydraServer server = new Server.Builder("localhost", 8888, new SampleProtocol())
                .bossThreads(2)
                .workerThreads(4)
                .option(StandardSocketOptions.TCP_NODELAY, true)
                .build();

The first thing one might notice is the usage of the builder-pattern (method-chaining). It is supposed to make the setup easier, as it allows the user to change configurations of the server with ease at any time. The builder returns an instance of the class HydraServer. One probably would expect the builder to return an instance of type Server, but for convenience and "builder-pattern reasons" the HydraServer class concludes the most important methods the user needs. The HydraServer gives you all the methods you need to work with the server. We are going to see more of that later. The constructor of the nested Builder class in the Server class takes three parameters. They are not part of the builder-pattern methods because they are obligatory. The first and second parameter are obviously the local address and the port number the server is supposed to be bind to. The third parameter is a bit more interesting. Let's say for now that the protocol handles the packets that are send between the communicating opponents. So you are going to be responsible of registering your packets in the protocol. In case you would like to skip to the part of the wiki where the protocol is elaborated: The protocol.

Step Two:

In step two we will add some configuration to the server. The provided example shows 3 options that are already set. The first one of them is the amount of worker threads. The standard is set to two, in this case it is set to four. The boss threads are set to two, the standard amount is set to one. The next configuration are the channel options. Now there are two kinds of channel options. The normal options for the channel of the server but also the child options for the connections that are created when clients connect. In this example there is just one option for the server channel set. For more information take a look at the Server class. In this class are all the configurations explained using Javadocs.

Step Three:

Step three is going to be the session listener. The session listener can be added via a method of the Builder. The methods name is 'addListener()'. This method is supposed to take an instance of the HydraSessionListener interface. Let's take a look at an example:

.addListener(new HydraSessionListener() {
                 @Override
                 public void onConnected(Session session) {
                     System.out.println("Client connected!");
                 }

                 @Override
                 public void onDisconnected(Session session) {
                     System.out.println("Client disconnected!");
                 }
             })

The session listener interface includes two methods. They get triggered by connect/disconnect events. The parameter of the interface method provides the session which was created for the connection. It allows you to e.g. send packets. We are going to dig deeper into Sessions later.

With these three steps you should be good to go. You might want to check out the other methods the HydraServer class has to offer.