Class TransportHandlerAdapter

java.lang.Object
express.mvp.myra.transport.TransportHandlerAdapter
All Implemented Interfaces:
TransportHandler

public class TransportHandlerAdapter extends Object implements TransportHandler
Abstract adapter class for 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 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:
      onConnected in interface TransportHandler
      Parameters:
      token - the token returned by Transport.connect(SocketAddress)
    • onConnectionFailed

      public void onConnectionFailed(long token, Throwable cause)
      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:
      onConnectionFailed in interface TransportHandler
      Parameters:
      token - the token returned by Transport.connect(SocketAddress)
      cause - the exception describing the failure (e.g., connection refused, timeout)
    • onDataReceived

      public void onDataReceived(MemorySegment data)
      Called when data is received from the remote peer.

      Important: The data segment 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:
      onDataReceived in interface TransportHandler
      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:
      onSendComplete in interface TransportHandler
      Parameters:
      token - the token returned by Transport.send(MemorySegment)
    • onSendFailed

      public void onSendFailed(long token, Throwable cause)
      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:
      onSendFailed in interface TransportHandler
      Parameters:
      token - the token returned by Transport.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:
      onClosed in interface TransportHandler