-
Notifications
You must be signed in to change notification settings - Fork 8
/
binaryio.mm
77 lines (64 loc) · 1.6 KB
/
binaryio.mm
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "binaryio.h"
#include "api.h"
ssize_t recvall(int s, void *buf, size_t size, int flags)
{
ssize_t totalreceived = 0;
ssize_t sizeleft = size;
unsigned char *buffer = (unsigned char *)buf;
// enter recvall
flags = flags | MSG_WAITALL;
while (sizeleft > 0)
{
ssize_t i = recv(s, &buffer[totalreceived], sizeleft, flags);
if (i == 0)
{
debug_log("recv returned 0\n");
return i;
}
if (i == -1)
{
debug_log("recv returned -1\n");
if (errno == EINTR)
{
debug_log("errno = EINTR\n");
i = 0;
}
else
{
debug_log("Error during recvall: %d. errno=%d\n", (int)i, errno);
return i; // read error, or disconnected
}
}
totalreceived += i;
sizeleft -= i;
}
// leave recvall
return totalreceived;
}
ssize_t sendall(int s, void *buf, size_t size, int flags)
{
ssize_t totalsent = 0;
ssize_t sizeleft = size;
unsigned char *buffer = (unsigned char *)buf;
while (sizeleft > 0)
{
ssize_t i = send(s, &buffer[totalsent], sizeleft, flags);
if (i == 0)
{
return i;
}
if (i == -1)
{
if (errno == EINTR)
i = 0;
else
{
debug_log("Error during sendall: %d. errno=%d\n", (int)i, errno);
return i;
}
}
totalsent += i;
sizeleft -= i;
}
return totalsent;
}