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
|
/*
* Copyright (C) 2008, Brian Cully <bjc@kublai.com>
*/
#include "msg.h"
#include "io.h"
#include "log.h"
#include <string.h>
ETERM *
read_msg()
{
ETERM *msg;
unsigned char *buf;
msglen_t len;
logmsg("DEBUG: reading message length.");
if (restartable_read((unsigned char *)&len, sizeof(len)) == -1) {
logmsg("ERROR: couldn't read %d byte message prefix: %s.",
sizeof(len), strerror(errno));
exit(2);
}
len = ntohl(len);
buf = (unsigned char *)malloc(len);
if (buf == NULL) {
logmsg("ERROR: Couldn't malloc %d bytes: %s.", len,
strerror(errno));
exit(2);
}
logmsg("DEBUG: reading message body (len: %d).", len);
if (restartable_read(buf, len) == -1) {
logmsg("ERROR: couldn't read %d byte message: %s.",
len, strerror(errno));
free(buf);
exit(2);
}
msg = erl_decode(buf);
free(buf);
return msg;
}
int
write_msg(ETERM *msg)
{
unsigned char *buf;
msglen_t nlen, buflen;
buflen = erl_term_len(msg);
buf = (unsigned char *)malloc(buflen);
erl_encode(msg, buf);
erl_free_term(msg);
nlen = htonl(buflen);
if (restartable_write((unsigned char *)&nlen, sizeof(nlen)) == -1) {
free(buf);
return -1;
}
if (restartable_write(buf, buflen) == -1) {
free(buf);
return -1;
}
free(buf);
return 0;
}
|