Skip to content

OughtToPrevail/AsyncNetwork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsyncNetwork

AsyncNetwork short for AsynchronousNetwork is a library which implements an easy-to-use, fast, TCP network

Features

  • Cross platform (Windows, Linux, Mac, Android etc...)
  • Asynchronous
  • Can handle lots of clients at once
  • Easy-to-use
  • Fast and efficient

Maven

<dependency>
    <groupId>com.github.oughttoprevail</groupId>
    <artifactId>AsyncNetwork</artifactId>
    <version>1.3.1</version>
</dependency>

Here is The Central Repository.

How to use

To create a client you would do:

ClientSocket client = new ClientSocket();
client.onConnect(() -> WritablePacketBuilder.create().putByte(Byte.MAX_VALUE).build().writeAndClose(client));
client.connectLocalHost(/*Specify your port here (0-65535) example: 6000*/6000);

To create a server you would do:

ReadablePacket packet = ReadablePacketBuilder.create().aByte().build();
ServerSocket server = new ServerSocket();
server.onConnection(client ->
{
	client.always(true);
	packet.read(client, readResult ->
	{
		byte value = readResult.poll();
		System.out.println("value=" + value);
	});
	client.onDisconnect(disconnectionType -> System.out.println("Disconnected" + disconnectionType));
});
server.bindLocalHost(6000);
while(true);

And you're finished! now you can use AsyncNetwork for your networking projects. Good luck!

Server selector

Windows

Windows uses IO completion ports for best asynchronous performance.

Linux (Android is based on Linux)

Linux has sys/epoll which is famous for it's O(1) epoll_wait performance.

Mac

Mac has FreeBSD features including kqueue which is also O(1).

Other

Other operating systems are not supported by different native implementations, so AsyncNetwork uses the already implemented java.nio.channels.Selector.

Thanks

Special thanks to Jacob and despair who helped me make this!