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
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - UDP Client
  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 client_cfg = {.host = "127.0.0.1", .port = "4950", .type = SOCKET_UDP, .nonblocking = true};
  35. SocketResult *client_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(client_res->socket)) {
  56. bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen);
  57. }
  58. // If we received data, was that data a "Ping!" or a "Pong!"
  59. if (bytesRecv > 0) {
  60. if (strcmp(recvBuffer, pingmsg) == 0) { pong = true; }
  61. if (strcmp(recvBuffer, pongmsg) == 0) { ping = true; }
  62. }
  63. // After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
  64. elapsed += GetFrameTime();
  65. if (elapsed > delay) {
  66. if (ping) {
  67. ping = false;
  68. SocketSend(client_res->socket, pingmsg, msglen);
  69. } else if (pong) {
  70. pong = false;
  71. SocketSend(client_res->socket, pongmsg, msglen);
  72. }
  73. elapsed = 0.0f;
  74. }
  75. }
  76. int main()
  77. {
  78. // Setup
  79. int screenWidth = 800;
  80. int screenHeight = 450;
  81. InitWindow(
  82. screenWidth, screenHeight, "raylib [network] example - udp client");
  83. SetTargetFPS(60);
  84. SetTraceLogLevel(LOG_DEBUG);
  85. // Networking
  86. InitNetwork();
  87. // Create the client
  88. //
  89. // Performs
  90. // getaddrinfo
  91. // socket
  92. // setsockopt
  93. // connect (TCP only)
  94. client_res = AllocSocketResult();
  95. if (!SocketCreate(&client_cfg, client_res)) {
  96. TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d",
  97. client_res->status, client_res->socket->status);
  98. }
  99. // Create & Add sockets to the socket set
  100. socket_set = AllocSocketSet(1);
  101. msglen = strlen(pingmsg) + 1;
  102. ping = true;
  103. memset(recvBuffer, '\0', sizeof(recvBuffer));
  104. AddSocket(socket_set, client_res->socket);
  105. // Main game loop
  106. while (!WindowShouldClose()) {
  107. BeginDrawing();
  108. ClearBackground(RAYWHITE);
  109. NetworkUpdate();
  110. EndDrawing();
  111. }
  112. // Cleanup
  113. CloseWindow();
  114. return 0;
  115. }