Thunder - Networking Library icon

Thunder - Networking Library -----

Simple Java Networking for Java 8



Information:
This resource is used to send Packets from a Client to a Server or the other way around.

Basic Instructions:
Code (Java):

Thunder. useCompressor ( true ) ; //This compresses every Packet
Thunder. setLogging (LogLevel. ERROR ) ;
//LogLevel.OFF
//LogLevel.ALL
//LogLevel.DEBUG
//LogLevel.WARNING
//LogLevel.INFO
 
The ThunderClient and the ThunderServer both implement the ThunderConnection.
The ThunderConnection has its own unique Methods which the Client and Server then also have.

Code (Java):

ThunderConnection thunderConnection = (ThunderConnection ) yourObject ; //Server or Client
   
thunderConnection. disconnect ( ) ; //Stops the server or disconnects from the server if client
thunderConnection. addHandler (thunderListener ) ; //Adds a ThunderListener
thunderConnection. addPacketHandler (packet -> { } ) ; //Adds a packetHandler
thunderConnection. setName ( "YourName" ) ; //Sets the name of the connection
thunderConnection. sendPacket (packet ) ; //Method to send packets

boolean connected = thunderConnection. isConnected ( ) ; //Checks if connected
ThunderSession session = thunderConnection. getSession ( ) ; //Session of the Connection
PacketAdapter packetAdapter = thunderConnection. getPacketAdapter ( ) ; //Used to handle Packets
Creating a Server:
Code (Java):

ThunderServer thunderServer = Thunder. createServer ( ) ;
 
thunderServer. start ( 1401 ). perform ( ) ;
 
Creating a Client:
Code (Java):

ThunderClient thunderClient = Thunder. createClient ( ) ;

thunderClient. connect ( "yourHost", 1401 ). perform ( ) ;
 
Creating Packets:
Code (Java):

public class SamplePacket extends Packet {

    private String name ;
    private int age ;

    public SamplePacket ( String name, int age ) {
        this. name = name ;
        this. age = age ;
    }

    @Override
    public void write (PacketBuffer buf ) {
        buf. writeString ( this. name ) ;
        buf. writeInt ( this. age ) ;
    }

    @Override
    public void read (PacketBuffer buf ) {
        this. name = buf. readString ( ) ;
        this. age = buf. readInt ( ) ;
    }
}
Sending Packets (Also works with Server):

Code (Java):

       ThunderClient thunderClient = Thunder. createClient ( ) ;
        thunderClient. connect ( "yourHost", 1401 ). perform ( new Consumer <ThunderClient > ( ) {
            @Override
            public void accept (ThunderClient thunderClient ) {
                //This will be called when the Action ( .perform() )
                // is done and the client is connected
                SamplePacket samplePacket = new SamplePacket ( "Name", 16 ) ;
                thunderClient. sendPacket (samplePacket ) ;
            }
        } ) ;
 
Handling Packets (Also works with server):
Code (Java):

      thunderClient. addPacketHandler ( new PacketHandler ( ) {
            @Override
            public void handle (ThunderPacket packet ) {
                //Check if packet is SamplePacket
                if (packet instanceof SamplePacket ) {
                    SamplePacket samplePacket = (SamplePacket ) packet ;
                    System. out. println (samplePacket. getAge ( ) ) ;
                    System. out. println (samplePacket. getName ( ) ) ;
                    System. out. println (samplePacket. getProcessingTime ( ) + "ms" ) ; //The time the packet took
                    System. out. println (samplePacket. toString ( ) ) ; //Information on the Packet
                }
            }
        } ) ;
 

Working with ThunderSessions:
Every ThunderConnection has its own ThunderSession.
From it you can seperate Clients and Servers from eachother.

