rogue
Loading...
Searching...
No Matches
Transport.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <memory>
22
23#include "rogue/GeneralError.h"
24#include "rogue/GilRelease.h"
25#include "rogue/Logging.h"
29
32
33#ifndef NO_PYTHON
34 #include <boost/python.hpp>
35namespace bp = boost::python;
36#endif
37
39rpp::TransportPtr rpp::Transport::create() {
40 rpp::TransportPtr r = std::make_shared<rpp::Transport>();
41 return (r);
42}
43
44void rpp::Transport::setup_python() {
45#ifndef NO_PYTHON
46
47 bp::class_<rpp::Transport, rpp::TransportPtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>("Transport",
48 bp::init<>());
49
50 bp::implicitly_convertible<rpp::TransportPtr, ris::MasterPtr>();
51 bp::implicitly_convertible<rpp::TransportPtr, ris::SlavePtr>();
52#endif
53}
54
56rpp::Transport::Transport() {}
57
59rpp::Transport::~Transport() {
60 stop();
61}
62
64void rpp::Transport::stop() {
65 // No-op if setController() never ran or the thread was already joined.
66 if (!thread_ || !thread_->joinable()) return;
67
68 threadEn_ = false;
69 // Release the GIL: worker may be mid-sendFrame to a Python slave.
71 // Expired controller means the worker has already exited its loop.
72 if (rpp::ControllerPtr cntl = cntl_.lock()) cntl->stopQueue();
73 thread_->join();
74}
75
77void rpp::Transport::setController(rpp::ControllerPtr cntl) {
78 // One-shot: re-assigning a joinable std::thread triggers std::terminate.
79 if (thread_) return;
80
81 // threadEn_ must precede thread ctor; rollback both on spawn failure.
82 std::weak_ptr<rpp::Controller> prevCntl = cntl_;
83 cntl_ = cntl;
84 threadEn_ = true;
85 try {
86 thread_ = std::make_unique<std::thread>(&rpp::Transport::runThread, this);
87 } catch (...) {
88 threadEn_ = false;
89 cntl_ = prevCntl;
90 throw;
91 }
92
93 // Set a thread name
94#ifndef __MACH__
95 pthread_setname_np(thread_->native_handle(), "PackTrans");
96#endif
97}
98
100void rpp::Transport::acceptFrame(ris::FramePtr frame) {
101 // Drop if the controller is gone (owning Core destroyed).
102 if (rpp::ControllerPtr cntl = cntl_.lock()) cntl->transportRx(frame);
103}
104
106void rpp::Transport::runThread() {
107 ris::FramePtr frame;
108 Logging log("packetizer.Transport");
109 log.logThreadId();
110
111 while (threadEn_) {
112 rpp::ControllerPtr cntl = cntl_.lock();
113 if (!cntl) break;
114 if ((frame = cntl->transportTx()) != NULL) sendFrame(frame);
115 }
116}
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
Structured Rogue logging helper.
Definition Logging.h:59
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::protocols::packetizer::Transport > TransportPtr
Definition Transport.h:120
std::shared_ptr< rogue::protocols::packetizer::Controller > ControllerPtr
Definition Controller.h:148