I have to generate a shared library that uses protocol buffers for receiving and sending data. So, basically a function such as
@Base.ccallable function protobuf_IO(pb_in::Ptr{UInt8})::Ptr{UInt8}
...
end
that then is AOT-compiled into a shared library, e.g. using juliac
.
Obviously, I will use ProtoBuf.jl
( Home · ProtoBuf.jl) for this. The encoding part, i.e. how to return encoded data, should be no problem. Will be something along the lines of:
io = IOBuffer()
e = ProtoEncoder(io)
encode(e, <MessageInstance>)
return pointer(e.io.data)
But what about the other way 'round? The nice thing about protobuf is that I would not need to pass the information about the length of the binary data (byte array) along, since it is all encoded in the wire-transfer format. [EDIT: That’s not the case , see my follow-up post below.] But, the only suitable constructor I can see for
IOBuffer
is the one requiring an actual UInt8[...]
array, not Ptr{UInt8}
. To create the array, I’d need to know the length of the data, then unsafe_load
each value. Also, I don’t want to copy the entire data, which might be quite heavy.
The example (and docs) just illustrate the case when there already is an io::IOBuffer
, namely:
seekstart(io) # if buffer not at start yet
d = ProtoDecoder(io)
decode(d, <MessageType>)
Thanks for any help; I’m new to all that IOBuffer
-related stuff and am probably just missing something here .