Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.8 KiB

  1. #ifndef CRYPTOPP_NETWORK_H
  2. #define CRYPTOPP_NETWORK_H
  3. #include "config.h"
  4. #ifdef HIGHRES_TIMER_AVAILABLE
  5. #include "filters.h"
  6. #include "hrtimer.h"
  7. #include <deque>
  8. NAMESPACE_BEGIN(CryptoPP)
  9. class LimitedBandwidth
  10. {
  11. public:
  12. LimitedBandwidth(lword maxBytesPerSecond = 0)
  13. : m_maxBytesPerSecond(maxBytesPerSecond), m_timer(Timer::MILLISECONDS)
  14. , m_nextTransceiveTime(0)
  15. { m_timer.StartTimer(); }
  16. lword GetMaxBytesPerSecond() const
  17. { return m_maxBytesPerSecond; }
  18. void SetMaxBytesPerSecond(lword v)
  19. { m_maxBytesPerSecond = v; }
  20. lword ComputeCurrentTransceiveLimit();
  21. double TimeToNextTransceive();
  22. void NoteTransceive(lword size);
  23. public:
  24. /*! GetWaitObjects() must be called despite the 0 return from GetMaxWaitObjectCount();
  25. the 0 is because the ScheduleEvent() method is used instead of adding a wait object */
  26. unsigned int GetMaxWaitObjectCount() const { return 0; }
  27. void GetWaitObjects(WaitObjectContainer &container, const CallStack &callStack);
  28. private:
  29. lword m_maxBytesPerSecond;
  30. typedef std::deque<std::pair<double, lword> > OpQueue;
  31. OpQueue m_ops;
  32. Timer m_timer;
  33. double m_nextTransceiveTime;
  34. void ComputeNextTransceiveTime();
  35. double GetCurTimeAndCleanUp();
  36. };
  37. //! a Source class that can pump from a device for a specified amount of time.
  38. class CRYPTOPP_NO_VTABLE NonblockingSource : public AutoSignaling<Source>, public LimitedBandwidth
  39. {
  40. public:
  41. NonblockingSource(BufferedTransformation *attachment)
  42. : m_messageEndSent(false) , m_doPumpBlocked(false), m_blockedBySpeedLimit(false) {Detach(attachment);}
  43. //! \name NONBLOCKING SOURCE
  44. //@{
  45. //! pump up to maxSize bytes using at most maxTime milliseconds
  46. /*! If checkDelimiter is true, pump up to delimiter, which itself is not extracted or pumped. */
  47. size_t GeneralPump2(lword &byteCount, bool blockingOutput=true, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n');
  48. lword GeneralPump(lword maxSize=LWORD_MAX, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n')
  49. {
  50. GeneralPump2(maxSize, true, maxTime, checkDelimiter, delimiter);
  51. return maxSize;
  52. }
  53. lword TimedPump(unsigned long maxTime)
  54. {return GeneralPump(LWORD_MAX, maxTime);}
  55. lword PumpLine(byte delimiter='\n', lword maxSize=1024)
  56. {return GeneralPump(maxSize, INFINITE_TIME, true, delimiter);}
  57. size_t Pump2(lword &byteCount, bool blocking=true)
  58. {return GeneralPump2(byteCount, blocking, blocking ? INFINITE_TIME : 0);}
  59. size_t PumpMessages2(unsigned int &messageCount, bool blocking=true);
  60. //@}
  61. protected:
  62. virtual size_t DoPump(lword &byteCount, bool blockingOutput,
  63. unsigned long maxTime, bool checkDelimiter, byte delimiter) =0;
  64. bool BlockedBySpeedLimit() const { return m_blockedBySpeedLimit; }
  65. private:
  66. bool m_messageEndSent, m_doPumpBlocked, m_blockedBySpeedLimit;
  67. };
  68. //! Network Receiver
  69. class CRYPTOPP_NO_VTABLE NetworkReceiver : public Waitable
  70. {
  71. public:
  72. virtual bool MustWaitToReceive() {return false;}
  73. virtual bool MustWaitForResult() {return false;}
  74. //! receive data from network source, returns whether result is immediately available
  75. virtual bool Receive(byte* buf, size_t bufLen) =0;
  76. virtual unsigned int GetReceiveResult() =0;
  77. virtual bool EofReceived() const =0;
  78. };
  79. class CRYPTOPP_NO_VTABLE NonblockingSinkInfo
  80. {
  81. public:
  82. virtual ~NonblockingSinkInfo() {}
  83. virtual size_t GetMaxBufferSize() const =0;
  84. virtual size_t GetCurrentBufferSize() const =0;
  85. virtual bool EofPending() const =0;
  86. //! compute the current speed of this sink in bytes per second
  87. virtual float ComputeCurrentSpeed() =0;
  88. //! get the maximum observed speed of this sink in bytes per second
  89. virtual float GetMaxObservedSpeed() const =0;
  90. };
  91. //! a Sink class that queues input and can flush to a device for a specified amount of time.
  92. class CRYPTOPP_NO_VTABLE NonblockingSink : public Sink, public NonblockingSinkInfo, public LimitedBandwidth
  93. {
  94. public:
  95. NonblockingSink() : m_blockedBySpeedLimit(false) {}
  96. bool IsolatedFlush(bool hardFlush, bool blocking);
  97. //! flush to device for no more than maxTime milliseconds
  98. /*! This function will repeatedly attempt to flush data to some device, until
  99. the queue is empty, or a total of maxTime milliseconds have elapsed.
  100. If maxTime == 0, at least one attempt will be made to flush some data, but
  101. it is likely that not all queued data will be flushed, even if the device
  102. is ready to receive more data without waiting. If you want to flush as much data
  103. as possible without waiting for the device, call this function in a loop.
  104. For example: while (sink.TimedFlush(0) > 0) {}
  105. \return number of bytes flushed
  106. */
  107. lword TimedFlush(unsigned long maxTime, size_t targetSize = 0);
  108. virtual void SetMaxBufferSize(size_t maxBufferSize) =0;
  109. //! set a bound which will cause sink to flush if exceeded by GetCurrentBufferSize()
  110. virtual void SetAutoFlushBound(size_t bound) =0;
  111. protected:
  112. virtual lword DoFlush(unsigned long maxTime, size_t targetSize) = 0;
  113. bool BlockedBySpeedLimit() const { return m_blockedBySpeedLimit; }
  114. private:
  115. bool m_blockedBySpeedLimit;
  116. };
  117. //! Network Sender
  118. class CRYPTOPP_NO_VTABLE NetworkSender : public Waitable
  119. {
  120. public:
  121. virtual bool MustWaitToSend() {return false;}
  122. virtual bool MustWaitForResult() {return false;}
  123. virtual void Send(const byte* buf, size_t bufLen) =0;
  124. virtual unsigned int GetSendResult() =0;
  125. virtual bool MustWaitForEof() {return false;}
  126. virtual void SendEof() =0;
  127. virtual bool EofSent() {return false;} // implement if MustWaitForEof() == true
  128. };
  129. //! Network Source
  130. class CRYPTOPP_NO_VTABLE NetworkSource : public NonblockingSource
  131. {
  132. public:
  133. NetworkSource(BufferedTransformation *attachment);
  134. unsigned int GetMaxWaitObjectCount() const;
  135. void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
  136. bool SourceExhausted() const {return m_dataBegin == m_dataEnd && GetReceiver().EofReceived();}
  137. protected:
  138. size_t DoPump(lword &byteCount, bool blockingOutput, unsigned long maxTime, bool checkDelimiter, byte delimiter);
  139. virtual NetworkReceiver & AccessReceiver() =0;
  140. const NetworkReceiver & GetReceiver() const {return const_cast<NetworkSource *>(this)->AccessReceiver();}
  141. private:
  142. SecByteBlock m_buf;
  143. size_t m_putSize, m_dataBegin, m_dataEnd;
  144. bool m_waitingForResult, m_outputBlocked;
  145. };
  146. //! Network Sink
  147. class CRYPTOPP_NO_VTABLE NetworkSink : public NonblockingSink
  148. {
  149. public:
  150. NetworkSink(unsigned int maxBufferSize, unsigned int autoFlushBound);
  151. unsigned int GetMaxWaitObjectCount() const;
  152. void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
  153. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  154. void SetMaxBufferSize(size_t maxBufferSize) {m_maxBufferSize = maxBufferSize; m_buffer.SetNodeSize(UnsignedMin(maxBufferSize, 16U*1024U+256U));}
  155. void SetAutoFlushBound(size_t bound) {m_autoFlushBound = bound;}
  156. size_t GetMaxBufferSize() const {return m_maxBufferSize;}
  157. size_t GetCurrentBufferSize() const {return (size_t)m_buffer.CurrentSize();}
  158. void ClearBuffer() { m_buffer.Clear(); }
  159. bool EofPending() const { return m_eofState > EOF_NONE && m_eofState < EOF_DONE; }
  160. //! compute the current speed of this sink in bytes per second
  161. float ComputeCurrentSpeed();
  162. //! get the maximum observed speed of this sink in bytes per second
  163. float GetMaxObservedSpeed() const;
  164. protected:
  165. lword DoFlush(unsigned long maxTime, size_t targetSize);
  166. virtual NetworkSender & AccessSender() =0;
  167. const NetworkSender & GetSender() const {return const_cast<NetworkSink *>(this)->AccessSender();}
  168. private:
  169. enum EofState { EOF_NONE, EOF_PENDING_SEND, EOF_PENDING_DELIVERY, EOF_DONE };
  170. size_t m_maxBufferSize, m_autoFlushBound;
  171. bool m_needSendResult, m_wasBlocked;
  172. EofState m_eofState;
  173. ByteQueue m_buffer;
  174. size_t m_skipBytes;
  175. Timer m_speedTimer;
  176. float m_byteCountSinceLastTimerReset, m_currentSpeed, m_maxObservedSpeed;
  177. };
  178. NAMESPACE_END
  179. #endif // #ifdef HIGHRES_TIMER_AVAILABLE
  180. #endif