50ris::TcpCore::TcpCore(
const std::string& addr, uint16_t port,
bool server) {
54 this->server_ = server;
56 logstr =
"stream.TcpCore.";
60 logstr.append(
"Server.");
62 logstr.append(
"Client.");
63 logstr.append(std::to_string(port));
67 this->rxPool_ = std::make_shared<ris::Pool>();
70 this->pullAddr_ =
"tcp://";
71 this->pullAddr_.append(addr);
72 this->pullAddr_.append(
":");
73 this->pushAddr_ = this->pullAddr_;
75 this->zmqCtx_ = zmq_ctx_new();
76 this->zmqPull_ = zmq_socket(this->zmqCtx_, ZMQ_PULL);
77 this->zmqPush_ = zmq_socket(this->zmqCtx_, ZMQ_PUSH);
83 if (zmq_setsockopt(this->zmqPush_, ZMQ_IMMEDIATE, &opt,
sizeof(int32_t)) != 0)
87 if (zmq_setsockopt(this->zmqPush_, ZMQ_LINGER, &opt,
sizeof(int32_t)) != 0)
90 if (zmq_setsockopt(this->zmqPull_, ZMQ_LINGER, &opt,
sizeof(int32_t)) != 0)
94 if (zmq_setsockopt(this->zmqPull_, ZMQ_RCVTIMEO, &opt,
sizeof(int32_t)) != 0)
95 throw(
rogue::GeneralError(
"stream::TcpCore::TcpCore",
"Failed to set socket receive timeout"));
99 this->pullAddr_.append(std::to_string(
static_cast<int64_t
>(port)));
100 this->pushAddr_.append(std::to_string(
static_cast<int64_t
>(port + 1)));
102 this->bridgeLog_->debug(
"Creating pull server port: %s", this->pullAddr_.c_str());
104 if (zmq_bind(this->zmqPull_, this->pullAddr_.c_str()) < 0)
106 "Failed to bind server to port %" PRIu16
107 " at address %s, another process may be using this port",
111 this->bridgeLog_->debug(
"Creating push server port: %s", this->pushAddr_.c_str());
113 if (zmq_bind(this->zmqPush_, this->pushAddr_.c_str()) < 0)
115 "Failed to bind server to port %" PRIu16
116 " at address %s, another process may be using this port",
122 this->pullAddr_.append(std::to_string(
static_cast<int64_t
>(port + 1)));
123 this->pushAddr_.append(std::to_string(
static_cast<int64_t
>(port)));
125 this->bridgeLog_->debug(
"Creating pull client port: %s", this->pullAddr_.c_str());
127 if (zmq_connect(this->zmqPull_, this->pullAddr_.c_str()) < 0)
129 "Failed to connect to remote port %" PRIu16
" at address %s",
133 this->bridgeLog_->debug(
"Creating push client port: %s", this->pushAddr_.c_str());
135 if (zmq_connect(this->zmqPush_, this->pushAddr_.c_str()) < 0)
137 "Failed to connect to remote port %" PRIu16
" at address %s",
142 this->bridgeLog_->debug(
"TCP stream bridge ready. pull=%s push=%s",
143 this->pullAddr_.c_str(),
144 this->pushAddr_.c_str());
147 this->thread_ = std::make_unique<std::thread>(&ris::TcpCore::runThread,
this);
151 pthread_setname_np(thread_->native_handle(),
"TcpCore");
168 if (zmqPull_ !=
nullptr) {
172 if (zmqPush_ !=
nullptr) {
176 if (zmqCtx_ !=
nullptr) {
177 zmq_ctx_destroy(zmqCtx_);
220bool ris::TcpCore::rebuildPushSocket() {
221 if (this->zmqPush_ !=
nullptr) {
222 zmq_close(this->zmqPush_);
223 this->zmqPush_ =
nullptr;
226 this->zmqPush_ = zmq_socket(this->zmqCtx_, ZMQ_PUSH);
227 if (this->zmqPush_ ==
nullptr) {
228 bridgeLog_->error(
"Failed to create replacement push socket on %s: %s",
229 this->pushAddr_.c_str(),
230 zmq_strerror(zmq_errno()));
235 if (zmq_setsockopt(this->zmqPush_, ZMQ_IMMEDIATE, &opt,
sizeof(opt)) != 0) {
236 bridgeLog_->error(
"Failed to set ZMQ_IMMEDIATE on rebuilt push socket: %s",
237 zmq_strerror(zmq_errno()));
238 zmq_close(this->zmqPush_);
239 this->zmqPush_ =
nullptr;
244 if (zmq_setsockopt(this->zmqPush_, ZMQ_LINGER, &opt,
sizeof(opt)) != 0) {
245 bridgeLog_->error(
"Failed to set ZMQ_LINGER on rebuilt push socket: %s",
246 zmq_strerror(zmq_errno()));
247 zmq_close(this->zmqPush_);
248 this->zmqPush_ =
nullptr;
253 if (zmq_bind(this->zmqPush_, this->pushAddr_.c_str()) < 0) {
254 bridgeLog_->error(
"Failed to rebind push socket to %s: %s",
255 this->pushAddr_.c_str(),
256 zmq_strerror(zmq_errno()));
257 zmq_close(this->zmqPush_);
258 this->zmqPush_ =
nullptr;
262 if (zmq_connect(this->zmqPush_, this->pushAddr_.c_str()) < 0) {
263 bridgeLog_->error(
"Failed to reconnect push socket to %s: %s",
264 this->pushAddr_.c_str(),
265 zmq_strerror(zmq_errno()));
266 zmq_close(this->zmqPush_);
267 this->zmqPush_ =
nullptr;
286 std::lock_guard<std::mutex> lock(bridgeMtx_);
289 bridgeLog_->debug(
"Dropping frame on stopped bridge %s (payload=%" PRIu32
")",
290 this->pushAddr_.c_str(),
291 frame->getPayload());
296 if (this->zmqPush_ ==
nullptr) {
297 if (!rebuildPushSocket()) {
298 bridgeLog_->warning(
"Push socket unavailable on %s; dropping frame "
299 "(payload=%" PRIu32
")",
300 this->pushAddr_.c_str(),
301 frame->getPayload());
306 if (zmq_msg_init_size(&(msg[0]), 2) < 0) {
307 bridgeLog_->warning(
"Failed to init message header");
310 if (zmq_msg_init_size(&(msg[1]), 1) < 0) {
311 bridgeLog_->warning(
"Failed to init message header");
312 zmq_msg_close(&(msg[0]));
315 if (zmq_msg_init_size(&(msg[2]), 1) < 0) {
316 bridgeLog_->warning(
"Failed to init message header");
317 zmq_msg_close(&(msg[0]));
318 zmq_msg_close(&(msg[1]));
322 if (zmq_msg_init_size(&(msg[3]), frame->getPayload()) < 0) {
323 bridgeLog_->warning(
"Failed to init message with size %" PRIu32, frame->getPayload());
324 zmq_msg_close(&(msg[0]));
325 zmq_msg_close(&(msg[1]));
326 zmq_msg_close(&(msg[2]));
330 flags = frame->getFlags();
331 std::memcpy(zmq_msg_data(&(msg[0])), &flags, 2);
333 chan = frame->getChannel();
334 std::memcpy(zmq_msg_data(&(msg[1])), &chan, 1);
336 err = frame->getError();
337 std::memcpy(zmq_msg_data(&(msg[2])), &err, 1);
341 data =
reinterpret_cast<uint8_t*
>(zmq_msg_data(&(msg[3])));
344 bool sendFailed =
false;
345 for (x = 0; x < 4; x++) {
346 if (zmq_sendmsg(this->zmqPush_, &(msg[x]), (x == 3) ? 0 : ZMQ_SNDMORE) < 0) {
347 bridgeLog_->warning(
"Failed to push message part %" PRIu32
" (frame size %" PRIu32
") on %s: %s",
350 this->pushAddr_.c_str(),
351 zmq_strerror(zmq_errno()));
353 zmq_msg_close(&(msg[x]));
354 for (uint32_t y = x + 1; y < 4; y++) zmq_msg_close(&(msg[y]));
360 bridgeLog_->error(
"Multi-part frame failed mid-stream on part %" PRIu32
361 "; peer may have received a torso-only frame. "
362 "Resetting push socket to clear PUSH multipart FSM.",
365 if (!rebuildPushSocket()) {
366 bridgeLog_->error(
"Unable to recover TcpCore push socket on %s; "
367 "next acceptFrame() will retry the rebuild",
368 this->pushAddr_.c_str());
373 bridgeLog_->debug(
"Pushed TCP frame with size %" PRIu32
" on %s", frame->getPayload(), this->pushAddr_.c_str());
377void ris::TcpCore::runThread() {
390 bridgeLog_->logThreadId();
393 for (x = 0; x < 4; x++) zmq_msg_init(&(msg[x]));
400 if (zmq_recvmsg(this->zmqPull_, &(msg[x]), 0) >= 0) {
407 zmq_getsockopt(this->zmqPull_, ZMQ_RCVMORE, &more, &moreSize);
411 }
while (threadEn_ && more);
414 if (threadEn_ && (msgCnt == 4)) {
416 if ((zmq_msg_size(&(msg[0])) != 2) || (zmq_msg_size(&(msg[1])) != 1) || (zmq_msg_size(&(msg[2])) != 1)) {
418 "Bad message sizes. flags=%zu channel=%zu error=%zu",
419 zmq_msg_size(&(msg[0])),
420 zmq_msg_size(&(msg[1])),
421 zmq_msg_size(&(msg[2])));
422 for (x = 0; x < msgCnt; x++) zmq_msg_close(&(msg[x]));
427 std::memcpy(&flags, zmq_msg_data(&(msg[0])), 2);
428 std::memcpy(&chan, zmq_msg_data(&(msg[1])), 1);
429 std::memcpy(&err, zmq_msg_data(&(msg[2])), 1);
432 data =
reinterpret_cast<uint8_t*
>(zmq_msg_data(&(msg[3])));
433 size = zmq_msg_size(&(msg[3]));
438 frame = rxPool_->acceptReq(size,
false);
439 frame->setPayload(size);
446 frame->setFlags(flags);
447 frame->setChannel(chan);
448 frame->setError(err);
450 bridgeLog_->debug(
"Pulled frame with size %" PRIu32, frame->getPayload());
454 for (x = 0; x < msgCnt; x++) zmq_msg_close(&(msg[x]));
458void ris::TcpCore::setup_python() {
461#pragma GCC diagnostic push
462#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
463 bp::class_<ris::TcpCore, ris::TcpCorePtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>(
"TcpCore",
465 .def(
"close", &ris::TcpCore::close);
466#pragma GCC diagnostic pop
468 bp::implicitly_convertible<ris::TcpCorePtr, ris::MasterPtr>();
469 bp::implicitly_convertible<ris::TcpCorePtr, ris::SlavePtr>();