UxNetIO - new lightweight scalable network lib

Hey everyone, just wanted to release my networking library as used in my MMO as open source. I know there’s many of these out there like KyroNet but I found this much easier to use (and maybe others will too?)

Link: https://github.com/Davidx1337/UxNetIO

License: MIT (do whatever you want basically)

It is based on the Apache MINA API (design wise) but i’ve removed many layers of abstraction that MINA provides as well as providing a backwards-compatible API for old raw java.net.Socket usage. This boils down to a 12kb jar file while MINA is over 300kb!

Example HTTP server (very basic):


public static void main(String[] argv)
            throws Throwable {
        SocketAcceptor acceptor = new SocketAcceptor(80, 10);
        acceptor.bind(new Protocol() {
            ProtocolEncoder encoder = new ProtocolEncoder() {
                public void dispose() {

                }

                public void encode(IoSession session, Object message, EncoderOutput output) {
                    String str = (String) message;
                    str = "<div style=\"color:blue;\">" + str;
                    str += "</div>\n";
                    ByteBuffer bb = ByteBuffer.allocate(str.length());
                    bb.put(str.getBytes());
                    bb.flip();
                    output.write(bb);
                }
            };
            ProtocolDecoder decoder = new ProtocolDecoder() {
                public void dispose() {

                }

                public void decode(IoSession session, ByteBuffer message, DecoderOutput output) {
                    byte[] data = message.array();
                    output.write(new String(data));
                }
            };

            public ProtocolEncoder getEncoder() {
                return encoder;
            }

            public ProtocolDecoder getDecoder() {
                return decoder;
            }
        }, new IoListener() {

            public void onSessionCreated(IoSession session) {
                System.err.println("SessionCreated");
            }

            public void onSessionOpened(IoSession session) {
                System.err.println("SessionOpened");
            }

            public void onMessageSent(IoSession session, Object message) {
                System.err.println("MessageSent: " + message);
            }

            public void onMessageReceived(IoSession session, Object message) {
                System.err.println("MessageReceived: " + message);
                for (int i = 0; i < 100; i++) {
                    StringBuilder b = new StringBuilder("Hello from UxNetIO! This is line:"+i);
                    session.write(b.toString());
                }
                session.close();
            }

            public void onSessionClosed(IoSession session) {
                System.err.println("SessionClosed");
            }

            public void onExceptionOccurred(IoSession session, Throwable ex) {
                ex.printStackTrace();
            }
        });
        
        System.err.println("LISTENING ON PORT: " + acceptor.getPort());
    }

Output: (Google Chrome)

One of the games utilising this library is my MMORPG Renoria (it worked for us so maybe it will work for someone else?):

Either way, I hope someone finds this useful!

Cool, thanks for sharing, good idea to simplify MINA. I did a similar thing a few years ago when I tried to make a networked game. MINA is very good but it takes so much learning and implementation of wrapper classes just to figure out how to send a byte array!

Nowadays there’s KryoNet which is used in many games and Riven’s NIO library which I think is used in minecraft which are obviously also very good alternatives as you point out.

Thanks, yeah, initially my server and client were both running off MINA but I decided MINA is a bit too generic and I wanted a library specifically suited for high performance network gaming, but still with an API structure similar to MINA. Hence this was born.

KyroNet is a good library, but it has its uses and I don’t believe it’s that great when used in a MMORPG server, but each to their own :slight_smile:

This also has old Socket fallback when you aren’t able to instantiate a Selector (for example sometimes when running from an Applet)

I’ll release some more demos and examples later for this (with binary serialization and stuff), I guess I just decided Ive used a lot of libraries from the community like LWJGL, LibGDX and stuff and decided to contribute something back.

Little update - Fixed some compilation errors some people were getting, and made the PacketWriter class work directly with a byte array instead of using BYteArrayOutputStream, speed increases of over 20x have been reported with the server JVM.

Please can you elaborate?

I believe KyroNet is single threaded, even on the server. This library allows you to pass in an Executor into the constructor and thread limit. Better for throughput vital systems which are able to take advantage of multi cores, like MMORPG servers.