forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cpp
More file actions
140 lines (126 loc) · 4.31 KB
/
Common.cpp
File metadata and controls
140 lines (126 loc) · 4.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
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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "rsocket/internal/Common.h"
#include <sstream>
#include <folly/Random.h>
#include <folly/String.h>
#include <folly/io/IOBuf.h>
#include <algorithm>
#include <random>
namespace rsocket {
static const char* getTerminatingSignalErrorMessage(int terminatingSignal) {
switch (static_cast<StreamCompletionSignal>(terminatingSignal)) {
case StreamCompletionSignal::CONNECTION_END:
return "connection closed";
case StreamCompletionSignal::CONNECTION_ERROR:
return "connection error";
case StreamCompletionSignal::ERROR:
return "socket or stream error";
case StreamCompletionSignal::APPLICATION_ERROR:
return "application error";
case StreamCompletionSignal::SOCKET_CLOSED:
return "reactive socket closed";
case StreamCompletionSignal::UNSUPPORTED_SETUP:
return "unsupported setup";
case StreamCompletionSignal::REJECTED_SETUP:
return "rejected setup";
case StreamCompletionSignal::INVALID_SETUP:
return "invalid setup";
case StreamCompletionSignal::COMPLETE:
case StreamCompletionSignal::CANCEL:
DCHECK(false) << "throwing exception for graceful termination?";
return "graceful termination";
default:
return "stream interrupted";
}
}
folly::StringPiece toString(StreamType t) {
switch (t) {
case StreamType::REQUEST_RESPONSE:
return "REQUEST_RESPONSE";
case StreamType::STREAM:
return "STREAM";
case StreamType::CHANNEL:
return "CHANNEL";
case StreamType::FNF:
return "FNF";
default:
DCHECK(false);
return "(invalid StreamType)";
}
}
std::ostream& operator<<(std::ostream& os, StreamType t) {
return os << toString(t);
}
std::ostream& operator<<(std::ostream& os, RSocketMode mode) {
switch (mode) {
case RSocketMode::CLIENT:
return os << "CLIENT";
case RSocketMode::SERVER:
return os << "SERVER";
}
DLOG(FATAL) << "Invalid RSocketMode";
return os << "INVALID_RSOCKET_MODE";
}
std::string to_string(StreamCompletionSignal signal) {
switch (signal) {
case StreamCompletionSignal::COMPLETE:
return "COMPLETE";
case StreamCompletionSignal::CANCEL:
return "CANCEL";
case StreamCompletionSignal::ERROR:
return "ERROR";
case StreamCompletionSignal::APPLICATION_ERROR:
return "APPLICATION_ERROR";
case StreamCompletionSignal::INVALID_SETUP:
return "INVALID_SETUP";
case StreamCompletionSignal::UNSUPPORTED_SETUP:
return "UNSUPPORTED_SETUP";
case StreamCompletionSignal::REJECTED_SETUP:
return "REJECTED_SETUP";
case StreamCompletionSignal::CONNECTION_ERROR:
return "CONNECTION_ERROR";
case StreamCompletionSignal::CONNECTION_END:
return "CONNECTION_END";
case StreamCompletionSignal::SOCKET_CLOSED:
return "SOCKET_CLOSED";
}
// this should be never hit because the switch is over all cases
LOG(FATAL) << "unknown StreamCompletionSignal=" << static_cast<int>(signal);
return "<unknown StreamCompletionSignal>";
}
std::ostream& operator<<(std::ostream& os, StreamCompletionSignal signal) {
return os << to_string(signal);
}
StreamInterruptedException::StreamInterruptedException(int _terminatingSignal)
: std::runtime_error(getTerminatingSignalErrorMessage(_terminatingSignal)),
terminatingSignal(_terminatingSignal) {}
std::string humanify(std::unique_ptr<folly::IOBuf> const& buf) {
std::string ret;
size_t cursor = 0;
for (const auto range : *buf) {
for (const unsigned char chr : range) {
if (cursor >= 20)
goto outer;
ret += chr;
cursor++;
}
}
outer:
return folly::humanify(ret);
}
std::string hexDump(folly::StringPiece s) {
return folly::hexDump(s.data(), std::min<size_t>(0xFF, s.size()));
}
} // namespace rsocket