rpc_encoding.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_RPC_ENCODING_H
20 #define GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_RPC_ENCODING_H
21 
23 
24 #include <stdint.h>
25 #include <string.h>
26 
27 #include "absl/base/internal/endian.h"
28 #include "absl/strings/string_view.h"
29 
30 namespace grpc {
31 
32 // TODO(unknown): This may not be needed. Check to see if opencensus requires
33 // a trailing server response.
34 // RpcServerStatsEncoding encapsulates the logic for encoding and decoding of
35 // rpc server stats messages. Rpc server stats consists of a uint64_t time
36 // value (server latency in nanoseconds).
38  public:
39  // Size of encoded RPC server stats.
40  static constexpr size_t kRpcServerStatsSize = 10;
41  // Error value.
42  static constexpr size_t kEncodeDecodeFailure = 0;
43 
44  // Deserializes rpc server stats from the incoming 'buf' into *time. Returns
45  // number of bytes decoded. If the buffer is of insufficient size (it must be
46  // at least kRpcServerStatsSize bytes) or the encoding version or field ID are
47  // unrecognized, *time will be set to 0 and it will return
48  // kEncodeDecodeFailure. Inlined for performance reasons.
49  static size_t Decode(absl::string_view buf, uint64_t* time) {
50  if (buf.size() < kRpcServerStatsSize) {
51  *time = 0;
52  return kEncodeDecodeFailure;
53  }
54 
57  if (version != kVersionId || fieldID != kServerElapsedTimeField) {
58  *time = 0;
59  return kEncodeDecodeFailure;
60  }
63  return kRpcServerStatsSize;
64  }
65 
66  // Serializes rpc server stats into the provided buffer. It returns the
67  // number of bytes written to the buffer. If the buffer is smaller than
68  // kRpcServerStatsSize bytes it will return kEncodeDecodeFailure. Inlined for
69  // performance reasons.
70  static size_t Encode(uint64_t time, char* buf, size_t buf_size) {
71  if (buf_size < kRpcServerStatsSize) {
72  return kEncodeDecodeFailure;
73  }
74 
78  time);
79  return kRpcServerStatsSize;
80  }
81 
82  private:
83  // Size of Version ID.
84  static constexpr size_t kVersionIdSize = 1;
85  // Size of Field ID.
86  static constexpr size_t kFieldIdSize = 1;
87 
88  // Offset and value for currently supported version ID.
89  static constexpr size_t kVersionIdOffset = 0;
90  static constexpr size_t kVersionId = 0;
91 
92  enum FieldIdValue {
94  };
95 
96  enum FieldSize {
98  };
99 
102  };
103 
104  RpcServerStatsEncoding() = delete;
109 };
110 
111 } // namespace grpc
112 
113 #endif /* GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_RPC_ENCODING_H */
grpc::RpcServerStatsEncoding::RpcServerStatsEncoding
RpcServerStatsEncoding()=delete
grpc::RpcServerStatsEncoding::kServerElapsedTimeOffset
@ kServerElapsedTimeOffset
Definition: rpc_encoding.h:101
grpc::RpcServerStatsEncoding::kServerElapsedTimeField
@ kServerElapsedTimeField
Definition: rpc_encoding.h:93
grpc
Definition: grpcpp/alarm.h:33
absl::little_endian::Store64
void Store64(void *p, uint64_t v)
Definition: abseil-cpp/absl/base/internal/endian.h:183
grpc::RpcServerStatsEncoding::kVersionId
static constexpr size_t kVersionId
Definition: rpc_encoding.h:90
grpc::RpcServerStatsEncoding::FieldIdOffset
FieldIdOffset
Definition: rpc_encoding.h:100
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc::RpcServerStatsEncoding::FieldIdValue
FieldIdValue
Definition: rpc_encoding.h:92
version
Definition: version.py:1
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc::RpcServerStatsEncoding::Encode
static size_t Encode(uint64_t time, char *buf, size_t buf_size)
Definition: rpc_encoding.h:70
grpc::RpcServerStatsEncoding::Decode
static size_t Decode(absl::string_view buf, uint64_t *time)
Definition: rpc_encoding.h:49
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc::RpcServerStatsEncoding::kVersionIdSize
static constexpr size_t kVersionIdSize
Definition: rpc_encoding.h:84
grpc::RpcServerStatsEncoding::operator=
RpcServerStatsEncoding operator=(const RpcServerStatsEncoding &)=delete
absl::little_endian::Load64
uint64_t Load64(const void *p)
Definition: abseil-cpp/absl/base/internal/endian.h:179
grpc::RpcServerStatsEncoding::FieldSize
FieldSize
Definition: rpc_encoding.h:96
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
grpc::RpcServerStatsEncoding::kVersionIdOffset
static constexpr size_t kVersionIdOffset
Definition: rpc_encoding.h:89
grpc::RpcServerStatsEncoding
Definition: rpc_encoding.h:37
stdint.h
grpc::RpcServerStatsEncoding::kRpcServerStatsSize
static constexpr size_t kRpcServerStatsSize
Definition: rpc_encoding.h:40
grpc::RpcServerStatsEncoding::kEncodeDecodeFailure
static constexpr size_t kEncodeDecodeFailure
Definition: rpc_encoding.h:42
grpc::RpcServerStatsEncoding::kServerElapsedTimeSize
@ kServerElapsedTimeSize
Definition: rpc_encoding.h:97
grpc::RpcServerStatsEncoding::kFieldIdSize
static constexpr size_t kFieldIdSize
Definition: rpc_encoding.h:86
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:13