|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnaga.NIOService
public class NIOService
This class forms the basis of the NIO handling in Naga.
Common usage is to create a single instance of this service and then run one other the select methods in a loop.
Use openSocket(String, int)
to open a socket to a remote server,
and openServerSocket(int)
to open a server socket locally.
Note that the NIOServerSocket by default opens in a "refuse connections" state. To start accepting players, the socket's acceptor must first be set to accept connections. See documentation for openServerSocket for more details.
Example use:
Using the server socket:
Using regular sockets:
NIOService service = new NIOService;
NIOServerSocket serverSocket = service.openServerSocket(1234);
serverSocket.setConnectionAcceptor(myAcceptor);
serverSocket.listen(myObserver);
NIOService service = new NIOService;
NIOSocket socket = service.openSocket("www.google.com", 1234);
socket.listen(myObserver);
// Asynchronous write by default:
socket.write("Some message".getBytes());
Constructor Summary | |
---|---|
NIOService()
Create a new nio service. |
Method Summary | |
---|---|
void |
close()
Close the entire service. |
java.util.Queue<java.lang.Runnable> |
getQueue()
Returns a copy of the internal event queue. |
boolean |
isOpen()
Determine if this service is open. |
NIOServerSocket |
openServerSocket(java.net.InetSocketAddress address,
int backlog)
Open a server socket on the address. |
NIOServerSocket |
openServerSocket(int port)
Open a server socket on the given port with the default connection backlog. |
NIOServerSocket |
openServerSocket(int port,
int backlog)
Open a server socket on the given port. |
NIOSocket |
openSocket(java.net.InetAddress inetAddress,
int port)
Open a normal socket to the host on the given port returning a NIOSocket. |
NIOSocket |
openSocket(java.lang.String host,
int port)
Open a normal socket to the host on the given port returning a NIOSocket. |
void |
queue(java.lang.Runnable event)
Queues an event on the NIOService queue. |
void |
selectBlocking()
Run all waiting NIO requests, blocking indefinitely until at least one request is handled. |
void |
selectBlocking(long timeout)
Run all waiting NIO requests, blocking until at least one request is found, or the method has blocked for the time given by the timeout value, whatever comes first. |
void |
selectNonBlocking()
Run all waiting NIO requests, returning immediately if no requests are found. |
void |
wakeup()
Runs wakeup on the selector, causing any blocking select to be released. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public NIOService() throws java.io.IOException
java.io.IOException
- if we failed to open the underlying selector used by the
service.Method Detail |
---|
public void selectBlocking() throws java.io.IOException
java.io.IOException
- if there is an IO error waiting for requests.
java.nio.channels.ClosedSelectorException
- if the underlying selector is closed
(in this case, NIOService#isOpen will return false)public void selectNonBlocking() throws java.io.IOException
java.io.IOException
- if there is an IO error waiting for requests.
java.nio.channels.ClosedSelectorException
- if the underlying selector is closed.
(in this case, NIOService#isOpen will return false)public void selectBlocking(long timeout) throws java.io.IOException
timeout
- the maximum time to wait for requests.
java.lang.IllegalArgumentException
- If the value of the timeout argument is negative.
java.io.IOException
- if there is an IO error waiting for requests.
java.nio.channels.ClosedSelectorException
- if the underlying selector is closed.
(in this case, NIOService#isOpen will return false)public NIOSocket openSocket(java.lang.String host, int port) throws java.io.IOException
This roughly corresponds to creating a regular socket using new Socket(host, port).
This method is thread-safe.
host
- the host we want to connect to.port
- the port to use for the connection.
java.io.IOException
- if registering the new socket failed.public NIOSocket openSocket(java.net.InetAddress inetAddress, int port) throws java.io.IOException
This roughly corresponds to creating a regular socket using new Socket(inetAddress, port).
This method is thread-safe.
inetAddress
- the address we want to connect to.port
- the port to use for the connection.
java.io.IOException
- if registering the new socket failed.public NIOServerSocket openServerSocket(int port, int backlog) throws java.io.IOException
This roughly corresponds to using new ServerSocket(port, backlog);
This method is thread-safe.
port
- the port to open.backlog
- the maximum connection backlog (i.e. connections pending accept)
java.io.IOException
- if registering the socket fails.public NIOServerSocket openServerSocket(int port) throws java.io.IOException
This roughly corresponds to using new ServerSocket(port);
This method is thread-safe.
port
- the port to open.
java.io.IOException
- if registering the socket fails.public NIOServerSocket openServerSocket(java.net.InetSocketAddress address, int backlog) throws java.io.IOException
This method is thread-safe.
address
- the address to open.backlog
- the maximum connection backlog (i.e. connections pending accept)
java.io.IOException
- if registering the socket fails.public void close()
This will disconnect all sockets associated with this service.
It is not possible to restart the service once closed.
This method is thread-safe.
public boolean isOpen()
This method is thread-safe.
public void queue(java.lang.Runnable event)
This method is thread-safe, but should in general not be used by other applications.
event
- the event to run on the NIOService-thread.public java.util.Queue<java.lang.Runnable> getQueue()
public void wakeup()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |