mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-07 05:08:51 -04:00
added more async networking features
This commit is contained in:
parent
7bb3547cda
commit
f715506ace
8 changed files with 268 additions and 80 deletions
|
|
@ -1,30 +1,74 @@
|
|||
import async_example.Delay;
|
||||
import async_example.Jokio;
|
||||
import async_example.Socket;
|
||||
import async_example.net.ServerSocket;
|
||||
import async_example.net.Socket;
|
||||
import generator.future.Future;
|
||||
import generator.future.Waker;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Examples {
|
||||
|
||||
public static Future<Void> test(){
|
||||
for(int i = 0; i < 1000; i ++){
|
||||
Jokio.runtime(Waker.waker()).spawn(echoForever("Message " + i + "\n"));
|
||||
static long sent = 0;
|
||||
static long received = 0;
|
||||
public static Future<Void, RuntimeException> test(){
|
||||
Jokio.runtime(Waker.waker()).spawn(server());
|
||||
|
||||
for(int i = 0; i < 10000; i ++){
|
||||
var builder = new StringBuilder();
|
||||
for(int c = 0; c < 4096*2; c ++)
|
||||
builder.append((char)((Math.random()*('z'-'a')+'a')));
|
||||
Jokio.runtime(Waker.waker()).spawn(echoForever(builder.toString()));
|
||||
}
|
||||
while(true){
|
||||
System.out.println(sent + " " + received + " " + Jokio.polled);
|
||||
Delay.delay(100).await();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Future<Void, IOException> server(){
|
||||
try(var ss = ServerSocket.bind(new InetSocketAddress("0.0.0.0", 42069))){
|
||||
while (true){
|
||||
var socket = ss.accept().await();
|
||||
Jokio.runtime(Waker.waker()).spawn(echo(socket));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Future.ret(null);
|
||||
}
|
||||
|
||||
public static Future<Void> echoForever(String message){
|
||||
try(var socket = Socket.connect(new InetSocketAddress("45.79.112.203", 4242)).await()){
|
||||
var buffer = ByteBuffer.allocate(500);
|
||||
public static Future<Void, IOException> echo(Socket socket){
|
||||
try(socket){
|
||||
var buffer = ByteBuffer.allocate(4096);
|
||||
while(true){
|
||||
buffer.limit(message.length()).put(message.getBytes(StandardCharsets.UTF_8)).position(0);
|
||||
var read = socket.read(buffer).await();
|
||||
buffer.clear().limit(read);
|
||||
socket.write_all(buffer).await();
|
||||
buffer.clear();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Future<Void, IOException> echoForever(String message){
|
||||
byte[] msg_bytes = message.getBytes(StandardCharsets.UTF_8);
|
||||
try(var socket = Socket.connect(new InetSocketAddress("localhost", 42069)).await()){
|
||||
var buffer = ByteBuffer.allocate(message.length());
|
||||
while(true){
|
||||
buffer.limit(message.length()).put(msg_bytes).position(0);
|
||||
var wrote = socket.write_all(buffer).await();
|
||||
sent++;
|
||||
buffer.clear().limit(wrote);
|
||||
var read = socket.read_all(buffer).await();
|
||||
System.out.print(new String(buffer.array(), 0, read));
|
||||
socket.read_all(buffer).await();
|
||||
if(!buffer.position(0).equals(ByteBuffer.wrap(msg_bytes)))
|
||||
throw new RuntimeException();
|
||||
received++;
|
||||
buffer.clear();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue