Simple & Lightweight Netty packet library + event system

Overview

Minimalistic Netty-Packet library

  • Create packets with ease
  • Bind events to packets

Example Packet:

public class TestPacket extends Packet {

    private UUID uuid;

    public TestPacket() {
    }

    @Override
    public void read(PacketBuffer packetBuffer) {
        uuid = packetBuffer.readUUID();
    }

    @Override
    public void write(PacketBuffer packetBuffer) {
        packetBuffer.writeUUID(uuid);
    }

    public UUID getUuid() {
        return uuid;
    }

    public TestPacket setUuid(UUID uuid) {
        this.uuid = uuid;
        return this;
    }
}

Example Server:

public class NettyTestServer extends ChannelInitializer<Channel> {

    private final ServerBootstrap bootstrap;
    private final IPacketRegistry packetRegistry;

    private EventLoopGroup parentGroup = new NioEventLoopGroup();
    private EventLoopGroup workerGroup = new NioEventLoopGroup();

    private Channel connectedChannel;

    public NettyTestServer(IPacketRegistry packetRegistry, Consumer<Future<? super Void>> doneCallback) {
        this.packetRegistry = packetRegistry;
        this.bootstrap = new ServerBootstrap()
                .option(ChannelOption.AUTO_READ, true)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .group(parentGroup, workerGroup)
                .childHandler(this)
                .channel(NioServerSocketChannel.class);

        try {
            this.bootstrap.bind(new InetSocketAddress("127.0.0.1", 1234))
                    .awaitUninterruptibly().sync().addListener(doneCallback::accept);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        channel.pipeline()
                .addLast(new PacketDecoder(packetRegistry), new PacketEncoder(packetRegistry));
        this.connectedChannel = channel;
        this.connectedChannel.writeAndFlush(new TestPacket().setUuid(UUID.randomUUID()));
    }

    public void shutdown() {
        try {
            parentGroup.shutdownGracefully().get();
            workerGroup.shutdownGracefully().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

}

Example Client:

public class NettyTestClient extends ChannelInitializer<Channel> {

    private final Bootstrap bootstrap;
    private final IPacketRegistry packetRegistry;
    private final EventRegistry eventRegistry;

    private final EventLoopGroup workerGroup = new NioEventLoopGroup();

    public NettyTestClient(IPacketRegistry packetRegistry, Consumer<Future<? super Void>> doneCallback, EventRegistry eventRegistry) {
        this.packetRegistry = packetRegistry;
        this.eventRegistry = eventRegistry;
        this.bootstrap = new Bootstrap()
                .option(ChannelOption.AUTO_READ, true)
                .group(workerGroup)
                .handler(this)
                .channel(NioSocketChannel.class);

        try {
            this.bootstrap.connect(new InetSocketAddress("127.0.0.1", 1234))
                    .awaitUninterruptibly().sync().addListener(doneCallback::accept);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        // Add the PacketChannelInboundHandler if you want to use the event functionality
        channel.pipeline()
                .addLast(new PacketDecoder(packetRegistry), new PacketEncoder(packetRegistry), new PacketChannelInboundHandler(eventRegistry));
    }

    public void shutdown() {
        try {
            workerGroup.shutdownGracefully().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

}

Example Packet and Event Registration

// Instantiate the required registries
EventRegistry eventRegistry = new EventRegistry();
IPacketRegistry registry = new SimplePacketRegistry();

// Register a packet with the id 1
registry.registerPacket(1, TestPacket.class);

// Register a PacketSubscriber for the registered packet
// Normally this would have been made in an external class to ensure a better readability
eventRegistry.registerEvents(new Object() {
    // The method signature of a PacketSubscriber must contain a valid packet and may contain the ChannelHandlerContext (optional)
    @PacketSubscriber
    public void onPacketReceive(TestPacket packet, ChannelHandlerContext ctx) {
        System.out.println("Received " + packet.getUuid().toString() + " from " + ctx.channel().remoteAddress().toString());
    }
});
You might also like...

BAIN Social is a Fully Decentralized Server/client system that utilizes Concepts pioneered by I2P, ToR, and PGP to create a system which bypasses singular hosts for data while keeping that data secure.

SYNOPSIS ---------------------------------------------------------------------------------------------------- Welcome to B.A.I.N - Barren's A.I. Natio

Jan 11, 2022

Apache Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation

Apache Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation

Apache Thrift Introduction Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation. Thrift provides clean a

Jan 4, 2023

Chaos engineering tool for simulating real-world distributed system failures

Chaos engineering tool for simulating real-world distributed system failures

Proxy for simulating real-world distributed system failures to improve resilience in your applications. Introduction Muxy is a proxy that mucks with y

Dec 25, 2022

Nzyme is a free and open next-generation WiFi defense system.

Nzyme is a free and open next-generation WiFi defense system.

Nzyme is a free and open next-generation WiFi defense system.

Jan 1, 2023

A simple multiplayer game

A simple multiplayer game

PixelExplorer Versions v0.0.1 : Play nothing together :) - 17/May/2021 So far we just have a basic echo server. How we did this was We build a new pro

Jun 14, 2021

A simple minecraft mod for 1.12.2 which logs sent and received packets.

Packet-Logger A simple minecraft mod for 1.12.2 which logs sent and received packets. Usage You must have Forge 1.12.2 installed. Download the jar fro

Dec 2, 2022

A simple, fast integration of WebSocket spring-stater

websocket-spring-boot-starter readme 介绍 一个简单,快速,低配的spring-boot-starter,是对spring-boot-starter-websocket的扩展与二次封装,简化了springboot应用对websocket的操作 特点 简单,低配 支

Dec 24, 2021

A simple proxy software made for Telegram. Works well for areas that have MTProxy or Socks5 blocked.

A simple proxy software made for Telegram. Works well for areas that have MTProxy or Socks5 blocked.

Dec 19, 2022
Comments
  • Bump netty-codec from 4.1.65.Final to 4.1.68.Final

    Bump netty-codec from 4.1.65.Final to 4.1.68.Final

    Bumps netty-codec from 4.1.65.Final to 4.1.68.Final.

    Commits
    • 7d34282 [maven-release-plugin] prepare release netty-4.1.68.Final
    • a0c9b2f Fix netty-tcnative* entries in bom
    • 6da4956 Merge pull request from GHSA-9vjp-v76f-g363
    • 41d3d61 Merge pull request from GHSA-grg4-wf29-r9vv
    • deb0489 Respect jdk.tls.namedGroups when using native SSL implementation (#11660)
    • f2de2bb Add support for mac m1 (#11666)
    • a53eb80 Throw exceptions when rule violating UDS paths been passed in. (#11663)
    • a329857 Updated "CipherSuitesConverter" to make it public. (#11656)
    • 21fb48e Deprecate UnaryPromiseNotifier (#11653)
    • 37c03cc Include number of maximum active streams in exception message (#11644)
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 1
Releases(1.3.0)
Owner
Pierre Maurice Schwang
18y/o Java Developer @gommehdnet
Pierre Maurice Schwang
A Linux packet crafting tool.

Pig Pig (which can be understood as Packet intruder generator) is a Linux packet crafting tool. You can use Pig to test your IDS/IPS among other stuff

Rafael Santiago 431 Dec 24, 2022
TCP/IP packet demultiplexer. Download from:

TCPFLOW 1.5.0 Downloads directory: http://digitalcorpora.org/downloads/tcpflow/ Installation Most common GNU/Linux distributions ship tcpflow in their

Simson L. Garfinkel 1.5k Jan 4, 2023
A packet sniffer for Realm of the Mad God in Java

A packet sniffer for Realm of the Mad God in Java. Most backend code is writen by Cortex, huge tanks to him for all the help. Inspired by work done by

Xcom 22 Dec 23, 2022
An netty based asynchronous socket library for benchion java applications

Benchion Sockets Library An netty based asynchronous socket library for benchion java applications ?? Documents ?? Report Bug · Request Feature Conten

Fitchle 3 Dec 25, 2022
LINE 4.1k Dec 31, 2022
IoT Platform, Device management, data collection, processing and visualization, multi protocol, rule engine, netty mqtt client

GIoT GIoT: GIoT是一个开源的IoT平台,支持设备管理、物模型,产品、设备管理、规则引擎、多种存储、多sink、多协议(http、mqtt、tcp,自定义协议)、多租户管理等等,提供插件化开发 Documentation Quick Start Module -> giot-starte

gerry 34 Sep 13, 2022
Experimental Netty-based Java 16 application/web framework

Experimental Netty-based application/web framework. An example application can be seen here. Should I use this? Probably not! It's still incredibly ea

amy null 8 Feb 17, 2022
Nifty is an implementation of Thrift clients and servers on Netty

his project is archived and no longer maintained. At the time of archiving, open issues and pull requests were clo

Meta Archive 902 Sep 9, 2022
A Java event based WebSocket and HTTP server

Webbit - A Java event based WebSocket and HTTP server Getting it Prebuilt JARs are available from the central Maven repository or the Sonatype Maven r

null 808 Dec 23, 2022
Microhttp - a fast, scalable, event-driven, self-contained Java web server

Microhttp is a fast, scalable, event-driven, self-contained Java web server that is small enough for a programmer to understand and reason about.

Elliot Barlas 450 Dec 23, 2022