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

« back to all changes in this revision

Viewing changes to tests/enet_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 <stdio.h>
2
 
#include <emscripten.h>
3
 
 
4
 
#include <enet/enet.h>
5
 
 
6
 
ENetHost * host;
7
 
 
8
 
void main_loop() {
9
 
  static int counter = 0;
10
 
#if EMSCRIPTEN
11
 
  counter++;
12
 
#endif
13
 
  if (counter == 100) {
14
 
    printf("stop!\n");
15
 
    emscripten_cancel_main_loop();
16
 
    return;
17
 
  }
18
 
 
19
 
  ENetEvent event;
20
 
  if (enet_host_service (host, & event, 0) == 0) return;
21
 
  switch (event.type)
22
 
  {
23
 
    case ENET_EVENT_TYPE_CONNECT:
24
 
      printf ("Connection succeeded!\n");
25
 
 
26
 
      break;
27
 
    case ENET_EVENT_TYPE_RECEIVE:
28
 
      printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
29
 
              event.packet -> dataLength,
30
 
              event.packet -> data,
31
 
              event.peer -> data,
32
 
              event.channelID);
33
 
 
34
 
      int result = strcmp("packetfoo", event.packet->data);
35
 
      REPORT_RESULT();
36
 
 
37
 
      /* Clean up the packet now that we're done using it. */
38
 
      enet_packet_destroy (event.packet);
39
 
      break;
40
 
    case ENET_EVENT_TYPE_DISCONNECT:
41
 
      printf ("%s disconected.\n", event.peer -> data);
42
 
      /* Reset the peer's client information. */
43
 
      event.peer -> data = NULL;
44
 
      enet_host_destroy(host);
45
 
      break;
46
 
    default:
47
 
      printf("whaaa? %d\n", event.type);
48
 
  }
49
 
}
50
 
 
51
 
int main (int argc, char ** argv)
52
 
{
53
 
  if (enet_initialize () != 0)
54
 
  {
55
 
    fprintf (stderr, "An error occurred while initializing ENet.\n");
56
 
    return EXIT_FAILURE;
57
 
  }
58
 
  atexit (enet_deinitialize);
59
 
 
60
 
  printf("creating host\n");
61
 
 
62
 
  host = enet_host_create (NULL /* create a client host */,
63
 
                              1 /* only allow 1 outgoing connection */,
64
 
                              2 /* allow up 2 channels to be used, 0 and 1 */,
65
 
                              57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
66
 
                              14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
67
 
  if (host == NULL)
68
 
  {
69
 
    fprintf (stderr,
70
 
              "An error occurred while trying to create an ENet client host.\n");
71
 
    exit (EXIT_FAILURE);
72
 
  }
73
 
 
74
 
  ENetAddress address;
75
 
  enet_address_set_host (& address, "localhost");
76
 
#if EMSCRIPTEN
77
 
  address.port = 1237;
78
 
#else
79
 
  address.port = 1235;
80
 
#endif
81
 
 
82
 
  printf("connecting to server...\n");
83
 
 
84
 
  ENetPeer *peer = enet_host_connect (host, & address, 2, 0);
85
 
 
86
 
  if (peer == NULL)
87
 
  {
88
 
    fprintf (stderr,
89
 
    "No available peers for initiating an ENet connection.\n");
90
 
    exit (EXIT_FAILURE);
91
 
  }
92
 
 
93
 
#if EMSCRIPTEN
94
 
  emscripten_run_script("console.log('adding iframe');"
95
 
                        "var iframe = document.createElement('iframe');"
96
 
                        "iframe.src = 'server.html';"
97
 
                        "iframe.width = '100%';"
98
 
                        "iframe.height = '33%';"
99
 
                        "document.body.appendChild(iframe);"
100
 
                        "console.log('added.');");
101
 
#endif
102
 
 
103
 
  emscripten_set_main_loop(main_loop, 3, 1);
104
 
 
105
 
  return 1;
106
 
}
107