Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. public class Core extends JavaPlugin {
  2.  
  3. private ExecutorService executorService = Executors.newCachedThreadPool();
  4. private boolean EPOLL = Epoll.isAvailable();
  5. private EventLoopGroup eventLoopGroup;
  6.  
  7. private static Core instance;
  8. private Channel channel;
  9.  
  10. @Override
  11. public void onEnable() {
  12. eventLoopGroup = EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup();
  13. try {
  14. newConnection();
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19.  
  20. @Override
  21. public void onDisable() {
  22. eventLoopGroup.shutdownGracefully();
  23. }
  24.  
  25. private void newConnection() throws InterruptedException {
  26. PacketHandler.registerPacket(PacketOutPing.class, PacketType.OUT);
  27. PacketHandler.registerPacket(PacketInPing.class, PacketType.IN);
  28.  
  29. EventLoopGroup eventLoopGroup = EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup();
  30. try {
  31. channel = new Bootstrap()
  32. .group(eventLoopGroup)
  33. .channel(EPOLL ? EpollSocketChannel.class : NioSocketChannel.class)
  34. .handler(new ChannelInitializer<Channel>() {
  35. @Override
  36. protected void initChannel(Channel channel) throws Exception {
  37. channel.pipeline()
  38. .addFirst(new PacketEncoder())
  39. .addLast(new PacketDecoder())
  40. .addLast(new NetworkHandler());
  41. }
  42. })
  43. .connect("127.0.0.1", 8000).sync().channel();
  44. } finally {
  45. eventLoopGroup.shutdownGracefully();
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement