build.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define ENET_IMPLEMENTATION
  3. #include "enet.h"
  4. #include <stdio.h>
  5. typedef struct {
  6. ENetHost *host;
  7. ENetPeer *peer;
  8. } Client;
  9. void host_server(ENetHost *server) {
  10. ENetEvent event;
  11. while (enet_host_service(server, &event, 2) > 0) {
  12. switch (event.type) {
  13. case ENET_EVENT_TYPE_CONNECT:
  14. printf("A new client connected from ::1:%u.\n", event.peer->address.port);
  15. /* Store any relevant client information here. */
  16. event.peer->data = "Client information";
  17. break;
  18. case ENET_EVENT_TYPE_RECEIVE:
  19. printf("A packet of length %zu containing %s was received from %s on channel %u.\n",
  20. event.packet->dataLength,
  21. event.packet->data,
  22. (char *)event.peer->data,
  23. event.channelID);
  24. /* Clean up the packet now that we're done using it. */
  25. enet_packet_destroy (event.packet);
  26. break;
  27. case ENET_EVENT_TYPE_DISCONNECT:
  28. printf ("%s disconnected.\n", (char *)event.peer->data);
  29. /* Reset the peer's client information. */
  30. event.peer->data = NULL;
  31. break;
  32. case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
  33. printf ("%s timeout.\n", (char *)event.peer->data);
  34. event.peer->data = NULL;
  35. break;
  36. case ENET_EVENT_TYPE_NONE: break;
  37. }
  38. }
  39. }
  40. int main() {
  41. if (enet_initialize() != 0) {
  42. printf("An error occurred while initializing ENet.\n");
  43. return 1;
  44. }
  45. #define MAX_CLIENTS 32
  46. int i = 0;
  47. ENetHost *server;
  48. Client clients[MAX_CLIENTS];
  49. ENetAddress address = {0};
  50. address.host = ENET_HOST_ANY; /* Bind the server to the default localhost. */
  51. address.port = 7777; /* Bind the server to port 7777. */
  52. /* create a server */
  53. printf("starting server...\n");
  54. server = enet_host_create(&address, MAX_CLIENTS, 2, 0, 0);
  55. if (server == NULL) {
  56. printf("An error occurred while trying to create an ENet server host.\n");
  57. return 1;
  58. }
  59. printf("starting clients...\n");
  60. for (i = 0; i < MAX_CLIENTS; ++i) {
  61. enet_address_set_host(&address, "127.0.0.1");
  62. clients[i].host = enet_host_create(NULL, 1, 2, 0, 0);
  63. clients[i].peer = enet_host_connect(clients[i].host, &address, 2, 0);
  64. if (clients[i].peer == NULL) {
  65. printf("coundlnt connect\n");
  66. return 1;
  67. }
  68. }
  69. // program will make N iterations, and then exit
  70. static int counter = 1000;
  71. do {
  72. host_server(server);
  73. ENetEvent event;
  74. for (i = 0; i < MAX_CLIENTS; ++i) {
  75. enet_host_service(clients[i].host, &event, 0);
  76. }
  77. counter--;
  78. } while (counter > 0);
  79. for (i = 0; i < MAX_CLIENTS; ++i) {
  80. enet_peer_disconnect_now(clients[i].peer, 0);
  81. enet_host_destroy(clients[i].host);
  82. }
  83. host_server(server);
  84. enet_host_destroy(server);
  85. enet_deinitialize();
  86. return 0;
  87. }