42
42
//! Network Receiver
43
class NetworkReceiver : public Waitable
43
class CRYPTOPP_NO_VTABLE NetworkReceiver : public Waitable
46
46
virtual bool MustWaitToReceive() {return false;}
47
47
virtual bool MustWaitForResult() {return false;}
48
virtual void Receive(byte* buf, unsigned int bufLen) =0;
48
//! receive data from network source, returns whether result is immediately available
49
virtual bool Receive(byte* buf, unsigned int bufLen) =0;
49
50
virtual unsigned int GetReceiveResult() =0;
50
51
virtual bool EofReceived() const =0;
54
class CRYPTOPP_NO_VTABLE NonblockingSinkInfo
57
virtual ~NonblockingSinkInfo() {}
58
virtual unsigned int GetMaxBufferSize() const =0;
59
virtual unsigned int GetCurrentBufferSize() const =0;
60
//! compute the current speed of this sink in bytes per second
61
virtual float ComputeCurrentSpeed() =0;
62
//! get the maximum observed speed of this sink in bytes per second
63
virtual float GetMaxObservedSpeed() const =0;
53
66
//! a Sink class that queues input and can flush to a device for a specified amount of time.
54
class NonblockingSink : public Sink
67
class CRYPTOPP_NO_VTABLE NonblockingSink : public Sink, public NonblockingSinkInfo
57
70
bool IsolatedFlush(bool hardFlush, bool blocking);
69
82
virtual unsigned int TimedFlush(unsigned long maxTime, unsigned int targetSize = 0) =0;
71
84
virtual void SetMaxBufferSize(unsigned int maxBufferSize) =0;
72
virtual void SetAutoFlush(bool autoFlush = true) =0;
74
virtual unsigned int GetMaxBufferSize() const =0;
75
virtual unsigned int GetCurrentBufferSize() const =0;
85
//! set a bound which will cause sink to flush if exceeded by GetCurrentBufferSize()
86
virtual void SetAutoFlushBound(unsigned int bound) =0;
79
class NetworkSender : public Waitable
90
class CRYPTOPP_NO_VTABLE NetworkSender : public Waitable
82
93
virtual bool MustWaitToSend() {return false;}
89
100
#ifdef HIGHRES_TIMER_AVAILABLE
91
102
//! Network Source
92
class NetworkSource : public NonblockingSource
103
class CRYPTOPP_NO_VTABLE NetworkSource : public NonblockingSource
95
106
NetworkSource(BufferedTransformation *attachment);
97
108
unsigned int GetMaxWaitObjectCount() const
98
109
{return GetReceiver().GetMaxWaitObjectCount() + AttachedTransformation()->GetMaxWaitObjectCount();}
99
void GetWaitObjects(WaitObjectContainer &container)
100
{AccessReceiver().GetWaitObjects(container); AttachedTransformation()->GetWaitObjects(container);}
110
void GetWaitObjects(WaitObjectContainer &container);
102
112
unsigned int GeneralPump2(unsigned long &byteCount, bool blockingOutput=true, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n');
103
bool SourceExhausted() const {return GetReceiver().EofReceived();}
113
bool SourceExhausted() const {return m_dataBegin == m_dataEnd && GetReceiver().EofReceived();}
106
116
virtual NetworkReceiver & AccessReceiver() =0;
107
117
const NetworkReceiver & GetReceiver() const {return const_cast<NetworkSource *>(this)->AccessReceiver();}
110
enum {NORMAL, WAITING_FOR_RESULT, OUTPUT_BLOCKED};
111
120
SecByteBlock m_buf;
112
unsigned int m_bufSize, m_putSize, m_state;
121
unsigned int m_putSize, m_dataBegin, m_dataEnd;
122
bool m_waitingForResult, m_outputBlocked;
116
class NetworkSink : public NonblockingSink
126
class CRYPTOPP_NO_VTABLE NetworkSink : public NonblockingSink
119
NetworkSink(unsigned int maxBufferSize, bool autoFlush)
120
: m_maxBufferSize(maxBufferSize), m_autoFlush(autoFlush), m_needSendResult(false), m_blockedBytes(0) {}
129
NetworkSink(unsigned int maxBufferSize, unsigned int autoFlushBound);
122
131
unsigned int GetMaxWaitObjectCount() const
123
132
{return GetSender().GetMaxWaitObjectCount();}
124
133
void GetWaitObjects(WaitObjectContainer &container)
125
{if (m_blockedBytes || !m_buffer.IsEmpty()) AccessSender().GetWaitObjects(container);}
134
{if (m_wasBlocked || !m_buffer.IsEmpty()) AccessSender().GetWaitObjects(container);}
127
136
unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
129
138
unsigned int TimedFlush(unsigned long maxTime, unsigned int targetSize = 0);
131
void SetMaxBufferSize(unsigned int maxBufferSize) {m_maxBufferSize = maxBufferSize;}
132
void SetAutoFlush(bool autoFlush = true) {m_autoFlush = autoFlush;}
140
void SetMaxBufferSize(unsigned int maxBufferSize) {m_maxBufferSize = maxBufferSize; m_buffer.SetNodeSize(STDMIN(16U*1024U+256, maxBufferSize));}
141
void SetAutoFlushBound(unsigned int bound) {m_autoFlushBound = bound;}
134
143
unsigned int GetMaxBufferSize() const {return m_maxBufferSize;}
135
144
unsigned int GetCurrentBufferSize() const {return m_buffer.CurrentSize();}
146
void ClearBuffer() {m_buffer.Clear();}
148
//! compute the current speed of this sink in bytes per second
149
float ComputeCurrentSpeed();
150
//! get the maximum observed speed of this sink in bytes per second
151
float GetMaxObservedSpeed() const {return m_maxObservedSpeed;}
138
154
virtual NetworkSender & AccessSender() =0;
139
155
const NetworkSender & GetSender() const {return const_cast<NetworkSink *>(this)->AccessSender();}
142
unsigned int m_maxBufferSize;
143
bool m_autoFlush, m_needSendResult;
158
unsigned int m_maxBufferSize, m_autoFlushBound;
159
bool m_needSendResult, m_wasBlocked;
144
160
ByteQueue m_buffer;
145
unsigned int m_blockedBytes;
161
unsigned int m_skipBytes;
163
float m_byteCountSinceLastTimerReset, m_currentSpeed, m_maxObservedSpeed;
148
166
#endif // #ifdef HIGHRES_TIMER_AVAILABLE