载入中...
搜索中...
未找到
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 Channel;
23class Session;
24class NetWorker;
25class NetWriter;
26class NetReader;
27class UdpLink;
28class NetHost;
29class NetRpc;
30
36public:
39 Network();
41 ~Network() override;
42
44 std::unique_ptr<TcpSocket> makeTcp();
46 std::unique_ptr<UdpSocket> makeUdp();
48 std::unique_ptr<HttpRequest> makeHttp(std::string method, std::string url);
50 std::unique_ptr<Channel> makeChannel(TcpSocket* socket);
52 std::unique_ptr<Session> makeSession();
54 std::unique_ptr<NetWriter> makeWriter();
56 std::unique_ptr<NetReader> makeReader(std::string bytes);
58 std::unique_ptr<UdpLink> makeUdpLink(UdpSocket* socket);
60 std::unique_ptr<NetRpc> makeRpc(UdpLink* link);
62 std::unique_ptr<NetHost> makeHost();
63
65 TcpSocket* newTcp();
67 UdpSocket* newUdp();
69 HttpRequest* newHttp(std::string method, std::string url);
71 Channel* newChannel(TcpSocket* socket);
73 Session* newSession();
75 NetWriter* newWriter();
77 NetReader* newReader(std::string bytes);
79 UdpLink* newUdpLink(UdpSocket* socket);
81 NetRpc* newRpc(UdpLink* link);
83 NetHost* newHost();
84
86 void setTimeout(int ms);
88 int getTimeout() const;
90 void setVerifySsl(bool verify);
92 bool getVerifySsl() const;
93
95 NetTelemetrySnapshot telemetrySnapshot() const;
97 void resetTelemetry();
98
100 void pump();
101
103 bool httpRequest(const std::string& method, const std::string& url,
104 const std::string& body, int timeoutMs, int& status,
105 std::string& responseBody) override;
106
107private:
108 friend class Channel;
109 friend class HttpRequest;
110 friend class NetHost;
111 friend class NetWorker;
112 friend class TcpSocket;
113 friend class UdpSocket;
114
116 void post(NetCompletion c);
118 void recordSent(size_t bytes);
120 void drainCompletions(std::vector<NetCompletion>& out);
122 NetWorker* worker() const { return worker_.get(); }
123
125 void watchTcp(TcpSocket* sock);
126 void unwatchTcp(TcpSocket* sock);
127 void watchUdp(UdpSocket* sock);
128 void unwatchUdp(UdpSocket* sock);
130 void pollSockets();
131
133 void bindChannel(TcpSocket* sock, Channel* ch);
134 void unbindChannel(TcpSocket* sock);
135 Channel* channelFor(TcpSocket* sock) const;
136
138 void bindUdpLink(UdpSocket* sock, UdpLink* link);
139 void unbindUdpLink(UdpSocket* sock);
140 void bindUdpHost(UdpSocket* sock, NetHost* host);
141 void unbindUdpHost(UdpSocket* sock);
142
143 void emitCompletion(const NetCompletion& c);
144
145 int timeoutMs_ = 10000;
146 bool verifySsl_ = true;
147 std::unique_ptr<NetWorker> worker_;
148
149 mutable std::mutex watchMu_;
150 std::vector<TcpSocket*> watchedTcp_;
151 std::vector<UdpSocket*> watchedUdp_;
152
153 mutable std::mutex channelMu_;
154 std::unordered_map<TcpSocket*, Channel*> channels_;
155
156 std::unordered_map<UdpSocket*, UdpLink*> udpLinks_;
157 std::unordered_map<UdpSocket*, NetHost*> udpHosts_;
158 std::atomic<uint64_t> telemetryRevision_{1};
159 std::atomic<uint64_t> sentBytes_{0}, receivedBytes_{0}, completions_{0};
160 std::atomic<uint64_t> errors_{0}, connections_{0};
161};
162
163} // namespace eve::network
std::uint16_t method
#define EVENGINE_API_PLATFORM
Definition Export.h:107
wgpu::PopErrorScopeStatus status
std::int32_t c
std::uint64_t bytes
UIHostHandle host
std::string body
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
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:26
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:35
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.
Definition UdpSocket.h:26
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