载入中...
搜索中...
未找到
NetHost.h
浏览该文件的文档.
1#pragma once
2
3#include "network/UdpLink.h"
4
5#include <cstdint>
6#include <functional>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11namespace eve::network {
12
13class Network;
14class UdpSocket;
15
22class NetHost {
23public:
24 explicit NetHost(Network* net);
25 ~NetHost();
26
27 bool start(uint16_t port);
28 void setLossRate(float rate);
29 void setTimeoutMs(int ms);
30
31 void onDatagram(const std::vector<char>& bytes, const std::string& from);
32 void pump(int64_t nowMs);
33
34 UdpLink* linkByPeerId(uint32_t peerId) const;
35 size_t peerCount() const { return byId_.size(); }
36
37 void sendTo(uint32_t peerId, UdpLink::MsgType type, uint8_t channel,
38 const void* data, size_t n);
39 bool sendStringTo(uint32_t peerId, UdpLink::MsgType type, uint8_t channel,
40 const std::string& s);
41
42 using MessageHandler = std::function<void(uint32_t peerId, UdpLink::MsgType,
43 uint8_t channel, const char*, size_t)>;
44 using PeerHandler = std::function<void(uint32_t peerId)>;
45
46 void setMessageHandler(MessageHandler h) { onMessage_ = std::move(h); }
47 void setPeerConnectedHandler(PeerHandler h) { onConnect_ = std::move(h); }
48 void setPeerDisconnectedHandler(PeerHandler h) { onDisconnect_ = std::move(h); }
49
50private:
51 void emitPeerConnected(uint32_t peerId);
52 void emitPeerDisconnected(uint32_t peerId);
53
54 Network* net_ = nullptr;
55 UdpSocket* sock_ = nullptr;
56 std::unordered_map<std::string, UdpLink*> byAddr_;
57 std::unordered_map<uint32_t, UdpLink*> byId_;
58 std::vector<UdpLink*> owned_;
59 std::vector<uint32_t> pendingRemove_;
60 uint32_t nextPeerId_ = 1;
61 float lossRate_ = 0.f;
62 int timeoutMs_ = 10000;
63 MessageHandler onMessage_;
64 PeerHandler onConnect_;
65 PeerHandler onDisconnect_;
66};
67
68} // namespace eve::network
std::string type
glm::vec3 n
Definition Grass.cpp:64
int h
uint32_t s
Definition Weather.cpp:28
void setMessageHandler(MessageHandler h)
Definition NetHost.h:46
bool sendStringTo(uint32_t peerId, UdpLink::MsgType type, uint8_t channel, const std::string &s)
Definition NetHost.cpp:87
void setLossRate(float rate)
Definition NetHost.cpp:33
void onDatagram(const std::vector< char > &bytes, const std::string &from)
Definition NetHost.cpp:43
void pump(int64_t nowMs)
Definition NetHost.cpp:70
bool start(uint16_t port)
Definition NetHost.cpp:21
void sendTo(uint32_t peerId, UdpLink::MsgType type, uint8_t channel, const void *data, size_t n)
Definition NetHost.cpp:81
void setTimeoutMs(int ms)
Definition NetHost.cpp:38
std::function< void(uint32_t peerId, UdpLink::MsgType, uint8_t channel, const char *, size_t)> MessageHandler
Definition NetHost.h:43
std::function< void(uint32_t peerId)> PeerHandler
Definition NetHost.h:44
size_t peerCount() const
Definition NetHost.h:35
void setPeerConnectedHandler(PeerHandler h)
Definition NetHost.h:47
void setPeerDisconnectedHandler(PeerHandler h)
Definition NetHost.h:48
UdpLink * linkByPeerId(uint32_t peerId) const
Definition NetHost.cpp:76
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