|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public static interface NioServer.Listener
An interface for listening to events from a NioServer
.
A single NioServer.Event
is shared for all invocations
of these methods.
This code is released into the Public Domain. Since this is Public Domain, you don't need to worry about licensing, and you can simply copy this NioServer.java file to your own package and use it as you like. Enjoy. Please consider leaving the following statement here in this code:
This NioServer class was copied to this project from its source as found at iHarder.net.
NioServer
,
NioServer.Adapter
,
NioServer.Event
Method Summary | |
---|---|
void |
nioServerConnectionClosed(NioServer.Event evt)
Called when a connection is closed remotely. |
void |
nioServerNewConnectionReceived(NioServer.Event evt)
Called when a new connection is received. |
void |
nioServerTcpDataReceived(NioServer.Event evt)
Called when TCP data is received. |
void |
nioServerUdpDataReceived(NioServer.Event evt)
Called when UDP data is received. |
Method Detail |
---|
void nioServerNewConnectionReceived(NioServer.Event evt)
Called when a new connection is received. The SelectionKey associated
with the event (an OP_READ
key), is the key that will be
used with the data received event. In this way, you can seed a
Map
or other data structure and associate this very
key with the connection. You will only get new connection
events for TCP connections (not UDP).
The key's attachment mechanism is unused by NioServer and is available for you to store whatever you like.
If your protocol requires the server to respond to a client upon connection, this sample code demonstrates such an arrangement:
public void nioServerNewConnectionReceived(NioServer.Event evt) { SocketChannel ch = (SocketChannel) evt.getKey().channel(); try { ch.write(ByteBuffer.wrap("Greetings\r\n".getBytes())); } catch (IOException ex) { ex.printStackTrace(); // Please don't do printStackTrace in production code } }
evt
- the shared eventvoid nioServerTcpDataReceived(NioServer.Event evt)
Called when TCP data is received. Retrieve the associated ByteBuffer
with evt.getBuffer()
. This is the source ByteBuffer
used by the server directly to receive the data. It is a
"direct" ByteBuffer (created with ByteBuffer.allocateDirect(..)
).
Read from it as much as
you can. Any data that remains on or after the value
of position()
will be saved for the next
time an event is fired. In this way, you can defer
processing incomplete data until everything arrives.
Be careful that you don't leave the buffer full
or you won't be able to receive anything next time around
(unless you call NioServer.setBufferSize(int)
to resize buffer).
The key's attachment mechanism is unused by NioServer and is available for you to store whatever you like.
Example: You are receiving lines of text. The ByteBuffer returned here contains one and a half lines of text. When you realize this, you process the first line as you like, but you leave this buffer's position at the beginning of the second line. In this way, The beginning of the second line will be the start of the buffer the next time around.
evt
- the shared eventvoid nioServerUdpDataReceived(NioServer.Event evt)
Called when UDP data is received. Retrieve the associated ByteBuffer
with evt.getBuffer()
. This is the source ByteBuffer
used by the server directly to receive the data. It is a
"direct" ByteBuffer (created with ByteBuffer.allocateDirect(..)
).
The contents of the ByteBuffer will be the entire contents
received from the UDP datagram.
evt
- the shared eventvoid nioServerConnectionClosed(NioServer.Event evt)
evt
- the shared event
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |