1
#ifndef __TEST_SOCKETS_MSG_H__
2
#define __TEST_SOCKETS_MSG_H__
9
int do_msg_read(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t *addrlen) {
13
// read the message length
14
res = recvfrom(sockfd, &msg->length, sizeof(int), 0, (struct sockaddr *)addr, addrlen);
16
assert(errno == EAGAIN);
21
printf("do_msg_read: allocating %d bytes for message\n", msg->length);
23
msg->buffer = malloc(msg->length);
26
// read the actual message
27
int max = msg->length - offset;
28
if (length && max > length) {
31
res = recvfrom(sockfd, msg->buffer + offset, max, 0, (struct sockaddr *)addr, addrlen);
33
assert(errno == EAGAIN);
37
printf("do_msg_read: read %d bytes\n", res);
42
int do_msg_write(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t addrlen) {
45
// send the message length first
48
res = sendto(sockfd, &msg->length, sizeof(int), 0, addr, addrlen);
50
res = send(sockfd, &msg->length, sizeof(int), 0);
53
assert(errno == EAGAIN);
56
printf("do_msg_write: sending message header for %d bytes\n", msg->length);
57
assert(res == sizeof(int));
60
// then the actual message
61
int max = msg->length - offset;
62
if (length && max > length) {
66
res = sendto(sockfd, msg->buffer + offset, max, 0, addr, addrlen);
68
res = send(sockfd, msg->buffer + offset, max, 0);
71
assert(errno == EAGAIN);
75
printf("do_msg_write: wrote %d bytes %d\n", res, msg->length);
b'\\ No newline at end of file'