SendToThread.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "SendToThread.h"
  11. #ifdef USE_THREADED_SEND
  12. #include "RakThread.h"
  13. #include "InternalPacket.h"
  14. #include "GetTime.h"
  15. #if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1
  16. #include "CCRakNetUDT.h"
  17. #else
  18. #include "CCRakNetSlidingWindow.h"
  19. #endif
  20. using namespace RakNet;
  21. int SendToThread::refCount=0;
  22. DataStructures::ThreadsafeAllocatingQueue<SendToThread::SendToThreadBlock> SendToThread::objectQueue;
  23. ThreadPool<SendToThread::SendToThreadBlock*,SendToThread::SendToThreadBlock*> SendToThread::threadPool;
  24. SendToThread::SendToThreadBlock* SendToWorkerThread(SendToThread::SendToThreadBlock* input, bool *returnOutput, void* perThreadData)
  25. {
  26. (void) perThreadData;
  27. *returnOutput=false;
  28. // RakNet::TimeUS *mostRecentTime=(RakNet::TimeUS *)input->data;
  29. // *mostRecentTime=RakNet::GetTimeUS();
  30. SocketLayer::SendTo(input->s, input->data, input->dataWriteOffset, input->systemAddress, _FILE_AND_LINE_);
  31. SendToThread::objectQueue.Push(input);
  32. return 0;
  33. }
  34. SendToThread::SendToThread()
  35. {
  36. }
  37. SendToThread::~SendToThread()
  38. {
  39. }
  40. void SendToThread::AddRef(void)
  41. {
  42. if (++refCount==1)
  43. {
  44. threadPool.StartThreads(1,0);
  45. }
  46. }
  47. void SendToThread::Deref(void)
  48. {
  49. if (refCount>0)
  50. {
  51. if (--refCount==0)
  52. {
  53. threadPool.StopThreads();
  54. RakAssert(threadPool.NumThreadsWorking()==0);
  55. unsigned i;
  56. SendToThreadBlock* info;
  57. for (i=0; i < threadPool.InputSize(); i++)
  58. {
  59. info = threadPool.GetInputAtIndex(i);
  60. objectQueue.Push(info);
  61. }
  62. threadPool.ClearInput();
  63. objectQueue.Clear(_FILE_AND_LINE_);
  64. }
  65. }
  66. }
  67. SendToThread::SendToThreadBlock* SendToThread::AllocateBlock(void)
  68. {
  69. SendToThread::SendToThreadBlock *b;
  70. b=objectQueue.Pop();
  71. if (b==0)
  72. b=objectQueue.Allocate(_FILE_AND_LINE_);
  73. return b;
  74. }
  75. void SendToThread::ProcessBlock(SendToThread::SendToThreadBlock* threadedSend)
  76. {
  77. RakAssert(threadedSend->dataWriteOffset>0 && threadedSend->dataWriteOffset<=MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
  78. threadPool.AddInput(SendToWorkerThread,threadedSend);
  79. }
  80. #endif
粤ICP备19079148号