~ubuntu-branches/ubuntu/intrepid/libcrypto++/intrepid

« back to all changes in this revision

Viewing changes to network.h

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Machard
  • Date: 2004-08-27 12:35:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040827123505-7evgxiu7k8memiyk
Tags: 5.2.1a-1
* Urgency set to high because lastest upload was unclean
* Rename libcrypto++-5.2.1.orig.tar.gz in  libcrypto++-5.2.1a.orig.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
NAMESPACE_BEGIN(CryptoPP)
8
8
 
9
9
//! a Source class that can pump from a device for a specified amount of time.
10
 
class NonblockingSource : public AutoSignaling<Source>
 
10
class CRYPTOPP_NO_VTABLE NonblockingSource : public AutoSignaling<Source>
11
11
{
12
12
public:
13
13
        NonblockingSource(BufferedTransformation *attachment)
14
 
                : AutoSignaling<Source>(attachment), m_messageEndSent(false) {}
 
14
                : m_messageEndSent(false) {Detach(attachment);}
15
15
 
16
16
        //!     \name NONBLOCKING SOURCE
17
17
        //@{
40
40
};
41
41
 
42
42
//! Network Receiver
43
 
class NetworkReceiver : public Waitable
 
43
class CRYPTOPP_NO_VTABLE NetworkReceiver : public Waitable
44
44
{
45
45
public:
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;
51
52
};
52
53
 
 
54
class CRYPTOPP_NO_VTABLE NonblockingSinkInfo
 
55
{
 
56
public:
 
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;
 
64
};
 
65
 
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
55
68
{
56
69
public:
57
70
        bool IsolatedFlush(bool hardFlush, bool blocking);
69
82
        virtual unsigned int TimedFlush(unsigned long maxTime, unsigned int targetSize = 0) =0;
70
83
 
71
84
        virtual void SetMaxBufferSize(unsigned int maxBufferSize) =0;
72
 
        virtual void SetAutoFlush(bool autoFlush = true) =0;
73
 
 
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;
76
87
};
77
88
 
78
89
//! Network Sender
79
 
class NetworkSender : public Waitable
 
90
class CRYPTOPP_NO_VTABLE NetworkSender : public Waitable
80
91
{
81
92
public:
82
93
        virtual bool MustWaitToSend() {return false;}
89
100
#ifdef HIGHRES_TIMER_AVAILABLE
90
101
 
91
102
//! Network Source
92
 
class NetworkSource : public NonblockingSource
 
103
class CRYPTOPP_NO_VTABLE NetworkSource : public NonblockingSource
93
104
{
94
105
public:
95
106
        NetworkSource(BufferedTransformation *attachment);
96
107
 
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);
101
111
 
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();}
104
114
 
105
115
protected:
106
116
        virtual NetworkReceiver & AccessReceiver() =0;
107
117
        const NetworkReceiver & GetReceiver() const {return const_cast<NetworkSource *>(this)->AccessReceiver();}
108
118
 
109
119
private:
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;
113
123
};
114
124
 
115
125
//! Network Sink
116
 
class NetworkSink : public NonblockingSink
 
126
class CRYPTOPP_NO_VTABLE NetworkSink : public NonblockingSink
117
127
{
118
128
public:
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);
121
130
 
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);}
126
135
 
127
136
        unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
128
137
 
129
138
        unsigned int TimedFlush(unsigned long maxTime, unsigned int targetSize = 0);
130
139
 
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;}
133
142
 
134
143
        unsigned int GetMaxBufferSize() const {return m_maxBufferSize;}
135
144
        unsigned int GetCurrentBufferSize() const {return m_buffer.CurrentSize();}
136
145
 
 
146
        void ClearBuffer() {m_buffer.Clear();}
 
147
 
 
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;}
 
152
 
137
153
protected:
138
154
        virtual NetworkSender & AccessSender() =0;
139
155
        const NetworkSender & GetSender() const {return const_cast<NetworkSink *>(this)->AccessSender();}
140
156
 
141
157
private:
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;
 
162
        Timer m_speedTimer;
 
163
        float m_byteCountSinceLastTimerReset, m_currentSpeed, m_maxObservedSpeed;
146
164
};
147
165
 
148
166
#endif  // #ifdef HIGHRES_TIMER_AVAILABLE