Class TcpTransport
- All Implemented Interfaces:
Transport, AutoCloseable
Transport using a pluggable I/O backend.
This is the primary transport implementation, providing asynchronous TCP communication with zero-copy I/O support via registered buffers.
Architecture
┌─────────────────────────────────────────────────────────────┐ │ TcpTransport │ ├─────────────────────────────────────────────────────────────┤ │ Application Thread │ Poller Thread │ │ ───────────────── │ ───────────── │ │ • connect() │ • pollLoop() │ │ • send() │ • processCommands() │ │ • acquireBuffer() │ • backend.poll() │ │ │ │ │ │ │ ▼ │ ▼ │ │ ┌───────────────┐ │ ┌─────────────┐ │ │ │ Command Queue │───────────────▶│ Backend │ │ │ │ (MPSC Ring) │ │ │ (io_uring) │ │ │ └───────────────┘ │ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘
Thread Model
- Application threads: Submit commands via lock-free MPSC queue
- Poller thread: Processes commands, submits to kernel, polls completions
Token-Based Correlation
Operations are tracked using 64-bit tokens with the following bit layout:
- Bit 63: Receive operation flag
- Bit 62: Connect operation flag
- Bits 0-61: Monotonically increasing sequence number
CPU Affinity
If configured, the poller thread is pinned to a specific CPU core for reduced latency variance and improved cache locality.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTcpTransport(TransportBackend backend, RegisteredBufferPool bufferPool, SocketAddress remoteAddress, int cpuAffinity) Creates a new TCP transport with the specified backend and configuration. -
Method Summary
Modifier and TypeMethodDescriptionAcquires a registered buffer for zero-copy I/O.intReturns the number of buffers currently available in the pool.voidclose()Closes the transport and releases all resources.longconnect(SocketAddress remoteAddress) Initiates an asynchronous connection to a remote endpoint.Returns the connection pool managing connections for this transport.Returns health metrics for this transport.Returns the local socket address.Returns the remote socket address.booleanChecks if the transport has an active connection.longsend(MemorySegment data) Sends data asynchronously.voidstart(TransportHandler handler) Starts the transport with the specified handler for receiving callbacks.
-
Constructor Details
-
TcpTransport
public TcpTransport(TransportBackend backend, RegisteredBufferPool bufferPool, SocketAddress remoteAddress, int cpuAffinity) Creates a new TCP transport with the specified backend and configuration.- Parameters:
backend- the I/O backend to usebufferPool- the registered buffer poolremoteAddress- the remote endpoint to connect tocpuAffinity- CPU core to pin poller thread (-1 for no affinity)
-
-
Method Details
-
start
Description copied from interface:TransportStarts the transport with the specified handler for receiving callbacks.This method must be called before any I/O operations. The handler receives all completion callbacks (connect, send, receive, errors).
Lifecycle: After calling start, the transport is ready for I/O. Call
Transport.close()to shut down cleanly. -
connect
Description copied from interface:TransportInitiates an asynchronous connection to a remote endpoint.The connection attempt runs asynchronously. When complete, the
TransportHandler.onConnected(long)callback is invoked with the result (0 for success, negative errno for failure).Token Usage
The returned token is passed to the completion handler and can be used to correlate this operation with application-level state.
-
send
Description copied from interface:TransportSends data asynchronously.The send operation is queued and executed asynchronously. For best performance, use buffers obtained from
Transport.acquireBuffer()which enables zero-copy I/O.Memory Ownership: The caller retains ownership of the data segment. The data is copied (or pinned for zero-copy) before this method returns, so the segment can be reused immediately.
-
acquireBuffer
Description copied from interface:TransportAcquires a registered buffer for zero-copy I/O.Registered buffers are pre-validated by the kernel, eliminating per-operation address validation overhead. This provides approximately 1.7x throughput improvement with io_uring.
Blocking Behavior: This method blocks if no buffers are available until one is released.
Resource Management: Always release buffers via
RegisteredBuffer.close()when done, typically in a try-with-resources block.- Specified by:
acquireBufferin interfaceTransport- Returns:
- a registered buffer from the pool, ready for use
- See Also:
-
availableBufferSpace
public int availableBufferSpace()Description copied from interface:TransportReturns the number of buffers currently available in the pool.This can be used to implement backpressure or monitoring. Note that the value may change immediately after the call returns.
- Specified by:
availableBufferSpacein interfaceTransport- Returns:
- the number of available buffers
-
getConnectionPool
Description copied from interface:TransportReturns the connection pool managing connections for this transport.The connection pool tracks active connections and provides metrics about connection state.
- Specified by:
getConnectionPoolin interfaceTransport- Returns:
- the connection pool (never null)
-
getHealth
Description copied from interface:TransportReturns health metrics for this transport.Health information includes buffer utilization, error rates, and throughput metrics.
-
isConnected
public boolean isConnected()Description copied from interface:TransportChecks if the transport has an active connection.- Specified by:
isConnectedin interfaceTransport- Returns:
trueif connected to a remote endpoint
-
getLocalAddress
Description copied from interface:TransportReturns the local socket address.- Specified by:
getLocalAddressin interfaceTransport- Returns:
- the local address, or
nullif not bound
-
getRemoteAddress
Description copied from interface:TransportReturns the remote socket address.- Specified by:
getRemoteAddressin interfaceTransport- Returns:
- the remote address, or
nullif not connected
-
close
public void close()Description copied from interface:TransportCloses the transport and releases all resources.This method:
- Closes all active connections gracefully
- Releases the buffer pool
- Shuts down the I/O backend
- Frees all off-heap memory
After closing, the transport cannot be reused. Create a new instance if needed.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceTransport
-