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 // No-op if setController() never ran.
61 if (thread_) {
62 threadEn_ = false;
63 // Release the GIL: worker may be mid-sendFrame to a Python slave.
65 cntl_->stopQueue();
66 thread_->join();
67 }
68}
69
71void rpp::Transport::setController(rpp::ControllerPtr cntl) {
72 // One-shot: re-assigning a joinable std::thread triggers std::terminate.
73 if (thread_) return;
74
75 // threadEn_ must precede thread ctor; rollback both on spawn failure.
76 rpp::ControllerPtr prevCntl = cntl_;
77 cntl_ = cntl;
78 threadEn_ = true;
79 try {
80 thread_ = std::make_unique<std::thread>(&rpp::Transport::runThread, this);
81 } catch (...) {
82 threadEn_ = false;
83 cntl_ = prevCntl;
84 throw;
85 }
86
87 // Set a thread name
88#ifndef __MACH__
89 pthread_setname_np(thread_->native_handle(), "PackTrans");
90#endif
91}
92
94void rpp::Transport::acceptFrame(ris::FramePtr frame) {
95 cntl_->transportRx(frame);
96}
97
99void rpp::Transport::runThread() {
100 ris::FramePtr frame;
101 Logging log("packetizer.Transport");
102 log.logThreadId();
103
104 while (threadEn_) {
105 if ((frame = cntl_->transportTx()) != NULL) sendFrame(frame);
106 }
107}
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:113
std::shared_ptr< rogue::protocols::packetizer::Controller > ControllerPtr
Definition Controller.h:141