Class TransportHandlerAdapter
- All Implemented Interfaces:
TransportHandler
TransportHandler with empty default implementations.
This adapter allows you to implement only the callback methods you care about, rather than
implementing all six methods of TransportHandler. All methods have empty implementations
that do nothing.
Usage Example
transport.start(new TransportHandlerAdapter() {
@Override
public void onConnected(long token) {
System.out.println("Connected!");
}
@Override
public void onDataReceived(MemorySegment data) {
// Only implement the callbacks you need
processData(data);
}
});
Thread Safety
This class is stateless and thread-safe. However, subclasses that add state must ensure their implementations are thread-safe since callbacks are invoked from the I/O thread.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidonClosed()Called when the transport is closed.voidonConnected(long token) Called when a connection is successfully established.voidonConnectionFailed(long token, Throwable cause) Called when a connection attempt fails.voidonDataReceived(MemorySegment data) Called when data is received from the remote peer.voidonSendComplete(long token) Called when a send operation completes successfully.voidonSendFailed(long token, Throwable cause) Called when a send operation fails.
-
Constructor Details
-
TransportHandlerAdapter
public TransportHandlerAdapter()
-
-
Method Details
-
onConnected
public void onConnected(long token) Called when a connection is successfully established.After this callback, the transport is ready for I/O operations. You can begin sending data or the transport will automatically start receiving.
Default implementation does nothing.
- Specified by:
onConnectedin interfaceTransportHandler- Parameters:
token- the token returned byTransport.connect(SocketAddress)
-
onConnectionFailed
Called when a connection attempt fails.The transport remains in a disconnected state. You may attempt to reconnect by calling
Transport.connect(SocketAddress)again.Default implementation does nothing. Consider logging the failure in production code.
- Specified by:
onConnectionFailedin interfaceTransportHandler- Parameters:
token- the token returned byTransport.connect(SocketAddress)cause- the exception describing the failure (e.g., connection refused, timeout)
-
onDataReceived
Called when data is received from the remote peer.Important: The
datasegment is a view into the transport's internal buffer. It is only valid for the duration of this callback. If you need to preserve the data, copy it to your own buffer before returning.The transport automatically posts the next receive operation after this callback returns.
Default implementation does nothing. Override to process received data.
- Specified by:
onDataReceivedin interfaceTransportHandler- Parameters:
data- a read-only view of the received data; valid only during this callback
-
onSendComplete
public void onSendComplete(long token) Called when a send operation completes successfully.The buffer used for the send has been released back to the pool and may be reused for subsequent operations.
Default implementation does nothing.
- Specified by:
onSendCompletein interfaceTransportHandler- Parameters:
token- the token returned byTransport.send(MemorySegment)
-
onSendFailed
Called when a send operation fails.Common causes include connection reset, broken pipe, or buffer allocation failure. The associated buffer has been released.
Default implementation does nothing. Consider logging the failure in production code.
- Specified by:
onSendFailedin interfaceTransportHandler- Parameters:
token- the token returned byTransport.send(MemorySegment)cause- the exception describing the failure
-
onClosed
public void onClosed()Called when the transport is closed.This is called regardless of whether the close was initiated locally (via
Transport.close()) or remotely (peer disconnected). After this callback, the transport is no longer usable.Default implementation does nothing.
- Specified by:
onClosedin interfaceTransportHandler
-