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.

161 lines
6.0 KiB

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