org.xsocket.stream
Interface IDataHandler
- All Superinterfaces:
- IHandler
- All Known Implementing Classes:
- HandlerChain
public interface IDataHandler
- extends IHandler
Reads and processes the incoming data. This method will be called
each time when data is available. Because this depends on the
underlying tcp protocol, it is not predictable how often and
when this method will be call. Furthermore the call of this
method is independent of the received data size. The data handler
is responsible to extract the application specific data packages
(like HTTP request or SMTP commands) based on the received data.
Calling a read method on the given connection instance like
connection.readLong() or connection.readStringByDelimiter(…)
will throw a BufferUnderflowException
exception
if the required data isn’t available .
public final class MyHandler implements IDataHandler, IConnectionScoped {
...
public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
...
// BufferUnderflowException will been thrown if delimiter hasn`t been found.
// A MaxReadSizeExceededException will be thrown if the max read size has been exceeded. Unhandling this exception causes
// that the server closes the underlying connection
String command = connection.readStringByDelimiter("\r\n", "US-ASCII", 5000);
...
connection.write(resonse, "US-ASCII");
return true;
}
}
onData
boolean onData(INonBlockingConnection connection)
throws java.io.IOException,
java.nio.BufferUnderflowException,
MaxReadSizeExceededException
- processes the incomming data based on the given connection.
Please note, that the onData
callback method could also be called
for an already closed connection. This occurs when data has been received
(and buffered internally) and the connection has been closed by the peer,
immediately. In this case the isOpen call within the onData
Method will return false. Reading of already received data wouldn`t fail.
To detect if a connection has been closed the callback method onDisconnect
should be implemented. The correct callback order will be managed by the xSocket.
- Parameters:
connection
- the underlying connection
- Returns:
- true for positive result of handling, false for negative result of handling.
The return value will be used by the
HandlerChain
to interrupted
the chaining (if result is true)
- Throws:
java.io.IOException
- If some other I/O error occurs. Throwing this exception causes that the underlying connection will be closed.
java.nio.BufferUnderflowException
- if more incoming data is required to process. The BufferUnderflowException will be swallowed by the framework
MaxReadSizeExceededException
- if the max read size has been reached (e.g. by calling method INonBlockingConnection.readStringByDelimiter(String, int)
).
Throwing this exception causes that the underlying connection will be closed.