载入中...
搜索中...
未找到
Network.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/Capability.h"
6#include "common/Module.h"
8#include "NetTypes.h"
9
10#include <memory>
11#include <mutex>
12#include <atomic>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17namespace eve::network {
18
19class TcpSocket;
20class UdpSocket;
21class HttpRequest;
22class HttpRuntime;
23class Channel;
24class Session;
25class NetWorker;
26class NetWriter;
27class NetReader;
28class UdpLink;
29class NetHost;
30class NetRpc;
31
40public:
43 Network();
48 ~Network() override;
49
51 std::unique_ptr<TcpSocket> makeTcp();
53 std::unique_ptr<UdpSocket> makeUdp();
55 std::unique_ptr<HttpRequest> makeHttp(std::string method, std::string url);
57 std::unique_ptr<Channel> makeChannel(TcpSocket* socket);
59 std::unique_ptr<Session> makeSession();
61 std::unique_ptr<NetWriter> makeWriter();
63 std::unique_ptr<NetReader> makeReader(std::string bytes);
65 std::unique_ptr<UdpLink> makeUdpLink(UdpSocket* socket);
67 std::unique_ptr<NetRpc> makeRpc(UdpLink* link);
69 std::unique_ptr<NetHost> makeHost();
70
72 TcpSocket* newTcp();
74 UdpSocket* newUdp();
76 HttpRequest* newHttp(std::string method, std::string url);
78 Channel* newChannel(TcpSocket* socket);
80 Session* newSession();
82 NetWriter* newWriter();
84 NetReader* newReader(std::string bytes);
86 UdpLink* newUdpLink(UdpSocket* socket);
88 NetRpc* newRpc(UdpLink* link);
90 NetHost* newHost();
91
93 void setTimeout(int ms);
95 int getTimeout() const;
97 void setVerifySsl(bool verify);
99 bool getVerifySsl() const;
105 void setCaLocation(std::string location);
106
108 NetTelemetrySnapshot telemetrySnapshot() const;
110 void resetTelemetry();
111
113 void pump();
114
119 bool httpRequest(const std::string& method, const std::string& url,
120 const std::string& body, int timeoutMs, int& status,
121 std::string& responseBody) override;
122
123private:
124 friend class Channel;
125 friend class HttpRequest;
126 friend class NetHost;
127 friend class NetWorker;
128 friend class TcpSocket;
129 friend class UdpSocket;
130
132 void post(NetCompletion c);
134 void recordSent(size_t bytes);
135 void recordCompletion(const NetCompletion& c);
137 void drainCompletions(std::vector<NetCompletion>& out);
139 NetWorker* worker() const { return worker_.get(); }
140
142 void watchTcp(TcpSocket* sock);
143 void unwatchTcp(TcpSocket* sock);
144 void watchUdp(UdpSocket* sock);
145 void unwatchUdp(UdpSocket* sock);
147 void pollSockets();
148
150 void bindChannel(TcpSocket* sock, Channel* ch);
151 void unbindChannel(TcpSocket* sock);
152 Channel* channelFor(TcpSocket* sock) const;
153
155 void bindUdpLink(UdpSocket* sock, UdpLink* link);
156 void unbindUdpLink(UdpSocket* sock);
157 void bindUdpHost(UdpSocket* sock, NetHost* host);
158 void unbindUdpHost(UdpSocket* sock);
159
160 void emitCompletion(const NetCompletion& c);
161
162 std::shared_ptr<HttpRuntime> http_;
163 std::unique_ptr<NetWorker> worker_;
164
165 mutable std::mutex watchMu_;
166 std::vector<TcpSocket*> watchedTcp_;
167 std::vector<UdpSocket*> watchedUdp_;
168
169 mutable std::mutex channelMu_;
170 std::unordered_map<TcpSocket*, Channel*> channels_;
171
172 std::unordered_map<UdpSocket*, UdpLink*> udpLinks_;
173 std::unordered_map<UdpSocket*, NetHost*> udpHosts_;
174 std::atomic<uint64_t> telemetryRevision_{1};
175 std::atomic<uint64_t> sentBytes_{0}, receivedBytes_{0}, completions_{0};
176 std::atomic<uint64_t> errors_{0}, connections_{0};
177};
178
179} // namespace eve::network
std::uint16_t method
#define EVENGINE_API_PLATFORM
Definition Export.h:107
wgpu::PopErrorScopeStatus status
std::int32_t c
PhysicsJointLink link
std::uint64_t bytes
std::string body
ScriptStructure out
UIHostHandle host
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Length-prefixed (big-endian uint32) message framing over a TcpSocket. sendMsg() writes one framed mes...
Definition Channel.h:23
Asynchronous HTTP request (Poco-based). Configure headers/body, call submit(), then receive the respo...
Definition HttpRequest.h:33
EVENGINE_API_PLATFORM public API.
Definition NetHost.h:25
EVENGINE_API_PLATFORM public API.
Definition NetStream.h:71
EVENGINE_API_PLATFORM public API.
Definition NetRpc.h:26
Background thread that runs blocking socket I/O and HTTP jobs. Completions are queued and drained by ...
Definition NetWorker.h:22
EVENGINE_API_PLATFORM public API.
Definition NetStream.h:20
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:39
Named collection of Channels (a "session"). Lookup by name, close all. Does not own the channels; the...
Definition Session.h:16
TCP socket backed by Poco::Net; supports both client (connect) and server (listen/accept) roles....
Definition TcpSocket.h:30
UDP socket backed by Poco::Net; supports connect/bind and datagram send. @ownership The caller owns t...
Definition UdpSocket.h:30
Minimal synchronous HTTP request surface. Providers: eve::network::Network. Mocks return canned statu...
One asynchronous network result/event. handle points at the originating TcpSocket/UdpSocket/HttpReque...
Definition NetTypes.h:33
Copied aggregate counters for editor/profiler network inspection.
Definition NetTypes.h:44