mirror of
https://github.com/google/pebble.git
synced 2025-05-27 13:33:12 +00:00
Import of the watch repository from Pebble
This commit is contained in:
commit
3b92768480
10334 changed files with 2564465 additions and 0 deletions
43
third_party/nanopb/examples/network_server/common.c
vendored
Normal file
43
third_party/nanopb/examples/network_server/common.c
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
/* Simple binding of nanopb streams to TCP sockets.
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <pb_encode.h>
|
||||
#include <pb_decode.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
||||
{
|
||||
int fd = (intptr_t)stream->state;
|
||||
return send(fd, buf, count, 0) == count;
|
||||
}
|
||||
|
||||
static bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
|
||||
{
|
||||
int fd = (intptr_t)stream->state;
|
||||
int result;
|
||||
|
||||
if (count == 0)
|
||||
return true;
|
||||
|
||||
result = recv(fd, buf, count, MSG_WAITALL);
|
||||
|
||||
if (result == 0)
|
||||
stream->bytes_left = 0; /* EOF */
|
||||
|
||||
return result == count;
|
||||
}
|
||||
|
||||
pb_ostream_t pb_ostream_from_socket(int fd)
|
||||
{
|
||||
pb_ostream_t stream = {&write_callback, (void*)(intptr_t)fd, SIZE_MAX, 0};
|
||||
return stream;
|
||||
}
|
||||
|
||||
pb_istream_t pb_istream_from_socket(int fd)
|
||||
{
|
||||
pb_istream_t stream = {&read_callback, (void*)(intptr_t)fd, SIZE_MAX};
|
||||
return stream;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue