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.

211 lines
7.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - Client/Server ping-pong
  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. float elapsed = 0.0f;
  15. float delay = 1.0f;
  16. bool ping = false;
  17. bool pong = false;
  18. bool connected = false;
  19. bool clientConnected = false;
  20. const char *pingmsg = "Ping!";
  21. const char *pongmsg = "Pong!";
  22. int msglen = 0;
  23. SocketConfig serverConfig = { .host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .server = true, .nonblocking = true };
  24. SocketConfig clientConfig = { .host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .nonblocking = true };
  25. SocketConfig connectionConfig = { .nonblocking = true };
  26. SocketResult *serverResult = NULL;
  27. SocketResult *clientResult = NULL;
  28. SocketSet *socketSet = NULL;
  29. Socket *connection = NULL;
  30. char receiveBuffer[512] = { 0 };
  31. // Attempt to connect to the network (Either TCP, or UDP)
  32. static void NetworkConnect(void)
  33. {
  34. // If the server is configured as UDP, ignore connection requests
  35. if ((serverConfig.type == SOCKET_UDP) && (clientConfig.type == SOCKET_UDP))
  36. {
  37. ping = true;
  38. connected = true;
  39. }
  40. else
  41. {
  42. // If the client is connected, run the server code to check for a connection
  43. if (clientConnected)
  44. {
  45. int active = CheckSockets(socketSet, 0);
  46. if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
  47. if (active > 0)
  48. {
  49. if ((connection = SocketAccept(serverResult->socket, &connectionConfig)) != NULL)
  50. {
  51. AddSocket(socketSet, connection);
  52. connected = true;
  53. ping = true;
  54. }
  55. }
  56. }
  57. else
  58. {
  59. // Check if we're connected every _delay_ seconds
  60. elapsed += GetFrameTime();
  61. if (elapsed > delay)
  62. {
  63. if (IsSocketConnected(clientResult->socket)) clientConnected = true;
  64. elapsed = 0.0f;
  65. }
  66. }
  67. }
  68. }
  69. // Once connected to the network, check the sockets for pending information
  70. // and when information is ready, send either a Ping or a Pong.
  71. static void UpdateNetwork(void)
  72. {
  73. // CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
  74. // then mark the socket as being ready. You can check this with IsSocketReady(clientResult->socket)
  75. int active = CheckSockets(socketSet, 0);
  76. if (active != 0) TraceLog(LOG_DEBUG, "There are currently %d socket(s) with data to be processed.", active);
  77. // IsSocketReady, if the socket is ready, attempt to receive data from the socket
  78. int bytesRecv = 0;
  79. if ((serverConfig.type == SOCKET_UDP) && (clientConfig.type == SOCKET_UDP))
  80. {
  81. if (IsSocketReady(clientResult->socket)) bytesRecv = SocketReceive(clientResult->socket, receiveBuffer, msglen);
  82. if (IsSocketReady(serverResult->socket)) bytesRecv = SocketReceive(serverResult->socket, receiveBuffer, msglen);
  83. }
  84. else if (IsSocketReady(connection)) bytesRecv = SocketReceive(connection, receiveBuffer, msglen);
  85. // If we received data, was that data a "Ping!" or a "Pong!"
  86. if (bytesRecv > 0)
  87. {
  88. if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
  89. if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
  90. }
  91. // After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
  92. elapsed += GetFrameTime();
  93. if (elapsed > delay)
  94. {
  95. if (ping)
  96. {
  97. ping = false;
  98. if (serverConfig.type == SOCKET_UDP && clientConfig.type == SOCKET_UDP) SocketSend(clientResult->socket, pingmsg, msglen);
  99. else SocketSend(clientResult->socket, pingmsg, msglen);
  100. }
  101. else if (pong)
  102. {
  103. pong = false;
  104. if (serverConfig.type == SOCKET_UDP && clientConfig.type == SOCKET_UDP) SocketSend(clientResult->socket, pongmsg, msglen);
  105. else SocketSend(clientResult->socket, pongmsg, msglen);
  106. }
  107. elapsed = 0.0f;
  108. }
  109. }
  110. int main(void)
  111. {
  112. // Initialization
  113. //--------------------------------------------------------------------------------------
  114. const int screenWidth = 800;
  115. const int screenHeight = 450;
  116. InitWindow(screenWidth, screenHeight, "raylib [network] example - ping pong");
  117. InitNetworkDevice(); // Init network communications
  118. // Create the server: getaddrinfo + socket + setsockopt + bind + listen
  119. serverResult = LoadSocketResult();
  120. if (!SocketCreate(&serverConfig, serverResult))
  121. {
  122. TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", serverResult->status, serverResult->socket->status);
  123. }
  124. else
  125. {
  126. if (!SocketBind(&serverConfig, serverResult))
  127. {
  128. TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d", serverResult->status, serverResult->socket->status);
  129. }
  130. else
  131. {
  132. if (!(serverConfig.type == SOCKET_UDP))
  133. {
  134. if (!SocketListen(&serverConfig, serverResult))
  135. {
  136. TraceLog(LOG_WARNING, "Failed to start listen server: status %d, errno %d", serverResult->status, serverResult->socket->status);
  137. }
  138. }
  139. }
  140. }
  141. // Create the client: getaddrinfo + socket + setsockopt + connect (TCP only)
  142. clientResult = LoadSocketResult();
  143. if (!SocketCreate(&clientConfig, clientResult))
  144. {
  145. TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status);
  146. }
  147. else
  148. {
  149. if (!(clientConfig.type == SOCKET_UDP))
  150. {
  151. if (!SocketConnect(&clientConfig, clientResult))
  152. {
  153. TraceLog(LOG_WARNING, "Failed to connect to server: status %d, errno %d", clientResult->status, clientResult->socket->status);
  154. }
  155. }
  156. }
  157. // Create and add sockets to the socket set
  158. socketSet = LoadSocketSet(3);
  159. AddSocket(socketSet, serverResult->socket);
  160. AddSocket(socketSet, clientResult->socket);
  161. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  162. //--------------------------------------------------------------------------------------
  163. // Main game loop
  164. while (!WindowShouldClose()) // Detect window close button or ESC key
  165. {
  166. // Update
  167. //----------------------------------------------------------------------------------
  168. if (connected) UpdateNetwork();
  169. //else NetworkConnect();
  170. //----------------------------------------------------------------------------------
  171. // Draw
  172. //----------------------------------------------------------------------------------
  173. BeginDrawing();
  174. ClearBackground(RAYWHITE);
  175. // TODO: Draw relevant connection info
  176. EndDrawing();
  177. //----------------------------------------------------------------------------------
  178. }
  179. // De-Initialization
  180. //--------------------------------------------------------------------------------------
  181. CloseNetworkDevice(); // Close network communication
  182. CloseWindow(); // Close window and OpenGL context
  183. //--------------------------------------------------------------------------------------
  184. return 0;
  185. }