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

« back to all changes in this revision

Viewing changes to tests/sockets/test_enet_server.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
// g++ -fpermissive ../enet_server.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_server ; g++ ../enet_client.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_client
 
2
 
 
3
#include <stdio.h>
 
4
#include <string.h>
 
5
#include <enet/enet.h>
 
6
 
 
7
#if EMSCRIPTEN
 
8
#include <emscripten.h>
 
9
#endif
 
10
 
 
11
ENetHost *host;
 
12
 
 
13
void send_msg(ENetPeer *peer) {
 
14
   /* Create a reliable packet of size 7 containing "packet\0" */
 
15
  ENetPacket * packet = enet_packet_create ("packet",
 
16
                                            strlen ("packet") + 1,
 
17
                                            ENET_PACKET_FLAG_RELIABLE);
 
18
  /* Extend the packet so and append the string "foo", so it now */
 
19
  /* contains "packetfoo\0" */
 
20
  enet_packet_resize (packet, strlen ("packetfoo") + 1);
 
21
  strcpy (& packet -> data [strlen ("packet")], "foo");
 
22
  /* Send the packet to the peer over channel id 0. */
 
23
  /* One could also broadcast the packet by */
 
24
  /* enet_host_broadcast (host, 0, packet); */
 
25
  enet_peer_send (peer, 0, packet);
 
26
  /* One could just use enet_host_service() instead. */
 
27
  enet_host_flush (host);
 
28
}
 
29
 
 
30
void main_loop() {
 
31
  static int counter = 0;
 
32
#if EMSCRIPTEN
 
33
  counter++;
 
34
#endif
 
35
  if (counter == 100) {
 
36
    printf("stop!\n");
 
37
#if EMSCRIPTEN
 
38
    emscripten_cancel_main_loop();
 
39
#endif
 
40
    return;
 
41
  }
 
42
 
 
43
  ENetEvent event;
 
44
//printf("enet host?\n");
 
45
  if (enet_host_service (host, & event, 0) == 0) return;
 
46
printf("enet host, got event of type %d\n", event.type);
 
47
  switch (event.type)
 
48
  {
 
49
    case ENET_EVENT_TYPE_CONNECT:
 
50
      printf ("A new client connected from %x:%u.\n",
 
51
              event.peer -> address.host,
 
52
              event.peer -> address.port);
 
53
      /* Store any relevant client information here. */
 
54
      event.peer -> data = "Client information";
 
55
 
 
56
      send_msg(event.peer);
 
57
 
 
58
      break;
 
59
    case ENET_EVENT_TYPE_RECEIVE:
 
60
      printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
 
61
              event.packet -> dataLength,
 
62
              event.packet -> data,
 
63
              event.peer -> data,
 
64
              event.channelID);
 
65
      /* Clean up the packet now that we're done using it. */
 
66
      enet_packet_destroy (event.packet);
 
67
      break;
 
68
    case ENET_EVENT_TYPE_DISCONNECT:
 
69
      printf ("%s disconected.\n", event.peer -> data);
 
70
      /* Reset the peer's client information. */
 
71
      event.peer -> data = NULL;
 
72
      enet_host_destroy(host);
 
73
      break;
 
74
    default:
 
75
      printf("whaaa? %d\n", event.type);
 
76
  }
 
77
}
 
78
 
 
79
int main (int argc, char ** argv)
 
80
{
 
81
  if (enet_initialize () != 0)
 
82
  {
 
83
    fprintf (stderr, "An error occurred while initializing ENet.\n");
 
84
    return EXIT_FAILURE;
 
85
  }
 
86
  atexit (enet_deinitialize);
 
87
 
 
88
  ENetAddress address;
 
89
  address.host = ENET_HOST_ANY;
 
90
  address.port = SOCKK;
 
91
  printf("create!\n");
 
92
  host = enet_host_create (& address /* the address to bind the server host to */,
 
93
                             32 /* allow up to 32 clients and/or outgoing connections */,
 
94
                             2 /* allow up to 2 channels to be used, 0 and 1 */,
 
95
                             0 /* assume any amount of incoming bandwidth */,
 
96
                             0 /* assume any amount of outgoing bandwidth */);
 
97
  if (host == NULL)
 
98
  {
 
99
    fprintf (stderr,
 
100
    "An error occurred while trying to create an ENet server host.\n");
 
101
    exit (EXIT_FAILURE);
 
102
  }
 
103
 
 
104
#if EMSCRIPTEN
 
105
  emscripten_set_main_loop(main_loop, 3, 1);
 
106
#else
 
107
  while (1) main_loop();
 
108
#endif
 
109
 
 
110
  return 1;
 
111
}
 
112