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

« back to all changes in this revision

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