BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
udp_posix.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <chrono>
7#include <cstdint>
8#include <netinet/in.h>
9
10namespace bcnp {
11
21public:
28 explicit UdpPosixAdapter(uint16_t listenPort, const char* targetIp = nullptr, uint16_t targetPort = 0);
29 ~UdpPosixAdapter() override;
30
31 bool SendBytes(const uint8_t* data, std::size_t length) override;
32 std::size_t ReceiveChunk(uint8_t* buffer, std::size_t maxLength) override;
33
34 bool IsValid() const { return m_socket >= 0; }
35
36 // Peer security: when true, locks to initial peer and ignores other sources
37 void SetPeerLockMode(bool locked);
38 void SetPairingToken(uint32_t token);
39 void UnlockPeer();
40
41 // V3 Schema handshake
42 bool IsHandshakeComplete() const { return m_pairingComplete && m_schemaValidated; }
43 bool SendHandshake(); // Send schema hash to peer
44 uint32_t GetRemoteSchemaHash() const { return m_remoteSchemaHash; }
45
46private:
47 bool ProcessPairingPacket(const uint8_t* buffer, std::size_t length, const sockaddr_in& src);
48
49 int m_socket{-1};
50 sockaddr_in m_bind{};
51 sockaddr_in m_lastPeer{};
52 bool m_hasPeer{false};
53 bool m_peerLocked{false};
54 bool m_pairingComplete{false};
55 bool m_schemaValidated{false};
56 bool m_requirePairing{false};
57 bool m_fixedPeerConfigured{false};
58 uint32_t m_pairingToken{0x42434E50U};
59 uint32_t m_remoteSchemaHash{0};
60 sockaddr_in m_initialPeer{};
61 std::chrono::steady_clock::time_point m_lastPeerRx{};
62 static constexpr std::chrono::milliseconds kPeerTimeout{5000};
63};
64
65} // namespace bcnp
Combined send/receive interface for bidirectional transports.
Definition adapter.h:43
UDP transport adapter for BCNP over POSIX sockets.
Definition udp_posix.h:20
~UdpPosixAdapter() override
Destructor. Closes the socket.
UdpPosixAdapter(uint16_t listenPort, const char *targetIp=nullptr, uint16_t targetPort=0)
Construct UDP adapter.
Definition udp_posix.cpp:64
std::size_t ReceiveChunk(uint8_t *buffer, std::size_t maxLength) override
Receives a UDP datagram.
void SetPairingToken(uint32_t token)
Sets the expected pairing token for handshake validation.
bool SendHandshake()
Sends the V3 protocol handshake to the current peer.
bool SendBytes(const uint8_t *data, std::size_t length) override
Sends bytes to the current peer via UDP.
bool IsValid() const
Definition udp_posix.h:34
uint32_t GetRemoteSchemaHash() const
Definition udp_posix.h:44
bool IsHandshakeComplete() const
Definition udp_posix.h:42
void UnlockPeer()
Resets pairing state to allow re-pairing with a new peer.
void SetPeerLockMode(bool locked)
Enables or disables peer locking.