BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
bcnp::TcpPosixAdapter Class Reference

TCP transport adapter for BCNP over POSIX sockets. More...

#include <tcp_posix.h>

Inheritance diagram for bcnp::TcpPosixAdapter:
[legend]
Collaboration diagram for bcnp::TcpPosixAdapter:
[legend]

Public Member Functions

 TcpPosixAdapter (uint16_t listenPort, const char *targetIp=nullptr, uint16_t targetPort=0)
 Construct TCP adapter.
 
 ~TcpPosixAdapter () override
 Destructor. Closes all open sockets.
 
bool SendBytes (const uint8_t *data, std::size_t length) override
 Sends bytes through the TCP connection.
 
std::size_t ReceiveChunk (uint8_t *buffer, std::size_t maxLength) override
 Receives bytes from the TCP connection.
 
bool IsValid () const
 
bool IsConnected () const
 
bool IsHandshakeComplete () const
 
bool SendHandshake ()
 Sends the V3 protocol handshake to the peer.
 
uint32_t GetRemoteSchemaHash () const
 
void SetExpectedSchemaHash (uint32_t hash)
 Override expected schema hash (for testing with custom schemas)
 
- Public Member Functions inherited from bcnp::DuplexAdapter
 ~DuplexAdapter () override=default
 
- Public Member Functions inherited from bcnp::ByteWriter
virtual ~ByteWriter ()=default
 
- Public Member Functions inherited from bcnp::ByteStream
virtual ~ByteStream ()=default
 

Detailed Description

TCP transport adapter for BCNP over POSIX sockets.

Supports both server and client modes with automatic reconnection, non-blocking I/O, and V3 schema handshake validation.

Server mode: Listens on a port, accepts one client at a time. Client mode: Connects to a remote server, auto-reconnects on disconnect.

Definition at line 22 of file tcp_posix.h.

Constructor & Destructor Documentation

◆ TcpPosixAdapter()

bcnp::TcpPosixAdapter::TcpPosixAdapter ( uint16_t listenPort,
const char * targetIp = nullptr,
uint16_t targetPort = 0 )
explicit

Construct TCP adapter.

Constructs a TCP adapter in server or client mode.

Parameters
listenPortPort to listen on (server mode) or 0 (client mode)
targetIpRemote IP address (client mode only)
targetPortRemote port (client mode only)
listenPortPort to bind for server mode. Pass 0 for client mode.
targetIpTarget IP address for client mode (ignored in server mode).
targetPortTarget port for client mode (ignored in server mode).

Server mode (listenPort > 0): Creates a listening socket on the specified port Accepts one client at a time Auto-disconnects zombie clients after timeout

Client mode (listenPort == 0, targetIp/targetPort set): Initiates connect to the target Automatically reconnects on disconnect

Definition at line 54 of file tcp_posix.cpp.

◆ ~TcpPosixAdapter()

bcnp::TcpPosixAdapter::~TcpPosixAdapter ( )
override

Destructor. Closes all open sockets.

Definition at line 100 of file tcp_posix.cpp.

Member Function Documentation

◆ GetRemoteSchemaHash()

uint32_t bcnp::TcpPosixAdapter::GetRemoteSchemaHash ( ) const
inline

Definition at line 41 of file tcp_posix.h.

◆ IsConnected()

bool bcnp::TcpPosixAdapter::IsConnected ( ) const
inline

Definition at line 37 of file tcp_posix.h.

◆ IsHandshakeComplete()

bool bcnp::TcpPosixAdapter::IsHandshakeComplete ( ) const
inline

Definition at line 39 of file tcp_posix.h.

◆ IsValid()

bool bcnp::TcpPosixAdapter::IsValid ( ) const
inline

Definition at line 36 of file tcp_posix.h.

◆ ReceiveChunk()

std::size_t bcnp::TcpPosixAdapter::ReceiveChunk ( uint8_t * buffer,
std::size_t maxLength )
overridevirtual

Receives bytes from the TCP connection.

Performs non-blocking receive. Handles V3 handshake protocol transparently - handshake bytes are consumed internally and not returned to the caller.

Parameters
bufferDestination buffer for received data.
maxLengthMaximum bytes to receive.
Returns
Number of bytes received (0 if no data or error).

Implements bcnp::ByteStream.

Definition at line 351 of file tcp_posix.cpp.

Here is the call graph for this function:

◆ SendBytes()

bool bcnp::TcpPosixAdapter::SendBytes ( const uint8_t * data,
std::size_t length )
overridevirtual

Sends bytes through the TCP connection.

Data is buffered and sent asynchronously to prevent blocking on slow connections. Implements congestion control by rejecting new packets when buffer exceeds 50% capacity.

Parameters
dataPointer to byte buffer to send.
lengthNumber of bytes to send.
Returns
true if data was accepted for sending, false if buffer full or not connected.

Implements bcnp::ByteWriter.

Definition at line 316 of file tcp_posix.cpp.

◆ SendHandshake()

bool bcnp::TcpPosixAdapter::SendHandshake ( )

Sends the V3 protocol handshake to the peer.

The handshake contains the protocol magic bytes and schema hash. Peers must have matching schema hashes for full interoperability.

Returns
true if handshake was queued for sending, false on error.

Definition at line 561 of file tcp_posix.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetExpectedSchemaHash()

void bcnp::TcpPosixAdapter::SetExpectedSchemaHash ( uint32_t hash)
inline

Override expected schema hash (for testing with custom schemas)

Definition at line 44 of file tcp_posix.h.


The documentation for this class was generated from the following files: