forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame.cpp
More file actions
179 lines (143 loc) · 5.95 KB
/
Frame.cpp
File metadata and controls
179 lines (143 loc) · 5.95 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2004-present Facebook. All Rights Reserved.
#include "rsocket/framing/Frame.h"
#include <folly/Memory.h>
#include <folly/io/Cursor.h>
#include <map>
#include <sstream>
#include "rsocket/RSocketParameters.h"
namespace rsocket {
const uint32_t Frame_LEASE::kMaxTtl;
const uint32_t Frame_LEASE::kMaxNumRequests;
const uint32_t Frame_SETUP::kMaxKeepaliveTime;
const uint32_t Frame_SETUP::kMaxLifetime;
std::ostream& operator<<(std::ostream& os, const Frame_REQUEST_Base& frame) {
return os << frame.header_ << "(" << frame.requestN_ << ", "
<< frame.payload_;
}
std::ostream& operator<<(std::ostream& os, const Frame_REQUEST_N& frame) {
return os << frame.header_ << "(" << frame.requestN_ << ")";
}
std::ostream& operator<<(
std::ostream& os,
const Frame_REQUEST_RESPONSE& frame) {
return os << frame.header_ << ", " << frame.payload_;
}
std::ostream& operator<<(std::ostream& os, const Frame_REQUEST_FNF& frame) {
return os << frame.header_ << ", " << frame.payload_;
}
std::ostream& operator<<(std::ostream& os, const Frame_METADATA_PUSH& frame) {
return os << frame.header_ << ", "
<< (frame.metadata_ ? frame.metadata_->computeChainDataLength()
: 0);
}
std::ostream& operator<<(std::ostream& os, const Frame_CANCEL& frame) {
return os << frame.header_;
}
Frame_PAYLOAD Frame_PAYLOAD::complete(StreamId streamId) {
return Frame_PAYLOAD(streamId, FrameFlags::COMPLETE, Payload());
}
std::ostream& operator<<(std::ostream& os, const Frame_PAYLOAD& frame) {
return os << frame.header_ << ", " << frame.payload_;
}
Frame_ERROR Frame_ERROR::invalidSetup(std::string message) {
return connectionErr(ErrorCode::INVALID_SETUP, std::move(message));
}
Frame_ERROR Frame_ERROR::unsupportedSetup(std::string message) {
return connectionErr(ErrorCode::UNSUPPORTED_SETUP, std::move(message));
}
Frame_ERROR Frame_ERROR::rejectedSetup(std::string message) {
return connectionErr(ErrorCode::REJECTED_SETUP, std::move(message));
}
Frame_ERROR Frame_ERROR::rejectedResume(std::string message) {
return connectionErr(ErrorCode::REJECTED_RESUME, std::move(message));
}
Frame_ERROR Frame_ERROR::connectionError(std::string message) {
return connectionErr(ErrorCode::CONNECTION_ERROR, std::move(message));
}
Frame_ERROR Frame_ERROR::applicationError(
StreamId stream,
std::string message) {
return streamErr(ErrorCode::APPLICATION_ERROR, std::move(message), stream);
}
Frame_ERROR Frame_ERROR::rejected(StreamId stream, std::string message) {
return streamErr(ErrorCode::REJECTED, std::move(message), stream);
}
Frame_ERROR Frame_ERROR::canceled(StreamId stream, std::string message) {
return streamErr(ErrorCode::CANCELED, std::move(message), stream);
}
Frame_ERROR Frame_ERROR::invalid(StreamId stream, std::string message) {
return streamErr(ErrorCode::INVALID, std::move(message), stream);
}
Frame_ERROR Frame_ERROR::connectionErr(ErrorCode err, std::string message) {
return Frame_ERROR{0, err, Payload{std::move(message)}};
}
Frame_ERROR
Frame_ERROR::streamErr(ErrorCode err, std::string message, StreamId stream) {
if (stream == 0) {
throw std::invalid_argument{"Can't make stream error for stream zero"};
}
return Frame_ERROR{stream, err, Payload{std::move(message)}};
}
std::ostream& operator<<(std::ostream& os, const Frame_ERROR& frame) {
return os << frame.header_ << ", " << frame.errorCode_ << ", "
<< frame.payload_;
}
std::ostream& operator<<(std::ostream& os, const Frame_KEEPALIVE& frame) {
return os << frame.header_ << "(<"
<< (frame.data_ ? frame.data_->computeChainDataLength() : 0)
<< ">)";
}
std::ostream& operator<<(std::ostream& os, const Frame_SETUP& frame) {
return os << frame.header_ << ", Version: " << frame.versionMajor_ << "."
<< frame.versionMinor_ << ", "
<< "Token: " << frame.token_ << ", " << frame.payload_;
}
void Frame_SETUP::moveToSetupPayload(SetupParameters& setupPayload) {
setupPayload.metadataMimeType = std::move(metadataMimeType_);
setupPayload.dataMimeType = std::move(dataMimeType_);
setupPayload.payload = std::move(payload_);
setupPayload.token = std::move(token_);
setupPayload.resumable = !!(header_.flags & FrameFlags::RESUME_ENABLE);
setupPayload.protocolVersion = ProtocolVersion(versionMajor_, versionMinor_);
}
std::ostream& operator<<(std::ostream& os, const Frame_LEASE& frame) {
return os << frame.header_ << ", ("
<< (frame.metadata_ ? frame.metadata_->computeChainDataLength() : 0)
<< ")";
}
std::ostream& operator<<(std::ostream& os, const Frame_RESUME& frame) {
return os << frame.header_ << ", ("
<< "token " << frame.token_ << ", @server "
<< frame.lastReceivedServerPosition_ << ", @client "
<< frame.clientPosition_ << ")";
}
std::ostream& operator<<(std::ostream& os, const Frame_RESUME_OK& frame) {
return os << frame.header_ << ", (@" << frame.position_ << ")";
}
std::ostream& operator<<(std::ostream& os, const Frame_REQUEST_CHANNEL& frame) {
return os << frame.header_ << ", " << frame.payload_;
}
std::ostream& operator<<(std::ostream& os, const Frame_REQUEST_STREAM& frame) {
return os << frame.header_ << ", initialRequestN=" << frame.requestN_ << ", "
<< frame.payload_;
}
StreamType getStreamType(FrameType frameType) {
if (frameType == FrameType::REQUEST_STREAM) {
return StreamType::STREAM;
} else if (frameType == FrameType::REQUEST_CHANNEL) {
return StreamType::CHANNEL;
} else if (frameType == FrameType::REQUEST_RESPONSE) {
return StreamType::REQUEST_RESPONSE;
} else if (frameType == FrameType::REQUEST_FNF) {
return StreamType::FNF;
} else {
LOG(FATAL) << "Unknown open stream frame : " << frameType;
}
}
bool isNewStreamFrame(FrameType frameType) {
return frameType == FrameType::REQUEST_CHANNEL ||
frameType == FrameType::REQUEST_STREAM ||
frameType == FrameType::REQUEST_RESPONSE ||
frameType == FrameType::REQUEST_FNF;
}
} // namespace rsocket