1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
/*
20140120
20241211 - reformated using clang-format
Jan Mojzis
Public domain.
*/
#include <unistd.h>
#include "e.h"
#include "buf.h"
#include "purge.h"
#include "packet.h"
int packet_recvisready(void) {
return buf_ready(&packet.recvbuf, PACKET_FULLLIMIT);
}
int packet_recv(void) {
long long r;
struct buf *b = &packet.recvbuf;
if (b->len < PACKET_ZEROBYTES) {
b->len = PACKET_ZEROBYTES;
purge(b->buf, PACKET_ZEROBYTES);
}
if (!packet_recvisready()) return 1;
r = read(0, b->buf + b->len, PACKET_FULLLIMIT);
if (r == 0) {
errno = ECONNRESET;
return 0;
}
if (r == -1) {
if (errno == EINTR) return 1;
if (errno == EAGAIN) return 1;
if (errno == EWOULDBLOCK) return 1;
return 0;
}
b->len += r;
return 1;
}
|