io.windmill.net.ServerSocket Maven / Gradle / Ivy
package io.windmill.net;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import io.windmill.core.CPU;
import io.windmill.core.Future;
import io.windmill.core.tasks.VoidTask1;
import io.windmill.utils.IOUtils;
public class ServerSocket extends Future implements AutoCloseable
{
private final ServerSocketChannel channel;
private final VoidTask1 onAccept;
private final VoidTask1 onFailure;
public ServerSocket(CPU cpu,
Selector selector,
InetSocketAddress address,
VoidTask1 onAccept,
VoidTask1 onFailure)
{
super(cpu);
ServerSocketChannel server;
try
{
server = ServerSocketChannel.open();
java.net.ServerSocket socket = server.socket();
server.configureBlocking(false);
socket.setReuseAddress(true);
socket.setSoTimeout(0);
socket.bind(address);
server.register(selector, SelectionKey.OP_ACCEPT, this);
}
catch (Exception | Error e)
{
cpu.schedule(() -> onFailure.compute(e));
server = null;
}
this.channel = server;
this.onAccept = onAccept;
this.onFailure = onFailure;
}
protected void onAccept()
{
cpu.getSocket().register(channel, onAccept, onFailure);
}
@Override
public void close()
{
IOUtils.closeQuietly(channel);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy