platformNetAsync.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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/platform.h"
  23. #include "platform/threads/mutex.h"
  24. #include "platform/threads/thread.h"
  25. #include "platform/platformNetAsync.h"
  26. #include "console/console.h"
  27. #if defined(TORQUE_OS_WIN)
  28. # include <winsock.h>
  29. #else
  30. #include <netdb.h>
  31. #include <unistd.h>
  32. #endif
  33. #include <errno.h>
  34. #include <string.h>
  35. NetAsync gNetAsync;
  36. #define LOOKUP_REQUEST_CHECK_INTERVAL 500
  37. void* gNetAsyncMutex = NULL;
  38. static void lockNetAsyncMutex()
  39. {
  40. if(!gNetAsyncMutex)
  41. gNetAsyncMutex = Mutex::createMutex();
  42. AssertFatal(gNetAsyncMutex, "Could not create gNetAsyncMutex!");
  43. Mutex::lockMutex(gNetAsyncMutex);
  44. }
  45. static void unlockNetAsyncMutex()
  46. {
  47. if(!gNetAsyncMutex)
  48. gNetAsyncMutex = Mutex::createMutex();
  49. AssertFatal(gNetAsyncMutex, "Could not create gNetAsyncMutex!");
  50. Mutex::unlockMutex(gNetAsyncMutex);
  51. }
  52. // internal structure for storing information about a name lookup request
  53. struct NameLookupRequest
  54. {
  55. NetSocket sock;
  56. char remoteAddr[4096];
  57. char out_h_addr[4096];
  58. int out_h_length;
  59. bool complete;
  60. NameLookupRequest()
  61. {
  62. sock = NetSocket::INVALID;
  63. remoteAddr[0] = 0;
  64. out_h_addr[0] = 0;
  65. out_h_length = -1;
  66. complete = false;
  67. }
  68. };
  69. void NetAsync::queueLookup(const char* remoteAddr, NetSocket socket)
  70. {
  71. lockNetAsyncMutex();
  72. // do we have it already?
  73. unsigned int i = 0;
  74. for (i = 0; i < mLookupRequests.size(); ++i)
  75. {
  76. if (mLookupRequests[i]->sock == socket)
  77. // found it. ignore more than one lookup at a time for a socket.
  78. break;
  79. }
  80. if (i == mLookupRequests.size())
  81. {
  82. // not found, so add it
  83. NameLookupRequest* lookupRequest = new NameLookupRequest();
  84. lookupRequest->sock = socket;
  85. dStrncpy(lookupRequest->remoteAddr, remoteAddr,
  86. sizeof(lookupRequest->remoteAddr));
  87. mLookupRequests.push_back(lookupRequest);
  88. }
  89. unlockNetAsyncMutex();
  90. }
  91. void NetAsync::run()
  92. {
  93. if (isRunning())
  94. return;
  95. mRunning = true;
  96. NameLookupRequest* lookupRequest = NULL;
  97. while (isRunning())
  98. {
  99. lookupRequest = NULL;
  100. // lock
  101. lockNetAsyncMutex();
  102. // if there is a request...
  103. if (mLookupRequests.size() > 0)
  104. {
  105. // assign the first incomplete request
  106. for (unsigned int i = 0; i < mLookupRequests.size(); ++i)
  107. if (!mLookupRequests[i]->complete)
  108. lookupRequest = mLookupRequests[i];
  109. }
  110. // unlock so that more requests can be added
  111. unlockNetAsyncMutex();
  112. // if we have a lookup request
  113. if (lookupRequest != NULL)
  114. {
  115. NetAddress address;
  116. Net::Error error = Net::stringToAddress(lookupRequest->remoteAddr, &address, true);
  117. // do it
  118. if (error != Net::NoError)
  119. {
  120. // oh well! leave the lookup data unmodified (h_length) should
  121. // still be -1 from initialization
  122. lookupRequest->complete = true;
  123. }
  124. else
  125. {
  126. // copy the stuff we need from the hostent
  127. dMemset(lookupRequest->out_h_addr, 0,
  128. sizeof(lookupRequest->out_h_addr));
  129. dMemcpy(lookupRequest->out_h_addr, &address, sizeof(address));
  130. lookupRequest->out_h_length = sizeof(address);
  131. lookupRequest->complete = true;
  132. }
  133. }
  134. else
  135. {
  136. // no lookup request. sleep for a bit
  137. Platform::sleep(LOOKUP_REQUEST_CHECK_INTERVAL);
  138. }
  139. };
  140. }
  141. bool NetAsync::checkLookup(NetSocket socket, void* out_h_addr,
  142. int* out_h_length, int out_h_addr_size)
  143. {
  144. lockNetAsyncMutex();
  145. bool found = false;
  146. // search for the socket
  147. Vector<NameLookupRequest*>::iterator iter;
  148. for (iter = mLookupRequests.begin();
  149. iter != mLookupRequests.end();
  150. ++iter)
  151. // if we found it and it is complete...
  152. if (socket == (*iter)->sock && (*iter)->complete)
  153. {
  154. // copy the lookup data to the callers parameters
  155. dMemcpy(out_h_addr, (*iter)->out_h_addr, out_h_addr_size);
  156. *out_h_length = (*iter)->out_h_length;
  157. found = true;
  158. break;
  159. }
  160. // we found the socket, so we are done with it. erase.
  161. if (found)
  162. {
  163. delete *iter;
  164. mLookupRequests.erase(iter);
  165. }
  166. unlockNetAsyncMutex();
  167. return found;
  168. }
  169. // this is called by the pthread module to start the thread
  170. static void StartThreadFunc(S32 nothing)
  171. {
  172. if (gNetAsync.isRunning())
  173. return;
  174. gNetAsync.run();
  175. return;
  176. }
  177. void NetAsync::startAsync()
  178. {
  179. if (gNetAsync.isRunning())
  180. return;
  181. // create the thread...
  182. Thread *zThread = new Thread((ThreadRunFunction)StartThreadFunc, 0, true);
  183. if (!zThread)
  184. Con::errorf("Error starting net async thread.");
  185. }
  186. void NetAsync::stopAsync()
  187. {
  188. if (gNetAsync.isRunning())
  189. gNetAsync.stop();
  190. }