载入中...
搜索中...
未找到
UdpSocket.cpp
浏览该文件的文档.
1#include "network/UdpSocket.h"
2#include "network/Network.h"
3#include "network/NetWorker.h"
4#include "data/ByteData.h"
5
6#include <Poco/Net/DatagramSocket.h>
7#include <Poco/Net/SocketAddress.h>
8#include <Poco/Exception.h>
9
10namespace eve::network {
11
12UdpSocket::UdpSocket(Network* net) : net_(net) {}
13
17
18Poco::Net::DatagramSocket* UdpSocket::datagram() {
19 return sock_.get();
20}
21
22bool UdpSocket::bind(uint16_t port) {
23 try {
24 sock_ = std::make_unique<Poco::Net::DatagramSocket>();
25 sock_->bind(Poco::Net::SocketAddress(port), true);
26 sock_->setBlocking(false);
27 sock_->setReceiveBufferSize(1024 * 1024);
28 sock_->setSendBufferSize(1024 * 1024);
29 bound_ = true;
30 if (net_) net_->watchUdp(this);
31 return true;
32 } catch (...) {
33 sock_.reset();
34 bound_ = false;
35 if (net_) {
38 c.kind = NetKind::Udp;
39 c.handle = this;
40 c.reason = "refused";
41 net_->post(std::move(c));
42 }
43 return false;
44 }
45}
46
47bool UdpSocket::connect(std::string host, uint16_t port) {
48 try {
49 if (!sock_) sock_ = std::make_unique<Poco::Net::DatagramSocket>();
50 sock_->connect(Poco::Net::SocketAddress(host, port));
51 sock_->setBlocking(false);
52 sock_->setReceiveBufferSize(1024 * 1024);
53 sock_->setSendBufferSize(1024 * 1024);
54 connected_ = true;
55 if (net_) net_->watchUdp(this);
56 return true;
57 } catch (...) {
58 if (net_) {
61 c.kind = NetKind::Udp;
62 c.handle = this;
63 c.reason = "refused";
64 net_->post(std::move(c));
65 }
66 return false;
67 }
68}
69
70bool UdpSocket::sendTo(eve::data::ByteData* data, std::string host, uint16_t port) {
71 if (!data || !sock_ || !net_ || !net_->worker()) return false;
72 size_t n = data->getSize();
73 auto buf = std::make_shared<std::vector<char>>(
74 static_cast<char*>(data->getData()),
75 static_cast<char*>(data->getData()) + n);
76 auto* self = this;
77 net_->worker()->submit([self, buf, host, port]() {
78 try {
79 if (!self->sock_) return;
80 self->sock_->sendTo(buf->data(), static_cast<int>(buf->size()),
81 Poco::Net::SocketAddress(host, port));
82 } catch (...) {
85 c.kind = NetKind::Udp;
86 c.handle = self;
87 c.reason = "closed";
88 self->net_->post(std::move(c));
89 }
90 });
91 return true;
92}
93
94bool UdpSocket::sendToString(std::string s, std::string host, uint16_t port) {
95 if (s.empty()) return false;
96 eve::data::ByteData data(s.data(), s.size());
97 return sendTo(&data, host, port);
98}
99
101 if (!data || !sock_ || !connected_ || !net_ || !net_->worker()) return false;
102 size_t n = data->getSize();
103 auto buf = std::make_shared<std::vector<char>>(
104 static_cast<char*>(data->getData()),
105 static_cast<char*>(data->getData()) + n);
106 auto* self = this;
107 net_->worker()->submit([self, buf]() {
108 try {
109 if (!self->sock_) return;
110 self->sock_->sendBytes(buf->data(), static_cast<int>(buf->size()));
111 } catch (...) {
114 c.kind = NetKind::Udp;
115 c.handle = self;
116 c.reason = "closed";
117 self->net_->post(std::move(c));
118 }
119 });
120 return true;
121}
122
123bool UdpSocket::sendString(std::string s) {
124 if (s.empty()) return false;
125 eve::data::ByteData data(s.data(), s.size());
126 return send(&data);
127}
128
130 if (net_) net_->unwatchUdp(this);
131 bound_ = false;
132 connected_ = false;
133 try {
134 if (sock_) sock_->close();
135 } catch (...) {
136 }
137 sock_.reset();
138}
139
140} // namespace eve::network
glm::vec3 n
Definition Grass.cpp:64
uint32_t c
Light2D::Data * data
uint32_t s
Definition Weather.cpp:28
In-memory byte buffer implementing eve::Data (ref-counted).
Definition ByteData.h:11
void submit(std::function< void()> job)
Queues an arbitrary blocking job to run on the worker thread.
Definition NetWorker.cpp:40
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:30
void unwatchUdp(UdpSocket *sock)
Definition Network.cpp:192
NetWorker * worker() const
Background worker thread handle (advanced).
Definition Network.h:73
void watchUdp(UdpSocket *sock)
Definition Network.cpp:186
void post(NetCompletion c)
Posts a completion to the worker queue (thread-safe).
Definition Network.cpp:167
bool sendTo(eve::data::ByteData *data, std::string host, uint16_t port)
Sends a datagram to an explicit host:port.
Definition UdpSocket.cpp:70
Poco::Net::DatagramSocket * datagram()
Internal: underlying Poco datagram socket.
Definition UdpSocket.cpp:18
UdpSocket(Network *net)
Creates an unbound socket owned by the given module.
Definition UdpSocket.cpp:12
bool sendToString(std::string s, std::string host, uint16_t port)
Sends a string datagram to an explicit host:port.
Definition UdpSocket.cpp:94
void close()
Closes the socket.
bool bind(uint16_t port)
Binds to a local port; true on success.
Definition UdpSocket.cpp:22
bool sendString(std::string s)
Sends a string datagram to the connected peer.
bool connect(std::string host, uint16_t port)
Connects to a remote host:port (restricts send()).
Definition UdpSocket.cpp:47
bool send(eve::data::ByteData *data)
Sends a datagram to the connected peer.
One asynchronous network result/event. handle points at the originating TcpSocket/UdpSocket/HttpReque...
Definition NetTypes.h:33