DEPRECATED BY https://github.com/logykk/enet-jni
This project provides Java bindings for the ENet reliable UDP networking library through JNI.
- JDK 8 or higher (java 21 recommended)
- CMake 3.10 or higher (4.0 recommended)
- A C++ compiler (GCC, MSVC, Clang)
- Gradle
- Clone the repository with submodules:
git clone --recursive https://github.com/theoplegends/java-enet-jni.git- Build the project using Gradle:
./gradlew buildThis will:
- Generate JNI headers
- Build the native library
- Run the tests
Add the library to your project dependencies and make sure the native library is in your library path.
Basic example:
import op.legends.enet.ENetLibrary;
public class Example {
public static void main(String[] args) {
// Initialize ENet
if (ENetLibrary.initialize() < 0) {
System.err.println("Failed to initialize ENet");
return;
}
try {
// Create a server host
long serverHost = ENetLibrary.createHost("127.0.0.1", 34567, 32, 2, 0, 0);
if (serverHost == 0) {
System.err.println("Failed to create server host");
return;
}
// Create an event handle
long event = ENetLibrary.createEvent();
// Service the host
while (true) {
if (ENetLibrary.hostService(serverHost, event, 1000) > 0) {
switch (ENetLibrary.getEventType(event)) {
case ENetLibrary.EVENT_TYPE_CONNECT:
System.out.println("A new client connected!");
break;
case ENetLibrary.EVENT_TYPE_DISCONNECT:
System.out.println("A client disconnected!");
break;
case ENetLibrary.EVENT_TYPE_RECEIVE:
byte[] data = ENetLibrary.getEventPacketData(event);
System.out.println("Received message: " + new String(data));
break;
}
}
}
} finally {
ENetLibrary.deinitialize();
}
}
}The library defines the following event types:
EVENT_TYPE_NONE(0): No event occurredEVENT_TYPE_CONNECT(1): A peer has connectedEVENT_TYPE_DISCONNECT(2): A peer has disconnectedEVENT_TYPE_RECEIVE(3): A peer has sent data
The ENetLibrary class provides the following main methods:
initialize(): Initialize the ENet librarydeinitialize(): Cleanup the ENet library
createHost(String address, int port, int peerCount, int channelLimit, long incomingBandwidth, long outgoingBandwidth): Create a host for sending/receiving datahostDestroy(long host): Destroy a hosthostService(long host, long event, int timeout): Service a host for events
connect(long host, String address, int port, int channelCount, long data): Connect to a remote hostpeerDisconnect(long peer, long data): Gracefully disconnect from a peerpeerDisconnectNow(long peer, long data): Force immediate disconnectionpeerReset(long peer): Reset the peer's connectionpeerSend(long peer, int channelID, byte[] data): Send data to a peer
createEvent(): Create an event handledestroyEvent(long event): Destroy an event handlegetEventType(long event): Get the type of the eventgetEventPeer(long event): Get the peer associated with the eventgetEventChannelID(long event): Get the channel ID for the eventgetEventData(long event): Get the event datagetEventPacketData(long event): Get the packet data from the event
createPacket(byte[] data, int flags): Create a packetdestroyPacket(long packet): Destroy a packet
getPeerState(long peer): Get the current state of a peergetPeerRoundTripTime(long peer): Get the current round trip timegetPeerLastRoundTripTime(long peer): Get the last round trip timegetPeerLastSendTime(long peer): Get the last send timegetPeerLastReceiveTime(long peer): Get the last receive timegetPeerPacketsSent(long peer): Get the number of packets sentgetPeerPacketsLost(long peer): Get the number of packets lost
The project includes comprehensive unit tests that demonstrate the functionality of the library. Run the tests using:
./gradlew testContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the SKID License - see the LICENSE file for details.