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.

147 lines
4.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [network] example - Network Test
  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. void test_network_initialise()
  26. {
  27. assert(InitNetwork() == true);
  28. }
  29. void test_socket_result()
  30. {
  31. SocketResult *result = AllocSocketResult();
  32. assert(result != NULL);
  33. FreeSocketResult(&result);
  34. assert(result == NULL);
  35. }
  36. void test_socket()
  37. {
  38. Socket *socket = AllocSocket();
  39. assert(socket != NULL);
  40. FreeSocket(&socket);
  41. assert(socket == NULL);
  42. }
  43. void test_resolve_ip()
  44. {
  45. const char *host = "8.8.8.8";
  46. const char *port = "8080";
  47. char ip[ADDRESS_IPV6_ADDRSTRLEN];
  48. char service[ADDRESS_MAXSERV];
  49. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  50. ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
  51. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  52. assert(strcmp(ip, "8.8.8.8") == 0);
  53. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  54. ResolveIP(host, port, NAME_INFO_DEFAULT, ip, service);
  55. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  56. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  57. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  58. ResolveIP(host, port, NAME_INFO_NOFQDN, ip, service);
  59. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  60. assert(strcmp(ip, "google-public-dns-a") == 0);
  61. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  62. ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
  63. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  64. assert(strcmp(ip, "8.8.8.8") == 0);
  65. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  66. ResolveIP(host, port, NAME_INFO_NAMEREQD, ip, service);
  67. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  68. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  69. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  70. ResolveIP(host, port, NAME_INFO_NUMERICSERV, ip, service);
  71. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  72. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  73. memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
  74. ResolveIP(host, port, NAME_INFO_DGRAM, ip, service);
  75. TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
  76. assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
  77. }
  78. void test_resolve_host()
  79. {
  80. const char * address = "localhost";
  81. const char * port = "80";
  82. AddressInformation *addr = AllocAddressList(3);
  83. int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr);
  84. assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6);
  85. assert(GetAddressFamily(addr[1]) == ADDRESS_TYPE_IPV4);
  86. assert(GetAddressSocketType(addr[0]) == 0);
  87. assert(GetAddressProtocol(addr[0]) == 0);
  88. // for (size_t i = 0; i < count; i++) { PrintAddressInfo(addr[i]); }
  89. }
  90. void test_address()
  91. {
  92. }
  93. void test_address_list()
  94. {
  95. }
  96. void test_socket_create()
  97. {
  98. SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true};
  99. Socket * socket = AllocSocket();
  100. SocketResult *server_res = AllocSocketResult();
  101. SocketSet * socket_set = AllocSocketSet(1);
  102. assert(SocketCreate(&server_cfg, server_res));
  103. assert(AddSocket(socket_set, server_res->socket));
  104. assert(SocketListen(&server_cfg, server_res));
  105. }
  106. int main()
  107. {
  108. int screenWidth = 800;
  109. int screenHeight = 450;
  110. InitWindow(
  111. screenWidth, screenHeight, "raylib [network] example - network test");
  112. SetTargetFPS(60);
  113. // Run the tests
  114. test_network_initialise();
  115. test_resolve_host();
  116. // test_socket_create();
  117. // Main game loop
  118. while (!WindowShouldClose()) {
  119. BeginDrawing();
  120. ClearBackground(RAYWHITE);
  121. DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
  122. EndDrawing();
  123. }
  124. CloseWindow();
  125. return 0;
  126. }