CommunicationsFramework - Java (Netty) Socket Communication made easy

Discussion in 'Resources' started by iKeirNez, May 2, 2014.

Thread Status:
Not open for further replies.
  1. Recently I had the need to be able to talk to multiple game servers whilst players were not connected to the server. We originally used the plugin message api (to which my library PluginMessageAPI+ was born) however that didn't quite do it, we needed to add functionality that required communication between the Lobby & the game servers regardless of whether players are online or not.

    The project started off simply using normal Java sockets (non NIO, with Object Input & Output streams) however I intended to use Netty at some future point. I quickly learnt the basics of Netty (NIO) and updated the framework to use it. The framework very quickly expanded to features I needed and it became what it is.

    GitHub: here
    JavaDocs: here

    Requirements:
    • Java 7 or higher (Java 8 is recommended for simpler/shorter code with lambdas)
    • Any ports used be forwarded on the server-side (if you need to have access to them externally)
    Features (up to date list here) :
    • Easy persistent Client <-> Server connections.
    • Secure connections with a key.
    • Create your own packets with ease.
    • Listen for connection events (such as connected, lost connection, reconnect and more to be added).
    • Automatic handling of reconnects.
    • Packet queue system which allows packets to be queued for sending when the connection currently isn't available (waiting for connection or reconnecting).
    • Graceful disconnecting, no more exception spams when one side disconnects for whatever reason.
    • Heartbeats which allow us to determine if the other side has disconnected quickly.
    • Fully documented frontend, no need to deal with confusing internals (just make sure you only use classes within the "api" package).
    • Community support (hopefully), the efforts of the community will usually always be better than what someone can achieve on their own and if one person adds a new feature, everyone benefits.
    Usage:

    Unfortunately I do not currently own a dedicated server to which I can host a Maven repo for the project, you will have to clone the repo & build it locally to add the project to your local Maven repo. If that is not an option for you, you can always simply download the files from the repo and copy them into your project directly, however this is not recommended. I will update this thread as soon as I acquire a means of hosting CommunicationsFramework on a Maven repo.​

    The best way to learn is by example, I have included in the source code, 2 examples with lots of comments to explain what everything does. These examples will be updated as the framework evolves.​



    I highly recommend using the Authenticated client to prevent anyone from simply connecting to your server (unless that's what is intended).​
    Future:

    One of the highest on my list is to implement a way of sending very short messages in a much better matter (currently it involves creating an empty class which implements Packet).​
    Pull requests would be awesome and much appreciated! See the GitHub README for more details.​

    Feedback is greatly appreciated and helps motivate me. Let me know where you use this, how it benefits you or what you think needs improved upon.
     
    Goblom and chasechocolate like this.
  2. Offline

    xTrollxDudex

    iKeirNez
    Any javadocs for this? It would take great advantage of gh-pages branch as I have done (not self advertising or anything, but I use as an example here. The javadocs are here).
     
  3. Offline

    chasechocolate

    iKeirNez what's the delay between sending and receiving packets?
     
  4. chasechocolate Depends on the latency between the servers, if its on the same machine it should be pretty much instant. Any idea on a good way to get timings, or do I just need manually calculate it?
     
  5. Offline

    chasechocolate

    iKeirNez Well I guess you could just send System.currentTimeMillis() in the data, and then compare it to the current time on the receiving server.
     
  6. Alright, I'll try get some timings soon.


    I spent some time writing a nice small project to test the timings of CommunicationsFramework. I sent 5000 packets with an average of 0.34 milliseconds (this includes processing and distributing to listeners) for the packet to be sent and received by the other side (locally, so latency doesn't affect the timings).

    I will post the source for the application I used to determine the speed.

    Edit: Here it is https://github.com/iKeirNez/Communi...rNez/CommunicationsFramework/examples/timings

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. Offline

    br456

    iKeirNez
    How do you use this in a plugin?
     

  8. Are you asking how I personally use it or are you looking for code examples?
     
  9. Offline

    br456

    iKeirNez
    If I want to send something between servers, how do I implement this in my plugin

    Do I have to add a dependency, or add the code to my project

    iKeirNez
    I cloned it to eclipse but I get many errors

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Normally you'd add it in via a Maven repo, however I currently don't have one to host it on. You have to clone it into Eclipse as you have done but you also need to add Maven support to the project so that Maven will download needed dependencies. And then build it using Maven which will add the library to your local Maven repo.
     
  11. Offline

    br456

    iKeirNez
    I am still getting errors

    For example, in StandaredInitializer:
    connection.getConnectionManager() gives an error:"The method getConnectionManager is undefined for the type IConnection"

    Code:java
    1. public static void addObjectHandlers(IConnection connection, ChannelPipeline channelPipeline){
    2. channelPipeline.addLast(
    3. new ObjectEncoder(),
    4. new ObjectDecoder(ClassResolvers.weakCachingResolver(connection.getConnectionManager().classLoader)),
    5. new BasicHandler(connection));
    6. }
     
  12. Looks like lombok maybe isn't enabled? Make sure your compiling with Maven and not Eclipse.
     
  13. Offline

    br456


  14. If your compiling with Maven, lombok should work. As for the Maven in Eclipse, what Eclipse version are you using? Eclipse support for Maven isn't great, I'd recommend switching to IntelliJ IDEA which has this functionality built-in.
     
    br456 likes this.
  15. Offline

    br456

    @iKeirNez
    Switching to intellij worked, Thanks
     
  16. Awesome. you'll never regret it.
     
  17. Just thought I'd let you guys know that I'm planning a major update for this soon however its taking me longer due to school, private projects and life. However it will be very powerful when its done ;)
     
  18. Offline

    iiHeroo


    Is this for an MC Server only or could it be used for non-mc server?
     
  19. Can be used for anything as long as it's Java :)
     
  20. Offline

    iiHeroo


    Thanks, that'll be good for my new project.
     

  21. Awesome, if its open to the public, keep me updated? Maybe via PM? Just interested to see how people use the framework. We'll have it up on the sonatype nexus maven repo soon (hopefully) and JavaDocs will be hosted by GitHub pages. Planning some very big changes for 3.0.
     
  22. Offline

    iiHeroo


    Yeah, sadly I'll need to change a lot of my code, but luckily I'm not far in xD. I'll PM you too.
     

  23. Shouldn't be too hard to update it, 3.0 update could be awhile though. If its too much work you could always stay on a lower version.
     
  24. Offline

    iiHeroo


    JavaDocs are led to a 404.
     
  25. Fixed
     
  26. Offline

    lenis0012

    io.netty.channel.ChannelPipelineException: net.theemeraldisle.libs.netty.handlers.PacketHandler is not a @Sharable handler, so can't be added or removed multiple times.

    Error at StandardInitializer.java:39

    I think you need to add @Sharable to your packet handler.

    Also, your ConnectionManagerFactory returns a null instance.
    I used the deprecated ConcreteConnectionManager constructor in there for now.
    But you may want to change that

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  27. lenis0012 Okay, the issue currently is that I'm working on version 3 and it's kinda tricky to go back to previous versions and add fixes. I believe in a more recent version I fixed the ConnectionManagerFactory returning null. I wasn't aware of the first issue but will look into it.
     
  28. Offline

    lenis0012

    Ok.
    But id id clone the latest code from github this morning

    What about multi server support?

    Right now you can only connect one client to a server.

    Other then that, i like it a lot.
     
  29. Currently you have to use different ports however we're working on allowing multiple server connections to share a port.
     
Thread Status:
Not open for further replies.

Share This Page