org.tiger.netty.rpc.all.server.NettyServer.java Source code

Java tutorial

Introduction

Here is the source code for org.tiger.netty.rpc.all.server.NettyServer.java

Source

/*
 * Copyright 2013-2018 Lilinfeng.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tiger.netty.rpc.all.server;

import org.tiger.netty.rpc.all.NettyConstant;
import org.tiger.netty.rpc.all.codec.NettyMessageDecoder;
import org.tiger.netty.rpc.all.codec.NettyMessageEncoder;

import java.io.IOException;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * @author Lilinfeng
 * @version 1.0
 * @date 2014315
 */
public class NettyServer {

    public void bind() throws Exception {
        //??NIO
        //????EventLoopGroup
        //NioEventLoopGroupReactor
        //???boss?Reactor?
        //???worker??IOReactor?
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        //Netty??
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                //Channel????????
                .channel(NioServerSocketChannel.class)
                //TCP?
                //backlog???
                //??TCP??
                //??listen?syn(connect)??
                //????syn???(?synack)
                // ????????
                //accept???????
                //backlog5?Web??lighttpd128*8
                //???syn??
                //Nettybacklog100???
                .option(ChannelOption.SO_BACKLOG, 100)
                //?HandlerHandler???HandlerNioServerSocketChannelChannelPipelineHandler
                //HandlerSocketChannelChannelPipelineHandler
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws IOException {
                        ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4));
                        ch.pipeline().addLast(new NettyMessageEncoder());
                        ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
                        ch.pipeline().addLast(new LoginAuthRespHandler());
                        ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
                    }
                });

        // ???
        ChannelFuture future = b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync();
        System.out.println("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
        future.channel().closeFuture().sync();
    }

    public static void main(String[] args) throws Exception {
        new NettyServer().bind();
    }
}