47rpr::ControllerPtr rpr::Controller::create(uint32_t segSize,
48 rpr::TransportPtr tran,
49 rpr::ApplicationPtr app,
51 rpr::ControllerPtr r = std::make_shared<rpr::Controller>(segSize, tran, app, server);
56rpr::Controller::Controller(uint32_t segSize, rpr::TransportPtr tran, rpr::ApplicationPtr app,
bool server) {
64 appQueue_.setThold(2);
76 gettimeofday(&stTime_, NULL);
83 gettimeofday(&txTime_, NULL);
86 locMaxSegment_ = segSize;
94 curMaxSegment_ = segSize;
101 locConnId_ = 0x12345678;
104 convTime(tryPeriodD1_, locTryPeriod_);
105 convTime(tryPeriodD4_, locTryPeriod_ / 4);
106 convTime(retranToutD1_, curRetranTout_);
107 convTime(nullToutD3_, curNullTout_ / 3);
108 convTime(cumAckToutD1_, curCumAckTout_);
109 convTime(cumAckToutD2_, curCumAckTout_ / 2);
111 memset(&zeroTme_, 0,
sizeof(
struct timeval));
124rpr::Controller::~Controller() {
129void rpr::Controller::stopQueue() {
134void rpr::Controller::stop() {
135 if (thread_ != NULL) {
146void rpr::Controller::start() {
147 if (thread_ == NULL) {
150 thread_ =
new std::thread(&rpr::Controller::runThread,
this);
154 pthread_setname_np(thread_->native_handle(),
"RssiControler");
168 nSize = size + rpr::Header::HeaderSize;
169 if (nSize > curMaxSegment_ && curMaxSegment_ > 0) nSize = curMaxSegment_;
170 if (nSize > locMaxSegment_) nSize = locMaxSegment_;
173 frame = tran_->reqFrame(nSize,
false);
174 buffer = *(frame->beginBuffer());
177 if (buffer->getAvailable() < rpr::Header::HeaderSize)
179 "Buffer size %" PRIu32
" is less than min header size %" PRIu32,
180 buffer->getAvailable(),
181 rpr::Header::HeaderSize));
184 buffer->adjustHeader(rpr::Header::HeaderSize);
187 frame = ris::Frame::create();
188 frame->appendBuffer(buffer);
196 std::map<uint8_t, rpr::HeaderPtr>::iterator it;
198 rpr::HeaderPtr head = rpr::Header::create(frame);
203 if (frame->getError() || frame->isEmpty() || !head->verify()) {
204 log_->warning(
"Dumping bad frame state=%" PRIu32
" server=%d", state_, server_);
209 log_->debug(
"RX frame: state=%" PRIu32
" server=%d size=%" PRIu32
" syn=%d ack=%d"
210 " nul=%d, bsy=%d, rst=%d, ack#=%" PRIu8
" seq=%" PRIu8
", nxt=%" PRIu8,
224 if (head->ack && (head->acknowledge != lastAckRx_)) {
226 std::vector<rpr::HeaderPtr> acked;
229 std::unique_lock<std::mutex> lock(txMtx_);
236 uint8_t pending =
static_cast<uint8_t
>(locSequence_ - lastAckRx_ - 1);
237 uint8_t delta =
static_cast<uint8_t
>(head->acknowledge - lastAckRx_);
239 if (delta <= pending) {
241 acked.push_back(std::move(txList_[++lastAckRx_]));
242 if (txListCount_ != 0) txListCount_--;
243 }
while (lastAckRx_ != head->acknowledge);
245 log_->warning(
"Ignoring out of window ack=%" PRIu8
" lastAckRx=%" PRIu8
" locSeq=%" PRIu8
" server=%d",
253 releaseHeaders(acked);
257 if (!remBusy_ && head->busy) remBusyCnt_++;
260 remBusy_ = head->busy;
264 if (state_ == StOpen || state_ == StWaitSyn) {
270 }
else if (head->syn) {
271 if (state_ == StOpen || state_ == StWaitSyn) {
272 lastSeqRx_ = head->sequence;
273 nextSeqRx_ = lastSeqRx_ + 1;
278 }
else if (state_ == StOpen && (head->nul || frame->getPayload() > rpr::Header::HeaderSize)) {
279 if (head->sequence == nextSeqRx_) {
283 lastSeqRx_ = nextSeqRx_;
284 nextSeqRx_ = nextSeqRx_ + 1;
285 appQueue_.push(head);
288 if (!oooQueue_.empty()) {
290 if ((it = oooQueue_.find(head->sequence)) != oooQueue_.end()) {
291 log_->warning(
"Removed duplicate frame. server=%d, head->sequence=%" PRIu8
292 ", next sequence=%" PRIu8,
303 while ((it = oooQueue_.find(nextSeqRx_)) != oooQueue_.end()) {
304 lastSeqRx_ = nextSeqRx_;
305 nextSeqRx_ = nextSeqRx_ + 1;
307 appQueue_.push(it->second);
308 log_->info(
"Using frame from ooo queue. server=%d, head->sequence=%" PRIu8,
310 (it->second)->sequence);
316 stCond_.notify_all();
319 }
else if ((it = oooQueue_.find(head->sequence)) != oooQueue_.end()) {
320 log_->warning(
"Dropped duplicate frame. server=%d, head->sequence=%" PRIu8
321 ", next sequence=%" PRIu8,
331 uint8_t x = nextSeqRx_;
332 uint8_t windowEnd = (nextSeqRx_ + curMaxBuffers_ + 1);
334 while (++x != windowEnd) {
335 if (head->sequence == x) {
336 oooQueue_.insert(std::make_pair(head->sequence, head));
337 log_->info(
"Adding frame to ooo queue. server=%d, head->sequence=%" PRIu8
338 ", nextSeqRx_=%" PRIu8
", windowsEnd=%" PRIu8,
347 if (x == windowEnd) {
348 log_->warning(
"Dropping out of window frame. server=%d, head->sequence=%" PRIu8
349 ", nextSeqRx_=%" PRIu8
", windowsEnd=%" PRIu8,
369 if ((head = appQueue_.pop()) == NULL)
return (frame);
370 stCond_.notify_all();
372 frame = head->getFrame();
375 ackSeqRx_ = head->sequence;
382 (*(frame->beginBuffer()))->adjustHeader(rpr::Header::HeaderSize);
392 struct timeval startTime;
394 gettimeofday(&startTime, NULL);
399 if (frame->isEmpty()) {
400 log_->warning(
"Dropping empty application frame");
404 if (frame->getError()) {
405 log_->warning(
"Dropping errored application frame");
410 (*(frame->beginBuffer()))->adjustHeader(-rpr::Header::HeaderSize);
413 rpr::HeaderPtr head = rpr::Header::create(frame);
418 if (state_ != StOpen) {
423 while (txListCount_ >= curMaxBuffers_) {
425 if (timePassed(startTime, timeout_)) {
426 gettimeofday(&startTime, NULL);
427 log_->critical(
"Controller::applicationRx: Timeout waiting for outbound queue after %" PRIu32
".%" PRIu32
428 " seconds! May be caused by outbound backpressure.",
435 transportTx(head,
true,
false);
436 stCond_.notify_all();
440bool rpr::Controller::getOpen() {
441 return (state_ == StOpen);
445uint32_t rpr::Controller::getDownCount() {
450uint32_t rpr::Controller::getDropCount() {
455uint32_t rpr::Controller::getRetranCount() {
456 return (retranCount_);
460bool rpr::Controller::getLocBusy() {
461 bool queueBusy = appQueue_.busy();
462 if (!locBusy_ && queueBusy) locBusyCnt_++;
463 locBusy_ = queueBusy;
468uint32_t rpr::Controller::getLocBusyCnt() {
469 return (locBusyCnt_);
473bool rpr::Controller::getRemBusy() {
478uint32_t rpr::Controller::getRemBusyCnt() {
479 return (remBusyCnt_);
482void rpr::Controller::setLocTryPeriod(uint32_t val) {
485 "Invalid LocTryPeriod Value = %" PRIu32,
488 convTime(tryPeriodD1_, locTryPeriod_);
489 convTime(tryPeriodD4_, locTryPeriod_ / 4);
492uint32_t rpr::Controller::getLocTryPeriod() {
493 return locTryPeriod_;
496void rpr::Controller::setLocMaxBuffers(uint8_t val) {
499 "Invalid LocMaxBuffers Value = %" PRIu8,
502 locMaxBuffers_ = val;
505uint8_t rpr::Controller::getLocMaxBuffers() {
506 return locMaxBuffers_;
509void rpr::Controller::setLocMaxSegment(uint16_t val) {
512 "Invalid LocMaxSegment Value = %" PRIu16,
514 locMaxSegment_ = val;
517uint16_t rpr::Controller::getLocMaxSegment() {
518 return locMaxSegment_;
521void rpr::Controller::setLocCumAckTout(uint16_t val) {
524 "Invalid LocCumAckTout Value = %" PRIu16,
526 locCumAckTout_ = val;
529uint16_t rpr::Controller::getLocCumAckTout() {
530 return locCumAckTout_;
533void rpr::Controller::setLocRetranTout(uint16_t val) {
536 "Invalid LocRetranTout Value = %" PRIu16,
538 locRetranTout_ = val;
541uint16_t rpr::Controller::getLocRetranTout() {
542 return locRetranTout_;
545void rpr::Controller::setLocNullTout(uint16_t val) {
548 "Invalid LocNullTout Value = %" PRIu16,
553uint16_t rpr::Controller::getLocNullTout() {
557void rpr::Controller::setLocMaxRetran(uint8_t val) {
560 "Invalid LocMaxRetran Value = %" PRIu8,
565uint8_t rpr::Controller::getLocMaxRetran() {
566 return locMaxRetran_;
569void rpr::Controller::setLocMaxCumAck(uint8_t val) {
575uint8_t rpr::Controller::getLocMaxCumAck() {
576 return locMaxCumAck_;
579uint8_t rpr::Controller::curMaxBuffers() {
580 return curMaxBuffers_;
583uint16_t rpr::Controller::curMaxSegment() {
584 return curMaxSegment_;
587uint16_t rpr::Controller::curCumAckTout() {
588 return curCumAckTout_;
591uint16_t rpr::Controller::curRetranTout() {
592 return curRetranTout_;
595uint16_t rpr::Controller::curNullTout() {
599uint8_t rpr::Controller::curMaxRetran() {
600 return curMaxRetran_;
603uint8_t rpr::Controller::curMaxCumAck() {
604 return curMaxCumAck_;
607void rpr::Controller::resetCounters() {
621void rpr::Controller::releaseHeaders(std::vector<rpr::HeaderPtr>& heads) {
623 if (Py_IsInitialized()) {
633void rpr::Controller::transportTx(rpr::HeaderPtr head,
bool seqUpdate,
bool txReset) {
634 std::vector<rpr::HeaderPtr> acked;
635 std::unique_lock<std::mutex> lock(txMtx_);
637 head->sequence = locSequence_;
641 txList_[locSequence_] = head;
648 for (uint32_t x = 0; x < 256; x++) {
649 if (txList_[x]) acked.push_back(std::move(txList_[x]));
655 head->acknowledge = lastAckTx_;
658 head->acknowledge = ackSeqRx_;
659 lastAckTx_ = ackSeqRx_;
664 gettimeofday(&txTime_, NULL);
670 "TX frame: state=%" PRIu32
" server=%d size=%" PRIu32
" syn=%d ack=%d nul=%d"
671 ", bsy=%d, rst=%d, ack#=%" PRIu8
", seq=%" PRIu8
", recount=%" PRIu32
", ptr=%p",
674 head->getFrame()->getPayload(),
683 static_cast<void*
>(head->getFrame().get()));
689 releaseHeaders(acked);
692 tran_->sendFrame(head->getFrame());
696int8_t rpr::Controller::retransmit(uint8_t
id) {
697 std::unique_lock<std::mutex> lock(txMtx_);
699 rpr::HeaderPtr head = txList_[id];
700 if (head == NULL)
return 0;
703 if (!timePassed(head->getTime(), retranToutD1_))
return 0;
706 if (head->count() >= curMaxRetran_)
return -1;
711 head->acknowledge = lastAckTx_;
714 head->acknowledge = ackSeqRx_;
715 lastAckTx_ = ackSeqRx_;
720 gettimeofday(&txTime_, NULL);
723 "Retran frame: state=%" PRIu32
" server=%d size=%" PRIu32
" syn=%d ack=%d"
724 " nul=%d, rst=%d, ack#=%" PRIu8
", seq=%" PRIu8
", recount=%" PRIu32
", ptr=%p",
727 head->getFrame()->getPayload(),
735 static_cast<void*
>(head->getFrame().get()));
743 tran_->sendFrame(head->getFrame());
748void rpr::Controller::convTime(
struct timeval& tme, uint32_t rssiTime) {
749 float units = std::pow(10, -TimeoutUnit);
750 float value = units *
static_cast<float>(rssiTime);
752 uint32_t usec =
static_cast<uint32_t
>(value / 1e-6);
754 div_t divResult = div(usec, 1000000);
755 tme.tv_sec = divResult.quot;
756 tme.tv_usec = divResult.rem;
760bool rpr::Controller::timePassed(
struct timeval& lastTime,
struct timeval& tme) {
761 struct timeval endTime;
762 struct timeval currTime;
764 gettimeofday(&currTime, NULL);
765 timeradd(&lastTime, &tme, &endTime);
766 return (timercmp(&currTime, &endTime, >=));
770void rpr::Controller::runThread() {
779 if (wait.tv_sec != 0 || wait.tv_usec != 0) {
781 std::unique_lock<std::mutex> lock(stMtx_);
784 stCond_.wait_for(lock, std::chrono::microseconds(wait.tv_usec) + std::chrono::seconds(wait.tv_sec));
790 wait = stateClosedWait();
794 wait = stateSendSynAck();
798 wait = stateSendSeqAck();
818struct timeval& rpr::Controller::stateClosedWait() {
822 if (!stQueue_.empty()) {
823 head = stQueue_.pop();
828 log_->warning(
"Closing link after reset request. Server=%d", server_);
831 }
else if (head->syn && (head->ack || server_)) {
832 curMaxBuffers_ = head->maxOutstandingSegments;
833 curMaxSegment_ = head->maxSegmentSize;
834 curCumAckTout_ = head->cumulativeAckTimeout;
835 curRetranTout_ = head->retransmissionTimeout;
836 curNullTout_ = head->nullTimeout;
837 curMaxRetran_ = head->maxRetransmissions;
838 curMaxCumAck_ = head->maxCumulativeAck;
839 lastAckRx_ = head->acknowledge;
842 convTime(retranToutD1_, curRetranTout_);
843 convTime(cumAckToutD1_, curCumAckTout_);
844 convTime(cumAckToutD2_, curCumAckTout_ / 2);
845 convTime(nullToutD3_, curNullTout_ / 3);
848 state_ = StSendSynAck;
851 state_ = StSendSeqAck;
853 gettimeofday(&stTime_, NULL);
857 curMaxBuffers_ = locMaxBuffers_;
858 curMaxSegment_ = locMaxSegment_;
859 curCumAckTout_ = locCumAckTout_;
860 curRetranTout_ = locRetranTout_;
861 curNullTout_ = locNullTout_;
862 curMaxRetran_ = locMaxRetran_;
863 curMaxCumAck_ = locMaxCumAck_;
867 }
else if ((!server_) && timePassed(stTime_, tryPeriodD1_)) {
869 head = rpr::Header::create(tran_->reqFrame(rpr::Header::SynSize,
false));
873 head->version = Version;
875 head->maxOutstandingSegments = locMaxBuffers_;
876 head->maxSegmentSize = locMaxSegment_;
877 head->retransmissionTimeout = locRetranTout_;
878 head->cumulativeAckTimeout = locCumAckTout_;
879 head->nullTimeout = locNullTout_;
880 head->maxRetransmissions = locMaxRetran_;
881 head->maxCumulativeAck = locMaxCumAck_;
882 head->timeoutUnit = TimeoutUnit;
883 head->connectionId = locConnId_;
885 transportTx(head,
true,
false);
888 gettimeofday(&stTime_, NULL);
890 }
else if (server_) {
894 return (tryPeriodD4_);
898struct timeval& rpr::Controller::stateSendSynAck() {
900 rpr::HeaderPtr head = rpr::Header::create(tran_->reqFrame(rpr::Header::SynSize,
false));
905 head->version = Version;
907 head->maxOutstandingSegments = curMaxBuffers_;
908 head->maxSegmentSize = curMaxSegment_;
909 head->retransmissionTimeout = curRetranTout_;
910 head->cumulativeAckTimeout = curCumAckTout_;
911 head->nullTimeout = curNullTout_;
912 head->maxRetransmissions = curMaxRetran_;
913 head->maxCumulativeAck = curMaxCumAck_;
914 head->timeoutUnit = TimeoutUnit;
915 head->connectionId = locConnId_;
917 transportTx(head,
true,
true);
920 log_->info(
"Link state is open. Server=%d", server_);
922 return (cumAckToutD2_);
926struct timeval& rpr::Controller::stateSendSeqAck() {
928 rpr::HeaderPtr ack = rpr::Header::create(tran_->reqFrame(rpr::Header::HeaderSize,
false));
933 ackSeqRx_ = lastSeqRx_;
935 transportTx(ack,
false,
true);
939 log_->info(
"Link state is open. Server=%d", server_);
940 return (cumAckToutD2_);
944struct timeval& rpr::Controller::stateOpen() {
949 struct timeval locTime;
952 while (!stQueue_.empty()) {
953 head = stQueue_.pop();
956 if ((head->rst) || (head->syn && (!head->ack))) {
958 gettimeofday(&stTime_, NULL);
965 std::unique_lock<std::mutex> lock(txMtx_);
967 ackPend = ackSeqRx_ - lastAckTx_;
971 if (timePassed(locTime, nullToutD3_))
977 if ((doNull || ((!getLocBusy()) && ackPend >= curMaxCumAck_) ||
978 ((ackPend > 0 || getLocBusy()) && timePassed(locTime, cumAckToutD1_)))) {
979 head = rpr::Header::create(tran_->reqFrame(rpr::Header::HeaderSize,
false));
982 transportTx(head, doNull,
false);
987 while ((!remBusy_) && (idx != locSequence_)) {
988 if (retransmit(idx++) < 0) {
990 gettimeofday(&stTime_, NULL);
995 return (cumAckToutD2_);
999struct timeval& rpr::Controller::stateError() {
1002 log_->warning(
"Entering reset state. Server=%d", server_);
1004 rst = rpr::Header::create(tran_->reqFrame(rpr::Header::HeaderSize,
false));
1007 transportTx(rst,
true,
true);
1010 log_->warning(
"Entering closed state after reset. Server=%d", server_);
1016 if (Py_IsInitialized()) {
1032 gettimeofday(&stTime_, NULL);
1033 return (tryPeriodD1_);
1037void rpr::Controller::setTimeout(uint32_t timeout) {
1038 div_t divResult = div(timeout, 1000000);
1039 timeout_.tv_sec = divResult.quot;
1040 timeout_.tv_usec = divResult.rem;
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
static const uint32_t Debug
Debug severity level constant.
static const uint32_t Warning
Warning severity level constant.
RAII helper that acquires the Python GIL for a scope.
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
void defaultTimeout(struct timeval &tout)
Returns Rogue default timeout as a timeval.