~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/sockets/test_sockets_msg.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __TEST_SOCKETS_MSG_H__
 
2
#define __TEST_SOCKETS_MSG_H__
 
3
 
 
4
typedef struct {
 
5
  char *buffer;
 
6
  int length;
 
7
} msg_t;
 
8
 
 
9
int do_msg_read(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t *addrlen) {
 
10
  int res;
 
11
 
 
12
  if (!msg->length) {
 
13
    // read the message length
 
14
    res = recvfrom(sockfd, &msg->length, sizeof(int), 0, (struct sockaddr *)addr, addrlen);
 
15
    if (res == -1) {
 
16
      assert(errno == EAGAIN);
 
17
      return res;
 
18
    }
 
19
    assert(res != 0);
 
20
 
 
21
    printf("do_msg_read: allocating %d bytes for message\n", msg->length);
 
22
 
 
23
    msg->buffer = malloc(msg->length);
 
24
  }
 
25
 
 
26
  // read the actual message
 
27
  int max = msg->length - offset;
 
28
  if (length && max > length) {
 
29
    max = length;
 
30
  }
 
31
  res = recvfrom(sockfd, msg->buffer + offset, max, 0, (struct sockaddr *)addr, addrlen);
 
32
  if (res == -1) {
 
33
    assert(errno == EAGAIN);
 
34
    return res;
 
35
  }
 
36
 
 
37
  printf("do_msg_read: read %d bytes\n", res);
 
38
 
 
39
  return res;
 
40
}
 
41
 
 
42
int do_msg_write(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t addrlen) {
 
43
  int res;
 
44
 
 
45
  // send the message length first
 
46
  if (!offset) {
 
47
    if (addr) {
 
48
      res = sendto(sockfd, &msg->length, sizeof(int), 0, addr, addrlen);
 
49
    } else {
 
50
      res = send(sockfd, &msg->length, sizeof(int), 0);
 
51
    }
 
52
    if (res == -1) {
 
53
      assert(errno == EAGAIN);
 
54
      return res;
 
55
    }
 
56
    printf("do_msg_write: sending message header for %d bytes\n", msg->length);
 
57
    assert(res == sizeof(int));
 
58
  }
 
59
 
 
60
  // then the actual message
 
61
  int max = msg->length - offset;
 
62
  if (length && max > length) {
 
63
    max = length;
 
64
  }
 
65
  if (addr) {
 
66
    res = sendto(sockfd, msg->buffer + offset, max, 0, addr, addrlen);
 
67
  } else {
 
68
    res = send(sockfd, msg->buffer + offset, max, 0);
 
69
  }
 
70
  if (res == -1) {
 
71
    assert(errno == EAGAIN);
 
72
    return res;
 
73
  }
 
74
 
 
75
  printf("do_msg_write: wrote %d bytes %d\n", res, msg->length);
 
76
 
 
77
  return res;
 
78
}
 
79
 
 
80
#endif
 
 
b'\\ No newline at end of file'