platformNetAsync.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #else
  29. # include <netdb.h>
  30. # include <unistd.h>
  31. #endif
  32. #include <errno.h>
  33. NetAsync gNetAsync;
  34. //--------------------------------------------------------------------------
  35. // NetAsync::NameLookupRequest.
  36. //--------------------------------------------------------------------------
  37. // internal structure for storing information about a name lookup request
  38. struct NetAsync::NameLookupRequest
  39. {
  40. NetSocket sock;
  41. char remoteAddr[4096];
  42. char out_h_addr[4096];
  43. S32 out_h_length;
  44. bool complete;
  45. NameLookupRequest()
  46. {
  47. sock = NetSocket::INVALID;
  48. remoteAddr[0] = 0;
  49. out_h_addr[0] = 0;
  50. out_h_length = -1;
  51. complete = false;
  52. }
  53. };
  54. //--------------------------------------------------------------------------
  55. // NetAsync::NameLookupWorkItem.
  56. //--------------------------------------------------------------------------
  57. /// Work item issued to the thread pool for each lookup request.
  58. struct NetAsync::NameLookupWorkItem : public ThreadPool::WorkItem
  59. {
  60. typedef ThreadPool::WorkItem Parent;
  61. NameLookupWorkItem( NameLookupRequest& request, ThreadPool::Context* context = 0 )
  62. : Parent( context ),
  63. mRequest( request )
  64. {
  65. }
  66. protected:
  67. virtual void execute()
  68. {
  69. NetAddress address;
  70. Net::Error error = Net::stringToAddress(mRequest.remoteAddr, &address, true);
  71. // do it
  72. if (error != Net::NoError)
  73. {
  74. // oh well! leave the lookup data unmodified (h_length) should
  75. // still be -1 from initialization
  76. mRequest.complete = true;
  77. }
  78. else
  79. {
  80. // copy the stuff we need from the hostent
  81. dMemset(mRequest.out_h_addr, 0,
  82. sizeof(mRequest.out_h_addr));
  83. dMemcpy(mRequest.out_h_addr, &address, sizeof(address));
  84. mRequest.out_h_length = sizeof(address);
  85. mRequest.complete = true;
  86. }
  87. }
  88. private:
  89. NameLookupRequest& mRequest;
  90. };
  91. //--------------------------------------------------------------------------
  92. // NetAsync.
  93. //--------------------------------------------------------------------------
  94. NetAsync::NetAsync()
  95. {
  96. VECTOR_SET_ASSOCIATION( mLookupRequests );
  97. }
  98. void NetAsync::queueLookup(const char* remoteAddr, NetSocket socket)
  99. {
  100. // do we have it already?
  101. U32 i = 0;
  102. for (i = 0; i < mLookupRequests.size(); ++i)
  103. {
  104. if (mLookupRequests[i].sock == socket)
  105. // found it. ignore more than one lookup at a time for a socket.
  106. return;
  107. }
  108. // not found, so add it
  109. mLookupRequests.increment();
  110. NameLookupRequest& lookupRequest = mLookupRequests.last();
  111. lookupRequest.sock = socket;
  112. dStrncpy(lookupRequest.remoteAddr, remoteAddr, sizeof(lookupRequest.remoteAddr));
  113. ThreadSafeRef< NameLookupWorkItem > workItem( new NameLookupWorkItem( lookupRequest ) );
  114. ThreadPool::GLOBAL().queueWorkItem( workItem );
  115. }
  116. bool NetAsync::checkLookup(NetSocket socket, void* out_h_addr,
  117. S32* out_h_length, S32 out_h_addr_size)
  118. {
  119. bool found = false;
  120. // search for the socket
  121. RequestIterator iter;
  122. for (iter = mLookupRequests.begin();
  123. iter != mLookupRequests.end();
  124. ++iter)
  125. // if we found it and it is complete...
  126. if (socket == iter->sock && iter->complete)
  127. {
  128. // copy the lookup data to the callers parameters
  129. dMemcpy(out_h_addr, iter->out_h_addr, out_h_addr_size);
  130. *out_h_length = iter->out_h_length;
  131. found = true;
  132. break;
  133. }
  134. // we found the socket, so we are done with it. erase.
  135. if (found)
  136. mLookupRequests.erase(iter);
  137. return found;
  138. }