Code (Java):

   
        ThunderSession thunderSession = thunderClient. getSession ( ) ;
        boolean authenticated = thunderSession. isAuthenticated ( ) ; //If the session is authenticated
        String sessionId = thunderSession. getSessionId ( ) ; //Name of the session (if set)
        UUID uniqueId = thunderSession. getUniqueId ( ) ; //UUID of the session
        long startTime = thunderSession. getStartTime ( ) ; //Time when the session started
        List <ThunderSession > connectedSessions = thunderSession. getConnectedSessions ( ) ; //Connected Sessions
        Channel channel = thunderSession. getChannel ( ) ; //Channel of this Session
 
Extra Features of the ThunderClient:
Code (Java):
   
       ThunderClient thunderClient = Thunder. createClient ( ) ;
        thunderClient. connect ( "yourHost", 1401 ). perform ( ) ;

        thunderClient. writePacket ( new SamplePacket ( "YourName", 16 ) ) ; //Sends a packet to the client
 
Extra Features of the ThunderServer:

Code (Java):
   
       ThunderServer thunderServer = Thunder. createServer ( ) ;
        thunderServer. start ( 1401 ). perform ( ) ;
   
        UUID uuid = ... uuid of the Client youre trying to find
        thunderServer. sendPacket ( new SamplePacket ( "YourName", 15 ), thunderServer. getSession ( ). getSession (uuid ) ) ;
        //Sends a packet only to one connection
 
Using the ThunderListener (Again works for Client aswell):

Code (Java):

      thunderServer. addHandler ( new ThunderListener ( ) {
            @Override
            public void handleConnect (ThunderConnection thunderConnection ) {
                ThunderClient thunderClient = (ThunderClient ) thunderConnection ;
                //This is a ThunderClient now do what you want
            }

            @Override
            public void handleDisconnect (ThunderConnection thunderConnection ) {
                ThunderClient thunderClient = (ThunderClient ) thunderConnection ;
                //This is a ThunderClient now do what you want
            }

            @Override
            public void handlePacket (ThunderPacket packet, ThunderConnection thunderConnection ) throws IOException {
                //Do what you want
                //Is the same as PacketHandler
            }

            @Override
            public void read (Packet packet, ThunderConnection thunderConnection ) throws IOException {
                //Do what you want
                //This is the raw Packet before getting transformed into
                //ThunderPacket or BufferedPacket
            }
        } ) ;
 
Global Example:

Code (Text):

public static void main(String[] args) {
    ThunderServer thunderServer = Thunder.createServer(new ThunderListener() {
        @Override
        public void handlePacket(Packet packet, ThunderConnection thunderConnection) throws IOException {
            if (packet instanceof SamplePacket) {
                packet.respond(ResponseStatus.SUCESS, "Packet Received");
            }
        }

        @Override
        public void handleConnect(ThunderConnection thunderConnection) {
            System.out.println("[Server] New Connection from " + thunderConnection + "!");
        }

        @Override
        public void handleDisconnect(ThunderConnection thunderConnection) {
            System.out.println("[Server] " + thunderConnection + " has disconnected!");
        }
    });
    ThunderClient thunderClient = Thunder.createClient();

    thunderServer.start(1401).perform();

    thunderClient.connect("127.0.0.1", 1401).perform(new Consumer<ThunderClient>() {
        @Override
        public void accept(ThunderClient thunderClient) {
            thunderClient.sendPacket(new SamplePacket("Luca", 16), new Consumer<Response>() {
                @Override
                public void accept(Response response) {
                    System.out.println(response.getStatus() + " - " + response.getMessage() + " [" + response.getProcessingTime() + "ms]");
                }
            });
        }
    });
}


 
----------------------------------------------------------------
If you got any questions, feel free to ask them!
Would be nice if you could report any bugs to me
or maybe even review this resource if you like it.
Resource Information
Author:
----------
Total Downloads: 156
First Release: May 2, 2021
Last Update: May 8, 2021
Category: ---------------
All-Time Rating:
0 ratings
Version -----
Released: --------------------
Downloads: ------
Version Rating:
----------------------
-- ratings