2014-04-21 02:14:48 +00:00
|
|
|
/* Copyright (c) 2008-2014, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-10-05 21:32:56 +00:00
|
|
|
package java.nio.channels;
|
|
|
|
|
2008-03-25 17:18:17 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2007-10-05 21:32:56 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2008-03-25 17:18:17 +00:00
|
|
|
import java.net.SocketAddress;
|
|
|
|
import java.net.ServerSocket;
|
2010-06-14 22:09:56 +00:00
|
|
|
import java.net.Socket;
|
2007-10-05 21:32:56 +00:00
|
|
|
|
2008-11-22 22:32:53 +00:00
|
|
|
public class ServerSocketChannel extends SelectableChannel {
|
2010-06-14 22:09:56 +00:00
|
|
|
private final SocketChannel channel;
|
2008-11-22 22:32:53 +00:00
|
|
|
|
2010-06-14 22:09:56 +00:00
|
|
|
private ServerSocketChannel() throws IOException {
|
|
|
|
channel = new SocketChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ServerSocketChannel open() throws IOException {
|
2014-11-07 18:31:19 +00:00
|
|
|
Socket.init();
|
|
|
|
|
2007-10-05 21:32:56 +00:00
|
|
|
return new ServerSocketChannel();
|
|
|
|
}
|
|
|
|
|
2009-09-28 22:54:49 +00:00
|
|
|
public int socketFD() {
|
|
|
|
return channel.socketFD();
|
|
|
|
}
|
|
|
|
|
2011-01-13 14:03:28 +00:00
|
|
|
public void handleReadyOps(int ops) {
|
|
|
|
channel.handleReadyOps(ops);
|
|
|
|
}
|
|
|
|
|
2009-08-03 14:58:56 +00:00
|
|
|
public SelectableChannel configureBlocking(boolean v) throws IOException {
|
2008-11-22 22:32:53 +00:00
|
|
|
return channel.configureBlocking(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
|
|
|
channel.close();
|
|
|
|
}
|
|
|
|
|
2010-10-13 19:38:31 +00:00
|
|
|
public SocketChannel accept() throws IOException {
|
2007-10-05 21:32:56 +00:00
|
|
|
SocketChannel c = new SocketChannel();
|
|
|
|
c.socket = doAccept();
|
|
|
|
c.connected = true;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-03-25 17:18:17 +00:00
|
|
|
public ServerSocket socket() {
|
2007-10-05 21:32:56 +00:00
|
|
|
return new Handle();
|
|
|
|
}
|
|
|
|
|
2008-03-25 17:18:17 +00:00
|
|
|
private int doAccept() throws IOException {
|
2009-11-17 00:23:09 +00:00
|
|
|
while (true) {
|
|
|
|
int s = natDoAccept(channel.socket);
|
|
|
|
if (s != -1) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
// todo: throw ClosedByInterruptException if this thread was
|
|
|
|
// interrupted during the accept call
|
|
|
|
}
|
2007-10-05 21:32:56 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 18:30:20 +00:00
|
|
|
private void doListen(int socket, int host, int port) throws IOException {
|
2010-06-14 22:09:56 +00:00
|
|
|
Socket.init();
|
|
|
|
|
2014-03-28 18:30:20 +00:00
|
|
|
natDoListen(socket, host, port);
|
2007-10-05 21:32:56 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 17:18:17 +00:00
|
|
|
public class Handle extends ServerSocket {
|
|
|
|
public void bind(SocketAddress address)
|
|
|
|
throws IOException
|
2007-10-05 21:32:56 +00:00
|
|
|
{
|
2008-03-25 17:18:17 +00:00
|
|
|
InetSocketAddress a;
|
|
|
|
try {
|
|
|
|
a = (InetSocketAddress) address;
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
2014-03-28 18:30:20 +00:00
|
|
|
doListen(channel.socket, a.getAddress().getRawAddress(), a.getPort());
|
2007-10-05 21:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-25 17:18:17 +00:00
|
|
|
private static native int natDoAccept(int socket) throws IOException;
|
2014-03-28 18:30:20 +00:00
|
|
|
private static native void natDoListen(int socket, int host, int port) throws IOException;
|
2007-10-05 21:32:56 +00:00
|
|
|
}
|