项目作者: PierreSchwang

项目描述 :
Simple & Lightweight Netty packet library + event system
高级语言: Java
项目地址: git://github.com/PierreSchwang/Netty-Packet.git
创建时间: 2021-05-31T16:56:58Z
项目社区:https://github.com/PierreSchwang/Netty-Packet

开源协议:MIT License

下载


Minimalistic Netty-Packet library

  • Create packets with ease
  • Bind events to packets

Example Packet:

  1. public class TestPacket extends Packet {
  2. private UUID uuid;
  3. public TestPacket() {
  4. }
  5. @Override
  6. public void read(PacketBuffer packetBuffer) {
  7. uuid = packetBuffer.readUUID();
  8. }
  9. @Override
  10. public void write(PacketBuffer packetBuffer) {
  11. packetBuffer.writeUUID(uuid);
  12. }
  13. public UUID getUuid() {
  14. return uuid;
  15. }
  16. public TestPacket setUuid(UUID uuid) {
  17. this.uuid = uuid;
  18. return this;
  19. }
  20. }

Example Server:

  1. public class NettyTestServer extends ChannelInitializer<Channel> {
  2. private final ServerBootstrap bootstrap;
  3. private final IPacketRegistry packetRegistry;
  4. private EventLoopGroup parentGroup = new NioEventLoopGroup();
  5. private EventLoopGroup workerGroup = new NioEventLoopGroup();
  6. private Channel connectedChannel;
  7. public NettyTestServer(IPacketRegistry packetRegistry, Consumer<Future<? super Void>> doneCallback) {
  8. this.packetRegistry = packetRegistry;
  9. this.bootstrap = new ServerBootstrap()
  10. .option(ChannelOption.AUTO_READ, true)
  11. .childOption(ChannelOption.SO_KEEPALIVE, true)
  12. .group(parentGroup, workerGroup)
  13. .childHandler(this)
  14. .channel(NioServerSocketChannel.class);
  15. try {
  16. this.bootstrap.bind(new InetSocketAddress("127.0.0.1", 1234))
  17. .awaitUninterruptibly().sync().addListener(doneCallback::accept);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. @Override
  23. protected void initChannel(Channel channel) throws Exception {
  24. channel.pipeline()
  25. .addLast(new PacketDecoder(packetRegistry), new PacketEncoder(packetRegistry));
  26. this.connectedChannel = channel;
  27. this.connectedChannel.writeAndFlush(new TestPacket().setUuid(UUID.randomUUID()));
  28. }
  29. public void shutdown() {
  30. try {
  31. parentGroup.shutdownGracefully().get();
  32. workerGroup.shutdownGracefully().get();
  33. } catch (InterruptedException | ExecutionException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }

Example Client:

  1. public class NettyTestClient extends ChannelInitializer<Channel> {
  2. private final Bootstrap bootstrap;
  3. private final IPacketRegistry packetRegistry;
  4. private final EventRegistry eventRegistry;
  5. private final EventLoopGroup workerGroup = new NioEventLoopGroup();
  6. public NettyTestClient(IPacketRegistry packetRegistry, Consumer<Future<? super Void>> doneCallback, EventRegistry eventRegistry) {
  7. this.packetRegistry = packetRegistry;
  8. this.eventRegistry = eventRegistry;
  9. this.bootstrap = new Bootstrap()
  10. .option(ChannelOption.AUTO_READ, true)
  11. .group(workerGroup)
  12. .handler(this)
  13. .channel(NioSocketChannel.class);
  14. try {
  15. this.bootstrap.connect(new InetSocketAddress("127.0.0.1", 1234))
  16. .awaitUninterruptibly().sync().addListener(doneCallback::accept);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. @Override
  22. protected void initChannel(Channel channel) throws Exception {
  23. // Add the PacketChannelInboundHandler if you want to use the event functionality
  24. channel.pipeline()
  25. .addLast(new PacketDecoder(packetRegistry), new PacketEncoder(packetRegistry), new PacketChannelInboundHandler(eventRegistry));
  26. }
  27. public void shutdown() {
  28. try {
  29. workerGroup.shutdownGracefully().get();
  30. } catch (InterruptedException | ExecutionException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

Example Packet and Event Registration

  1. // Instantiate the required registries
  2. EventRegistry eventRegistry = new EventRegistry();
  3. IPacketRegistry registry = new SimplePacketRegistry();
  4. // Register a packet with the id 1
  5. registry.registerPacket(1, TestPacket.class);
  6. // Register a PacketSubscriber for the registered packet
  7. // Normally this would have been made in an external class to ensure a better readability
  8. eventRegistry.registerEvents(new Object() {
  9. // The method signature of a PacketSubscriber must contain a valid packet and may contain the ChannelHandlerContext (optional)
  10. @PacketSubscriber
  11. public void onPacketReceive(TestPacket packet, ChannelHandlerContext ctx) {
  12. System.out.println("Received " + packet.getUuid().toString() + " from " + ctx.channel().remoteAddress().toString());
  13. }
  14. });