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

« back to all changes in this revision

Viewing changes to tests/websockets_partial.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 <stdio.h>
7
 
#include <stdlib.h>
8
 
#include <string.h>
9
 
#include <unistd.h>
10
 
#include <sys/ioctl.h>
11
 
#include <assert.h>
12
 
#if EMSCRIPTEN
13
 
#include <emscripten.h>
14
 
#endif
15
 
 
16
 
int SocketFD;
17
 
int done = 0;
18
 
int sum = 0;
19
 
 
20
 
void iter(void *arg) {
21
 
  char buffer[1024];
22
 
  char packetLength;
23
 
  int n;
24
 
  int i;
25
 
 
26
 
  if (done) {
27
 
    return;
28
 
  }
29
 
 
30
 
  n = recv(SocketFD, buffer, 1, 0);
31
 
 
32
 
  if (n == -1) {
33
 
    if (errno == EAGAIN || errno == EWOULDBLOCK) {
34
 
      return; //try again
35
 
    }
36
 
 
37
 
    fprintf(stderr, "unexcepted end of data");
38
 
    exit(EXIT_FAILURE);
39
 
  }
40
 
 
41
 
  if (n != 1) {
42
 
    fprintf(stderr, "should read 1 byte");
43
 
    exit(EXIT_FAILURE);
44
 
  }
45
 
 
46
 
  packetLength = buffer[0];
47
 
  n = recv(SocketFD, buffer, packetLength, 0);
48
 
 
49
 
  printf("got %d,%d\n", n, packetLength);
50
 
 
51
 
  if (n != packetLength) {
52
 
    fprintf(stderr, "lost packet data, expected: %d readed: %d", packetLength, n);
53
 
    exit(EXIT_FAILURE);
54
 
  }
55
 
 
56
 
  for (i = 0; i < packetLength; ++i) {
57
 
    if (buffer[i] != i+1) {
58
 
      fprintf(stderr, "packet corrupted, expected: %d, actual: %d", i+1, buffer[i]);
59
 
      exit(EXIT_FAILURE);
60
 
    }
61
 
 
62
 
    sum += buffer[i];
63
 
  }
64
 
 
65
 
  if (packetLength == buffer[0]) { // \x01\x01 - end marker
66
 
    shutdown(SocketFD, SHUT_RDWR);
67
 
    close(SocketFD);
68
 
    done = 1;
69
 
 
70
 
    #if EMSCRIPTEN
71
 
        printf("sum: %d\n", sum);
72
 
        int result = sum;
73
 
        REPORT_RESULT();
74
 
    #endif
75
 
  }
76
 
}
77
 
 
78
 
int main(void)
79
 
{
80
 
  struct sockaddr_in stSockAddr;
81
 
  int Res;
82
 
  SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
83
 
 
84
 
  if (-1 == SocketFD)
85
 
  {
86
 
    perror("cannot create socket");
87
 
    exit(EXIT_FAILURE);
88
 
  }
89
 
 
90
 
  memset(&stSockAddr, 0, sizeof(stSockAddr));
91
 
 
92
 
  stSockAddr.sin_family = AF_INET;
93
 
  stSockAddr.sin_port = htons(
94
 
#if EMSCRIPTEN
95
 
    8991
96
 
#else
97
 
    8990
98
 
#endif
99
 
  );
100
 
  Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
101
 
 
102
 
  if (0 > Res) {
103
 
    perror("error: first parameter is not a valid address family");
104
 
    close(SocketFD);
105
 
    exit(EXIT_FAILURE);
106
 
  } else if (0 == Res) {
107
 
    perror("char string (second parameter does not contain valid ipaddress)");
108
 
    close(SocketFD);
109
 
    exit(EXIT_FAILURE);
110
 
  }
111
 
 
112
 
  if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
113
 
    perror("connect failed");
114
 
    close(SocketFD);
115
 
    exit(EXIT_FAILURE);
116
 
 
117
 
  }
118
 
 
119
 
#if EMSCRIPTEN
120
 
  emscripten_set_main_loop(iter, 0, 0);
121
 
#else
122
 
  while (!done) iter(NULL);
123
 
#endif
124
 
 
125
 
  return EXIT_SUCCESS;
126
 
}
127