18#ifndef FLATBUFFERS_GRPC_H_
19#define FLATBUFFERS_GRPC_H_
24#include "grpc/byte_buffer_reader.h"
25#include "grpcpp/support/byte_buffer.h"
41 Message(grpc_slice slice,
bool add_ref) : slice_(add_ref ? grpc_slice_ref(slice) : slice) {}
45 Message(
Message &&other) : slice_(other.slice_) { other.slice_ = grpc_empty_slice(); }
51 grpc_slice_unref(slice_);
52 slice_ = other.slice_;
53 other.slice_ = grpc_empty_slice();
59 const uint8_t *
mutable_data()
const {
return GRPC_SLICE_START_PTR(slice_); }
61 const uint8_t *
data()
const {
return GRPC_SLICE_START_PTR(slice_); }
63 size_t size()
const {
return GRPC_SLICE_LENGTH(slice_); }
68 return verifier.VerifyBuffer<T>(
nullptr);
73 const T *
GetRoot()
const {
return flatbuffers::GetRoot<T>(
data()); }
112 swap(slice_, other.slice_);
120 slice_ = grpc_slice_malloc(
size);
121 return GRPC_SLICE_START_PTR(slice_);
128 grpc_slice_unref(slice_);
129 slice_ = grpc_empty_slice();
133 size_t in_use_back,
size_t in_use_front)
override
138 grpc_slice old_slice = slice_;
139 grpc_slice new_slice = grpc_slice_malloc(new_size);
140 uint8_t *new_p = GRPC_SLICE_START_PTR(new_slice);
141 memcpy_downward(old_p, old_size, new_p, new_size, in_use_back, in_use_front);
143 grpc_slice_unref(old_slice);
148 grpc_slice &get_slice(uint8_t *p,
size_t size)
165struct SliceAllocatorMember
196 src.SwapBufAllocator(*
this);
201 slice_allocator_.slice_ = grpc_slice_new_with_len(buf, capacity, dealloc);
205 slice_allocator_.slice_ = grpc_empty_slice();
230 slice_allocator_.swap(other.slice_allocator_);
246 slice = slice_allocator_.slice_;
247 slice_allocator_.slice_ = grpc_empty_slice();
268 auto begin = msg_data - buf_data;
269 auto end =
begin + msg_size;
271 grpc_slice slice = slice_allocator_.get_slice(buf_data, buf_size);
273 grpc_slice subslice = grpc_slice_sub(slice,
begin, end);
296template <
class T>
class SerializationTraits<
flatbuffers::grpc::Message<T>>
306 grpc_slice *slice =
const_cast<grpc_slice *
>(&msg.
BorrowSlice());
309 *buffer = grpc_raw_byte_buffer_create(slice, 1);
311 return grpc::Status::OK;
317 grpc_byte_buffer *buffer = *
reinterpret_cast<grpc_byte_buffer **
>(buf);
320 return ::grpc::Status(::grpc::StatusCode::INTERNAL,
"No payload");
323 if ((buffer->type == GRPC_BB_RAW) && (buffer->data.raw.compression == GRPC_COMPRESS_NONE) &&
324 (buffer->data.raw.slice_buffer.count == 1))
327 grpc_slice slice = buffer->data.raw.slice_buffer.slices[0];
336 grpc_byte_buffer_reader reader;
337 grpc_byte_buffer_reader_init(&reader, buffer);
338 grpc_slice slice = grpc_byte_buffer_reader_readall(&reader);
339 grpc_byte_buffer_reader_destroy(&reader);
343 grpc_byte_buffer_destroy(buffer);
344#if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
345 return ::grpc::Status::OK;
349 return ::grpc::Status::OK;
353 return ::grpc::Status(::grpc::StatusCode::INTERNAL,
"Message verification failed");
#define FLATBUFFERS_ASSERT
void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p, size_t new_size, size_t in_use_back, size_t in_use_front)
static void dealloc(void *p, size_t)
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
void Swap(FlatBufferBuilder &other)
uint8_t * ReleaseRaw(size_t &size, size_t &offset)
Get the released pointer to the serialized buffer.
MessageBuilder & operator=(FlatBufferBuilder &&src)
void Swap(MessageBuilder &other)
Message< T > GetMessage()
MessageBuilder(const MessageBuilder &other)=delete
MessageBuilder(MessageBuilder &&other)
Message< T > ReleaseMessage()
uint8_t * ReleaseRaw(size_t &size, size_t &offset, grpc_slice &slice)
MessageBuilder & operator=(MessageBuilder &&other)
MessageBuilder & operator=(const MessageBuilder &other)=delete
MessageBuilder(uoffset_t initial_size=1024)
MessageBuilder(FlatBufferBuilder &&src, void(*dealloc)(void *, size_t)=&DefaultAllocator::dealloc)
Create a MessageBuilder from a FlatBufferBuilder.
const uint8_t * mutable_data() const
Message & operator=(Message &&other)
const uint8_t * data() const
const grpc_slice & BorrowSlice() const
Message(grpc_slice slice, bool add_ref)
Message(const Message &other)=delete
const T * GetRoot() const
Message & operator=(const Message &other)=delete
SliceAllocator & operator=(SliceAllocator &&other)
virtual void deallocate(uint8_t *p, size_t size) override
SliceAllocator & operator=(const SliceAllocator &other)=delete
virtual ~SliceAllocator()
virtual uint8_t * allocate(size_t size) override
SliceAllocator(SliceAllocator &&other)
virtual uint8_t * reallocate_downward(uint8_t *old_p, size_t old_size, size_t new_size, size_t in_use_back, size_t in_use_front) override
SliceAllocator(const SliceAllocator &other)=delete
void swap(SliceAllocator &other)
void swap_allocator(vector_downward &other)
uint8_t * scratch_data() const
__global uchar * offset(const Image *img, int x, int y)