PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libUPnP/Neptune/Source/Tests/Sockets1/SocketsTest1.cpp

https://github.com/energy6/xbmc
C++ | 283 lines | 225 code | 26 blank | 32 comment | 25 complexity | 6144f4dfc5d804a3ab00b57d972adc9f MD5 | raw file
  1. /*****************************************************************
  2. |
  3. | Sockets Test Program 1
  4. |
  5. | (c) 2001-2010 Gilles Boccon-Gibod
  6. | Author: Gilles Boccon-Gibod (bok@bok.net)
  7. |
  8. ****************************************************************/
  9. /*----------------------------------------------------------------------
  10. | includes
  11. +---------------------------------------------------------------------*/
  12. #include "Neptune.h"
  13. #include "NptDebug.h"
  14. #if defined(WIN32) && defined(_DEBUG)
  15. #include <crtdbg.h>
  16. #endif
  17. #define CHECK(x) { \
  18. if (!(x)) { \
  19. printf("TEST FAILED line %d\n", __LINE__); \
  20. return 1; \
  21. } \
  22. }
  23. NPT_IpAddress RemoteIpAddress;
  24. /*----------------------------------------------------------------------
  25. | TcpServerThread
  26. +---------------------------------------------------------------------*/
  27. class TcpServerThread : public NPT_Thread
  28. {
  29. public:
  30. TcpServerThread() : m_Socket(NPT_SOCKET_FLAG_CANCELLABLE), m_Interrupted(false) {}
  31. void Run() {
  32. NPT_Console::Output("{02} waiting for connection on port 10000\n");
  33. NPT_SocketAddress address(NPT_IpAddress::Any, 10000);
  34. NPT_Result result = m_Socket.Bind(address, true);
  35. m_Ready.SetValue(1);
  36. if (NPT_FAILED(result)) {
  37. NPT_Console::OutputF("bind failed (%d) (%s)\n", result, NPT_ResultText(result));
  38. return;
  39. }
  40. NPT_Socket* client = NULL;
  41. result = m_Socket.WaitForNewClient(client);
  42. NPT_Console::Output("{02} client connected\n");
  43. for (;;) {
  44. NPT_System::Sleep(1.0);
  45. if (m_Interrupted) {
  46. NPT_Console::Output("{02} thread interrupted\n");
  47. break;
  48. }
  49. }
  50. delete client;
  51. NPT_Console::Output("{02} tcp server thread done\n");
  52. }
  53. NPT_TcpServerSocket m_Socket;
  54. NPT_SharedVariable m_Ready;
  55. bool m_Interrupted;
  56. };
  57. /*----------------------------------------------------------------------
  58. | CancellerThread
  59. +---------------------------------------------------------------------*/
  60. class CancellerThread : public NPT_Thread
  61. {
  62. public:
  63. CancellerThread(NPT_Socket* socket, float delay, bool shutdown) :
  64. m_Socket(socket),
  65. m_Delay(delay),
  66. m_Shutdown(shutdown) {
  67. Start();
  68. m_Ready.WaitUntilEquals(1);
  69. }
  70. void Run() {
  71. NPT_Console::OutputF("[XX] will cancel socket in %f\n", m_Delay);
  72. m_Ready.SetValue(1);
  73. NPT_System::Sleep(m_Delay);
  74. NPT_Console::OutputF("[XX] cancelling socket (shutdown=%s)\n", m_Shutdown?"yes":"no");
  75. m_Socket->Cancel(m_Shutdown);
  76. }
  77. NPT_Socket* m_Socket;
  78. float m_Delay;
  79. bool m_Shutdown;
  80. NPT_SharedVariable m_Ready;
  81. };
  82. /*----------------------------------------------------------------------
  83. | main
  84. +---------------------------------------------------------------------*/
  85. int
  86. main(int /*argc*/, char** /*argv*/)
  87. {
  88. // setup debugging
  89. #if defined(WIN32) && defined(_DEBUG)
  90. int flags = _crtDbgFlag |
  91. _CRTDBG_ALLOC_MEM_DF |
  92. _CRTDBG_DELAY_FREE_MEM_DF |
  93. _CRTDBG_CHECK_ALWAYS_DF;
  94. _CrtSetDbgFlag(flags);
  95. //AllocConsole();
  96. //freopen("CONOUT$", "w", stdout);
  97. #endif
  98. NPT_Result result;
  99. TcpServerThread* server_thread = NULL;
  100. NPT_TcpClientSocket* tcp_client = NULL;
  101. NPT_TcpServerSocket* tcp_server = NULL;
  102. CancellerThread* canceller = NULL;
  103. NPT_SocketAddress address(NPT_IpAddress(127,0,0,1), 10000);
  104. result = RemoteIpAddress.ResolveName("www.google.com");
  105. CHECK(result == NPT_SUCCESS);
  106. NPT_Console::Output("--- test for immediate connection\n");
  107. NPT_Console::Output("[01] starting write server thread\n");
  108. server_thread = new TcpServerThread();
  109. server_thread->Start();
  110. NPT_Console::Output("[01] waiting for server to be ready...\n");
  111. server_thread->m_Ready.WaitUntilEquals(1);
  112. NPT_Console::Output("[01] server thread ready\n");
  113. NPT_Console::Output("[01] waiting a while...\n");
  114. NPT_System::Sleep(3.0);
  115. tcp_client = new NPT_TcpClientSocket();
  116. NPT_Console::Output("[01] connection to 127.0.0.1:10000\n");
  117. result = tcp_client->Connect(address);
  118. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  119. CHECK(result == NPT_SUCCESS);
  120. delete tcp_client;
  121. NPT_Console::Output("[01] terminating server\n");
  122. server_thread->m_Interrupted = true;
  123. server_thread->Wait();
  124. delete server_thread;
  125. NPT_Console::Output("\n--- test for refused local connection\n");
  126. address.SetPort(89);
  127. tcp_client = new NPT_TcpClientSocket();
  128. NPT_Console::Output("[01] connecting to 127.0.0.1:89\n");
  129. result = tcp_client->Connect(address);
  130. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  131. CHECK(result == NPT_ERROR_CONNECTION_REFUSED);
  132. delete tcp_client;
  133. /*NPT_Console::Output("\n--- test for refused remote connection\n");
  134. address.SetIpAddress(RemoteIpAddress);
  135. address.SetPort(81);
  136. tcp_client = new NPT_TcpClientSocket();
  137. NPT_Console::Output("[01] connecting to www.google.com:81\n");
  138. result = tcp_client->Connect(address);
  139. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  140. CHECK(result == NPT_ERROR_CONNECTION_REFUSED);
  141. delete tcp_client;*/
  142. NPT_Console::Output("\n--- test for connection timeout\n");
  143. address.SetIpAddress(NPT_IpAddress(1,1,1,1));
  144. NPT_Console::Output("[01] connecting to 1.1.1.1:89\n");
  145. tcp_client = new NPT_TcpClientSocket();
  146. result = tcp_client->Connect(address, 3000);
  147. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  148. CHECK(result == NPT_ERROR_TIMEOUT);
  149. delete tcp_client;
  150. NPT_Console::Output("\n--- test for remote connection\n");
  151. address.SetIpAddress(RemoteIpAddress);
  152. address.SetPort(80);
  153. NPT_Console::Output("[01] connecting to www.google.com:80\n");
  154. tcp_client = new NPT_TcpClientSocket();
  155. result = tcp_client->Connect(address);
  156. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  157. CHECK(result == NPT_SUCCESS);
  158. delete tcp_client;
  159. for (int i=0; i<2; i++) {
  160. NPT_Console::OutputF("\n--- test for cancelled connection, shutdown=%d\n", i);
  161. address.SetIpAddress(NPT_IpAddress(1,1,1,1));
  162. address.SetPort(89);
  163. NPT_Console::Output("[01] connecting to 1.1.1.1:89\n");
  164. tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE);
  165. canceller = new CancellerThread(tcp_client, 3.0f, i==1);
  166. result = tcp_client->Connect(address);
  167. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  168. CHECK(result == NPT_ERROR_CANCELLED);
  169. canceller->Wait();
  170. delete canceller;
  171. delete tcp_client;
  172. }
  173. for (int i=0; i<2; i++) {
  174. NPT_Console::OutputF("\n--- testing read cancellation, shutdown=%d\n", i);
  175. address.SetIpAddress(RemoteIpAddress);
  176. address.SetPort(80);
  177. NPT_Console::Output("[01] connecting to www.google.com:80\n");
  178. tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE);
  179. result = tcp_client->Connect(address);
  180. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  181. CHECK(result == NPT_SUCCESS);
  182. canceller = new CancellerThread(tcp_client, 3.0f, i==1);
  183. NPT_InputStreamReference input;
  184. tcp_client->GetInputStream(input);
  185. unsigned char buffer[4096];
  186. NPT_SetMemory(buffer, 0, sizeof(buffer));
  187. result = input->Read(buffer, 4096);
  188. NPT_Console::OutputF("{00} read returned %d (%s)\n", result, NPT_ResultText(result));
  189. CHECK(result == NPT_ERROR_CANCELLED);
  190. delete tcp_client;
  191. canceller->Wait();
  192. delete canceller;
  193. }
  194. for (int i=0; i<2; i++) {
  195. NPT_Console::OutputF("\n--- testing write cancellation, shutdown=%d\n", i);
  196. server_thread = new TcpServerThread();
  197. server_thread->Start();
  198. NPT_Console::Output("[01] waiting for server to be ready...\n");
  199. server_thread->m_Ready.WaitUntilEquals(1);
  200. NPT_Console::Output("[01] server thread ready\n");
  201. NPT_Console::Output("[01] waiting a while...\n");
  202. NPT_System::Sleep(3.0);
  203. address.SetIpAddress(NPT_IpAddress(127,0,0,1));
  204. address.SetPort(10000);
  205. NPT_Console::Output("[01] connecting to localhost:10000\n");
  206. tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE);
  207. result = tcp_client->Connect(address);
  208. NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
  209. CHECK(result == NPT_SUCCESS);
  210. canceller = new CancellerThread(tcp_client, 3.0f, i==1);
  211. NPT_OutputStreamReference output;
  212. tcp_client->GetOutputStream(output);
  213. NPT_Size total_written = 0;
  214. unsigned char buffer[4096];
  215. NPT_SetMemory(buffer, 0, sizeof(buffer));
  216. do {
  217. NPT_Size bytes_written = 0;
  218. result = output->Write(buffer, 4096, &bytes_written);
  219. if (NPT_SUCCEEDED(result)) {
  220. total_written += bytes_written;
  221. }
  222. } while (NPT_SUCCEEDED(result));
  223. output = NULL;
  224. NPT_Console::OutputF("{01} write returned %d (%s)\n", result, NPT_ResultText(result));
  225. NPT_Console::OutputF("{01} wrote %d bytes total\n", total_written);
  226. CHECK(result == NPT_ERROR_CANCELLED);
  227. delete tcp_client;
  228. canceller->Wait();
  229. delete canceller;
  230. server_thread->m_Interrupted = true;
  231. server_thread->Wait();
  232. delete server_thread;
  233. }
  234. for (int i=0; i<2; i++) {
  235. NPT_Console::OutputF("\n--- testing accept cancellation, shutdown=%d\n", i);
  236. NPT_Console::Output("{03} waiting for connection on port 10000\n");
  237. address.SetIpAddress(NPT_IpAddress(127,0,0,1));
  238. address.SetPort(10000);
  239. tcp_server = new NPT_TcpServerSocket(NPT_SOCKET_FLAG_CANCELLABLE);
  240. result = tcp_server->Bind(address, true);
  241. CHECK(result == NPT_SUCCESS);
  242. canceller = new CancellerThread(tcp_server, 3.0f, i==1);
  243. NPT_Socket* new_client = NULL;
  244. result = tcp_server->WaitForNewClient(new_client);
  245. NPT_Console::OutputF("{03} WaitForNewClient returned %d (%s)\n", result, NPT_ResultText(result));
  246. CHECK(result == NPT_ERROR_CANCELLED);
  247. canceller->Wait();
  248. delete canceller;
  249. delete tcp_server;
  250. }
  251. NPT_Console::Output("------------\n");
  252. NPT_Console::Output("bye bye\n");
  253. #if defined(WIN32) && defined(_DEBUG)
  254. _CrtDumpMemoryLeaks();
  255. #endif
  256. return 0;
  257. }