~ubuntu-branches/ubuntu/saucy/emscripten/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/websockets_bi_bigdata.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

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
#if EMSCRIPTEN
 
12
#include <emscripten.h>
 
13
#endif
 
14
 
 
15
#include "websockets_bigdata.h"
 
16
 
 
17
#define EXPECTED_BYTES DATA_SIZE
 
18
 
 
19
int SocketFD;
 
20
 
 
21
unsigned int get_all_buf(int sock, char* output, unsigned int maxsize)
 
22
{
 
23
  int bytes;
 
24
  if (ioctl(sock, FIONREAD, &bytes)) return 0;
 
25
  if (bytes == 0) return 0;
 
26
 
 
27
  char buffer[EXPECTED_BYTES];
 
28
  int n;
 
29
  unsigned int offset = 0;
 
30
  while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) ||
 
31
    errno == EINTR) {
 
32
    if(n>0)
 
33
    {
 
34
      if (((unsigned int) n)+offset > maxsize) { fprintf(stderr, "too much data!"); exit(EXIT_FAILURE); }
 
35
      memcpy(output+offset, buffer, n);
 
36
      offset += n;
 
37
    }
 
38
  }
 
39
 
 
40
  if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
 
41
    fprintf(stderr, "error in get_all_buf!");
 
42
    exit(EXIT_FAILURE);
 
43
  }
 
44
  return offset;
 
45
}
 
46
 
 
47
int done = 0;
 
48
 
 
49
void iter(void *arg) {
 
50
  /* perform read write operations ... */
 
51
  static char out[EXPECTED_BYTES];
 
52
  static int pos = 0;
 
53
  printf("so far %d, expecting up to %d\n", pos, EXPECTED_BYTES-pos);
 
54
  int n = get_all_buf(SocketFD, out+pos, EXPECTED_BYTES-pos);
 
55
  if (n) printf("read! %d\n", n);
 
56
  pos += n;
 
57
  if (pos >= EXPECTED_BYTES) {
 
58
    shutdown(SocketFD, SHUT_RDWR);
 
59
 
 
60
    close(SocketFD);
 
61
 
 
62
    done = 1;
 
63
 
 
64
    emscripten_cancel_main_loop();
 
65
 
 
66
#if EMSCRIPTEN
 
67
    char *comp = generateData();
 
68
    int result = strcmp(comp, out);
 
69
    if (result != 0) {
 
70
      for (int i = 0; i < DATA_SIZE; i++) {
 
71
        printf("%d:%d\n", comp[i], out[i]);
 
72
      }
 
73
    }
 
74
    REPORT_RESULT();
 
75
#endif
 
76
  }
 
77
}
 
78
 
 
79
int main(void)
 
80
{
 
81
  printf("hello from main page\n");
 
82
 
 
83
  struct sockaddr_in stSockAddr;
 
84
  int Res;
 
85
  SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 
86
 
 
87
  if (-1 == SocketFD)
 
88
  {
 
89
    perror("cannot create socket");
 
90
    exit(EXIT_FAILURE);
 
91
  }
 
92
 
 
93
  memset(&stSockAddr, 0, sizeof(stSockAddr));
 
94
 
 
95
  stSockAddr.sin_family = AF_INET;
 
96
  stSockAddr.sin_port = htons(
 
97
#if EMSCRIPTEN
 
98
    3993
 
99
#else
 
100
    3992
 
101
#endif
 
102
  );
 
103
  Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
 
104
 
 
105
  if (0 > Res) {
 
106
    perror("error: first parameter is not a valid address family");
 
107
    close(SocketFD);
 
108
    exit(EXIT_FAILURE);
 
109
  } else if (0 == Res) {
 
110
    perror("char string (second parameter does not contain valid ipaddress)");
 
111
    close(SocketFD);
 
112
    exit(EXIT_FAILURE);
 
113
  }
 
114
 
 
115
  if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
 
116
    perror("connect failed");
 
117
    close(SocketFD);
 
118
    exit(EXIT_FAILURE);
 
119
 
 
120
  }
 
121
 
 
122
#if EMSCRIPTEN
 
123
  emscripten_run_script("console.log('adding iframe');"
 
124
                        "var iframe = document.createElement('iframe');"
 
125
                        "iframe.src = 'side.html';"
 
126
                        "iframe.width = '100%';"
 
127
                        "iframe.width = '40%';"
 
128
                        "document.body.appendChild(iframe);"
 
129
                        "console.log('added.');");
 
130
  emscripten_set_main_loop(iter, 3, 0);
 
131
#else
 
132
  while (!done) iter(NULL);
 
133
#endif
 
134
 
 
135
  return EXIT_SUCCESS;
 
136
}
 
137