1 #ifndef CRYPTOPP_NETWORK_H
2 #define CRYPTOPP_NETWORK_H
6 #if !defined(NO_OS_DEPENDENCE) && defined(SOCKETS_AVAILABLE)
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();
32 void NoteTransceive(lword size);
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');
67 lword GeneralPump(lword maxSize=LWORD_MAX,
unsigned long maxTime=
INFINITE_TIME,
bool checkDelimiter=
false,
byte delimiter=
'\n')
69 GeneralPump2(maxSize,
true, maxTime, checkDelimiter, delimiter);
72 lword TimedPump(
unsigned long maxTime)
73 {
return GeneralPump(LWORD_MAX, maxTime);}
74 lword PumpLine(
byte delimiter=
'\n', lword maxSize=1024)
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;
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;
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;}
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);
182 size_t m_putSize, m_dataBegin, m_dataEnd;
183 bool m_waitingForResult, m_outputBlocked;
190 NetworkSink(
unsigned int maxBufferSize,
unsigned int autoFlushBound);
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));}
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; }
213 lword DoFlush(
unsigned long maxTime,
size_t targetSize);
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