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.

123 lines
4.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - UDP Client
  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 client");
  21. InitNetworkDevice(); // Init network communications
  22. const char *pingmsg = "Ping!";
  23. const char *pongmsg = "Pong!";
  24. bool ping = true;
  25. bool pong = false;
  26. float elapsed = 0.0f;
  27. float delay = 1.0f;
  28. SocketConfig clientConfig = {
  29. .host = "127.0.0.1",
  30. .port = "4950",
  31. .type = SOCKET_UDP,
  32. .nonblocking = true
  33. };
  34. SocketResult *clientResult = NULL;
  35. SocketSet *socketSet = NULL;
  36. char receiveBuffer[512] = { 0 };
  37. // Create the client: getaddrinfo + socket + setsockopt + connect (TCP only)
  38. clientResult = LoadSocketResult();
  39. if (!SocketCreate(&clientConfig, clientResult))
  40. {
  41. TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status);
  42. }
  43. // Create and add sockets to the socket set
  44. socketSet = LoadSocketSet(1);
  45. AddSocket(socketSet, clientResult->socket);
  46. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. // Once connected to the network, check the sockets for pending information
  54. // and when information is ready, send either a Ping or a Pong.
  55. // CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
  56. // then mark the socket as being ready. You can check this with IsSocketReady(clientResult->socket)
  57. int active = CheckSockets(socketSet, 0);
  58. if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
  59. // IsSocketReady, if the socket is ready, attempt to receive data from the socket
  60. int bytesRecv = 0;
  61. if (IsSocketReady(clientResult->socket)) bytesRecv = SocketReceive(clientResult->socket, receiveBuffer, (int)strlen(pingmsg) + 1);
  62. // If we received data, was that data a "Ping!" or a "Pong!"
  63. if (bytesRecv > 0)
  64. {
  65. if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
  66. if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
  67. }
  68. // After each delay has expired, send a response "Ping!" for a "Pong!" and vice-versa
  69. elapsed += GetFrameTime();
  70. if (elapsed > delay)
  71. {
  72. if (ping)
  73. {
  74. ping = false;
  75. SocketSend(clientResult->socket, pingmsg, (int)strlen(pingmsg) + 1);
  76. }
  77. else if (pong)
  78. {
  79. pong = false;
  80. SocketSend(clientResult->socket, pongmsg, (int)strlen(pongmsg) + 1);
  81. }
  82. elapsed = 0.0f;
  83. }
  84. //----------------------------------------------------------------------------------
  85. // Draw
  86. //----------------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(RAYWHITE);
  89. // TODO: Draw relevant connection info
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. //--------------------------------------------------------------------------------------
  95. CloseNetworkDevice(); // Close network communication
  96. CloseWindow(); // Close window and OpenGL context
  97. //--------------------------------------------------------------------------------------
  98. return 0;
  99. }