platformNetAsync.unix.cc 6.2 KB

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