Table of Contents
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
To get a local copy up and running follow these simple example steps.
You don't need anything more than a Java IDE to get started!
Clone the repo
git clone https://github.com/Radi3nt/NetworkingAPI.git
Some of the classes don't exist anymore, I have to rewrite this section. Still pretty much the same though
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
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();
}
}
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();
}
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.
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 !
If you still need help, there are two class, called MainNetworkingAPIDemoServer and MainNetworkingAPIDemoClient in the main package for you to study.
See the open issues for a full list of proposed features (and known issues).
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!
- Fork the Project
- Create your Feature Branch
- Commit your Changes
- Push to the Branch
- Open a Pull Request
Distributed under the Creative Commons Zero v1.0 Universal. See LICENSE.txt
for more information.
Radi3nt - pro.radi3nt@gmail.com
Project Link: https://github.com/Radi3nt/NetworkingAPI