载入中...
搜索中...
未找到
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
12// The socket and queued jobs share only this cancellation record. Access to the
13// observer is scoped by the mutex; close invalidates it before destruction.
19
20UdpSocket::UdpSocket(Network* net) : sendState_(std::make_shared<SendState>(this)), net_(net) {}
21
25
26Poco::Net::DatagramSocket* UdpSocket::datagram() {
27 return sock_.get();
28}
29
30bool UdpSocket::bind(uint16_t port) {
31 close();
32 sendState_ = std::make_shared<SendState>(this);
33 try {
34 sock_ = std::make_unique<Poco::Net::DatagramSocket>();
35 sock_->bind(Poco::Net::SocketAddress(port), true);
36 sock_->setBlocking(false);
37 sock_->setReceiveBufferSize(1024 * 1024);
38 sock_->setSendBufferSize(1024 * 1024);
39 bound_ = true;
40 if (net_) net_->watchUdp(this);
41 return true;
42 } catch (...) {
43 sock_.reset();
44 bound_ = false;
45 if (net_) {
48 c.kind = NetKind::Udp;
49 c.handle = this;
50 c.reason = "refused";
51 net_->post(std::move(c));
52 }
53 return false;
54 }
55}
56
57bool UdpSocket::connect(std::string host, uint16_t port) {
58 if (net_) net_->unwatchUdp(this);
59 {
60 std::lock_guard<std::mutex> lock(sendState_->mutex);
61 sendState_->socket = nullptr;
62 }
63 sendState_ = std::make_shared<SendState>(this);
64 try {
65 if (!sock_) sock_ = std::make_unique<Poco::Net::DatagramSocket>();
66 sock_->connect(Poco::Net::SocketAddress(host, port));
67 sock_->setBlocking(false);
68 sock_->setReceiveBufferSize(1024 * 1024);
69 sock_->setSendBufferSize(1024 * 1024);
70 connected_ = true;
71 if (net_) net_->watchUdp(this);
72 return true;
73 } catch (...) {
74 if (net_ && bound_ && sock_) net_->watchUdp(this);
75 if (net_) {
78 c.kind = NetKind::Udp;
79 c.handle = this;
80 c.reason = "refused";
81 net_->post(std::move(c));
82 }
83 return false;
84 }
85}
86
87bool UdpSocket::sendTo(eve::data::ByteData* data, std::string host, uint16_t port) {
88 if (!data || !sock_ || !net_ || !net_->worker()) return false;
89 size_t n = data->getSize();
90 auto buf = std::make_shared<std::vector<char>>(
91 static_cast<char*>(data->getData()),
92 static_cast<char*>(data->getData()) + n);
93 net_->worker()->submit([state = sendState_, buf, host, port]() {
94 std::lock_guard<std::mutex> lock(state->mutex);
95 auto* self = state->socket;
96 if (!self) return;
97 try {
98 if (!self->sock_) return;
99 if (self->connected_) {
100 self->sock_->sendBytes(buf->data(), static_cast<int>(buf->size()));
101 } else {
102 self->sock_->sendTo(buf->data(), static_cast<int>(buf->size()), Poco::Net::SocketAddress(host, port));
103 }
104 self->net_->recordSent(buf->size());
105 } catch (...) {
108 c.kind = NetKind::Udp;
109 c.handle = self;
110 c.reason = "closed";
111 self->net_->post(std::move(c));
112 }
113 });
114 return true;
115}
116
117bool UdpSocket::sendToString(std::string s, std::string host, uint16_t port) {
118 if (s.empty()) return false;
119 eve::data::ByteData data(s.data(), s.size());
120 return sendTo(&data, std::move(host), port);
121}
122
124 if (!data || !sock_ || !connected_ || !net_ || !net_->worker()) return false;
125 size_t n = data->getSize();
126 auto buf = std::make_shared<std::vector<char>>(
127 static_cast<char*>(data->getData()),
128 static_cast<char*>(data->getData()) + n);
129 net_->worker()->submit([state = sendState_, buf]() {
130 std::lock_guard<std::mutex> lock(state->mutex);
131 auto* self = state->socket;
132 if (!self) return;
133 try {
134 if (!self->sock_) return;
135 self->sock_->sendBytes(buf->data(), static_cast<int>(buf->size()));
136 self->net_->recordSent(buf->size());
137 } catch (...) {
140 c.kind = NetKind::Udp;
141 c.handle = self;
142 c.reason = "closed";
143 self->net_->post(std::move(c));
144 }
145 });
146 return true;
147}
148
149bool UdpSocket::sendString(std::string s) {
150 if (s.empty()) return false;
151 eve::data::ByteData data(s.data(), s.size());
152 return send(&data);
153}
154
156 if (net_) net_->unwatchUdp(this);
157 std::lock_guard<std::mutex> lock(sendState_->mutex);
158 sendState_->socket = nullptr;
159 bound_ = false;
160 connected_ = false;
161 try {
162 if (sock_) sock_->close();
163 } catch (...) {
164 }
165 sock_.reset();
166}
167
168} // namespace eve::network
const std::string & s
glm::vec3 n
Definition Grass.cpp:63
std::int32_t c
std::shared_ptr< OceanGpuSpectrum > owner
UIHostHandle host
In-memory byte buffer implementing eve::Data (ref-counted).
Definition ByteData.h:13
void submit(std::function< void()> job)
Queues an arbitrary blocking job to run on the worker thread.
Definition NetHost.cpp:434
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:39
UDP socket backed by Poco::Net; supports connect/bind and datagram send. @ownership The caller owns t...
Definition UdpSocket.h:30
bool sendTo(eve::data::ByteData *data, std::string host, uint16_t port)
Sends a datagram to an explicit host:port.
Definition UdpSocket.cpp:87
Poco::Net::DatagramSocket * datagram()
Internal: underlying Poco datagram socket.
Definition UdpSocket.cpp:26
UdpSocket(Network *net)
Creates an unbound socket owned by the given module.
Definition UdpSocket.cpp:20
bool sendToString(std::string s, std::string host, uint16_t port)
Sends a string datagram to an explicit host:port.
void close()
Closes the socket.
bool bind(uint16_t port)
Binds to a local port; true on success.
Definition UdpSocket.cpp:30
bool sendString(std::string s)
Sends a string datagram to the connected peer.
~UdpSocket()
Udp socket.
Definition UdpSocket.cpp:22
bool connect(std::string host, uint16_t port)
Connects to a remote host:port (restricts send()).
Definition UdpSocket.cpp:57
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