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

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

#include <udp_posix.h>

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

Public Member Functions

 UdpPosixAdapter (uint16_t listenPort, const char *targetIp=nullptr, uint16_t targetPort=0)
 Construct UDP adapter.
 
 ~UdpPosixAdapter () override
 Destructor. Closes the socket.
 
bool SendBytes (const uint8_t *data, std::size_t length) override
 Sends bytes to the current peer via UDP.
 
std::size_t ReceiveChunk (uint8_t *buffer, std::size_t maxLength) override
 Receives a UDP datagram.
 
bool IsValid () const
 
void SetPeerLockMode (bool locked)
 Enables or disables peer locking.
 
void SetPairingToken (uint32_t token)
 Sets the expected pairing token for handshake validation.
 
void UnlockPeer ()
 Resets pairing state to allow re-pairing with a new peer.
 
bool IsHandshakeComplete () const
 
bool SendHandshake ()
 Sends the V3 protocol handshake to the current peer.
 
uint32_t GetRemoteSchemaHash () const
 
- 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

UDP transport adapter for BCNP over POSIX sockets.

Connectionless transport with optional peer locking for security. Supports pairing tokens and schema handshake validation.

Note: UDP does not guarantee delivery. Use TCP for reliable transport.

Definition at line 20 of file udp_posix.h.

Constructor & Destructor Documentation

◆ UdpPosixAdapter()

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

Construct UDP adapter.

Constructs a UDP adapter bound to the specified port.

Parameters
listenPortPort to bind for receiving
targetIpFixed target IP (optional, can learn from first packet)
targetPortFixed target port (optional)
listenPortPort to bind for receiving UDP datagrams.
targetIpOptional fixed target IP (bypasses pairing).
targetPortOptional fixed target port (requires targetIp).

When a fixed target is configured, the adapter is automatically locked to that peer and skips the pairing handshake. Otherwise, pairing is required before sending data.

Definition at line 64 of file udp_posix.cpp.

◆ ~UdpPosixAdapter()

bcnp::UdpPosixAdapter::~UdpPosixAdapter ( )
override

Destructor. Closes the socket.

Definition at line 116 of file udp_posix.cpp.

Member Function Documentation

◆ GetRemoteSchemaHash()

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

Definition at line 44 of file udp_posix.h.

◆ IsHandshakeComplete()

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

Definition at line 42 of file udp_posix.h.

◆ IsValid()

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

Definition at line 34 of file udp_posix.h.

◆ ReceiveChunk()

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

Receives a UDP datagram.

Performs non-blocking receive. Handles peer locking and pairing: In locked mode, packets from non-paired sources are ignored Pairing packets are processed internally and not returned Automatic peer timeout triggers re-pairing after kPeerTimeout

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

Implements bcnp::ByteStream.

Definition at line 210 of file udp_posix.cpp.

Here is the call graph for this function:

◆ SendBytes()

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

Sends bytes to the current peer via UDP.

Parameters
dataPointer to byte buffer to send.
lengthNumber of bytes to send.
Returns
true if all bytes were sent, false if no peer or socket error.
Note
UDP does not guarantee delivery. Consider using TCP for reliable transport.

Implements bcnp::ByteWriter.

Definition at line 131 of file udp_posix.cpp.

◆ SendHandshake()

bool bcnp::UdpPosixAdapter::SendHandshake ( )

Sends the V3 protocol handshake to the current peer.

The handshake contains the protocol magic bytes and schema hash. Sent automatically as a response to receiving a valid pairing packet.

Returns
true if handshake was sent, false if no peer or socket error.

Definition at line 315 of file udp_posix.cpp.

Here is the call graph for this function:

◆ SetPairingToken()

void bcnp::UdpPosixAdapter::SetPairingToken ( uint32_t token)

Sets the expected pairing token for handshake validation.

Both peers must use the same token for pairing to succeed. Changing the token resets pairing state (unless fixed peer is configured).

Parameters
token32-bit pairing token (default is "BCNP" = 0x42434E50).

Definition at line 178 of file udp_posix.cpp.

◆ SetPeerLockMode()

void bcnp::UdpPosixAdapter::SetPeerLockMode ( bool locked)

Enables or disables peer locking.

When locked, the adapter only accepts packets from the initial peer. This provides security against spoofing in multi-device environments.

Parameters
lockedtrue to lock to current/next peer, false to accept any source.

Definition at line 151 of file udp_posix.cpp.

◆ UnlockPeer()

void bcnp::UdpPosixAdapter::UnlockPeer ( )

Resets pairing state to allow re-pairing with a new peer.

No-op if a fixed peer is configured (must recreate adapter to change).

Definition at line 191 of file udp_posix.cpp.

Here is the caller graph for this function:

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