1 // 2 // 3 // Copyright 2017 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_SRC_CORE_LIB_COMPRESSION_COMPRESSION_INTERNAL_H 20 #define GRPC_SRC_CORE_LIB_COMPRESSION_COMPRESSION_INTERNAL_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <stdint.h> 25 26 #include <initializer_list> 27 28 #include "absl/strings/string_view.h" 29 #include "absl/types/optional.h" 30 31 #include <grpc/impl/compression_types.h> 32 33 #include "src/core/lib/channel/channel_args.h" 34 #include "src/core/lib/gprpp/bitset.h" 35 #include "src/core/lib/slice/slice.h" 36 37 namespace grpc_core { 38 39 // Given a string naming a compression algorithm, return the corresponding enum 40 // or nullopt on error. 41 absl::optional<grpc_compression_algorithm> ParseCompressionAlgorithm( 42 absl::string_view algorithm); 43 // Convert a compression algorithm to a string. Returns nullptr if a name is not 44 // known. 45 const char* CompressionAlgorithmAsString(grpc_compression_algorithm algorithm); 46 // Retrieve the default compression algorithm from channel args, return nullopt 47 // if not found. 48 absl::optional<grpc_compression_algorithm> 49 DefaultCompressionAlgorithmFromChannelArgs(const ChannelArgs& args); 50 51 // A set of grpc_compression_algorithm values. 52 class CompressionAlgorithmSet { 53 public: 54 // Construct from a uint32_t bitmask - bit 0 => algorithm 0, bit 1 => 55 // algorithm 1, etc. 56 static CompressionAlgorithmSet FromUint32(uint32_t value); 57 // Locate in channel args and construct from the found value. 58 static CompressionAlgorithmSet FromChannelArgs(const ChannelArgs& args); 59 // Parse a string of comma-separated compression algorithms. 60 static CompressionAlgorithmSet FromString(absl::string_view str); 61 // Construct an empty set. 62 CompressionAlgorithmSet(); 63 // Construct from a std::initializer_list of grpc_compression_algorithm 64 // values. 65 CompressionAlgorithmSet( 66 std::initializer_list<grpc_compression_algorithm> algorithms); 67 68 // Given a compression level, choose an appropriate algorithm from this set. 69 grpc_compression_algorithm CompressionAlgorithmForLevel( 70 grpc_compression_level level) const; 71 // Return true if this set contains algorithm, false otherwise. 72 bool IsSet(grpc_compression_algorithm algorithm) const; 73 // Add algorithm to this set. 74 void Set(grpc_compression_algorithm algorithm); 75 76 // Return a comma separated string of the algorithms in this set. 77 absl::string_view ToString() const; 78 Slice ToSlice() const; 79 80 // Return a bitmask of the algorithms in this set. 81 uint32_t ToLegacyBitmask() const; 82 83 bool operator==(const CompressionAlgorithmSet& other) const { 84 return set_ == other.set_; 85 } 86 87 private: 88 BitSet<GRPC_COMPRESS_ALGORITHMS_COUNT> set_; 89 }; 90 91 grpc_compression_options CompressionOptionsFromChannelArgs( 92 const ChannelArgs& args); 93 94 } // namespace grpc_core 95 96 #endif // GRPC_SRC_CORE_LIB_COMPRESSION_COMPRESSION_INTERNAL_H 97