-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmsg.c
executable file
·115 lines (99 loc) · 2.56 KB
/
msg.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2009 Lluís Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include "main.h"
void send_bytes(const int fd, const char *data, int bytes)
{
int res;
int offset = 0;
while(1)
{
res = send(fd, data + offset, bytes, 0);
if(res == -1)
{
warning("Sending %i bytes to %i.", bytes, fd);
break;
}
if(res == bytes)
break;
offset += res;
bytes -= res;
}
}
int recv_bytes(const int fd, char *data, int bytes)
{
int res;
int offset = 0;
while(1)
{
res = recv(fd, data + offset, bytes, 0);
if(res == -1)
{
warning("Receiving %i bytes from %i.", bytes, fd);
break;
}
if(res == bytes)
break;
offset += res;
bytes -= res;
}
return res;
}
void send_msg(const int fd, const struct Msg *m)
{
int res;
if (0)
msgdump(stderr, m);
res = send(fd, m, sizeof(*m), 0);
if(res == -1 || res != sizeof(*m))
warning_msg(m, "Sending a message to %i, sent %i bytes, should "
"send %i.", fd,
res, sizeof(*m));
}
int recv_msg(const int fd, struct Msg *m)
{
int res;
res = recv(fd, m, sizeof(*m), 0);
if(res == -1)
warning_msg(m, "Receiving a message from %i.", fd);
if (res == sizeof(*m) && 0)
msgdump(stderr, m);
if (res != sizeof(*m) && res > 0)
warning_msg(m, "Receiving a message from %i, received %i bytes, "
"should have received %i.", fd,
res, sizeof(*m));
return res;
}
void send_ints(const int fd, const int* data, int num) {
int res;
res = write(fd, &num, sizeof(int));
if (res == -1)
warning("Sending to %i.", fd);
if (num) {
res = write(fd, data, num * sizeof(int));
if (res == -1)
warning("Sending %i bytes to %i.", num * sizeof(int), fd);
}
}
int *recv_ints(const int fd, int *num) {
int res;
res = read(fd, num, sizeof(int));
if (res == -1)
warning("Receiving from %i.", fd);
int *data = 0;
if (*num) {
data = (int *) malloc(*num * sizeof(int));
res = read(fd, data, sizeof(int) * *num);
if (res == -1)
warning("Receiving %i bytes from %i.", sizeof(int) * *num, fd);
}
return data;
}