forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamRequester.cpp
More file actions
81 lines (66 loc) · 1.98 KB
/
StreamRequester.cpp
File metadata and controls
81 lines (66 loc) · 1.98 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2004-present Facebook. All Rights Reserved.
#include "rsocket/statemachine/StreamRequester.h"
namespace rsocket {
void StreamRequester::setRequested(size_t n) {
VLOG(3) << "Setting allowance to " << n;
requested_ = true;
addImplicitAllowance(n);
}
void StreamRequester::request(int64_t n) noexcept {
if (n == 0) {
return;
}
if(!requested_) {
requested_ = true;
auto initialN =
n > Frame_REQUEST_N::kMaxRequestN ? Frame_REQUEST_N::kMaxRequestN : n;
auto remainingN = n > Frame_REQUEST_N::kMaxRequestN
? n - Frame_REQUEST_N::kMaxRequestN
: 0;
// Send as much as possible with the initial request.
CHECK_GE(Frame_REQUEST_N::kMaxRequestN, initialN);
// We must inform ConsumerBase about an implicit allowance we have
// requested from the remote end.
addImplicitAllowance(initialN);
newStream(
StreamType::STREAM,
static_cast<uint32_t>(initialN),
std::move(initialPayload_));
// Pump the remaining allowance into the ConsumerBase _after_ sending the
// initial request.
if (remainingN) {
generateRequest(remainingN);
}
return;
}
generateRequest(n);
}
void StreamRequester::cancel() noexcept {
VLOG(5) << "StreamRequester::cancel(requested_=" << requested_ << ")";
if (requested_) {
cancelConsumer();
cancelStream();
}
closeStream(StreamCompletionSignal::CANCEL);
}
void StreamRequester::endStream(StreamCompletionSignal signal) {
VLOG(5) << "StreamRequester::endStream()";
ConsumerBase::endStream(signal);
}
void StreamRequester::handlePayload(
Payload&& payload,
bool complete,
bool next) {
CHECK(requested_);
processPayload(std::move(payload), next);
if (complete) {
completeConsumer();
closeStream(StreamCompletionSignal::COMPLETE);
}
}
void StreamRequester::handleError(folly::exception_wrapper errorPayload) {
CHECK(requested_);
errorConsumer(std::move(errorPayload));
closeStream(StreamCompletionSignal::ERROR);
}
}