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.

164 lines
5.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - TCP Server
  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. bool connected = false;
  32. const char * pingmsg = "Ping!";
  33. const char * pongmsg = "Pong!";
  34. int msglen = 0;
  35. SocketConfig server_cfg = {.host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .server = true, .nonblocking = true};
  36. SocketConfig connection_cfg = {.nonblocking = true};
  37. SocketResult *server_res = NULL;
  38. SocketSet * socket_set = NULL;
  39. Socket * connection = NULL;
  40. char recvBuffer[512];
  41. // Attempt to connect to the network (Either TCP, or UDP)
  42. void NetworkConnect()
  43. {
  44. int active = CheckSockets(socket_set, 0);
  45. if (active != 0) {
  46. TraceLog(LOG_DEBUG,
  47. "There are currently %d socket(s) with data to be processed.", active);
  48. }
  49. if (active > 0) {
  50. if ((connection = SocketAccept(server_res->socket, &connection_cfg)) != NULL) {
  51. AddSocket(socket_set, connection);
  52. ping = true;
  53. connected = true;
  54. }
  55. }
  56. }
  57. // Once connected to the network, check the sockets for pending information
  58. // and when information is ready, send either a Ping or a Pong.
  59. void NetworkUpdate()
  60. {
  61. // CheckSockets
  62. //
  63. // If any of the sockets in the socket_set are pending (received data, or requests)
  64. // then mark the socket as being ready. You can check this with IsSocketReady(client_res->socket)
  65. int active = CheckSockets(socket_set, 0);
  66. if (active != 0) {
  67. TraceLog(LOG_DEBUG,
  68. "There are currently %d socket(s) with data to be processed.", active);
  69. }
  70. // IsSocketReady
  71. //
  72. // If the socket is ready, attempt to receive data from the socket
  73. int bytesRecv = 0;
  74. if (IsSocketReady(connection)) {
  75. bytesRecv = SocketReceive(connection, recvBuffer, msglen);
  76. }
  77. // If we received data, was that data a "Ping!" or a "Pong!"
  78. if (bytesRecv > 0) {
  79. if (strcmp(recvBuffer, pingmsg) == 0) { pong = true; }
  80. if (strcmp(recvBuffer, pongmsg) == 0) { ping = true; }
  81. }
  82. // After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
  83. elapsed += GetFrameTime();
  84. if (elapsed > delay) {
  85. if (ping) {
  86. ping = false;
  87. SocketSend(connection, pingmsg, msglen);
  88. } else if (pong) {
  89. pong = false;
  90. SocketSend(connection, pongmsg, msglen);
  91. }
  92. elapsed = 0.0f;
  93. }
  94. }
  95. int main()
  96. {
  97. // Setup
  98. int screenWidth = 800;
  99. int screenHeight = 450;
  100. InitWindow(
  101. screenWidth, screenHeight, "raylib [network] example - tcp server");
  102. SetTargetFPS(60);
  103. SetTraceLogLevel(LOG_DEBUG);
  104. // Networking
  105. InitNetwork();
  106. // Create the server
  107. //
  108. // Performs
  109. // getaddrinfo
  110. // socket
  111. // setsockopt
  112. // bind
  113. // listen
  114. server_res = AllocSocketResult();
  115. if (!SocketCreate(&server_cfg, server_res)) {
  116. TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d",
  117. server_res->status, server_res->socket->status);
  118. } else {
  119. if (!SocketBind(&server_cfg, server_res)) {
  120. TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d",
  121. server_res->status, server_res->socket->status);
  122. } else {
  123. if (!(server_cfg.type == SOCKET_UDP)) {
  124. if (!SocketListen(&server_cfg, server_res)) {
  125. TraceLog(LOG_WARNING,
  126. "Failed to start listen server: status %d, errno %d",
  127. server_res->status, server_res->socket->status);
  128. }
  129. }
  130. }
  131. }
  132. // Create & Add sockets to the socket set
  133. socket_set = AllocSocketSet(2);
  134. msglen = strlen(pingmsg) + 1;
  135. memset(recvBuffer, '\0', sizeof(recvBuffer));
  136. AddSocket(socket_set, server_res->socket);
  137. // Main game loop
  138. while (!WindowShouldClose()) {
  139. BeginDrawing();
  140. ClearBackground(RAYWHITE);
  141. if (connected) {
  142. NetworkUpdate();
  143. } else {
  144. NetworkConnect();
  145. }
  146. EndDrawing();
  147. }
  148. // Cleanup
  149. CloseWindow();
  150. return 0;
  151. }