Java tutorial
/* * This file is part of Flow Network, licensed under the MIT License (MIT). * * Copyright (c) 2013 Flow Powered <https://flowpowered.com/> * * 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 com.flowpowered.network; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import java.net.SocketAddress; /** * This class defines an easy, general way to start a server. It is recommended that any server use or extend this class. */ public abstract class NetworkServer implements ConnectionManager { /** * The {@link ServerBootstrap} used to initialize Netty. */ private final ServerBootstrap bootstrap = new ServerBootstrap(); private final EventLoopGroup bossGroup = new NioEventLoopGroup(); private final EventLoopGroup workerGroup = new NioEventLoopGroup(); public NetworkServer() { bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new BasicChannelInitializer(this)).childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_KEEPALIVE, true); } public ChannelFuture bind(final SocketAddress address) { return bootstrap.bind(address).addListener(f -> { if (f.isSuccess()) { onBindSuccess(address); } else { onBindFailure(address, f.cause()); } }); } /** * Called when a bind is successfully made. * @param address The address we are now bound too. */ public void onBindSuccess(SocketAddress address) { } /** * Called when a bind fails. * @param address The address we attempted to bind too. * @param t The cause of why the binding failed. This can be null. */ public void onBindFailure(SocketAddress address, Throwable t) { } @Override public void shutdown() { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }