1 /* Simple binding of nanopb streams to TCP sockets.
4 #include <sys/socket.h>
11 static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
13 int fd = (int)stream->state;
14 return send(fd, buf, count, 0) == count;
17 static bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
19 int fd = (int)stream->state;
24 /* Well, this is a really inefficient way to skip input. */
25 /* It is only used when there are unknown fields. */
27 while (count-- && recv(fd, &dummy, 1, 0) == 1);
31 result = recv(fd, buf, count, MSG_WAITALL);
34 stream->bytes_left = 0; /* EOF */
36 return result == count;
39 pb_ostream_t pb_ostream_from_socket(int fd)
41 pb_ostream_t stream = {&write_callback, (void*)fd, SIZE_MAX, 0};
45 pb_istream_t pb_istream_from_socket(int fd)
47 pb_istream_t stream = {&read_callback, (void*)fd, SIZE_MAX};