Search in sources :

Example 1 with Client

use of com.rs.entity.player.Client in project runesource by PureCS.

the class Server method accept.

/**
     * Accepts any incoming connections.
     *
     * @throws IOException
     */
private void accept() throws IOException {
    SocketChannel socket;
    /*
         * Here we use a for loop so that we can accept multiple clients per
		 * tick for lower latency. We limit the amount of clients that we
		 * accept per tick to better combat potential denial of service type
		 * attacks.
		 */
    for (int i = 0; i < 10; i++) {
        socket = serverChannel.accept();
        if (socket == null) {
            // No more connections to accept (as this one was invalid).
            break;
        }
        // Register the connection
        HostGateway.enter(socket.socket().getInetAddress().getHostAddress());
        // Set up the new connection.
        socket.configureBlocking(false);
        SelectionKey key = socket.register(selector, SelectionKey.OP_READ);
        Client client = new Player(key);
        System.out.println("Accepted " + client + ".");
        getClientMap().put(key, client);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Player(com.rs.entity.player.Player) Client(com.rs.entity.player.Client)

Aggregations

Client (com.rs.entity.player.Client)1 Player (com.rs.entity.player.Player)1 SelectionKey (java.nio.channels.SelectionKey)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1