BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
tcp_posix.h
Go to the documentation of this file.
1#pragma once
2
3#include "bcnp/packet.h"
6
7#include <chrono>
8#include <memory>
9#include <netinet/in.h>
10
11namespace bcnp {
12
23public:
30 explicit TcpPosixAdapter(uint16_t listenPort, const char* targetIp = nullptr, uint16_t targetPort = 0);
31 ~TcpPosixAdapter() override;
32
33 bool SendBytes(const uint8_t* data, std::size_t length) override;
34 std::size_t ReceiveChunk(uint8_t* buffer, std::size_t maxLength) override;
35
36 bool IsValid() const { return m_socket >= 0 || (!m_isServer && m_peerAddrValid); }
37 bool IsConnected() const { return m_isConnected && m_handshakeComplete; }
38
39 bool IsHandshakeComplete() const { return m_handshakeComplete && m_schemaValidated; }
40 bool SendHandshake();
41 uint32_t GetRemoteSchemaHash() const { return m_remoteSchemaHash; }
42
44 void SetExpectedSchemaHash(uint32_t hash) { m_expectedSchemaHash = hash; }
45
46private:
47 bool CreateBaseSocket();
48 bool ConfigureSocket(int sock);
49 void BeginClientConnect(bool forceImmediate);
50 void PollConnection();
51 void HandleConnectionLoss();
52 void TryFlushTxBuffer(int targetSock);
53 bool EnqueueTx(const uint8_t* data, std::size_t length);
54 void DropPendingTx();
55 void LogError(const char* message);
56 bool ProcessHandshake(const uint8_t* data, std::size_t length);
57 uint32_t GetExpectedSchemaHash() const;
58
59 int m_socket{-1};
60 int m_clientSocket{-1}; // For server mode, the connected client
61 bool m_isServer{false};
62 bool m_isConnected{false};
63 bool m_connectInProgress{false};
64 bool m_handshakeComplete{false};
65 bool m_handshakeSent{false};
66 bool m_schemaValidated{false};
67 uint32_t m_remoteSchemaHash{0};
68 uint32_t m_expectedSchemaHash{0}; // 0 = use kSchemaHash from generated header
69 sockaddr_in m_peerAddr{};
70 bool m_peerAddrValid{false};
71 std::chrono::steady_clock::time_point m_nextReconnectAttempt{};
72 std::chrono::steady_clock::time_point m_lastServerRx{};
73 std::chrono::milliseconds m_serverClientTimeout{5000}; // 5 second zombie timeout
74 // Max packet size: header + largest reasonable message payload + CRC
75 static constexpr std::size_t kMaxPacketSize = 65536;
76 static constexpr std::size_t kTxBufferCapacity = kMaxPacketSize * 8; // Real-time: limit buffering
77 std::unique_ptr<uint8_t[]> m_txBuffer;
78 std::size_t m_txHead{0};
79 std::size_t m_txTail{0};
80 std::size_t m_txSize{0};
81 std::chrono::steady_clock::time_point m_lastErrorLog{};
82
83 // Handshake receive buffer
84 uint8_t m_handshakeBuffer[kHandshakeSize]{};
85 std::size_t m_handshakeReceived{0};
86};
87
88} // namespace bcnp
Combined send/receive interface for bidirectional transports.
Definition adapter.h:43
TCP transport adapter for BCNP over POSIX sockets.
Definition tcp_posix.h:22
bool SendBytes(const uint8_t *data, std::size_t length) override
Sends bytes through the TCP connection.
bool IsConnected() const
Definition tcp_posix.h:37
uint32_t GetRemoteSchemaHash() const
Definition tcp_posix.h:41
bool IsValid() const
Definition tcp_posix.h:36
bool SendHandshake()
Sends the V3 protocol handshake to the peer.
~TcpPosixAdapter() override
Destructor. Closes all open sockets.
bool IsHandshakeComplete() const
Definition tcp_posix.h:39
std::size_t ReceiveChunk(uint8_t *buffer, std::size_t maxLength) override
Receives bytes from the TCP connection.
void SetExpectedSchemaHash(uint32_t hash)
Override expected schema hash (for testing with custom schemas)
Definition tcp_posix.h:44
TcpPosixAdapter(uint16_t listenPort, const char *targetIp=nullptr, uint16_t targetPort=0)
Construct TCP adapter.
Definition tcp_posix.cpp:54
constexpr std::size_t kHandshakeSize
BCNP packet structures, encoding, and decoding utilities.