Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public class DataBridgeNetwork extends ChannelInitializer<SocketChannel> {
  2.  
  3. private Main main;
  4. private ChannelFuture future;
  5. private NioEventLoopGroup acceptorThreads = new NioEventLoopGroup(2), workerThreads = new NioEventLoopGroup();
  6.  
  7. public DataBridgeNetwork(Main main){
  8. this.main = main;
  9.  
  10. try {
  11. ServerBootstrap serverBootstrap = new ServerBootstrap();
  12. serverBootstrap.group(acceptorThreads, workerThreads)
  13. .channel(NioServerSocketChannel.class)
  14. .childHandler(this)
  15. .option(ChannelOption.SO_BACKLOG, 5)
  16. .childOption(ChannelOption.SO_KEEPALIVE, true);
  17.  
  18. int port = (int) Config.SETTINGS.get("port");
  19. Main.log("Starting DataBridge on port: " + port);
  20. future = serverBootstrap.bind(port).sync();
  21. System.out.println(future.isSuccess());
  22. System.out.println(future.cause() == null);
  23. if (future.cause() != null){
  24. future.cause().printStackTrace();
  25. }
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. @Override
  32. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  33. super.exceptionCaught(ctx, cause);
  34. System.out.println("1234");
  35. }
  36.  
  37. @Override
  38. protected void initChannel(SocketChannel socketChannel) throws Exception {
  39. ChannelPipeline pipeline = socketChannel.pipeline();
  40. pipeline.addLast(new ByteArrayDecoder());
  41. pipeline.addLast(new ByteArrayEncoder());
  42. pipeline.addLast(new DataBridgeServer(main, socketChannel));
  43. }
  44.  
  45. @Override
  46. public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
  47. super.channelUnregistered(ctx);
  48. }
  49.  
  50. public void shutdown(){
  51. acceptorThreads.shutdownGracefully();
  52. workerThreads.shutdownGracefully();
  53. future.channel().close();
  54. if (future != null){
  55. try {
  56. future.channel().closeFuture().sync();
  57. } catch (InterruptedException e){
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62.  
  63. public class DataBridgeServer extends SimpleChannelInboundHandler<byte[]> {
  64.  
  65. private Main main;
  66. private String name;
  67. private SocketChannel channel;
  68. private ChannelHandlerContext context;
  69.  
  70. public DataBridgeServer(Main main, SocketChannel channel){
  71. super(true);
  72. this.main = main;
  73. this.channel = channel;
  74. }
  75.  
  76. public void sendPacket(PacketOut packet){
  77. if (context == null){
  78. throw new DataBridgeException("null channel context for server '" + name + "'");
  79. }
  80. context.writeAndFlush(packet.getBytes());
  81. }
  82.  
  83. public void disconnect() {
  84. context.channel().close();
  85. if (main.getServers().containsKey(name)){
  86. main.getServers().remove(name);
  87. }
  88. }
  89.  
  90. @Override
  91. protected void channelRead0(ChannelHandlerContext channelHandlerContext, byte[] data) throws Exception {
  92. PacketIn packet = Utils.getPacket(main, data[0]);
  93. packet.readData(data);
  94. if (packet instanceof PacketInLogin){
  95. PacketInLogin login = (PacketInLogin) packet;
  96. boolean auth = login.authenticate();
  97. Main.log("Authentication test for server '" + login.getServer() + "' has " + (auth ? "PASSED" : "FAILED"));
  98. sendPacket(new PacketOutAuthResult(main, auth));
  99. if (auth){
  100. this.name = login.getServer();
  101. main.getServers().put(name, this);
  102. } else {
  103. disconnect();
  104. }
  105. }
  106. }
  107.  
  108. @Override
  109. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  110. super.channelActive(ctx);
  111. this.context = ctx;
  112. }
  113.  
  114. public String getName() {
  115. return name;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement