Hey JGO, I’d like to present to you GNetLib, a simple yet effective java networking library that I’ve been working on.
NOTE: A list of features can be seen below.
This library is in a very early stage of development, so if there’s a feature you’d like to see added that would prove useful please post it and I will do my best to implement it in future updates
Library details:
Name: GNetLib
Version: 0.0.0.3
Project creation date: 10/27/2014 (Re-written from scratch no c&p)
Developer notes:
-> Working on a multiplayer demo to showoff the features of GNetLib.
-> Server capacity tested on 127.0.0.1, reached around 4,136 online clients.
–> (Ran all clients on 1 JVM while server was ran on a separate JVM :D)
- UDP feature
- Customization of serialization
- I will invest time and post back when I’ve found more features GNetLib has that kryonet doesn’t
How to get started using this library:
You’ll need to download the .jar below, and add it to your projects workspace/build path.
GNetLib download -> GNetLib.jar (24.2kb)
GNetLib source download link -> GNetLib Source.zip (13.9kb)
(Source code note: If you use this library for a project posted online, please include my name in the credits ;D)
V0.0.0.3 Bugs:
Fixed a few client-sided bugs relating to the new logging system - 10/28/2014 ✓
Fixed a few server-sided bugs relating to the new event system/logging system - 10/28/2014 ✓
Fixed a client-sided bug where the client would crash when the server randomly shut down - 10/28/2014 ✓
Fixed a server-sided issue where the online player count would be wrong upon connecting (during event) - 10/28/2014 ✓
Development status:
Green = finished.
Red = not yet started.
Blue = undergoing development.
-> Server now has a multi-threaded TCP framework
-> Server now has a ServerMonitor GUI (Options to show/dispose of statistics interface)
-> Server can now send/receive TCP objects
-> Server/Client can now send/receive custom packets (org.gnet.packet.Packet)
-> New and improved Packet system, 1 class vs 3 (Now hash-mapped)
-> Server now has the ability to choose which client to forcefully disconnect (org.gnet.packet.ClientShutdownPacket)
-> Server/Client has a new EventListener that handles connection related events along with incoming packet events
-> Server now has a list that contains all online clients (ClientModel instances)
-> Server now generates a unique identification number for each client that connects (UID)
-> Server/Client can now report all error/debug messages in the EventListener
-> Server now has the ability to send mass messages to all online clients (With ability to single out a client)
-> ServerMonitor can now be enabled/disabled on the fly (No longer enabled prior to server.start())
-> Working on giving the server a option to set the maximum number of clients online at once
-> Working on giving the server a login queue system (Encase too many connection requests are performed at the same time)
-> Working on giving the client a internal ping system
-> Working on giving the client a option to request the # of clients connected to the server (Will be a internal packet)
-> Working on a multiplayer demo
-> Add a peer to peer feature for this library
-> Add the option to allow sending of UDP packets along with the current TCP method of sending packets
-> Add the option for a client to re-connect if a connection attempt failed (# connect attempts & delay between attempts)
Code to create/start a server:
// Host to use for the server:
final String host = "127.0.0.1";
// Port # to broadcast the host on:
final int port = 43594;
// Setup our server.
final GNetServer networkedServer = new GNetServer(host, port);
// Disable debugging, (enabled by default) as we'll manually handle it via our EventListener.
networkedServer.setDebugging(false);
// Add our event listener to manage events.
networkedServer.addEventListener(new ServerEventListener() {
@Override
protected void clientConnected(ClientModel client) {
}
@Override
protected void clientDisconnected(ClientModel client) {
}
@Override
protected void packetReceived(ClientModel client, Packet packet) {
}
@Override
protected void debugMessage(String msg) {
}
@Override
protected void errorMessage(String msg) {
}
});
// Attempt to bind the server.
networkedServer.bind();
// Once binded, finally start our server.
networkedServer.start();
Code to create/connect a client to the server:
// Host to connect to:
final String host = "127.0.0.1";
// Port # to connect to the host on:
final int port = 43594;
// Setup our client.
final GNetClient networkedClient = new GNetClient(host, port);
// Disable debugging, (enabled by default) as we'll manually handle it via our EventListener.
networkedClient.setDebugging(false);
// Add our event listener to manage events.
networkedClient.addEventListener(new ClientEventListener() {
@Override
protected void clientConnected(ServerModel server) {
}
@Override
protected void clientDisconnected(ServerModel server) {
}
@Override
protected void packetReceived(ServerModel server, Packet packet) {
}
@Override
protected void debugMessage(String msg) {
}
@Override
protected void errorMessage(String msg) {
}
});
// Attempt to bind the client.
networkedClient.bind();
// Once binded, finally start our client.
networkedClient.start();
How to use the new ServerMonitor:
(Picture of it can be seen here: link)
// To enable/show it:
server.enableServerMonitor();
// To dispose of it:
server.disableServerMonitor();
How to create your own custom packets:
This library sends “packets” (data) across the network to/from clients via org.gnet.packet.Packet class, you can create a class that extends this class or you can create the Packet on the fly.
A demonstration of both methods can be seen below.
NOTE: The Packet class constructor takes 2 parameters.
First parameter is a String that represents the packets name. (This will be used on the receiving end to detect the packets name)
Second parameter is a Integer that represents how many data slots to reserve for this packet. (Can not exceed this amount)
How to create your own custom Packet on the fly:
Packet loginPacket = new Packet("LoginPacket", 2); // 2 slots reserved
loginPacket.addEntry("username", new String("jimmy")); // 1st slot (name/value)
loginPacket.addEntry("pinCode", new Integer(5600)); // 2nd slot (name/value)
How to create your own custom Packet via extending Packet:
public class LoginPacket extends Packet {
public LoginPacket(String user, int pinCode) {
super("LoginPacket", 2); // super must be envoked
// You can add your data here
// or add it to the packet once created.
addEntry("username", user);
addEntry("pinCode", pinCode);
}
}
How to send your own custom packet:
At the moment you must use a instance of “ClientModel” or “ServerModel” to send a packet, future updates will include a method within the packet to send to a destination address.
ClientModel = Server-sided instance for a client.
ServerModel = Client-sided instance for the server.
In this example we will be sending the server a mock “LoginPacket” once we’ve been connected.
@Override
protected void clientConnected(ServerModel server) {
// Client has just connected to the server, here we'll send our packet.
Packet loginPacket = new Packet("MockLoginPacket", 2);
loginPacket.addEntry("username", new String("Jimmy"));
loginPacket.addEntry("pinCode", new Integer(5600));
server.sendPacket(loginPacket);
return; // event handled.
}
How to receive your own custom packet:
In the example below we’ll be receiving the packet server-sided we sent from the client earlier.
@Override
protected void packetReceived(ClientModel client, Packet packet) {
// Make sure the incoming packet has the title we wish to process data from.
if (packet.getPacketName().equals("MockLoginPacket")) {
String user = (String) packet.getEntry("username");
Integer pinCode = (Integer) packet.getEntry("pinCode");
// Do something with the data...
return; // event handled.
}
}
Project source overview:
Thanks for reading JGO, I’ve provided everything required to get started using this library so hopefully some users/browsers will find this library useful and use it in a project of theirs.