rogue
Loading...
Searching...
No Matches
Client.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <inttypes.h>
22#include <arpa/inet.h>
23#include <unistd.h>
24
25#include <cerrno>
26#include <chrono>
27#include <cstdlib>
28#include <cstring>
29#include <memory>
30#include <string>
31#include <thread>
32
33#include "rogue/GeneralError.h"
34#include "rogue/GilRelease.h"
35#include "rogue/Logging.h"
41
42namespace rpu = rogue::protocols::udp;
44
45#ifndef NO_PYTHON
46 #include <boost/python.hpp>
47namespace bp = boost::python;
48#endif
49
51rpu::ClientPtr rpu::Client::create(std::string host, uint16_t port, bool jumbo) {
52 rpu::ClientPtr r = std::make_shared<rpu::Client>(host, port, jumbo);
53 return (r);
54}
55
57rpu::Client::Client(std::string host, uint16_t port, bool jumbo) : rpu::Core(jumbo) {
58 struct addrinfo aiHints;
59 struct addrinfo* aiList = 0;
60 const sockaddr_in* addr;
61 int32_t val;
62 int32_t ret;
63 uint32_t size;
64
65 address_ = host;
66 port_ = port;
67 udpLog_ = rogue::Logging::create("udp.Client");
68
69 // Create a shared pointer to use as a lock for runThread()
70 std::shared_ptr<int> scopePtr = std::make_shared<int>(0);
71
72 // Create socket
73 if ((fd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
74 throw(rogue::GeneralError::create("Client::Client",
75 "Failed to create socket for port %" PRIu16 " at address %s",
76 port_,
77 address_.c_str()));
78
79 // Lookup host address
80 bzero(&aiHints, sizeof(aiHints));
81 aiHints.ai_flags = AI_CANONNAME;
82 aiHints.ai_family = AF_INET;
83 aiHints.ai_socktype = SOCK_DGRAM;
84 aiHints.ai_protocol = IPPROTO_UDP;
85
86 // POSIX leaves *aiList undefined on failure; do not freeaddrinfo() it.
87 if (::getaddrinfo(address_.c_str(), 0, &aiHints, &aiList) != 0 || aiList == nullptr) {
88 ::close(fd_);
89 fd_ = -1;
90 throw(rogue::GeneralError::create("Client::Client", "Failed to resolve address %s", address_.c_str()));
91 }
92
93 addr = (const sockaddr_in*)(aiList->ai_addr);
94
95 // Setup Remote Address
96 memset(&remAddr_, 0, sizeof(struct sockaddr_in));
97 ((struct sockaddr_in*)(&remAddr_))->sin_family = AF_INET;
98 ((struct sockaddr_in*)(&remAddr_))->sin_addr.s_addr = addr->sin_addr.s_addr;
99 ((struct sockaddr_in*)(&remAddr_))->sin_port = htons(port_);
100
101 ::freeaddrinfo(aiList);
102 aiList = nullptr;
103
104 // Fixed size buffer pool
106 setPoolSize(10000); // Initial value, 10K frames
107
108 udpLog_->debug("UDP client ready. remote=%s:%" PRIu16 ", maxPayload=%" PRIu32,
109 address_.c_str(),
110 port_,
111 maxPayload());
112
113 threadEn_ = true;
114 try {
115 thread_ = new std::thread(&rpu::Client::runThread, this, std::weak_ptr<int>(scopePtr));
116 } catch (...) {
117 threadEn_ = false;
118 ::close(fd_);
119 fd_ = -1;
120 throw;
121 }
122
123 // Set a thread name
124#ifndef __MACH__
125 pthread_setname_np(thread_->native_handle(), "UdpClient");
126#endif
127}
128
130rpu::Client::~Client() {
131 this->stop();
132}
133
134void rpu::Client::stop() {
135 if (threadEn_) {
136 threadEn_ = false;
137 // The worker never blocks for more than one select() timeout, so it
138 // can be joined before the socket is closed; closing first would let
139 // a reused descriptor number reach the worker's final syscalls.
140 rogue::GilRelease noGil;
141 thread_->join();
142 delete thread_;
143 thread_ = nullptr;
144 ::close(fd_);
145 fd_ = -1;
146 udpLog_->debug("Stopping UDP client for remote %s:%" PRIu16, address_.c_str(), port_);
147 }
148}
149
151void rpu::Client::acceptFrame(ris::FramePtr frame) {
152 ris::Frame::BufferIterator it;
153 int32_t res;
154 fd_set fds;
155 struct timeval tout;
156 uint32_t x;
157 struct msghdr msg;
158 struct iovec msg_iov[1];
159
160 // Setup message header
161 msg.msg_name = &remAddr_;
162 msg.msg_namelen = sizeof(struct sockaddr_in);
163 msg.msg_iov = msg_iov;
164 msg.msg_iovlen = 1;
165 msg.msg_control = NULL;
166 msg.msg_controllen = 0;
167 msg.msg_flags = 0;
168
169 rogue::GilRelease noGil;
170 ris::FrameLockPtr frLock = frame->lock();
171 std::lock_guard<std::mutex> lock(udpMtx_);
172
173 // Drop errored frames
174 if (frame->getError()) {
175 udpLog_->warning("Dropping errored outbound frame. remote=%s:%" PRIu16 ", error=0x%" PRIx8,
176 address_.c_str(),
177 port_,
178 frame->getError());
179 return;
180 }
181
182 // Go through each buffer in the frame
183 for (it = frame->beginBuffer(); it != frame->endBuffer(); ++it) {
184 if ((*it)->getPayload() == 0) break;
185
186 // Setup IOVs
187 msg_iov[0].iov_base = (*it)->begin();
188 msg_iov[0].iov_len = (*it)->getPayload();
189
190 // Keep trying since select call can fire
191 // but write fails because we did not win the (*it)er lock
192 do {
193 // Setup fds for select call
194 FD_ZERO(&fds);
195 FD_SET(fd_, &fds);
196
197 // Setup select timeout
198 tout = timeout_;
199
200 if (select(fd_ + 1, NULL, &fds, NULL, &tout) <= 0) {
201 udpLog_->critical("Client::acceptFrame: Timeout waiting for outbound transmit after %" PRIu32
202 ".%" PRIu32 " seconds! May be caused by outbound backpressure.",
203 timeout_.tv_sec,
204 timeout_.tv_usec);
205 res = 0;
206 } else if ((res = sendmsg(fd_, &msg, 0)) < 0) {
207 udpLog_->warning("UDP write call failed for %s: %s", address_.c_str(), std::strerror(errno));
208 }
209 } while (res == 0); // Continue while write result was zero
210 }
211}
212
214void rpu::Client::runThread(std::weak_ptr<int> lockPtr) {
215 ris::BufferPtr buff;
216 ris::FramePtr frame;
217 fd_set fds;
218 int32_t res;
219 struct timeval tout;
220 uint32_t avail;
221
222 // Wait until constructor completes
223 while (!lockPtr.expired()) continue;
224
225 udpLog_->logThreadId();
226
227 // The inherited Pool needs shared_from_this() to allocate, and the
228 // constructor starts this thread before a shared_ptr owns the endpoint.
229 // Datagrams arriving meanwhile queue in the socket.
230 while (threadEn_) {
231 try {
232 ris::Pool::shared_from_this();
233 break;
234 } catch (const std::bad_weak_ptr&) {
235 std::this_thread::sleep_for(std::chrono::milliseconds(1));
236 }
237 }
238 if (!threadEn_) return;
239
240 // Preallocate frame
241 frame = reqLocalFrame(maxPayload(), false);
242
243 while (threadEn_) {
244 // Attempt receive
245 buff = *(frame->beginBuffer());
246 avail = buff->getAvailable();
247 res = recvfrom(fd_, buff->begin(), avail, MSG_TRUNC | MSG_DONTWAIT, NULL, 0);
248
249 if (res > 0) {
250 // Message was too big
251 if (res > avail) {
252 udpLog_->warning("Receive data was too large. remote=%s:%" PRIu16 ", rx=%i, avail=%" PRIu32
253 ". Dropping.",
254 address_.c_str(),
255 port_,
256 res,
257 avail);
258 } else {
259 buff->setPayload(res);
260 sendFrame(frame);
261 }
262
263 // Get new frame
264 frame = reqLocalFrame(maxPayload(), false);
265 } else {
266 // No datagram pending. Wait for one with a bounded timeout so the
267 // loop re-checks threadEn_ and stop() can join without traffic.
268 FD_ZERO(&fds);
269 FD_SET(fd_, &fds);
270
271 tout.tv_sec = 0;
272 tout.tv_usec = 100000;
273
274 select(fd_ + 1, &fds, NULL, NULL, &tout);
275 }
276 }
277}
278
279void rpu::Client::setup_python() {
280#ifndef NO_PYTHON
281
282 bp::class_<rpu::Client, rpu::ClientPtr, bp::bases<rpu::Core, ris::Master, ris::Slave>, boost::noncopyable>(
283 "Client",
284 bp::init<std::string, uint16_t, bool>());
285
286 bp::implicitly_convertible<rpu::ClientPtr, rpu::CorePtr>();
287 bp::implicitly_convertible<rpu::ClientPtr, ris::MasterPtr>();
288 bp::implicitly_convertible<rpu::ClientPtr, ris::SlavePtr>();
289#endif
290}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
void setFixedSize(uint32_t size)
Sets fixed-size mode.
Definition Pool.cpp:116
void setPoolSize(uint32_t size)
Sets buffer pool size.
Definition Pool.cpp:137
Shared UDP transport base for stream client/server endpoints.
Definition Core.h:64
std::atomic< bool > threadEn_
Definition Core.h:81
std::shared_ptr< rogue::Logging > udpLog_
Definition Core.h:66
struct sockaddr_in remAddr_
Definition Core.h:75
uint32_t maxPayload()
Returns maximum UDP payload size in bytes.
Definition Core.cpp:45
std::thread * thread_
Definition Core.h:80
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
Definition Buffer.h:270
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
Definition FrameLock.h:110
std::shared_ptr< rogue::protocols::udp::Client > ClientPtr
Definition Client.h:124