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