Sorry this is more of a snippet but may be helpful to some users who are new to networking.
Whilst I'm learning networking myself, I made the most basic RS2 netty server. If you're new to networking, or you want to use netty 4.0 for your applications but aren't sure how to get started you can use this as a reference. It's quite simple.
Make sure to download the Netty 4.0 library
http://netty.io/downloads.html
You can also view my github page as well.
https://github.com/Rust94/Basic-Nett...I%20Server/src
Code:
import java.util.logging.Level;
import java.util.logging.Logger;
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;
/**
* The core class for an RS2 server
*
* @author Rust
*/
public class Server {
/**
* Logging system used to log messages for the server
*/
private static final Logger logger = Logger.getLogger(Server.class
.getName());
/**
* Main method for the server application
*
* @param args
* The command-line arguments
*/
public static void main(String[] args) {
try {
new Server().init();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Error while starting the server.", t);
}
}
/**
* Creating an empty constructor
*/
public Server() throws Exception {
logger.log(Level.INFO, "RS2 Server initializing...");
}
/**
* Initializes the Server Channel Handler
*
* @throws InterruptedException
*/
public void init() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServiceChannelInitializer(new ChannelHandler()))
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
logger.log(Level.INFO, "Binding to port " + NetworkConstants.PORT);
ChannelFuture f = bootstrap.bind(NetworkConstants.PORT).sync();
logger.log(Level.INFO, "Server Online and bound to port "
+ NetworkConstants.PORT);
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
ChannelHandler
Code:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
/**
* An extension of {@link SimpleChannelInboundHandler} which is useful in this case
* vs {@link ChannelInboundHandler} because {@link SimpleChannelInboundHandler} releases the objects
* right away. {@link ChannelInboundHandlerAdapter} does not.
*
* @author Rust
*/
public class ChannelHandler extends SimpleChannelInboundHandler<Message> {
@Override
protected void channelRead0(ChannelHandlerContext arg0, Message arg1)
throws Exception {
}
}
ServiceChannelInitializer
Code:
package core.net;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
/**
* A {@link ChannelInitializer} for the service pipeline
*
* @author Rust
*/
public class ServiceChannelInitializer extends
ChannelInitializer<SocketChannel> {
private final ChannelHandler handler;
private static final Logger logger = Logger.getLogger(ServiceChannelInitializer.class.getName());
public ServiceChannelInitializer(ChannelHandler handler) {
this.handler = handler;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("timeout", new ReadTimeoutHandler(10));
//ch.pipeline().addLast("encoder", new RS2Encoder());
//ch.pipeline().addLast("decoder", new RS2LoginProtocol());
pipeline.addLast("handler", handler);
logger.log(Level.INFO, "Connection recieved from " + ch.remoteAddress().getAddress());
}
}
An Interface PacketType
Code:
package core.net.message;
import core.net.ChannelHandler;
/**
* We can use an interface to have objects implement this so that any class that
* implements this interface will be able to pass through the {@link ChannelHandler}
* as a message.
* @author Rust
*/
public interface PacketType {
}
NetworkConstants
Code:
/**
* Class consisting of only network constants
*/
public class NetworkConstants {
public static final int PORT = 43594;
}