be.yildizgames.module.network.netty.NettyChannelInitializer.java Source code

Java tutorial

Introduction

Here is the source code for be.yildizgames.module.network.netty.NettyChannelInitializer.java

Source

/*
 * This file is part of the Yildiz-Engine project, licenced under the MIT License  (MIT)
 *
 *  Copyright (c) 2018 Grgory Van den Borre
 *
 *  More infos available: https://www.yildiz-games.be
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 *  documentation files (the "Software"), to deal in the Software without restriction, including without
 *  limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *  of the Software, and to permit persons to whom the Software is furnished to do so,
 *  subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all copies or substantial
 *  portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 *  OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  SOFTWARE.
 *
 */

package be.yildizgames.module.network.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.CharsetUtil;

/**
 * Pipeline factory to build a pipeline to use for netty transfer, every time a channel is initialized, the pipeline is
 * filled with the handler provided by this class.
 *
 * @author Grgory Van den Borre
 */
public final class NettyChannelInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * Create additional handler every time a channel is initialized.
     */
    private final HandlerFactory factory;

    /**
     * Create a new instance and register the handlers.
     *
     * @param factory Create additional handler every time a channel is initialized.
     */
    //@Requires codec != null
    //@Requires factory != null
    public NettyChannelInitializer(final HandlerFactory factory) {
        super();
        this.factory = factory;
    }

    @Override
    protected void initChannel(final SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        switch (factory.getCodec()) {
        case STRING:
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            break;
        case HTTP:
            pipeline.addLast(new HttpServerCodec());
            pipeline.addLast(new HttpObjectAggregator(65536));
            pipeline.addLast(new ChunkedWriteHandler());
            break;
        case WEBSOCKET:
            if (this.factory.isServer()) {
                pipeline.addLast(new HttpServerCodec());
                pipeline.addLast(new HttpObjectAggregator(65536));
                pipeline.addLast(new WebSocketServerProtocolHandler("/websocket"));
            } else {
                pipeline.addLast(new HttpClientCodec());
                pipeline.addLast(new HttpObjectAggregator(8192));
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown codec: " + factory.getCodec());
        }
        pipeline.addLast("handler", this.factory.create());
    }
}