00001 #ifndef CRYPTOPP_NETWORK_H
00002 #define CRYPTOPP_NETWORK_H
00003
00004 #include "filters.h"
00005 #include "hrtimer.h"
00006
00007 NAMESPACE_BEGIN(CryptoPP)
00008
00009
00010 class CRYPTOPP_NO_VTABLE NonblockingSource : public AutoSignaling<Source>
00011 {
00012 public:
00013 NonblockingSource(BufferedTransformation *attachment)
00014 : m_messageEndSent(false) {Detach(attachment);}
00015
00016
00017
00018
00019
00020
00021 virtual size_t GeneralPump2(lword &byteCount, bool blockingOutput=true, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n') =0;
00022
00023 lword GeneralPump(lword maxSize=LWORD_MAX, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n')
00024 {
00025 GeneralPump2(maxSize, true, maxTime, checkDelimiter, delimiter);
00026 return maxSize;
00027 }
00028 lword TimedPump(unsigned long maxTime)
00029 {return GeneralPump(LWORD_MAX, maxTime);}
00030 lword PumpLine(byte delimiter='\n', lword maxSize=1024)
00031 {return GeneralPump(maxSize, INFINITE_TIME, true, delimiter);}
00032
00033 size_t Pump2(lword &byteCount, bool blocking=true)
00034 {return GeneralPump2(byteCount, blocking, blocking ? INFINITE_TIME : 0);}
00035 size_t PumpMessages2(unsigned int &messageCount, bool blocking=true);
00036
00037
00038 private:
00039 bool m_messageEndSent;
00040 };
00041
00042
00043 class CRYPTOPP_NO_VTABLE NetworkReceiver : public Waitable
00044 {
00045 public:
00046 virtual bool MustWaitToReceive() {return false;}
00047 virtual bool MustWaitForResult() {return false;}
00048
00049 virtual bool Receive(byte* buf, size_t bufLen) =0;
00050 virtual unsigned int GetReceiveResult() =0;
00051 virtual bool EofReceived() const =0;
00052 };
00053
00054 class CRYPTOPP_NO_VTABLE NonblockingSinkInfo
00055 {
00056 public:
00057 virtual ~NonblockingSinkInfo() {}
00058 virtual size_t GetMaxBufferSize() const =0;
00059 virtual size_t GetCurrentBufferSize() const =0;
00060
00061 virtual float ComputeCurrentSpeed() =0;
00062
00063 virtual float GetMaxObservedSpeed() const =0;
00064 };
00065
00066
00067 class CRYPTOPP_NO_VTABLE NonblockingSink : public Sink, public NonblockingSinkInfo
00068 {
00069 public:
00070 bool IsolatedFlush(bool hardFlush, bool blocking);
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 virtual lword TimedFlush(unsigned long maxTime, size_t targetSize=0) =0;
00083
00084 virtual void SetMaxBufferSize(size_t maxBufferSize) =0;
00085
00086 virtual void SetAutoFlushBound(size_t bound) =0;
00087 };
00088
00089
00090 class CRYPTOPP_NO_VTABLE NetworkSender : public Waitable
00091 {
00092 public:
00093 virtual bool MustWaitToSend() {return false;}
00094 virtual bool MustWaitForResult() {return false;}
00095 virtual void Send(const byte* buf, size_t bufLen) =0;
00096 virtual unsigned int GetSendResult() =0;
00097 virtual void SendEof() =0;
00098 };
00099
00100 #ifdef HIGHRES_TIMER_AVAILABLE
00101
00102
00103 class CRYPTOPP_NO_VTABLE NetworkSource : public NonblockingSource
00104 {
00105 public:
00106 NetworkSource(BufferedTransformation *attachment);
00107
00108 unsigned int GetMaxWaitObjectCount() const
00109 {return GetReceiver().GetMaxWaitObjectCount() + AttachedTransformation()->GetMaxWaitObjectCount();}
00110 void GetWaitObjects(WaitObjectContainer &container);
00111
00112 size_t GeneralPump2(lword &byteCount, bool blockingOutput=true, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n');
00113 bool SourceExhausted() const {return m_dataBegin == m_dataEnd && GetReceiver().EofReceived();}
00114
00115 protected:
00116 virtual NetworkReceiver & AccessReceiver() =0;
00117 const NetworkReceiver & GetReceiver() const {return const_cast<NetworkSource *>(this)->AccessReceiver();}
00118
00119 private:
00120 SecByteBlock m_buf;
00121 size_t m_putSize, m_dataBegin, m_dataEnd;
00122 bool m_waitingForResult, m_outputBlocked;
00123 };
00124
00125
00126 class CRYPTOPP_NO_VTABLE NetworkSink : public NonblockingSink
00127 {
00128 public:
00129 NetworkSink(unsigned int maxBufferSize, unsigned int autoFlushBound);
00130
00131 unsigned int GetMaxWaitObjectCount() const
00132 {return GetSender().GetMaxWaitObjectCount();}
00133 void GetWaitObjects(WaitObjectContainer &container)
00134 {if (m_wasBlocked || !m_buffer.IsEmpty()) AccessSender().GetWaitObjects(container);}
00135
00136 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
00137
00138 lword TimedFlush(unsigned long maxTime, size_t targetSize = 0);
00139
00140 void SetMaxBufferSize(size_t maxBufferSize) {m_maxBufferSize = maxBufferSize; m_buffer.SetNodeSize(UnsignedMin(maxBufferSize, 16U*1024U+256U));}
00141 void SetAutoFlushBound(size_t bound) {m_autoFlushBound = bound;}
00142
00143 size_t GetMaxBufferSize() const {return m_maxBufferSize;}
00144 size_t GetCurrentBufferSize() const {return (size_t)m_buffer.CurrentSize();}
00145
00146 void ClearBuffer() {m_buffer.Clear();}
00147
00148
00149 float ComputeCurrentSpeed();
00150
00151 float GetMaxObservedSpeed() const {return m_maxObservedSpeed;}
00152
00153 protected:
00154 virtual NetworkSender & AccessSender() =0;
00155 const NetworkSender & GetSender() const {return const_cast<NetworkSink *>(this)->AccessSender();}
00156
00157 private:
00158 size_t m_maxBufferSize, m_autoFlushBound;
00159 bool m_needSendResult, m_wasBlocked;
00160 ByteQueue m_buffer;
00161 size_t m_skipBytes;
00162 Timer m_speedTimer;
00163 float m_byteCountSinceLastTimerReset, m_currentSpeed, m_maxObservedSpeed;
00164 };
00165
00166 #endif // #ifdef HIGHRES_TIMER_AVAILABLE
00167
00168 NAMESPACE_END
00169
00170 #endif