platformNetAsync.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. S32 out_h_length;
  46. bool complete;
  47. NameLookupRequest()
  48. {
  49. sock = NetSocket::INVALID;
  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. NetAddress address;
  73. Net::Error error = Net::stringToAddress(mRequest.remoteAddr, &address, true);
  74. // do it
  75. if (error != Net::NoError)
  76. {
  77. // oh well! leave the lookup data unmodified (h_length) should
  78. // still be -1 from initialization
  79. mRequest.complete = true;
  80. }
  81. else
  82. {
  83. // copy the stuff we need from the hostent
  84. dMemset(mRequest.out_h_addr, 0,
  85. sizeof(mRequest.out_h_addr));
  86. dMemcpy(mRequest.out_h_addr, &address, sizeof(address));
  87. mRequest.out_h_length = sizeof(address);
  88. mRequest.complete = true;
  89. }
  90. #else
  91. XNDNS *pxndns = NULL;
  92. HANDLE hEvent = CreateEvent(NULL, false, false, NULL);
  93. XNetDnsLookup(mRequest.remoteAddr, hEvent, &pxndns);
  94. while(pxndns->iStatus == WSAEINPROGRESS)
  95. WaitForSingleObject(hEvent, INFINITE);
  96. if(pxndns->iStatus == 0 && pxndns->cina > 0)
  97. {
  98. dMemset(mRequest.out_h_addr, 0, sizeof(mRequest.out_h_addr));
  99. // This is a suspect section. I need to revisit. [2/22/2010 Pat]
  100. dMemcpy(mRequest.out_h_addr, pxndns->aina, sizeof(IN_ADDR));
  101. mRequest.out_h_length = sizeof(IN_ADDR);
  102. }
  103. mRequest.complete = true;
  104. XNetDnsRelease(pxndns);
  105. CloseHandle(hEvent);
  106. #endif
  107. }
  108. private:
  109. NameLookupRequest& mRequest;
  110. };
  111. //--------------------------------------------------------------------------
  112. // NetAsync.
  113. //--------------------------------------------------------------------------
  114. NetAsync::NetAsync()
  115. {
  116. VECTOR_SET_ASSOCIATION( mLookupRequests );
  117. }
  118. void NetAsync::queueLookup(const char* remoteAddr, NetSocket socket)
  119. {
  120. // do we have it already?
  121. U32 i = 0;
  122. for (i = 0; i < mLookupRequests.size(); ++i)
  123. {
  124. if (mLookupRequests[i].sock == socket)
  125. // found it. ignore more than one lookup at a time for a socket.
  126. return;
  127. }
  128. // not found, so add it
  129. mLookupRequests.increment();
  130. NameLookupRequest& lookupRequest = mLookupRequests.last();
  131. lookupRequest.sock = socket;
  132. dStrncpy(lookupRequest.remoteAddr, remoteAddr, sizeof(lookupRequest.remoteAddr));
  133. ThreadSafeRef< NameLookupWorkItem > workItem( new NameLookupWorkItem( lookupRequest ) );
  134. ThreadPool::GLOBAL().queueWorkItem( workItem );
  135. }
  136. bool NetAsync::checkLookup(NetSocket socket, void* out_h_addr,
  137. S32* out_h_length, S32 out_h_addr_size)
  138. {
  139. bool found = false;
  140. // search for the socket
  141. RequestIterator iter;
  142. for (iter = mLookupRequests.begin();
  143. iter != mLookupRequests.end();
  144. ++iter)
  145. // if we found it and it is complete...
  146. if (socket == iter->sock && iter->complete)
  147. {
  148. // copy the lookup data to the callers parameters
  149. dMemcpy(out_h_addr, iter->out_h_addr, out_h_addr_size);
  150. *out_h_length = iter->out_h_length;
  151. found = true;
  152. break;
  153. }
  154. // we found the socket, so we are done with it. erase.
  155. if (found)
  156. mLookupRequests.erase(iter);
  157. return found;
  158. }