forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameSerializer.cpp
More file actions
71 lines (57 loc) · 2.31 KB
/
FrameSerializer.cpp
File metadata and controls
71 lines (57 loc) · 2.31 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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "rsocket/framing/FrameSerializer.h"
#include <folly/Conv.h>
#include <folly/portability/GFlags.h>
#include "rsocket/framing/FrameSerializer_v0.h"
#include "rsocket/framing/FrameSerializer_v0_1.h"
#include "rsocket/framing/FrameSerializer_v1_0.h"
DEFINE_string(
rs_use_protocol_version,
"",
"override for the ReactiveSocket protocol version to be used"
" [MAJOR.MINOR].");
namespace rsocket {
constexpr const ProtocolVersion ProtocolVersion::Latest =
FrameSerializerV1_0::Version;
ProtocolVersion ProtocolVersion::Current() {
if (FLAGS_rs_use_protocol_version.empty()) {
return ProtocolVersion::Latest;
}
if (FLAGS_rs_use_protocol_version == "*") {
return ProtocolVersion::Unknown;
}
if (FLAGS_rs_use_protocol_version.size() != 3) {
LOG(ERROR) << "unknown protocol version " << FLAGS_rs_use_protocol_version
<< " defaulting to v" << ProtocolVersion::Latest;
return ProtocolVersion::Latest;
}
return ProtocolVersion(
folly::to<uint16_t>(FLAGS_rs_use_protocol_version[0] - '0'),
folly::to<uint16_t>(FLAGS_rs_use_protocol_version[2] - '0'));
}
std::unique_ptr<FrameSerializer> FrameSerializer::createFrameSerializer(
const ProtocolVersion& protocolVersion) {
if (protocolVersion == FrameSerializerV0::Version) {
return std::make_unique<FrameSerializerV0>();
} else if (protocolVersion == FrameSerializerV0_1::Version) {
return std::make_unique<FrameSerializerV0_1>();
} else if (protocolVersion == FrameSerializerV1_0::Version) {
return std::make_unique<FrameSerializerV1_0>();
}
DCHECK(protocolVersion == ProtocolVersion::Unknown);
LOG_IF(ERROR, protocolVersion != ProtocolVersion::Unknown)
<< "unknown protocol version " << protocolVersion;
return nullptr;
}
std::unique_ptr<FrameSerializer> FrameSerializer::createAutodetectedSerializer(
const folly::IOBuf& firstFrame) {
auto detectedVersion = FrameSerializerV1_0::detectProtocolVersion(firstFrame);
if (detectedVersion == ProtocolVersion::Unknown) {
detectedVersion = FrameSerializerV0_1::detectProtocolVersion(firstFrame);
}
return createFrameSerializer(detectedVersion);
}
std::ostream& operator<<(std::ostream& os, const ProtocolVersion& version) {
return os << version.major << "." << version.minor;
}
}