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 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
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,
unsigned int 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 unsigned int GetMaxBufferSize() const =0;
00059 virtual
unsigned int 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 unsigned int TimedFlush(
unsigned long maxTime,
unsigned int targetSize = 0) =0;
00083
00084
virtual void SetMaxBufferSize(
unsigned int maxBufferSize) =0;
00085
00086
virtual void SetAutoFlushBound(
unsigned int 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,
unsigned int 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
unsigned int GeneralPump2(
unsigned long &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
unsigned int 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
unsigned int Put2(
const byte *inString,
unsigned int length,
int messageEnd,
bool blocking);
00137
00138
unsigned int TimedFlush(
unsigned long maxTime,
unsigned int targetSize = 0);
00139
00140
void SetMaxBufferSize(
unsigned int maxBufferSize) {m_maxBufferSize = maxBufferSize; m_buffer.SetNodeSize(STDMIN(16U*1024U+256, maxBufferSize));}
00141 void SetAutoFlushBound(
unsigned int bound) {m_autoFlushBound = bound;}
00142
00143
unsigned int GetMaxBufferSize()
const {
return m_maxBufferSize;}
00144
unsigned int GetCurrentBufferSize()
const {
return 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
unsigned int m_maxBufferSize, m_autoFlushBound;
00159
bool m_needSendResult, m_wasBlocked;
00160
ByteQueue m_buffer;
00161
unsigned int 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