Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

network.h

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 //! a Source class that can pump from a device for a specified amount of time. 00010 class NonblockingSource : public AutoSignaling<Source> 00011 { 00012 public: 00013 NonblockingSource(BufferedTransformation *attachment) 00014 : AutoSignaling<Source>(attachment), m_messageEndSent(false) {} 00015 00016 //! \name NONBLOCKING SOURCE 00017 //@{ 00018 00019 //! pump up to maxSize bytes using at most maxTime milliseconds 00020 /*! If checkDelimiter is true, pump up to delimiter, which itself is not extracted or pumped. */ 00021 virtual unsigned int GeneralPump2(unsigned long &byteCount, bool blockingOutput=true, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n') =0; 00022 00023 unsigned long GeneralPump(unsigned long maxSize=ULONG_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 unsigned long TimedPump(unsigned long maxTime) 00029 {return GeneralPump(ULONG_MAX, maxTime);} 00030 unsigned long PumpLine(byte delimiter='\n', unsigned long maxSize=1024) 00031 {return GeneralPump(maxSize, INFINITE_TIME, true, delimiter);} 00032 00033 unsigned int Pump2(unsigned long &byteCount, bool blocking=true) 00034 {return GeneralPump2(byteCount, blocking, blocking ? INFINITE_TIME : 0);} 00035 unsigned int PumpMessages2(unsigned int &messageCount, bool blocking=true); 00036 //@} 00037 00038 private: 00039 bool m_messageEndSent; 00040 }; 00041 00042 //! Network Receiver 00043 class NetworkReceiver : public Waitable 00044 { 00045 public: 00046 virtual bool MustWaitToReceive() {return false;} 00047 virtual bool MustWaitForResult() {return false;} 00048 virtual void Receive(byte* buf, unsigned int bufLen) =0; 00049 virtual unsigned int GetReceiveResult() =0; 00050 virtual bool EofReceived() const =0; 00051 }; 00052 00053 //! a Sink class that queues input and can flush to a device for a specified amount of time. 00054 class NonblockingSink : public Sink 00055 { 00056 public: 00057 bool IsolatedFlush(bool hardFlush, bool blocking); 00058 00059 //! flush to device for no more than maxTime milliseconds 00060 /*! This function will repeatedly attempt to flush data to some device, until 00061 the queue is empty, or a total of maxTime milliseconds have elapsed. 00062 If maxTime == 0, at least one attempt will be made to flush some data, but 00063 it is likely that not all queued data will be flushed, even if the device 00064 is ready to receive more data without waiting. If you want to flush as much data 00065 as possible without waiting for the device, call this function in a loop. 00066 For example: while (sink.TimedFlush(0) > 0) {} 00067 \return number of bytes flushed 00068 */ 00069 virtual unsigned int TimedFlush(unsigned long maxTime, unsigned int targetSize = 0) =0; 00070 00071 virtual void SetMaxBufferSize(unsigned int maxBufferSize) =0; 00072 virtual void SetAutoFlush(bool autoFlush = true) =0; 00073 00074 virtual unsigned int GetMaxBufferSize() const =0; 00075 virtual unsigned int GetCurrentBufferSize() const =0; 00076 }; 00077 00078 //! Network Sender 00079 class NetworkSender : public Waitable 00080 { 00081 public: 00082 virtual bool MustWaitToSend() {return false;} 00083 virtual bool MustWaitForResult() {return false;} 00084 virtual void Send(const byte* buf, unsigned int bufLen) =0; 00085 virtual unsigned int GetSendResult() =0; 00086 virtual void SendEof() =0; 00087 }; 00088 00089 #ifdef HIGHRES_TIMER_AVAILABLE 00090 00091 //! Network Source 00092 class NetworkSource : public NonblockingSource 00093 { 00094 public: 00095 NetworkSource(BufferedTransformation *attachment); 00096 00097 unsigned int GetMaxWaitObjectCount() const 00098 {return GetReceiver().GetMaxWaitObjectCount() + AttachedTransformation()->GetMaxWaitObjectCount();} 00099 void GetWaitObjects(WaitObjectContainer &container) 00100 {AccessReceiver().GetWaitObjects(container); AttachedTransformation()->GetWaitObjects(container);} 00101 00102 unsigned int GeneralPump2(unsigned long &byteCount, bool blockingOutput=true, unsigned long maxTime=INFINITE_TIME, bool checkDelimiter=false, byte delimiter='\n'); 00103 bool SourceExhausted() const {return GetReceiver().EofReceived();} 00104 00105 protected: 00106 virtual NetworkReceiver & AccessReceiver() =0; 00107 const NetworkReceiver & GetReceiver() const {return const_cast<NetworkSource *>(this)->AccessReceiver();} 00108 00109 private: 00110 enum {NORMAL, WAITING_FOR_RESULT, OUTPUT_BLOCKED}; 00111 SecByteBlock m_buf; 00112 unsigned int m_bufSize, m_putSize, m_state; 00113 }; 00114 00115 //! Network Sink 00116 class NetworkSink : public NonblockingSink 00117 { 00118 public: 00119 NetworkSink(unsigned int maxBufferSize, bool autoFlush) 00120 : m_maxBufferSize(maxBufferSize), m_autoFlush(autoFlush), m_needSendResult(false), m_blockedBytes(0) {} 00121 00122 unsigned int GetMaxWaitObjectCount() const 00123 {return GetSender().GetMaxWaitObjectCount();} 00124 void GetWaitObjects(WaitObjectContainer &container) 00125 {if (m_blockedBytes || !m_buffer.IsEmpty()) AccessSender().GetWaitObjects(container);} 00126 00127 unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking); 00128 00129 unsigned int TimedFlush(unsigned long maxTime, unsigned int targetSize = 0); 00130 00131 void SetMaxBufferSize(unsigned int maxBufferSize) {m_maxBufferSize = maxBufferSize;} 00132 void SetAutoFlush(bool autoFlush = true) {m_autoFlush = autoFlush;} 00133 00134 unsigned int GetMaxBufferSize() const {return m_maxBufferSize;} 00135 unsigned int GetCurrentBufferSize() const {return m_buffer.CurrentSize();} 00136 00137 protected: 00138 virtual NetworkSender & AccessSender() =0; 00139 const NetworkSender & GetSender() const {return const_cast<NetworkSink *>(this)->AccessSender();} 00140 00141 private: 00142 unsigned int m_maxBufferSize; 00143 bool m_autoFlush, m_needSendResult; 00144 ByteQueue m_buffer; 00145 unsigned int m_blockedBytes; 00146 }; 00147 00148 #endif // #ifdef HIGHRES_TIMER_AVAILABLE 00149 00150 NAMESPACE_END 00151 00152 #endif

Generated on Fri Aug 13 09:56:54 2004 for Crypto++ by doxygen 1.3.7