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

« back to all changes in this revision

Viewing changes to tests/sockets/test_sockets_partial_client.c

  • 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
#include <errno.h>
 
2
#include <sys/types.h>
 
3
#include <sys/socket.h>
 
4
#include <netinet/in.h>
 
5
#include <arpa/inet.h>
 
6
#include <fcntl.h>
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
#include <unistd.h>
 
11
#include <sys/ioctl.h>
 
12
#include <assert.h>
 
13
#if EMSCRIPTEN
 
14
#include <emscripten.h>
 
15
#endif
 
16
 
 
17
int sockfd = -1;
 
18
int sum = 0;
 
19
 
 
20
void finish(int result) {
 
21
  close(sockfd);
 
22
#if EMSCRIPTEN
 
23
  REPORT_RESULT();
 
24
#endif
 
25
  exit(result);
 
26
}
 
27
 
 
28
void iter(void *arg) {
 
29
  char buffer[1024];
 
30
  char packetLength;
 
31
  fd_set fdr;
 
32
  int i;
 
33
  int res;
 
34
 
 
35
  // make sure that sockfd is ready to read
 
36
  FD_ZERO(&fdr);
 
37
  FD_SET(sockfd, &fdr);
 
38
  res = select(64, &fdr, NULL, NULL, NULL);
 
39
  if (res == -1) {
 
40
    perror("select failed");
 
41
    finish(EXIT_FAILURE);
 
42
  } else if (!FD_ISSET(sockfd, &fdr)) {
 
43
    return;
 
44
  }
 
45
 
 
46
  res = recv(sockfd, buffer, 1, 0);
 
47
  if (res == -1) {
 
48
    if (errno == EAGAIN || errno == EWOULDBLOCK) {
 
49
      return; //try again
 
50
    }
 
51
 
 
52
    perror("unexcepted end of data");
 
53
    finish(EXIT_FAILURE);
 
54
  }
 
55
 
 
56
  if (res != 1) {
 
57
    perror("should read 1 byte");
 
58
    finish(EXIT_FAILURE);
 
59
  }
 
60
 
 
61
  packetLength = buffer[0];
 
62
  res = recv(sockfd, buffer, packetLength, 0);
 
63
 
 
64
  printf("got %d,%d\n", res, packetLength);
 
65
 
 
66
  if (res != packetLength) {
 
67
    fprintf(stderr, "lost packet data, expected: %d readed: %d", packetLength, res);
 
68
    finish(EXIT_FAILURE);
 
69
  }
 
70
 
 
71
  for (i = 0; i < packetLength; ++i) {
 
72
    if (buffer[i] != i+1) {
 
73
      fprintf(stderr, "packet corrupted, expected: %d, actual: %d", i+1, buffer[i]);
 
74
      finish(EXIT_FAILURE);
 
75
    }
 
76
 
 
77
    sum += buffer[i];
 
78
  }
 
79
 
 
80
  if (packetLength == buffer[0]) { // \x01\x01 - end marker
 
81
    printf("sum: %d\n", sum);
 
82
    finish(sum);
 
83
  }
 
84
}
 
85
 
 
86
int main() {
 
87
  struct sockaddr_in addr;
 
88
  int res;
 
89
 
 
90
  sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 
91
  if (sockfd == -1) {
 
92
    perror("cannot create socket");
 
93
    exit(EXIT_FAILURE);
 
94
  }
 
95
  fcntl(sockfd, F_SETFL, O_NONBLOCK);
 
96
 
 
97
  memset(&addr, 0, sizeof(addr));
 
98
  addr.sin_family = AF_INET;
 
99
  addr.sin_port = htons(SOCKK);
 
100
  if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) {
 
101
    perror("inet_pton failed");
 
102
    finish(EXIT_FAILURE);
 
103
  }
 
104
 
 
105
  res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
 
106
  if (res == -1 && errno != EINPROGRESS) {
 
107
    perror("connect failed");
 
108
    finish(EXIT_FAILURE);
 
109
  }
 
110
 
 
111
#if EMSCRIPTEN
 
112
  emscripten_set_main_loop(iter, 0, 0);
 
113
#else
 
114
  while (1) iter(NULL);
 
115
#endif
 
116
 
 
117
  return EXIT_SUCCESS;
 
118
}
 
119