blob: 590d3d7fc1717971019b504c29da94ef6834ef69 [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001public protocol FlatBufferGRPCMessage {
2
3 /// Raw pointer which would be pointing to the beginning of the readable bytes
4 var rawPointer: UnsafeMutableRawPointer { get }
5
6 /// Size of readable bytes in the buffer
7 var size: Int { get }
8
9 init(byteBuffer: ByteBuffer)
10}
11
12/// Message is a wrapper around Buffers to to able to send Flatbuffers `Buffers` through the
13/// GRPC library
14public final class Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
15 internal var buffer: ByteBuffer
16
17 /// Returns the an object of type T that would be read from the buffer
18 public var object: T {
19 T.init(buffer, o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) + Int32(buffer.reader))
20 }
21
22 public var rawPointer: UnsafeMutableRawPointer { return buffer.memory.advanced(by: buffer.reader) }
23
24 public var size: Int { return Int(buffer.size) }
25
26 /// Initializes the message with the type Flatbuffer.Bytebuffer that is transmitted over
27 /// GRPC
28 /// - Parameter byteBuffer: Flatbuffer ByteBuffer object
29 public init(byteBuffer: ByteBuffer) {
30 buffer = byteBuffer
31 }
32
33 /// Initializes the message by copying the buffer to the message to be sent.
34 /// from the builder
35 /// - Parameter builder: FlatbufferBuilder that has the bytes created in
36 /// - Note: Use `builder.finish(offset)` before passing the builder without prefixing anything to it
37 public init(builder: inout FlatBufferBuilder) {
38 buffer = builder.sizedBuffer
39 builder.clear()
40 }
41}