Skip to content

NitrinNET/nitrin-network

Repository files navigation

nitrin-network

This is our current network code which we use to power our cloud and server. It uses its own packet implementation.
We use the framework netty for good performance and simplicity

⚠️ This code is experimental ⚠️


How do I use it?

For outgoing packets you need to implement the WriteablePacket and for incoming packets you need to implement Packet. Only a Packet needs to be registered in order to receive the packet. We use the already provided ByteBuf from netty to write and read data.

class PingPacket: WriteablePacket(PacketRegistry.DEFAULT + 0), Packet {

    override fun write(buffer: ByteBuf) {}
}

When a packet is received the system will try to find a PacketInHandler.

class PingPacketInHandler: PacketInHandler<PingPacket> {

    override fun handle(context: NetworkContext, packet: PingPacket) {
        val (component: NetworkComponent, _: Channel) = context

        component.sendPacket(PongPacket())
    }
}

Packets and PacketInHandler must be registered in the PacketRegistry using the code below. WriteablePackets don't need to be registered!

PacketRegistry.register(PingPacket::class, PingPacketInHandler())

PacketRegistry.register(PacketRegistry.DEFAULT + 0) { PingPacket() }

A component gets created when a channel connects. It is created by using the ComponentFactory. You can either define your own ComponentFactory or use our build-in factory. It will create a component with no information which just handles the packet sending

val factory = object : ComponentFactory {
    override fun create(channel: Channel): NetworkComponent {
        return DefaultNetworkComponent(channel)
    }
}

How does exceptions or a disconnect get handled?

The ComponentListener listens to disconnects and exceptions, either way the channel is closed before the event is triggered

val handler = object : ComponentListener {
    override fun disconnect(context: NetworkContext) {
        TODO("Not yet implemented")
    }

    override fun exception(context: NetworkContext) {
        TODO("Not yet implemented")
    }
}

You will need a SocketAddress and a ComponentListener in order to create a server, the ComponentFactory is optional.

val server = Server(factory, handler)

server.start(InetSocketAddress(port))

How do I create a client?

You will need a SocketAddress and a ComponentListener in order to create a client.

val client = Client(handler)

client.connect(InetSocketAddress(port))



You can find out about our project at https://www.nitrin.net/ or e-mail us your questions [email protected]

Releases

No releases published

Packages

No packages published

Languages