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.

141 lines
5.4 KiB

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