Radi3nt / NetworkingAPI

Simple library to send packets using Java's socket networking system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Table of Contents
  1. About The Project
  2. Getting Started
  3. Roadmap
  4. Contributing
  5. License
  6. Contact

About The Project

This is a complete networking API to use with Java. Why SHOULD you use this API:

  • This is a fast and efficient API that use dynamic compresseion
  • It's easly understandable and usable for anyone familliar with Java
  • Disgned for maximum extensibility and flexibility
  • This project is currently actively updated

(back to top)

Getting Started

To get a local copy up and running follow these simple example steps.

Installation

You don't need anything more than a Java IDE to get started!

Clone the repo

git clone https://github.com/Radi3nt/NetworkingAPI.git

(back to top)

Usage

Some of the classes don't exist anymore, I have to rewrite this section. Still pretty much the same though

Packets

This librairy uses packets to communicate on a connection. You can create a packet by implementing the class PacketWrite or PacketRead.

  • PacketWrite class is used to send packets through a connection.
  • PacketRead class is used for when you recieve a PacketRead through a connection, it reads the data for you to use it later.

Here is an exemple of a read and write packet sending an integer.

public class ExemplePacketReadWrite implements PacketRead, PacketWrite {

    public int number;

    @Override
    public void read(ReadablePacketBuffer packetBuffer) throws IOException {
        IntReader intReader = new IntReader();
        packetBuffer.read(intReader);

        number = intReader.getIntResult();
    }

    @Override
    public void write(WritablePacketBuffer packetBuffer) throws EncodeException {
        IntWriter intWriter = new IntWriter(number);
        packetBuffer.write(intWriter);
    }

    @Override
    public String toString() {
        return "TestPacketReadWrite{" +
                "number=" + number +
                '}';
    }
}

But wait, what are those WritablePacketBuffer and ReadablePacketBuffer ? These are in fact buffers which allow to read or write packet information through the connection.
Note: if you have a class more efficient than a ByteBuffer or a DataOutputStream, you can provide a custom implementation of those classes

Connections

Now that you know how to create packets and send information through them, how do you establish a connection between a client and a server ?
Note: this part uses the default implementation of the librairy, java sockets. You can totally use a different protocol and create your own protocol by extending the classes in the connection package

To create a client connection, you need a protocol. The protocol must be the same on the client and on the server to be sure that the data is read correctly. A protocol will define how your packets are sent through a connection (you can totally create your own protocol by extending the PacketProtocol class).

The default class, SizedIdPacketProtocol will encode the packet using it's id, provided by the PacketProtocolIdentification you gave it, and it's total size.

You then provide the adress and the port of the connection and it will instantly try to establish a link. Here is an exemple of creating a basic client connection

private static ConnectingConnection createConnection() throws NetworkException {
    PacketFactoryProtocolIdentification packetFactoryProtocolIdentification = createIdentification();
    SizedIdPacketProtocol simplePacketProtocol = new SizedIdPacketProtocol(packetFactoryProtocolIdentification);
    return new ConnectingConnection(new SocketAddress("140.82.124.4", 25565), simplePacketProtocol, System.out::println);
}

public static PacketFactoryProtocolIdentification createIdentification() {
    Map<Integer, PacketFactory> packetFactoryMap = new HashMap<>();
    packetFactoryMap.put(1, new TestPacketFactory());

    return new PacketFactoryProtocolIdentification(packetFactoryMap);
}

private static class TestPacketFactory implements PacketFactory {
        @Override
        public boolean isPacket(Packet packet) {
            return packet instanceof TestPacketReadWrite;
        }

        @Override
        public PacketRead create() {
            return new TestPacketReadWrite();
        }
    }

Listeners

After you have created your connection, you need to hook a listener onto it to be able to recieve packets from the server.
Use the InteractiveConnection.attachListener() method in a different thread to be able to recieve messages. You can create a PacketListener and give it to the connection on it's creation.
⚠The InteractiveConnection.attachListener() method blocks until the connection is closed, use it in a SEPARATED thread⚠

private static void attachListener(InteractiveConnection interactiveConnection) {
    Thread thread = new Thread(interactiveConnection::attachListener);
    thread.start();
}

Sending packets

Now that you have a working connection, you can send packets using the sendPacket(PacketWrite... packets) method in any connection. It will block until the packet has been written through the connection.

Server

You need to use a ConnectionHandler to recieve a connection. There is a implementation of it called SocketServerConnectionHandler that you can use.
Create one and call the accept() method. This method will block until a client is connecting to the server, and will then return a ServerConnection which you can interact with
Note: don't forget to attach a listener to this connection by using the attachListener() method !

Conclusion

If you still need help, there are two class, called MainNetworkingAPIDemoServer and MainNetworkingAPIDemoClient in the main package for you to study.

(back to top)

Roadmap

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch
  3. Commit your Changes
  4. Push to the Branch
  5. Open a Pull Request

(back to top)

License

Distributed under the Creative Commons Zero v1.0 Universal. See LICENSE.txt for more information.

(back to top)

Contact

Radi3nt - pro.radi3nt@gmail.com

Project Link: https://github.com/Radi3nt/NetworkingAPI

(back to top)

About

Simple library to send packets using Java's socket networking system

License:GNU General Public License v3.0


Languages

Language:Java 100.0%