platformNetAsync.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/platformNetAsync.h"
  23. #include "core/strings/stringFunctions.h"
  24. #include "platform/threads/threadPool.h"
  25. #include "console/console.h"
  26. #if defined(TORQUE_OS_WIN)
  27. # include <winsock.h>
  28. #elif defined(TORQUE_OS_XENON)
  29. # include <Xtl.h>
  30. #else
  31. # include <netdb.h>
  32. # include <unistd.h>
  33. #endif
  34. #include <errno.h>
  35. NetAsync gNetAsync;
  36. //--------------------------------------------------------------------------
  37. // NetAsync::NameLookupRequest.
  38. //--------------------------------------------------------------------------
  39. // internal structure for storing information about a name lookup request
  40. struct NetAsync::NameLookupRequest
  41. {
  42. NetSocket sock;
  43. char remoteAddr[4096];
  44. char out_h_addr[4096];
  45. int out_h_length;
  46. bool complete;
  47. NameLookupRequest()
  48. {
  49. sock = InvalidSocket;
  50. remoteAddr[0] = 0;
  51. out_h_addr[0] = 0;
  52. out_h_length = -1;
  53. complete = false;
  54. }
  55. };
  56. //--------------------------------------------------------------------------
  57. // NetAsync::NameLookupWorkItem.
  58. //--------------------------------------------------------------------------
  59. /// Work item issued to the thread pool for each lookup request.
  60. struct NetAsync::NameLookupWorkItem : public ThreadPool::WorkItem
  61. {
  62. typedef ThreadPool::WorkItem Parent;
  63. NameLookupWorkItem( NameLookupRequest& request, ThreadPool::Context* context = 0 )
  64. : Parent( context ),
  65. mRequest( request )
  66. {
  67. }
  68. protected:
  69. virtual void execute()
  70. {
  71. #ifndef TORQUE_OS_XENON
  72. // do it
  73. struct hostent* hostent = gethostbyname(mRequest.remoteAddr);
  74. if (hostent == NULL)
  75. {
  76. // oh well! leave the lookup data unmodified (h_length) should
  77. // still be -1 from initialization
  78. mRequest.complete = true;
  79. }
  80. else
  81. {
  82. // copy the stuff we need from the hostent
  83. dMemset(mRequest.out_h_addr, 0,
  84. sizeof(mRequest.out_h_addr));
  85. dMemcpy(mRequest.out_h_addr, hostent->h_addr, hostent->h_length);
  86. mRequest.out_h_length = hostent->h_length;
  87. mRequest.complete = true;
  88. }
  89. #else
  90. XNDNS *pxndns = NULL;
  91. HANDLE hEvent = CreateEvent(NULL, false, false, NULL);
  92. XNetDnsLookup(mRequest.remoteAddr, hEvent, &pxndns);
  93. while(pxndns->iStatus == WSAEINPROGRESS)
  94. WaitForSingleObject(hEvent, INFINITE);
  95. if(pxndns->iStatus == 0 && pxndns->cina > 0)
  96. {
  97. dMemset(mRequest.out_h_addr, 0, sizeof(mRequest.out_h_addr));
  98. // This is a suspect section. I need to revisit. [2/22/2010 Pat]
  99. dMemcpy(mRequest.out_h_addr, pxndns->aina, sizeof(IN_ADDR));
  100. mRequest.out_h_length = sizeof(IN_ADDR);
  101. }
  102. mRequest.complete = true;
  103. XNetDnsRelease(pxndns);
  104. CloseHandle(hEvent);
  105. #endif
  106. }
  107. private:
  108. NameLookupRequest& mRequest;
  109. };
  110. //--------------------------------------------------------------------------
  111. // NetAsync.
  112. //--------------------------------------------------------------------------
  113. NetAsync::NetAsync()
  114. {
  115. VECTOR_SET_ASSOCIATION( mLookupRequests );
  116. }
  117. void NetAsync::queueLookup(const char* remoteAddr, NetSocket socket)
  118. {
  119. // do we have it already?
  120. unsigned int i = 0;
  121. for (i = 0; i < mLookupRequests.size(); ++i)
  122. {
  123. if (mLookupRequests[i].sock == socket)
  124. // found it. ignore more than one lookup at a time for a socket.
  125. return;
  126. }
  127. // not found, so add it
  128. mLookupRequests.increment();
  129. NameLookupRequest& lookupRequest = mLookupRequests.last();
  130. lookupRequest.sock = socket;
  131. dStrncpy(lookupRequest.remoteAddr, remoteAddr, sizeof(lookupRequest.remoteAddr));
  132. ThreadSafeRef< NameLookupWorkItem > workItem( new NameLookupWorkItem( lookupRequest ) );
  133. ThreadPool::GLOBAL().queueWorkItem( workItem );
  134. }
  135. bool NetAsync::checkLookup(NetSocket socket, char* out_h_addr,
  136. int* out_h_length, int out_h_addr_size)
  137. {
  138. bool found = false;
  139. // search for the socket
  140. RequestIterator iter;
  141. for (iter = mLookupRequests.begin();
  142. iter != mLookupRequests.end();
  143. ++iter)
  144. // if we found it and it is complete...
  145. if (socket == iter->sock && iter->complete)
  146. {
  147. // copy the lookup data to the callers parameters
  148. dMemcpy(out_h_addr, iter->out_h_addr, out_h_addr_size);
  149. *out_h_length = iter->out_h_length;
  150. found = true;
  151. break;
  152. }
  153. // we found the socket, so we are done with it. erase.
  154. if (found)
  155. mLookupRequests.erase(iter);
  156. return found;
  157. }