You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - UDP Server
  4. *
  5. * This example has been created using raylib 3.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define RNET_IMPLEMENTATION
  13. #include "rnet.h"
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [network] example - udp server");
  21. InitNetworkDevice(); // Init network communications
  22. const char *pingmsg = "Ping!";
  23. const char *pongmsg = "Pong!";
  24. bool ping = false;
  25. bool pong = false;
  26. float elapsed = 0.0f;
  27. float delay = 1.0f;
  28. SocketConfig serverConfig = {
  29. .host = "127.0.0.1",
  30. .port = "4950",
  31. .server = true,
  32. .type = SOCKET_UDP,
  33. .nonblocking = true
  34. };
  35. SocketResult *serverResult = NULL;
  36. SocketSet *socketSet = NULL;
  37. char receiveBuffer[512] = { 0 };
  38. // Create the server: getaddrinfo + socket + setsockopt + bind + listen
  39. serverResult = LoadSocketResult();
  40. if (!SocketCreate(&serverConfig, serverResult)) TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", serverResult->status, serverResult->socket->status);
  41. else if (!SocketBind(&serverConfig, serverResult)) TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d", serverResult->status, serverResult->socket->status);
  42. // Create and add sockets to the socket set
  43. socketSet = LoadSocketSet(1);
  44. AddSocket(socketSet, serverResult->socket);
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. // Once connected to the network, check the sockets for pending information
  53. // and when information is ready, send either a Ping or a Pong.
  54. // CheckSockets, if any of the sockets in the set are pending (received data, or requests)
  55. // then mark the socket as being ready. You can check this with IsSocketReady(client_res->socket)
  56. int active = CheckSockets(socketSet, 0);
  57. if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
  58. // IsSocketReady, if the socket is ready, attempt to receive data from the socket
  59. // int bytesRecv = 0;
  60. // if (IsSocketReady(serverResult->socket)) {
  61. // bytesRecv = SocketReceive(serverResult->socket, receiveBuffer, msglen);
  62. // }
  63. int bytesRecv = SocketReceive(serverResult->socket, receiveBuffer, (int)strlen(pingmsg) + 1);
  64. // If we received data, is that data a "Ping!" or a "Pong!"?
  65. if (bytesRecv > 0)
  66. {
  67. if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
  68. if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
  69. }
  70. // After each delay has expired, send a response "Ping!" for a "Pong!" and vice-versa
  71. elapsed += GetFrameTime();
  72. if (elapsed > delay)
  73. {
  74. if (ping)
  75. {
  76. ping = false;
  77. SocketSend(serverResult->socket, pingmsg, (int)strlen(pingmsg) + 1);
  78. }
  79. else if (pong)
  80. {
  81. pong = false;
  82. SocketSend(serverResult->socket, pongmsg, (int)strlen(pongmsg) + 1);
  83. }
  84. elapsed = 0.0f;
  85. }
  86. //----------------------------------------------------------------------------------
  87. // Draw
  88. //----------------------------------------------------------------------------------
  89. BeginDrawing();
  90. ClearBackground(RAYWHITE);
  91. // TODO: Draw relevant connection info
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. CloseNetworkDevice(); // Close network communication
  98. CloseWindow(); // Close window and OpenGL context
  99. //--------------------------------------------------------------------------------------
  100. return 0;
  101. }