Added nanopb/nanomsg layer

This commit is contained in:
Snaipe 2015-12-10 12:40:49 +01:00
parent c064f845a4
commit 8b5f19f1d4
2 changed files with 39 additions and 0 deletions

View file

@ -209,6 +209,7 @@ set(SOURCE_FILES
src/entry/entry.c
src/protocol/criterion.pb.c
src/protocol/criterion.pb.h
src/protocol/protocol.c
src/common.h
src/config.h

38
src/protocol/protocol.c Normal file
View file

@ -0,0 +1,38 @@
#include <pb_encode.h>
#include <pb_decode.h>
#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>
static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count) {
int sock = (intptr_t) stream->state;
int result = nn_send(sock, buf, count, 0);
if (result < 0)
return false;
return (size_t) result == count;
}
static bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count) {
int sock = (intptr_t)stream->state;
int result;
result = nn_recv(sock, buf, count, 0);
if (result == 0)
stream->bytes_left = 0; /* EOF */
if (result < 0)
return false;
return (size_t) result == count;
}
pb_ostream_t pb_ostream_from_nn_socket(int sock) {
pb_ostream_t stream = {&write_callback, (void*)(intptr_t) sock, SIZE_MAX, 0, ""};
return stream;
}
pb_istream_t pb_istream_from_nn_socket(int sock) {
pb_istream_t stream = {&read_callback, (void*)(intptr_t) sock, SIZE_MAX, ""};
return stream;
}