Skip to content

Releases: Radi3nt/NetworkingAPI

Stable V1.0.0

27 Aug 12:08
181e09d
Compare
Choose a tag to compare

Stable V1.0.0

Usage

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) {
        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.