forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublisherBase.cpp
More file actions
62 lines (50 loc) · 1.61 KB
/
PublisherBase.cpp
File metadata and controls
62 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2004-present Facebook. All Rights Reserved.
#include "rsocket/statemachine/PublisherBase.h"
#include <glog/logging.h>
#include "rsocket/statemachine/RSocketStateMachine.h"
namespace rsocket {
PublisherBase::PublisherBase(uint32_t initialRequestN)
: initialRequestN_(initialRequestN) {}
void PublisherBase::publisherSubscribe(
yarpl::Reference<yarpl::flowable::Subscription> subscription) {
if (state_ == State::CLOSED) {
subscription->cancel();
return;
}
DCHECK(!producingSubscription_);
producingSubscription_ = std::move(subscription);
if (initialRequestN_) {
producingSubscription_->request(initialRequestN_.consumeAll());
}
}
void PublisherBase::checkPublisherOnNext() {
// we are either responding and publisherSubscribe method was called
// or we are already terminated
CHECK((state_ == State::RESPONDING) == !!producingSubscription_);
}
void PublisherBase::publisherComplete() {
state_ = State::CLOSED;
producingSubscription_ = nullptr;
}
bool PublisherBase::publisherClosed() const {
return state_ == State::CLOSED;
}
void PublisherBase::processRequestN(uint32_t requestN) {
if (!requestN || state_ == State::CLOSED) {
return;
}
// we might not have the subscription set yet as there can be REQUEST_N
// frames scheduled on the executor before onSubscribe method
if (producingSubscription_) {
producingSubscription_->request(requestN);
} else {
initialRequestN_.add(requestN);
}
}
void PublisherBase::terminatePublisher() {
state_ = State::CLOSED;
if (auto subscription = std::move(producingSubscription_)) {
subscription->cancel();
}
}
}