载入中...
搜索中...
未找到
UdpLink.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <map>
6#include <random>
7#include <string>
8#include <vector>
9
10namespace eve::network {
11
12class Network;
13class UdpSocket;
14
29class UdpLink {
30public:
31 enum class MsgType : uint8_t {
32 Reliable = 0,
33 Unreliable = 1,
35 };
36
38 std::function<void(MsgType, uint8_t channel, const char* data, size_t n)>;
39 using DisconnectHandler = std::function<void(const std::string& reason)>;
40
41 UdpLink(Network* net, UdpSocket* sock);
42 ~UdpLink();
43
44 // --- transport setup --------------------------------------------------
45 bool setRemote(std::string host, uint16_t port);
47 bool setRemoteString(const std::string& addr);
48 std::string peer() const { return remote_; }
49
50 // --- sending ----------------------------------------------------------
51 void send(MsgType type, uint8_t channel, const void* data, size_t n);
52 bool sendString(MsgType type, uint8_t channel, const std::string& s);
53
54 // --- main-thread pump hooks (called by Network::pump) -----------------
55 void onDatagram(const std::vector<char>& bytes, const std::string& from);
56 void pump(int64_t nowMs);
57
58 // --- callbacks & config ----------------------------------------------
59 void setMessageHandler(MessageHandler h) { onMessage_ = std::move(h); }
60 void setDisconnectHandler(DisconnectHandler h) { onDisconnect_ = std::move(h); }
61 void setLossRate(float rate); // 0..1, test hook: drop outbound datagrams
62 void setTimeoutMs(int ms) { timeoutMs_ = ms; }
63
64 bool isAlive() const { return alive_; }
65 uint32_t peerId() const { return peerId_; }
66 void setPeerId(uint32_t id) { peerId_ = id; }
67 size_t pendingReliable() const { return sendQueue_.size(); }
68 size_t pendingFragments() const { return fragments_.size(); }
69
70 // Max payload per UDP datagram (below typical MTU to avoid IP fragmentation).
71 static constexpr size_t kPayloadMTU = 1200;
72 static constexpr size_t kMaxMessage = 256 * 1024;
73
74private:
75 void sendDatagram(std::vector<char> pkt);
76 void sendAckNow();
77 void pruneAcked(uint32_t ack, uint32_t bits);
78 void deliver(MsgType type, uint8_t channel, std::vector<char> payload);
79 void handleData(uint8_t type, uint8_t channel, uint32_t seq,
80 const std::vector<char>& payload);
81 void noteReceived();
82
83 Network* net_ = nullptr;
84 UdpSocket* sock_ = nullptr;
85 std::string remote_;
86 std::string remoteHost_;
87 uint16_t remotePort_ = 0;
88 bool remoteSet_ = false;
89
90 // sender
91 uint32_t nextSeq_ = 0;
92 struct SendEntry {
93 std::vector<std::vector<char>> pkts; // one per datagram (fragments share a seq)
94 int64_t deadlineMs = 0;
95 int attempts = 0;
96 };
97 std::map<uint32_t, SendEntry> sendQueue_;
98
99 // receiver
100 uint32_t expectedReliable_ = 0;
101 std::map<uint32_t, std::vector<char>> outOfOrder_;
102 uint32_t expectedUnrelOrd_ = 0;
103 std::map<uint32_t, std::vector<char>> unrelOrdBuf_;
104 // Encoding "last contiguous received seq" = expectedReliable_ - 1, so the
105 // "nothing received yet" value wraps to 0xFFFFFFFF (never prune seq 0).
106 uint32_t ackSend_ = 0xFFFFFFFFu;
107 uint32_t ackBitsSend_ = 0;
108
109 // fragmentation
110 struct FragBuf {
111 uint32_t total = 0;
112 uint32_t received = 0;
113 size_t lastLen = 0;
114 std::vector<char> data;
115 std::vector<uint8_t> seen;
116 int64_t lastMs = 0;
117 };
118 std::map<uint32_t, FragBuf> fragments_;
119 uint32_t nextFragId_ = 0;
120
121 // liveness / config
122 int64_t lastRecvMs_ = 0;
123 int64_t lastSendMs_ = 0;
124 int64_t lastPingMs_ = 0;
125 int64_t lastAckMs_ = 0;
126 int timeoutMs_ = 10000;
127 int retryBaseMs_ = 100;
128 int maxAttempts_ = 10;
129 float lossRate_ = 0.f;
130 std::mt19937 rng_{0xE9E1};
131 bool alive_ = false;
132 bool disconnectNotified_ = false;
133
134 uint32_t peerId_ = 0;
135 MessageHandler onMessage_;
136 DisconnectHandler onDisconnect_;
137};
138
139} // namespace eve::network
std::string type
std::string id
glm::vec3 n
Definition Grass.cpp:64
int h
Light2D::Data * data
uint32_t s
Definition Weather.cpp:28
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:30
UDP socket backed by Poco::Net; supports connect/bind and datagram send.
Definition UdpSocket.h:24