Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DataBridgeNetwork extends ChannelInitializer<SocketChannel> {
- private Main main;
- private ChannelFuture future;
- private NioEventLoopGroup acceptorThreads = new NioEventLoopGroup(2), workerThreads = new NioEventLoopGroup();
- public DataBridgeNetwork(Main main){
- this.main = main;
- try {
- ServerBootstrap serverBootstrap = new ServerBootstrap();
- serverBootstrap.group(acceptorThreads, workerThreads)
- .channel(NioServerSocketChannel.class)
- .childHandler(this)
- .option(ChannelOption.SO_BACKLOG, 5)
- .childOption(ChannelOption.SO_KEEPALIVE, true);
- int port = (int) Config.SETTINGS.get("port");
- Main.log("Starting DataBridge on port: " + port);
- future = serverBootstrap.bind(port).sync();
- System.out.println(future.isSuccess());
- System.out.println(future.cause() == null);
- if (future.cause() != null){
- future.cause().printStackTrace();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- super.exceptionCaught(ctx, cause);
- System.out.println("1234");
- }
- @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- ChannelPipeline pipeline = socketChannel.pipeline();
- pipeline.addLast(new ByteArrayDecoder());
- pipeline.addLast(new ByteArrayEncoder());
- pipeline.addLast(new DataBridgeServer(main, socketChannel));
- }
- @Override
- public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
- super.channelUnregistered(ctx);
- }
- public void shutdown(){
- acceptorThreads.shutdownGracefully();
- workerThreads.shutdownGracefully();
- future.channel().close();
- if (future != null){
- try {
- future.channel().closeFuture().sync();
- } catch (InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- public class DataBridgeServer extends SimpleChannelInboundHandler<byte[]> {
- private Main main;
- private String name;
- private SocketChannel channel;
- private ChannelHandlerContext context;
- public DataBridgeServer(Main main, SocketChannel channel){
- super(true);
- this.main = main;
- this.channel = channel;
- }
- public void sendPacket(PacketOut packet){
- if (context == null){
- throw new DataBridgeException("null channel context for server '" + name + "'");
- }
- context.writeAndFlush(packet.getBytes());
- }
- public void disconnect() {
- context.channel().close();
- if (main.getServers().containsKey(name)){
- main.getServers().remove(name);
- }
- }
- @Override
- protected void channelRead0(ChannelHandlerContext channelHandlerContext, byte[] data) throws Exception {
- PacketIn packet = Utils.getPacket(main, data[0]);
- packet.readData(data);
- if (packet instanceof PacketInLogin){
- PacketInLogin login = (PacketInLogin) packet;
- boolean auth = login.authenticate();
- Main.log("Authentication test for server '" + login.getServer() + "' has " + (auth ? "PASSED" : "FAILED"));
- sendPacket(new PacketOutAuthResult(main, auth));
- if (auth){
- this.name = login.getServer();
- main.getServers().put(name, this);
- } else {
- disconnect();
- }
- }
- }
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- super.channelActive(ctx);
- this.context = ctx;
- }
- public String getName() {
- return name;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement