载入中...
搜索中...
未找到
Network.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Module.h"
4#include "NetTypes.h"
5
6#include <memory>
7#include <mutex>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12namespace eve::network {
13
14class TcpSocket;
15class UdpSocket;
16class HttpRequest;
17class Channel;
18class Session;
19class NetWorker;
20class NetWriter;
21class NetReader;
22class UdpLink;
23class NetHost;
24class NetRpc;
25
30class Network : public Module {
31public:
33 Network();
34 ~Network() override;
35
41 HttpRequest* newHttp(std::string method, std::string url);
49 NetReader* newReader(std::string bytes);
53 NetRpc* newRpc(UdpLink* link);
56
58 void setTimeout(int ms);
59 int getTimeout() const;
61 void setVerifySsl(bool verify);
62 bool getVerifySsl() const;
63
65 void pump();
66
68 void post(NetCompletion c);
70 void drainForTest(std::vector<NetCompletion>& out);
71
73 NetWorker* worker() const { return worker_.get(); }
74
76 void watchTcp(TcpSocket* sock);
77 void unwatchTcp(TcpSocket* sock);
78 void watchUdp(UdpSocket* sock);
79 void unwatchUdp(UdpSocket* sock);
81 void pollSockets(); // called from NetWorker thread
82
84 void bindChannel(TcpSocket* sock, Channel* ch);
85 void unbindChannel(TcpSocket* sock);
86 Channel* channelFor(TcpSocket* sock) const;
87
88 // Main-thread only (script calls + pump). The worker never touches these.
89 void bindUdpLink(UdpSocket* sock, UdpLink* link);
90 void unbindUdpLink(UdpSocket* sock);
91 void bindUdpHost(UdpSocket* sock, NetHost* host);
92 void unbindUdpHost(UdpSocket* sock);
93
94private:
95 void emitCompletion(const NetCompletion& c);
96
97 int timeoutMs_ = 10000;
98 bool verifySsl_ = true;
99 std::unique_ptr<NetWorker> worker_;
100
101 mutable std::mutex watchMu_;
102 std::vector<TcpSocket*> watchedTcp_;
103 std::vector<UdpSocket*> watchedUdp_;
104
105 mutable std::mutex channelMu_;
106 std::unordered_map<TcpSocket*, Channel*> channels_;
107
108 std::unordered_map<UdpSocket*, UdpLink*> udpLinks_;
109 std::unordered_map<UdpSocket*, NetHost*> udpHosts_;
110};
111
112} // namespace eve::network
uint32_t c
Length-prefixed (big-endian uint32) message framing over a TcpSocket. sendMsg() writes one framed mes...
Definition Channel.h:21
Asynchronous HTTP request (Poco-based). Configure headers/body, call submit(), then receive the respo...
Definition HttpRequest.h:24
Background thread that runs blocking socket I/O and HTTP jobs. Completions are queued and drained by ...
Definition NetWorker.h:20
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:30
NetHost * newHost()
Creates a UDP host (peer discovery / broadcast).
Definition Network.cpp:131
NetReader * newReader(std::string bytes)
Creates a streaming byte reader over a string buffer.
Definition Network.cpp:114
bool getVerifySsl() const
Definition Network.cpp:163
void setTimeout(int ms)
Default socket/HTTP timeout in milliseconds.
Definition Network.cpp:151
Session * newSession()
Creates a named-channel session container.
Definition Network.cpp:106
void watchTcp(TcpSocket *sock)
Internal: register/unregister sockets for pump polling.
Definition Network.cpp:175
void unbindUdpLink(UdpSocket *sock)
Definition Network.cpp:139
UdpLink * newUdpLink(UdpSocket *socket)
Creates a framed UDP link over a socket.
Definition Network.cpp:120
void unwatchUdp(UdpSocket *sock)
Definition Network.cpp:192
void setVerifySsl(bool verify)
Whether TLS peer certificates are verified (default true).
Definition Network.cpp:159
NetWorker * worker() const
Background worker thread handle (advanced).
Definition Network.h:73
void bindChannel(TcpSocket *sock, Channel *ch)
Internal: bind a Channel to its socket (and reverse lookup).
Definition Network.cpp:197
HttpRequest * newHttp(std::string method, std::string url)
Creates a new HTTP request (method e.g. "GET"/"POST", full URL).
Definition Network.cpp:95
void bindUdpLink(UdpSocket *sock, UdpLink *link)
Definition Network.cpp:135
int getTimeout() const
Definition Network.cpp:155
~Network() override
Definition Network.cpp:82
void watchUdp(UdpSocket *sock)
Definition Network.cpp:186
void unbindUdpHost(UdpSocket *sock)
Definition Network.cpp:147
Channel * channelFor(TcpSocket *sock) const
Definition Network.cpp:207
NetRpc * newRpc(UdpLink *link)
Creates an RPC client over a UDP link.
Definition Network.cpp:127
void pump()
Drains worker completions and emits them as events; call per frame.
Definition Network.cpp:363
NetWriter * newWriter()
Creates a streaming byte writer.
Definition Network.cpp:110
UdpSocket * newUdp()
Creates a new UDP socket.
Definition Network.cpp:91
void pollSockets()
Internal: polls watched sockets; called from the NetWorker thread.
Definition Network.cpp:213
void drainForTest(std::vector< NetCompletion > &out)
Test helper: drains pending completions into out.
Definition Network.cpp:171
TcpSocket * newTcp()
Creates a new TCP socket (client or server).
Definition Network.cpp:87
void unwatchTcp(TcpSocket *sock)
Definition Network.cpp:181
void post(NetCompletion c)
Posts a completion to the worker queue (thread-safe).
Definition Network.cpp:167
void unbindChannel(TcpSocket *sock)
Definition Network.cpp:202
Channel * newChannel(TcpSocket *socket)
Creates a length-prefixed Channel over a TCP socket.
Definition Network.cpp:99
void bindUdpHost(UdpSocket *sock, NetHost *host)
Definition Network.cpp:143
Named collection of Channels (a "session"). Lookup by name, close all. Does not own the channels; the...
Definition Session.h:14
TCP socket backed by Poco::Net; supports both client (connect) and server (listen/accept) roles....
Definition TcpSocket.h:28
UDP socket backed by Poco::Net; supports connect/bind and datagram send.
Definition UdpSocket.h:24
One asynchronous network result/event. handle points at the originating TcpSocket/UdpSocket/HttpReque...
Definition NetTypes.h:33