38 UnreliableOrdered = 2,
42 std::function<void(
MsgType, uint8_t channel,
const char* data,
size_t n)>;
49 bool setRemote(std::string
host, uint16_t port);
51 bool setRemoteString(
const std::string& addr);
52 std::string
peer()
const {
return remote_; }
55 void send(MsgType
type, uint8_t channel,
const void* data,
size_t n);
56 bool sendString(MsgType
type, uint8_t channel,
const std::string&
s);
59 void onDatagram(
const std::vector<char>&
bytes,
const std::string&
from);
60 void pump(int64_t nowMs);
65 void setLossRate(
float rate);
69 uint32_t
peerId()
const {
return peerId_; }
75 static constexpr size_t kPayloadMTU = 1200;
76 static constexpr size_t kMaxMessage = 256 * 1024;
79 void sendDatagram(std::vector<char> pkt);
81 void pruneAcked(uint32_t ack, uint32_t bits);
83 void handleData(uint8_t
type, uint8_t channel, uint32_t seq,
84 const std::vector<char>&
payload);
90 std::string remoteHost_;
91 uint16_t remotePort_ = 0;
92 bool remoteSet_ =
false;
95 uint32_t nextSeq_ = 0;
97 std::vector<std::vector<char>> pkts;
98 int64_t deadlineMs = 0;
101 std::map<uint32_t, SendEntry> sendQueue_;
104 uint32_t expectedReliable_ = 0;
105 std::map<uint32_t, std::vector<char>> outOfOrder_;
106 uint32_t expectedUnrelOrd_ = 0;
107 std::map<uint32_t, std::vector<char>> unrelOrdBuf_;
110 uint32_t ackSend_ = 0xFFFFFFFFu;
111 uint32_t ackBitsSend_ = 0;
116 uint32_t received = 0;
118 std::vector<char> data;
119 std::vector<uint8_t> seen;
122 std::map<uint32_t, FragBuf> fragments_;
123 uint32_t nextFragId_ = 0;
126 int64_t lastRecvMs_ = 0;
127 int64_t lastSendMs_ = 0;
128 int64_t lastPingMs_ = 0;
129 int64_t lastAckMs_ = 0;
130 int timeoutMs_ = 10000;
131 int retryBaseMs_ = 100;
132 int maxAttempts_ = 10;
133 float lossRate_ = 0.f;
134 std::mt19937 rng_{0xE9E1};
136 bool disconnectNotified_ =
false;
138 uint32_t peerId_ = 0;
139 MessageHandler onMessage_;
140 DisconnectHandler onDisconnect_;