1 #ifndef CRYPTOPP_NETWORK_H 2 #define CRYPTOPP_NETWORK_H 6 #if !defined(NO_OS_DEPENDENCE) && defined(SOCKETS_AVAILABLE) 14 class LimitedBandwidth
17 LimitedBandwidth(
lword maxBytesPerSecond = 0)
18 : m_maxBytesPerSecond(maxBytesPerSecond), m_timer(
Timer::MILLISECONDS)
19 , m_nextTransceiveTime(0)
20 { m_timer.StartTimer(); }
22 lword GetMaxBytesPerSecond()
const 23 {
return m_maxBytesPerSecond; }
25 void SetMaxBytesPerSecond(
lword v)
26 { m_maxBytesPerSecond = v; }
28 lword ComputeCurrentTransceiveLimit();
30 double TimeToNextTransceive();
37 unsigned int GetMaxWaitObjectCount()
const {
return 0; }
41 lword m_maxBytesPerSecond;
43 typedef std::deque<std::pair<double, lword> > OpQueue;
47 double m_nextTransceiveTime;
49 void ComputeNextTransceiveTime();
50 double GetCurTimeAndCleanUp();
58 : m_messageEndSent(false) , m_doPumpBlocked(false), m_blockedBySpeedLimit(false) {
Detach(attachment);}
65 size_t GeneralPump2(
lword &byteCount,
bool blockingOutput=
true,
unsigned long maxTime=
INFINITE_TIME,
bool checkDelimiter=
false,
byte delimiter=
'\n');
69 GeneralPump2(maxSize,
true, maxTime, checkDelimiter, delimiter);
72 lword TimedPump(
unsigned long maxTime)
75 {
return GeneralPump(maxSize,
INFINITE_TIME,
true, delimiter);}
77 size_t Pump2(
lword &byteCount,
bool blocking=
true)
78 {
return GeneralPump2(byteCount, blocking, blocking ?
INFINITE_TIME : 0);}
79 size_t PumpMessages2(
unsigned int &messageCount,
bool blocking=
true);
83 virtual size_t DoPump(
lword &byteCount,
bool blockingOutput,
84 unsigned long maxTime,
bool checkDelimiter,
byte delimiter) =0;
86 bool BlockedBySpeedLimit()
const {
return m_blockedBySpeedLimit; }
89 bool m_messageEndSent, m_doPumpBlocked, m_blockedBySpeedLimit;
96 virtual bool MustWaitToReceive() {
return false;}
97 virtual bool MustWaitForResult() {
return false;}
99 virtual bool Receive(
byte* buf,
size_t bufLen) =0;
100 virtual unsigned int GetReceiveResult() =0;
101 virtual bool EofReceived()
const =0;
107 virtual ~NonblockingSinkInfo() {}
108 virtual size_t GetMaxBufferSize()
const =0;
109 virtual size_t GetCurrentBufferSize()
const =0;
110 virtual bool EofPending()
const =0;
112 virtual float ComputeCurrentSpeed() =0;
114 virtual float GetMaxObservedSpeed()
const =0;
118 class CRYPTOPP_NO_VTABLE NonblockingSink :
public Sink,
public NonblockingSinkInfo,
public LimitedBandwidth
121 NonblockingSink() : m_blockedBySpeedLimit(false) {}
135 lword TimedFlush(
unsigned long maxTime,
size_t targetSize = 0);
137 virtual void SetMaxBufferSize(
size_t maxBufferSize) =0;
139 virtual void SetAutoFlushBound(
size_t bound) =0;
142 virtual lword DoFlush(
unsigned long maxTime,
size_t targetSize) = 0;
144 bool BlockedBySpeedLimit()
const {
return m_blockedBySpeedLimit; }
147 bool m_blockedBySpeedLimit;
154 virtual bool MustWaitToSend() {
return false;}
155 virtual bool MustWaitForResult() {
return false;}
156 virtual void Send(
const byte* buf,
size_t bufLen) =0;
157 virtual unsigned int GetSendResult() =0;
158 virtual bool MustWaitForEof() {
return false;}
159 virtual void SendEof() =0;
160 virtual bool EofSent() {
return false;}
169 unsigned int GetMaxWaitObjectCount()
const;
172 bool SourceExhausted()
const {
return m_dataBegin == m_dataEnd && GetReceiver().EofReceived();}
175 size_t DoPump(
lword &byteCount,
bool blockingOutput,
unsigned long maxTime,
bool checkDelimiter,
byte delimiter);
177 virtual NetworkReceiver & AccessReceiver() =0;
178 const NetworkReceiver & GetReceiver()
const {
return const_cast<NetworkSource *
>(
this)->AccessReceiver();}
182 size_t m_putSize, m_dataBegin, m_dataEnd;
183 bool m_waitingForResult, m_outputBlocked;
190 NetworkSink(
unsigned int maxBufferSize,
unsigned int autoFlushBound);
192 unsigned int GetMaxWaitObjectCount()
const;
195 size_t Put2(
const byte *inString,
size_t length,
int messageEnd,
bool blocking);
197 void SetMaxBufferSize(
size_t maxBufferSize) {m_maxBufferSize = maxBufferSize; m_buffer.SetNodeSize(
UnsignedMin(maxBufferSize, 16U*1024U+256U));}
198 void SetAutoFlushBound(
size_t bound) {m_autoFlushBound = bound;}
200 size_t GetMaxBufferSize()
const {
return m_maxBufferSize;}
201 size_t GetCurrentBufferSize()
const {
return (
size_t)m_buffer.CurrentSize();}
203 void ClearBuffer() { m_buffer.Clear(); }
205 bool EofPending()
const {
return m_eofState > EOF_NONE && m_eofState < EOF_DONE; }
208 float ComputeCurrentSpeed();
210 float GetMaxObservedSpeed()
const;
213 lword DoFlush(
unsigned long maxTime,
size_t targetSize);
215 virtual NetworkSender & AccessSender() =0;
216 const NetworkSender & GetSender()
const {
return const_cast<NetworkSink *
>(
this)->AccessSender();}
219 enum EofState { EOF_NONE, EOF_PENDING_SEND, EOF_PENDING_DELIVERY, EOF_DONE };
221 size_t m_maxBufferSize, m_autoFlushBound;
222 bool m_needSendResult, m_wasBlocked;
227 float m_byteCountSinceLastTimerReset, m_currentSpeed, m_maxObservedSpeed;
232 #endif // SOCKETS_AVAILABLE 234 #endif // CRYPTOPP_NETWORK_H
#define NAMESPACE_BEGIN(x)
Library configuration file.
Interface for objects that can be waited on.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Data structure used to store byte strings.
const unsigned long INFINITE_TIME
Represents infinite time.
#define CRYPTOPP_NO_VTABLE
Implementation of BufferedTransformation's attachment interface.
uint8_t const size_t const size
Implementation of BufferedTransformation's attachment interface.
Provides auto signaling support.