org.enderstone.server.packet.ConnectionInitializer.java Source code

Java tutorial

Introduction

Here is the source code for org.enderstone.server.packet.ConnectionInitializer.java

Source

/* 
 * Enderstone
 * Copyright (C) 2014 Sander Gielisse and Fernando van Loenhout
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.enderstone.server.packet;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.net.InetAddress;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.enderstone.server.packet.NetworkManager;

public class ConnectionInitializer extends ChannelInitializer<SocketChannel> {

    private final Map<InetAddress, Long> connectionThrottlingMap = new LinkedHashMap<InetAddress, Long>() {

        @Override
        protected boolean removeEldestEntry(Map.Entry<InetAddress, Long> eldest) {
            return this.size() > 200;
        }
    };

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline line = channel.pipeline();
        NetworkManager manager = new NetworkManager(connectionThrottlingMap, 5000); // TODO Add config option for this
        line.addLast("packet_r_timeout", new ReadTimeoutHandler(30, TimeUnit.SECONDS));
        line.addLast("packet_rw_converter", manager.createCodex());
        line.addLast("packet_rw_reader", manager);
    }
}