diff options
author | Sree Kuchibhotla <sreek@google.com> | 2017-05-03 09:36:02 -0700 |
---|---|---|
committer | Sree Kuchibhotla <sreek@google.com> | 2017-05-03 09:36:02 -0700 |
commit | 4afb4db49807f1811d08b6f41c857a0e238844cc (patch) | |
tree | af4a457dd07a93b286a6b25dc88743c14bb87b3d /include | |
parent | 1eabdab1139dc7cbb7d67d4a16f06472b817a705 (diff) | |
parent | 3f716baa0a33529592c6cfeb64950fe463c4595f (diff) |
Merge branch 'master' into cq_mpsc_based
Diffstat (limited to 'include')
-rw-r--r-- | include/grpc++/impl/codegen/call.h | 37 | ||||
-rw-r--r-- | include/grpc++/impl/codegen/thrift_serializer.h | 217 | ||||
-rw-r--r-- | include/grpc++/impl/codegen/thrift_utils.h | 83 | ||||
-rw-r--r-- | include/grpc/grpc_security.h | 2 | ||||
-rw-r--r-- | include/grpc/impl/codegen/compression_types.h | 7 | ||||
-rw-r--r-- | include/grpc/impl/codegen/gpr_types.h | 1 | ||||
-rw-r--r-- | include/grpc/impl/codegen/grpc_types.h | 3 | ||||
-rw-r--r-- | include/grpc/impl/codegen/port_platform.h | 6 | ||||
-rw-r--r-- | include/grpc/impl/codegen/slice.h | 3 | ||||
-rw-r--r-- | include/grpc/slice.h | 22 | ||||
-rw-r--r-- | include/grpc/slice_buffer.h | 4 |
11 files changed, 47 insertions, 338 deletions
diff --git a/include/grpc++/impl/codegen/call.h b/include/grpc++/impl/codegen/call.h index f334ba61d6..9fe2bbb75e 100644 --- a/include/grpc++/impl/codegen/call.h +++ b/include/grpc++/impl/codegen/call.h @@ -364,28 +364,6 @@ class CallOpRecvMessage { bool allow_not_getting_message_; }; -namespace CallOpGenericRecvMessageHelper { -class DeserializeFunc { - public: - virtual Status Deserialize(grpc_byte_buffer* buf) = 0; - virtual ~DeserializeFunc() {} -}; - -template <class R> -class DeserializeFuncType final : public DeserializeFunc { - public: - DeserializeFuncType(R* message) : message_(message) {} - Status Deserialize(grpc_byte_buffer* buf) override { - return SerializationTraits<R>::Deserialize(buf, message_); - } - - ~DeserializeFuncType() override {} - - private: - R* message_; // Not a managed pointer because management is external to this -}; -} // namespace CallOpGenericRecvMessageHelper - class CallOpGenericRecvMessage { public: CallOpGenericRecvMessage() @@ -393,11 +371,9 @@ class CallOpGenericRecvMessage { template <class R> void RecvMessage(R* message) { - // Use an explicit base class pointer to avoid resolution error in the - // following unique_ptr::reset for some old implementations. - CallOpGenericRecvMessageHelper::DeserializeFunc* func = - new CallOpGenericRecvMessageHelper::DeserializeFuncType<R>(message); - deserialize_.reset(func); + deserialize_ = [message](grpc_byte_buffer* buf) -> Status { + return SerializationTraits<R>::Deserialize(buf, message); + }; } // Do not change status if no message is received. @@ -420,7 +396,7 @@ class CallOpGenericRecvMessage { if (recv_buf_) { if (*status) { got_message = true; - *status = deserialize_->Deserialize(recv_buf_).ok(); + *status = deserialize_(recv_buf_).ok(); } else { got_message = false; g_core_codegen_interface->grpc_byte_buffer_destroy(recv_buf_); @@ -431,11 +407,12 @@ class CallOpGenericRecvMessage { *status = false; } } - deserialize_.reset(); + deserialize_ = DeserializeFunc(); } private: - std::unique_ptr<CallOpGenericRecvMessageHelper::DeserializeFunc> deserialize_; + typedef std::function<Status(grpc_byte_buffer*)> DeserializeFunc; + DeserializeFunc deserialize_; grpc_byte_buffer* recv_buf_; bool allow_not_getting_message_; }; diff --git a/include/grpc++/impl/codegen/thrift_serializer.h b/include/grpc++/impl/codegen/thrift_serializer.h deleted file mode 100644 index 86bc7105c0..0000000000 --- a/include/grpc++/impl/codegen/thrift_serializer.h +++ /dev/null @@ -1,217 +0,0 @@ -/* - * - * Copyright 2016, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#ifndef GRPCXX_IMPL_CODEGEN_THRIFT_SERIALIZER_H -#define GRPCXX_IMPL_CODEGEN_THRIFT_SERIALIZER_H - -#include <grpc/impl/codegen/byte_buffer_reader.h> -#include <grpc/impl/codegen/slice.h> -#include <thrift/protocol/TBinaryProtocol.h> -#include <thrift/protocol/TCompactProtocol.h> -#include <thrift/protocol/TProtocolException.h> -#include <thrift/transport/TBufferTransports.h> -#include <thrift/transport/TTransportUtils.h> -#include <boost/make_shared.hpp> -#include <memory> -#include <stdexcept> -#include <string> - -namespace apache { -namespace thrift { -namespace util { - -using apache::thrift::protocol::TBinaryProtocolT; -using apache::thrift::protocol::TCompactProtocolT; -using apache::thrift::protocol::TMessageType; -using apache::thrift::protocol::TNetworkBigEndian; -using apache::thrift::transport::TMemoryBuffer; -using apache::thrift::transport::TBufferBase; -using apache::thrift::transport::TTransport; - -template <typename Dummy, typename Protocol> -class ThriftSerializer { - public: - ThriftSerializer() - : prepared_(false), - last_deserialized_(false), - serialize_version_(false) {} - - virtual ~ThriftSerializer() {} - - // Serialize the passed type into the internal buffer - // and returns a pointer to internal buffer and its size - template <typename T> - void Serialize(const T& fields, const uint8_t** serialized_buffer, - size_t* serialized_len) { - // prepare or reset buffer - if (!prepared_ || last_deserialized_) { - prepare(); - } else { - buffer_->resetBuffer(); - } - last_deserialized_ = false; - - // if required serialize protocol version - if (serialize_version_) { - protocol_->writeMessageBegin("", TMessageType(0), 0); - } - - // serialize fields into buffer - fields.write(protocol_.get()); - - // write the end of message - if (serialize_version_) { - protocol_->writeMessageEnd(); - } - - uint8_t* byte_buffer; - uint32_t byte_buffer_size; - buffer_->getBuffer(&byte_buffer, &byte_buffer_size); - *serialized_buffer = byte_buffer; - *serialized_len = byte_buffer_size; - } - - // Serialize the passed type into the byte buffer - template <typename T> - void Serialize(const T& fields, grpc_byte_buffer** bp) { - const uint8_t* byte_buffer; - size_t byte_buffer_size; - - Serialize(fields, &byte_buffer, &byte_buffer_size); - - grpc_slice slice = grpc_slice_from_copied_buffer( - reinterpret_cast<const char*>(byte_buffer), byte_buffer_size); - - *bp = grpc_raw_byte_buffer_create(&slice, 1); - - grpc_slice_unref(slice); - } - - // Deserialize the passed char array into the passed type, returns the number - // of bytes that have been consumed from the passed string. - template <typename T> - uint32_t Deserialize(uint8_t* serialized_buffer, size_t length, T* fields) { - // prepare buffer if necessary - if (!prepared_) { - prepare(); - } - last_deserialized_ = true; - - // reset buffer transport - buffer_->resetBuffer(serialized_buffer, length); - - // read the protocol version if necessary - if (serialize_version_) { - std::string name = ""; - TMessageType mt = static_cast<TMessageType>(0); - int32_t seq_id = 0; - protocol_->readMessageBegin(name, mt, seq_id); - } - - // deserialize buffer into fields - uint32_t len = fields->read(protocol_.get()); - - // read the end of message - if (serialize_version_) { - protocol_->readMessageEnd(); - } - - return len; - } - - // Deserialize the passed byte buffer to passed type, returns the number - // of bytes consumed from byte buffer - template <typename T> - uint32_t Deserialize(grpc_byte_buffer* buffer, T* msg) { - grpc_byte_buffer_reader reader; - grpc_byte_buffer_reader_init(&reader, buffer); - - grpc_slice slice = grpc_byte_buffer_reader_readall(&reader); - - uint32_t len = - Deserialize(GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice), msg); - - grpc_slice_unref(slice); - - grpc_byte_buffer_reader_destroy(&reader); - - return len; - } - - // set serialization version flag - void SetSerializeVersion(bool value) { serialize_version_ = value; } - - // Set the container size limit to deserialize - // This function should be called after buffer_ is initialized - void SetContainerSizeLimit(int32_t container_limit) { - if (!prepared_) { - prepare(); - } - protocol_->setContainerSizeLimit(container_limit); - } - - // Set the string size limit to deserialize - // This function should be called after buffer_ is initialized - void SetStringSizeLimit(int32_t string_limit) { - if (!prepared_) { - prepare(); - } - protocol_->setStringSizeLimit(string_limit); - } - - private: - bool prepared_; - bool last_deserialized_; - boost::shared_ptr<TMemoryBuffer> buffer_; - std::shared_ptr<Protocol> protocol_; - bool serialize_version_; - - void prepare() { - buffer_ = boost::make_shared<TMemoryBuffer>(); - // create a protocol for the memory buffer transport - protocol_ = std::make_shared<Protocol>(buffer_); - prepared_ = true; - } - -}; // ThriftSerializer - -typedef ThriftSerializer<void, TBinaryProtocolT<TBufferBase, TNetworkBigEndian>> - ThriftSerializerBinary; -typedef ThriftSerializer<void, TCompactProtocolT<TBufferBase>> - ThriftSerializerCompact; - -} // namespace util -} // namespace thrift -} // namespace apache - -#endif // GRPCXX_IMPL_CODEGEN_THRIFT_SERIALIZER_H diff --git a/include/grpc++/impl/codegen/thrift_utils.h b/include/grpc++/impl/codegen/thrift_utils.h deleted file mode 100644 index 742d739703..0000000000 --- a/include/grpc++/impl/codegen/thrift_utils.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * - * Copyright 2016, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#ifndef GRPCXX_IMPL_CODEGEN_THRIFT_UTILS_H -#define GRPCXX_IMPL_CODEGEN_THRIFT_UTILS_H - -#include <grpc++/impl/codegen/config.h> -#include <grpc++/impl/codegen/core_codegen_interface.h> -#include <grpc++/impl/codegen/serialization_traits.h> -#include <grpc++/impl/codegen/status.h> -#include <grpc++/impl/codegen/status_code_enum.h> -#include <grpc++/impl/codegen/thrift_serializer.h> -#include <grpc/impl/codegen/byte_buffer_reader.h> -#include <grpc/impl/codegen/slice.h> -#include <cstdint> -#include <cstdlib> - -namespace grpc { - -using apache::thrift::util::ThriftSerializerCompact; - -template <class T> -class SerializationTraits<T, typename std::enable_if<std::is_base_of< - apache::thrift::TBase, T>::value>::type> { - public: - static Status Serialize(const T& msg, grpc_byte_buffer** bp, - bool* own_buffer) { - *own_buffer = true; - - ThriftSerializerCompact serializer; - serializer.Serialize(msg, bp); - - return Status(StatusCode::OK, "ok"); - } - - static Status Deserialize(grpc_byte_buffer* buffer, T* msg, - int max_receive_message_size) { - if (!buffer) { - return Status(StatusCode::INTERNAL, "No payload"); - } - - ThriftSerializerCompact deserializer; - deserializer.Deserialize(buffer, msg); - - grpc_byte_buffer_destroy(buffer); - - return Status(StatusCode::OK, "ok"); - } -}; - -} // namespace grpc - -#endif // GRPCXX_IMPL_CODEGEN_THRIFT_UTILS_H diff --git a/include/grpc/grpc_security.h b/include/grpc/grpc_security.h index 79199cc5d6..5d3cc4fd67 100644 --- a/include/grpc/grpc_security.h +++ b/include/grpc/grpc_security.h @@ -158,7 +158,7 @@ typedef struct { } grpc_ssl_pem_key_cert_pair; /* Creates an SSL credentials object. - - pem_roots_cert is the NULL-terminated string containing the PEM encoding + - pem_root_certs is the NULL-terminated string containing the PEM encoding of the server root certificates. If this parameter is NULL, the implementation will first try to dereference the file pointed by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable, and if that fails, diff --git a/include/grpc/impl/codegen/compression_types.h b/include/grpc/impl/codegen/compression_types.h index 170d99f431..a563711e32 100644 --- a/include/grpc/impl/codegen/compression_types.h +++ b/include/grpc/impl/codegen/compression_types.h @@ -34,8 +34,7 @@ #ifndef GRPC_IMPL_CODEGEN_COMPRESSION_TYPES_H #define GRPC_IMPL_CODEGEN_COMPRESSION_TYPES_H -#include <stdbool.h> -#include <stdint.h> +#include <grpc/impl/codegen/port_platform.h> #ifdef __cplusplus extern "C" { @@ -101,7 +100,7 @@ typedef struct grpc_compression_options { * precedence over \a default_algorithm. * TODO(dgq): currently only available for server channels. */ struct { - bool is_set; + int is_set; grpc_compression_level level; } default_level; @@ -109,7 +108,7 @@ typedef struct grpc_compression_options { * call specific settings. This option corresponds to the channel argument key * behind \a GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM. */ struct { - bool is_set; + int is_set; grpc_compression_algorithm algorithm; } default_algorithm; diff --git a/include/grpc/impl/codegen/gpr_types.h b/include/grpc/impl/codegen/gpr_types.h index ed9976f429..34d8156b61 100644 --- a/include/grpc/impl/codegen/gpr_types.h +++ b/include/grpc/impl/codegen/gpr_types.h @@ -37,7 +37,6 @@ #include <grpc/impl/codegen/port_platform.h> #include <stddef.h> -#include <stdint.h> #ifdef __cplusplus extern "C" { diff --git a/include/grpc/impl/codegen/grpc_types.h b/include/grpc/impl/codegen/grpc_types.h index 468a8edd3d..51cbb53912 100644 --- a/include/grpc/impl/codegen/grpc_types.h +++ b/include/grpc/impl/codegen/grpc_types.h @@ -34,6 +34,8 @@ #ifndef GRPC_IMPL_CODEGEN_GRPC_TYPES_H #define GRPC_IMPL_CODEGEN_GRPC_TYPES_H +#include <grpc/impl/codegen/port_platform.h> + #include <grpc/impl/codegen/compression_types.h> #include <grpc/impl/codegen/exec_ctx_fwd.h> #include <grpc/impl/codegen/gpr_types.h> @@ -41,7 +43,6 @@ #include <grpc/impl/codegen/status.h> #include <stddef.h> -#include <stdint.h> #ifdef __cplusplus extern "C" { diff --git a/include/grpc/impl/codegen/port_platform.h b/include/grpc/impl/codegen/port_platform.h index 813e08b86e..e12f6f4e99 100644 --- a/include/grpc/impl/codegen/port_platform.h +++ b/include/grpc/impl/codegen/port_platform.h @@ -290,6 +290,12 @@ #endif #endif /* GPR_NO_AUTODETECT_PLATFORM */ +#if defined(__has_include) +#if __has_include(<atomic>) +#define GRPC_HAS_CXX11_ATOMIC +#endif /* __has_include(<atomic>) */ +#endif /* defined(__has_include) */ + #ifndef GPR_PLATFORM_STRING #warning "GPR_PLATFORM_STRING not auto-detected" #define GPR_PLATFORM_STRING "unknown" diff --git a/include/grpc/impl/codegen/slice.h b/include/grpc/impl/codegen/slice.h index 0b09a0bfd8..b89a3f7910 100644 --- a/include/grpc/impl/codegen/slice.h +++ b/include/grpc/impl/codegen/slice.h @@ -34,8 +34,9 @@ #ifndef GRPC_IMPL_CODEGEN_SLICE_H #define GRPC_IMPL_CODEGEN_SLICE_H +#include <grpc/impl/codegen/port_platform.h> + #include <stddef.h> -#include <stdint.h> #include <grpc/impl/codegen/exec_ctx_fwd.h> #include <grpc/impl/codegen/gpr_slice.h> diff --git a/include/grpc/slice.h b/include/grpc/slice.h index 8c32a30d76..9c4b158ae8 100644 --- a/include/grpc/slice.h +++ b/include/grpc/slice.h @@ -53,6 +53,9 @@ GPRAPI grpc_slice grpc_slice_ref(grpc_slice s); where dest!=NULL , then (*dest)(start, len). Requires s initialized. */ GPRAPI void grpc_slice_unref(grpc_slice s); +/* Copy slice - create a new slice that contains the same data as s */ +GPRAPI grpc_slice grpc_slice_copy(grpc_slice s); + /* Create a slice pointing at some data. Calls malloc to allocate a refcount for the object, and arranges that destroy will be called with the pointer passed in at destruction. */ @@ -75,6 +78,13 @@ GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len, call. Aborts if malloc() fails. */ GPRAPI grpc_slice grpc_slice_malloc(size_t length); +GPRAPI grpc_slice grpc_slice_malloc_large(size_t length); + +#define GRPC_SLICE_MALLOC(len) \ + ((len) <= GRPC_SLICE_INLINED_SIZE \ + ? (grpc_slice){.refcount = NULL, \ + .data.inlined = {.length = (uint8_t)(len)}} \ + : grpc_slice_malloc_large((len))) /* Intern a slice: @@ -117,6 +127,18 @@ GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end); Requires s intialized, split <= s.length */ GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split); +typedef enum { + GRPC_SLICE_REF_TAIL = 1, + GRPC_SLICE_REF_HEAD = 2, + GRPC_SLICE_REF_BOTH = 1 + 2 +} grpc_slice_ref_whom; + +/* The same as grpc_slice_split_tail, but with an option to skip altering + * refcounts (grpc_slice_split_tail_maybe_ref(..., true) is equivalent to + * grpc_slice_split_tail(...)) */ +GPRAPI grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice *s, size_t split, + grpc_slice_ref_whom ref_whom); + /* Splits s into two: modifies s to be s[split:s.length], and returns a new slice, sharing a refcount with s, that contains s[0:split]. Requires s intialized, split <= s.length */ diff --git a/include/grpc/slice_buffer.h b/include/grpc/slice_buffer.h index 2ed896645b..cdbd74776c 100644 --- a/include/grpc/slice_buffer.h +++ b/include/grpc/slice_buffer.h @@ -77,6 +77,10 @@ GPRAPI void grpc_slice_buffer_trim_end(grpc_slice_buffer *src, size_t n, /* move the first n bytes of src into dst */ GPRAPI void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst); +/* move the first n bytes of src into dst without adding references */ +GPRAPI void grpc_slice_buffer_move_first_no_ref(grpc_slice_buffer *src, + size_t n, + grpc_slice_buffer *dst); /* move the first n bytes of src into dst (copying them) */ GPRAPI void grpc_slice_buffer_move_first_into_buffer(grpc_exec_ctx *exec_ctx, grpc_slice_buffer *src, |