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.

150 lines
4.3 KiB

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