Skip to content

Client Handling

Thorben Kuck edited this page Sep 19, 2018 · 3 revisions

Clients, that connect

Whenever a ClientStart connects via

clientStart.launch();

The Server handles this Socket and creates an serverside representation of that Client. This is represented by the
com.github.thorbenkuck.netcom2.network.shared.clients.Client
interface.

An Client holds multiple informations about the just connected ClientStart, like the (possible) multiple Connections, the above Connections shared Session and so on.

You can interlock this mechanism after the Client is created by simply inserting an ClientConnectedHandler, which is an FunctionalInterface to handle said newly created Client. Let's say, we wanted to create an logging-handler, that loggs the IP of a client, whenever an Client connects. We would create an ClientConnectedHandler which looks like this:

public LogClients implements ClientConnectedHandler {
    private Logging logging = Logging.unified();

    @Override
    public void handle(Client client) {
	client.getConnection(DefaultConnection.class).ifPresent(connection -> logging.debug(connection.getFormattedAddress()));
    }
}

And than add it to the ServerStart like this:

ServerStart serverStart = ...
serverStart.addClientConnectedHandler(new LogClients());

This call will return the DefaultConnection, which is always present and log the formatted address of that Client. You could also write this as an Lambda like this:

ServerStart serverStart = ...
Logging logging = Logging.unified();
serverStart.addClientConnectedHandler(client -> logging.debug(client));

Which, in this example, will log the complete Client.

You can not do the same at the client-side. If you successfully pass the .launch method, you are connected.


Note

~~ With this interlocking mechanism, you have the power to do something to every client that connects. Not however, that not every client will stay. This results in some complicated problems, when working with multiple Connections. And please, do not create a new Connection inside of an ClientConnectedHandler.~~

You will inforce an infinite recursion. Because before the .synchronize is finished, the ClientStart will create an new Client and trigger the same ClientConnectedHandler again and again. If you work with multiple Connections, keep this in mind and do stuff whenever an Message-Object arrives, not whenever an Client connects.

As of version 2.0, this is no longer true. Because of the new Handshake, the ClientConnectedHandler is not called for every Connection, but once after the default Connection is called. This shreds the backwards compatibility, but enhances the Framework by itself.

Clients, that Disconnect

You might be interested in detecting the disconnection of an Client at the Server-side or of the Server at the Client-side. Maybe you want to delete an custom User-Object, corresponding to an Client once it connects. For this problem, the DisconnectedHandler exists. This class is a callback object, which is triggered if the ClientStart is shutdown, the ServerStart is shutdown or the Connection between the Server and the Client break. At the Server-Side you would implement it, using a ClientConnectedHandler:

ServerStart serverStart = ...
serverStart.addClientConnectedHandler(client -> {
    // do something with the Client once it connects.
    client.addDisconnectedHandler(client -> {
        // do something with the Client, once it disconnects.
    });
});

Whilst at the client-side, you would simply state:

ClientStart clientStart = ...
clientStart.addDisconnectedHandler(client -> {
    // do something with the Client, once it disconnects.
});

You could also use the ClientList at the ServerSide, but using the ClientConnectedHandler is the cleanest way of ensuring, that every client is handled exactly as you want it to, once it disconnects.

Clone this wiki locally