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.

163 lines
5.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - Network Test
  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. #include <assert.h>
  15. void test_network_initialise()
  16. {
  17. assert(InitNetworkDevice() == true);
  18. }
  19. void test_socket_result()
  20. {
  21. SocketResult *result = LoadSocketResult();
  22. assert(result != NULL);
  23. UnloadSocketResult(&result);
  24. assert(result == NULL);
  25. }
  26. void test_socket()
  27. {
  28. Socket *socket = LoadSocket();
  29. assert(socket != NULL);
  30. UnloadSocket(&socket);
  31. assert(socket == NULL);
  32. }
  33. void test_resolve_ip()
  34. {
  35. const char *host = "8.8.8.8";
  36. const char *port = "8080";
  37. char ip[ADDRESS_IPV6_ADDRSTRLEN];
  38. char service[ADDRESS_MAXSERV];
  39. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  40. ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
  41. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  42. assert(strcmp(ip, "8.8.8.8") == 0);
  43. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  44. ResolveIP(host, port, NAME_INFO_DEFAULT, ip, service);
  45. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  46. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  47. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  48. ResolveIP(host, port, NAME_INFO_NOFQDN, ip, service);
  49. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  50. assert(strcmp(ip, "google-public-dns-a") == 0);
  51. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  52. ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
  53. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  54. assert(strcmp(ip, "8.8.8.8") == 0);
  55. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  56. ResolveIP(host, port, NAME_INFO_NAMEREQD, ip, service);
  57. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  58. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  59. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  60. ResolveIP(host, port, NAME_INFO_NUMERICSERV, ip, service);
  61. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  62. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  63. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  64. ResolveIP(host, port, NAME_INFO_DGRAM, ip, service);
  65. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  66. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  67. }
  68. void test_resolve_host()
  69. {
  70. const char *address = "localhost";
  71. const char *port = "80";
  72. AddressInformation *addr = LoadAddressList(3);
  73. int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr);
  74. assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6);
  75. assert(GetAddressFamily(addr[1]) == ADDRESS_TYPE_IPV4);
  76. assert(GetAddressSocketType(addr[0]) == 0);
  77. assert(GetAddressProtocol(addr[0]) == 0);
  78. // for (size_t i = 0; i < count; i++) { PrintAddressInfo(addr[i]); }
  79. }
  80. void test_address()
  81. {
  82. }
  83. void test_address_list()
  84. {
  85. }
  86. void test_socket_create()
  87. {
  88. SocketConfig server_cfg = { .host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true };
  89. Socket *socket = LoadSocket();
  90. SocketResult *server_res = LoadSocketResult();
  91. SocketSet *socket_set = LoadSocketSet(1);
  92. assert(SocketCreate(&server_cfg, server_res));
  93. assert(AddSocket(socket_set, server_res->socket));
  94. assert(SocketListen(&server_cfg, server_res));
  95. }
  96. int main(void)
  97. {
  98. // Initialization
  99. //--------------------------------------------------------------------------------------
  100. const int screenWidth = 800;
  101. const int screenHeight = 450;
  102. InitWindow(screenWidth, screenHeight, "raylib [network] example - network test");
  103. InitNetworkDevice(); // Init network communications
  104. // Run some tests
  105. test_resolve_host();
  106. //test_socket_create();
  107. //test_resolve_ip();
  108. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  109. //--------------------------------------------------------------------------------------
  110. // Main game loop
  111. while (!WindowShouldClose()) // Detect window close button or ESC key
  112. {
  113. // Update
  114. //----------------------------------------------------------------------------------
  115. // TODO: Update your variables here
  116. //----------------------------------------------------------------------------------
  117. // Draw
  118. //----------------------------------------------------------------------------------
  119. BeginDrawing();
  120. ClearBackground(RAYWHITE);
  121. // TODO: Draw relevant connection info
  122. EndDrawing();
  123. //----------------------------------------------------------------------------------
  124. }
  125. // De-Initialization
  126. //--------------------------------------------------------------------------------------
  127. CloseNetworkDevice(); // Close network communication
  128. CloseWindow(); // Close window and OpenGL context
  129. //--------------------------------------------------------------------------------------
  130. return 0;
  131. }