public class TcpConnectionHandler extends Object implements ConnectionHandler, AsynchronousManagedObject, WriteBufferReceiver, ServerTcpConnection
ConnectionHandler
.Constructor and Description |
---|
TcpConnectionHandler(TcpCommunicationProtocol protocol,
Connection connection,
int sendBufferSize,
long maxIdleTime)
Initiate.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Flags to close the
WriteBufferReceiver . |
WriteBuffer |
createWriteBuffer(byte[] data,
int length)
Creates a
WriteBuffer for the data. |
WriteBuffer |
createWriteBuffer(ByteBuffer buffer)
Creates the
WriteBuffer for the ByteBuffer . |
ServerInputStream |
getInputStream()
Obtains the
ServerInputStream that provides access to the data sent
from the client. |
Object |
getLock()
Obtains the lock to
synchronize for using this
WriteBufferReceiver . |
Object |
getObject()
Obtains the object being managed.
|
ServerOutputStream |
getOutputStream()
Obtains the
ServerOutputStream to write data back to the client. |
void |
handleHeartbeat(HeartBeatContext context)
Handles a heart beat on
Connection . |
void |
handleRead(ReadContext context)
Handles a read from the
SocketChannel . |
boolean |
isClosed()
Indicates if the
WriteBufferReceiver is closed. |
void |
registerAsynchronousCompletionListener(AsynchronousListener listener)
Provides the
AsynchronousListener to the
AsynchronousManagedObject to enable call back to notify state and
completion of asynchronous processing. |
boolean |
waitOnClientData()
Flags for the
ManagedObject to not execute another Task
until further data is received from the client. |
void |
writeData(WriteBuffer[] data)
Writes data to client of this
Connection . |
public TcpConnectionHandler(TcpCommunicationProtocol protocol, Connection connection, int sendBufferSize, long maxIdleTime)
protocol
- TcpCommunicationProtocol
.connection
- Connection
.sendBufferSize
- Send buffer size.maxIdleTime
- Maximum idle time for the Connection
measured in
milliseconds.public void handleHeartbeat(HeartBeatContext context) throws IOException
ConnectionHandler
Handles a heart beat on Connection
.
Typical use of the heart beat is to allow the ConnectionHandler
to close an idle Connection
.
handleHeartbeat
in interface ConnectionHandler
context
- HeartBeatContext
.IOException
- If fails to handle heart beat. Possibly from attempting to
close it.public void handleRead(ReadContext context) throws IOException
ConnectionHandler
SocketChannel
.handleRead
in interface ConnectionHandler
context
- ReadContext
.IOException
- If fails to obtain data from the ReadContext
.public Object getLock()
WriteBufferReceiver
synchronize
for using this
WriteBufferReceiver
.getLock
in interface WriteBufferReceiver
WriteBufferReceiver
.public WriteBuffer createWriteBuffer(byte[] data, int length)
WriteBufferReceiver
WriteBuffer
for the data.createWriteBuffer
in interface WriteBufferReceiver
data
- Data.length
- Length of data.WriteBuffer
.public WriteBuffer createWriteBuffer(ByteBuffer buffer)
WriteBufferReceiver
WriteBuffer
for the ByteBuffer
.createWriteBuffer
in interface WriteBufferReceiver
buffer
- ByteBuffer
.WriteBuffer
.public void writeData(WriteBuffer[] data) throws IOException
WriteBufferReceiver
Connection
.writeData
in interface WriteBufferReceiver
data
- Data to be written.IOException
- If fails to write data.public void close() throws IOException
WriteBufferReceiver
Flags to close the WriteBufferReceiver
.
Close occurs after all data has been written.
close
in interface WriteBufferReceiver
IOException
- If fails to close.public boolean isClosed()
WriteBufferReceiver
WriteBufferReceiver
is closed.isClosed
in interface WriteBufferReceiver
true
if the WriteBufferReceiver
is closed.public void registerAsynchronousCompletionListener(AsynchronousListener listener)
AsynchronousManagedObject
AsynchronousListener
to the
AsynchronousManagedObject
to enable call back to notify state and
completion of asynchronous processing.registerAsynchronousCompletionListener
in interface AsynchronousManagedObject
listener
- AsynchronousListener
.public Object getObject()
ManagedObject
getObject
in interface ManagedObject
public boolean waitOnClientData() throws IOException
ServerTcpConnection
Flags for the ManagedObject
to not execute another Task
until further data is received from the client.
On calling this the next time a Task
is invoked using this
ManagedObject
, data will be available from the
ServerInputStream
.
waitOnClientData
in interface ServerTcpConnection
true
indicating if will wait on client data.
false
if client data is available and therefore will
not wait.IOException
- If fails to initiate waiting on client.public ServerInputStream getInputStream()
ServerTcpConnection
ServerInputStream
that provides access to the data sent
from the client.getInputStream
in interface ServerTcpConnection
ServerInputStream
.public ServerOutputStream getOutputStream()
ServerTcpConnection
Obtains the ServerOutputStream
to write data back to the client.
Closing the ServerOutputStream
will result in closing the
Connection
.
getOutputStream
in interface ServerTcpConnection
ServerOutputStream
.Copyright © 2005–2016. All rights reserved.