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