connection_id_generator.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
18 
19 #ifndef GRPC_NO_BINDER
20 
21 #include "absl/strings/str_cat.h"
22 
23 namespace {
24 // Make sure `s` does not contain characters other than numbers, alphabets,
25 // period and underscore
27  std::string s = std::string(str_view);
28  for (size_t i = 0; i < s.length(); i++) {
29  if (!isalnum(s[i]) && s[i] != '.') {
30  s[i] = '_';
31  }
32  }
33  return s;
34 }
35 
36 // Remove prefix of the string if the string is longer than len
37 std::string StripToLength(const std::string& s, size_t len) {
38  if (s.length() > len) {
39  return s.substr(s.length() - len, len);
40  }
41  return s;
42 }
43 } // namespace
44 
45 namespace grpc_binder {
46 
49  // reserve some room for serial number
50  const size_t kReserveForNumbers = 15;
51  std::string s = StripToLength(
52  absl::StrCat(Normalize(package_name), "-", Normalize(class_name)),
53  kPathLengthLimit - kReserveForNumbers);
55  {
57  // Insert a hyphen before serial number
58  ret = absl::StrCat(s, "-", ++count_);
59  }
60  GPR_ASSERT(ret.length() < kPathLengthLimit);
61  return ret;
62 }
63 
65  static ConnectionIdGenerator* cig = new ConnectionIdGenerator();
66  return cig;
67 }
68 
69 } // namespace grpc_binder
70 #endif
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_binder
Definition: connection_id_generator.cc:45
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_binder::ConnectionIdGenerator::kPathLengthLimit
const size_t kPathLengthLimit
Definition: connection_id_generator.h:40
absl::FormatConversionChar::s
@ s
Normalize
void Normalize(std::string &contents)
Definition: bloaty_test_pe.cc:39
grpc_binder::GetConnectionIdGenerator
ConnectionIdGenerator * GetConnectionIdGenerator()
Definition: connection_id_generator.cc:64
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_binder::ConnectionIdGenerator::Generate
std::string Generate(absl::string_view package_name, absl::string_view class_name)
Definition: connection_id_generator.cc:47
class_name
static const char * class_name(int dnsclass)
Definition: adig.c:901
connection_id_generator.h
count_
int * count_
Definition: connectivity_state_test.cc:65
grpc_binder::ConnectionIdGenerator
Definition: connection_id_generator.h:32
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
grpc_binder::ConnectionIdGenerator::m_
grpc_core::Mutex m_
Definition: connection_id_generator.h:42
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:01