args_utils.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "test/core/compression/args_utils.h"
  15. #include <string.h>
  16. #include <grpc/support/log.h>
  17. #include "src/core/lib/compression/compression_internal.h"
  18. #include "src/core/lib/gpr/useful.h"
  19. const grpc_channel_args*
  20. grpc_channel_args_set_channel_default_compression_algorithm(
  21. const grpc_channel_args* a, grpc_compression_algorithm algorithm) {
  22. GPR_ASSERT(algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT);
  23. grpc_arg tmp;
  24. tmp.type = GRPC_ARG_INTEGER;
  25. tmp.key = const_cast<char*>(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
  26. tmp.value.integer = algorithm;
  27. return grpc_channel_args_copy_and_add(a, &tmp, 1);
  28. }
  29. /** Returns 1 if the argument for compression algorithm's enabled states bitset
  30. * was found in \a a, returning the arg's value in \a states. Otherwise, returns
  31. * 0. */
  32. static int find_compression_algorithm_states_bitset(const grpc_channel_args* a,
  33. int** states_arg) {
  34. if (a != nullptr) {
  35. size_t i;
  36. for (i = 0; i < a->num_args; ++i) {
  37. if (a->args[i].type == GRPC_ARG_INTEGER &&
  38. !strcmp(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
  39. a->args[i].key)) {
  40. *states_arg = &a->args[i].value.integer;
  41. **states_arg =
  42. (**states_arg & ((1 << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1)) |
  43. 0x1; /* forcefully enable support for no compression */
  44. return 1;
  45. }
  46. }
  47. }
  48. return 0; /* GPR_FALSE */
  49. }
  50. const grpc_channel_args* grpc_channel_args_compression_algorithm_set_state(
  51. const grpc_channel_args** a, grpc_compression_algorithm algorithm,
  52. int state) {
  53. int* states_arg = nullptr;
  54. const grpc_channel_args* result = *a;
  55. const int states_arg_found =
  56. find_compression_algorithm_states_bitset(*a, &states_arg);
  57. if (grpc_core::DefaultCompressionAlgorithmFromChannelArgs(*a) == algorithm &&
  58. state == 0) {
  59. const char* algo_name = nullptr;
  60. GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algo_name) != 0);
  61. gpr_log(GPR_ERROR,
  62. "Tried to disable default compression algorithm '%s'. The "
  63. "operation has been ignored.",
  64. algo_name);
  65. } else if (states_arg_found) {
  66. if (state != 0) {
  67. grpc_core::SetBit(reinterpret_cast<unsigned*>(states_arg), algorithm);
  68. } else if (algorithm != GRPC_COMPRESS_NONE) {
  69. grpc_core::ClearBit(reinterpret_cast<unsigned*>(states_arg), algorithm);
  70. }
  71. } else {
  72. /* create a new arg */
  73. grpc_arg tmp;
  74. tmp.type = GRPC_ARG_INTEGER;
  75. tmp.key =
  76. const_cast<char*>(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
  77. /* all enabled by default */
  78. tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
  79. if (state != 0) {
  80. grpc_core::SetBit(reinterpret_cast<unsigned*>(&tmp.value.integer),
  81. algorithm);
  82. } else if (algorithm != GRPC_COMPRESS_NONE) {
  83. grpc_core::ClearBit(reinterpret_cast<unsigned*>(&tmp.value.integer),
  84. algorithm);
  85. }
  86. result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
  87. grpc_channel_args_destroy(*a);
  88. *a = result;
  89. }
  90. return result;
  91. }