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.

133 lines
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - UDP Server
  4. *
  5. * Welcome to raylib!
  6. *
  7. * To test examples, just press F6 and execute raylib_compile_execute script
  8. * Note that compiled executable is placed in the same folder as .c file
  9. *
  10. * You can find all basic examples on C:\raylib\raylib\examples folder or
  11. * raylib official webpage: www.raylib.com
  12. *
  13. * Enjoy using raylib. :)
  14. *
  15. * This example has been created using raylib 2.0 (www.raylib.com)
  16. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h
  17. *for details)
  18. *
  19. * Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. #include "rnet.h"
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. float elapsed = 0.0f;
  28. float delay = 1.0f;
  29. bool ping = false;
  30. bool pong = false;
  31. const char * pingmsg = "Ping!";
  32. const char * pongmsg = "Pong!";
  33. int msglen = 0;
  34. SocketConfig server_cfg = {.host = "127.0.0.1", .port = "4950", .server = true, .type = SOCKET_UDP, .nonblocking = true};
  35. SocketResult *server_res = NULL;
  36. SocketSet * socket_set = NULL;
  37. char recvBuffer[512];
  38. // Once connected to the network, check the sockets for pending information
  39. // and when information is ready, send either a Ping or a Pong.
  40. void NetworkUpdate()
  41. {
  42. // CheckSockets
  43. //
  44. // If any of the sockets in the socket_set are pending (received data, or requests)
  45. // then mark the socket as being ready. You can check this with IsSocketReady(client_res->socket)
  46. int active = CheckSockets(socket_set, 0);
  47. if (active != 0) {
  48. TraceLog(LOG_DEBUG,
  49. "There are currently %d socket(s) with data to be processed.", active);
  50. }
  51. // IsSocketReady
  52. //
  53. // If the socket is ready, attempt to receive data from the socket
  54. // int bytesRecv = 0;
  55. // if (IsSocketReady(server_res->socket)) {
  56. // bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen);
  57. // }
  58. int bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen);
  59. // If we received data, was that data a "Ping!" or a "Pong!"
  60. if (bytesRecv > 0) {
  61. if (strcmp(recvBuffer, pingmsg) == 0) { pong = true; }
  62. if (strcmp(recvBuffer, pongmsg) == 0) { ping = true; }
  63. }
  64. // After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
  65. elapsed += GetFrameTime();
  66. if (elapsed > delay) {
  67. if (ping) {
  68. ping = false;
  69. SocketSend(server_res->socket, pingmsg, msglen);
  70. } else if (pong) {
  71. pong = false;
  72. SocketSend(server_res->socket, pongmsg, msglen);
  73. }
  74. elapsed = 0.0f;
  75. }
  76. }
  77. int main()
  78. {
  79. // Setup
  80. int screenWidth = 800;
  81. int screenHeight = 450;
  82. InitWindow(
  83. screenWidth, screenHeight, "raylib [network] example - udp server");
  84. SetTargetFPS(60);
  85. SetTraceLogLevel(LOG_DEBUG);
  86. // Networking
  87. InitNetwork();
  88. // Create the server
  89. //
  90. // Performs
  91. // getaddrinfo
  92. // socket
  93. // setsockopt
  94. // bind
  95. // listen
  96. server_res = AllocSocketResult();
  97. if (!SocketCreate(&server_cfg, server_res)) {
  98. TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d",
  99. server_res->status, server_res->socket->status);
  100. } else {
  101. if (!SocketBind(&server_cfg, server_res)) {
  102. TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d",
  103. server_res->status, server_res->socket->status);
  104. }
  105. }
  106. // Create & Add sockets to the socket set
  107. socket_set = AllocSocketSet(1);
  108. msglen = strlen(pingmsg) + 1;
  109. memset(recvBuffer, '\0', sizeof(recvBuffer));
  110. AddSocket(socket_set, server_res->socket);
  111. // Main game loop
  112. while (!WindowShouldClose()) {
  113. BeginDrawing();
  114. ClearBackground(RAYWHITE);
  115. NetworkUpdate();
  116. EndDrawing();
  117. }
  118. // Cleanup
  119. CloseWindow();
  120. return 0;
  121. }