testNet.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platformNet.h"
  23. #include "unit/test.h"
  24. #include "core/util/journal/process.h"
  25. using namespace UnitTesting;
  26. CreateUnitTest( TestTCPRequest, "Platform/Net/TCPRequest")
  27. {
  28. NetSocket mSocket;
  29. S32 mDataRecved;
  30. void handleNotify(NetSocket sock, U32 state)
  31. {
  32. // Only consider our own socket.
  33. if(mSocket != sock)
  34. return;
  35. // Ok - what's the state? We do some dumb responses to given states
  36. // in order to fulfill the request.
  37. if(state == Net::Connected)
  38. {
  39. U8 reqBuffer[] = {
  40. "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
  41. };
  42. Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
  43. test(e == Net::NoError, "Got an error sending our HTTP request!");
  44. }
  45. else if(state == Net::Disconnected)
  46. {
  47. Process::requestShutdown();
  48. mSocket = NULL;
  49. }
  50. }
  51. void handleReceive(NetSocket sock, RawData incomingData)
  52. {
  53. // Only consider our own socket.
  54. if(mSocket != sock)
  55. return;
  56. char buff[4096];
  57. dMemcpy(buff, incomingData.data, incomingData.size);
  58. buff[incomingData.size] = 0;
  59. UnitPrint("Got a message...\n");
  60. UnitPrint(buff);
  61. UnitPrint("------\n");
  62. mDataRecved += incomingData.size;
  63. }
  64. void run()
  65. {
  66. mSocket = InvalidSocket;
  67. mDataRecved = 0;
  68. // Initialize networking - done by initLibraries currently
  69. //test(Net::init(), "Failed to initialize networking!");
  70. // Hook into the signals.
  71. Net::smConnectionNotify. notify(this, &TestTCPRequest::handleNotify);
  72. Net::smConnectionReceive.notify(this, &TestTCPRequest::handleReceive);
  73. // Open a TCP connection to garagegames.com
  74. mSocket = Net::openConnectTo("ip:72.246.107.193:80");
  75. U32 limit = Platform::getRealMilliseconds() + (5*1000);
  76. while(Process::processEvents() && (Platform::getRealMilliseconds() < limit) )
  77. ;
  78. // Unhook from the signals.
  79. Net::smConnectionNotify. remove(this, &TestTCPRequest::handleNotify);
  80. Net::smConnectionReceive.remove(this, &TestTCPRequest::handleReceive);
  81. test(mDataRecved > 0, "Didn't get any data back!");
  82. }
  83. };
  84. CreateUnitTest( TestTCPRequestJournal, "Platform/Net/JournalTCPRequest")
  85. {
  86. NetSocket mSocket;
  87. S32 mDataRecved;
  88. void handleNotify(NetSocket sock, U32 state)
  89. {
  90. // Only consider our own socket.
  91. if(mSocket != sock)
  92. return;
  93. // Ok - what's the state? We do some dumb responses to given states
  94. // in order to fulfill the request.
  95. if(state == Net::Connected)
  96. {
  97. U8 reqBuffer[] = {
  98. "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
  99. };
  100. Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
  101. test(e == Net::NoError, "Got an error sending our HTTP request!");
  102. }
  103. else if(state == Net::Disconnected)
  104. {
  105. Process::requestShutdown();
  106. mSocket = NULL;
  107. }
  108. }
  109. void handleReceive(NetSocket sock, RawData incomingData)
  110. {
  111. // Only consider our own socket.
  112. if(mSocket != sock)
  113. return;
  114. char buff[4096];
  115. dMemcpy(buff, incomingData.data, incomingData.size);
  116. buff[incomingData.size] = 0;
  117. UnitPrint("Got a message...\n");
  118. UnitPrint(buff);
  119. UnitPrint("------\n");
  120. mDataRecved += incomingData.size;
  121. }
  122. void makeRequest()
  123. {
  124. mSocket = InvalidSocket;
  125. mDataRecved = 0;
  126. // Initialize networking - done by initLibraries currently
  127. //test(Net::init(), "Failed to initialize networking!");
  128. // Hook into the signals.
  129. Net::smConnectionNotify. notify(this, &TestTCPRequestJournal::handleNotify);
  130. Net::smConnectionReceive.notify(this, &TestTCPRequestJournal::handleReceive);
  131. // Open a TCP connection to garagegames.com
  132. mSocket = Net::openConnectTo("ip:72.246.107.193:80");
  133. // Let the callbacks enable things to process.
  134. while(Process::processEvents())
  135. ;
  136. // Unhook from the signals.
  137. Net::smConnectionNotify. remove(this, &TestTCPRequestJournal::handleNotify);
  138. Net::smConnectionReceive.remove(this, &TestTCPRequestJournal::handleReceive);
  139. test(mDataRecved > 0, "Didn't get any data back!");
  140. }
  141. void run()
  142. {
  143. Journal::Record("journalTCP.jrn");
  144. if( !Journal::IsRecording() )
  145. {
  146. test(0, "Failed.");
  147. return;
  148. }
  149. makeRequest();
  150. S32 bytesRead = mDataRecved;
  151. Journal::Stop();
  152. Journal::Play("journalTCP.jrn");
  153. makeRequest();
  154. Journal::Stop();
  155. test(bytesRead == mDataRecved, "Didn't get same data back from journal playback.");
  156. }
  157. };