<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Crypto++: network.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>network.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// network.cpp - written and placed in the public domain by Wei Dai</span> <a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span> <a name="l00004"></a>00004 <span class="preprocessor">#include "network.h"</span> <a name="l00005"></a>00005 <span class="preprocessor">#include "wait.h"</span> <a name="l00006"></a>00006 <a name="l00007"></a>00007 <span class="preprocessor">#define CRYPTOPP_TRACE_NETWORK 0</span> <a name="l00008"></a>00008 <span class="preprocessor"></span> <a name="l00009"></a>00009 NAMESPACE_BEGIN(CryptoPP) <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="preprocessor">#ifdef HIGHRES_TIMER_AVAILABLE</span> <a name="l00012"></a>00012 <span class="preprocessor"></span> <a name="l00013"></a>00013 lword LimitedBandwidth::ComputeCurrentTransceiveLimit() <a name="l00014"></a>00014 { <a name="l00015"></a>00015 <span class="keywordflow">if</span> (!m_maxBytesPerSecond) <a name="l00016"></a>00016 <span class="keywordflow">return</span> ULONG_MAX; <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="keywordtype">double</span> curTime = GetCurTimeAndCleanUp(); <a name="l00019"></a>00019 lword total = 0; <a name="l00020"></a>00020 <span class="keywordflow">for</span> (OpQueue::size_type i=0; i!=m_ops.size(); ++i) <a name="l00021"></a>00021 total += m_ops[i].second; <a name="l00022"></a>00022 <span class="keywordflow">return</span> SaturatingSubtract(m_maxBytesPerSecond, total); <a name="l00023"></a>00023 } <a name="l00024"></a>00024 <a name="l00025"></a>00025 <span class="keywordtype">double</span> LimitedBandwidth::TimeToNextTransceive() <a name="l00026"></a>00026 { <a name="l00027"></a>00027 <span class="keywordflow">if</span> (!m_maxBytesPerSecond) <a name="l00028"></a>00028 <span class="keywordflow">return</span> 0; <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="keywordflow">if</span> (!m_nextTransceiveTime) <a name="l00031"></a>00031 ComputeNextTransceiveTime(); <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="keywordflow">return</span> SaturatingSubtract(m_nextTransceiveTime, m_timer.ElapsedTimeAsDouble()); <a name="l00034"></a>00034 } <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="keywordtype">void</span> LimitedBandwidth::NoteTransceive(lword size) <a name="l00037"></a>00037 { <a name="l00038"></a>00038 <span class="keywordflow">if</span> (m_maxBytesPerSecond) <a name="l00039"></a>00039 { <a name="l00040"></a>00040 <span class="keywordtype">double</span> curTime = GetCurTimeAndCleanUp(); <a name="l00041"></a>00041 m_ops.push_back(std::make_pair(curTime, size)); <a name="l00042"></a>00042 m_nextTransceiveTime = 0; <a name="l00043"></a>00043 } <a name="l00044"></a>00044 } <a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="keywordtype">void</span> LimitedBandwidth::ComputeNextTransceiveTime() <a name="l00047"></a>00047 { <a name="l00048"></a>00048 <span class="keywordtype">double</span> curTime = GetCurTimeAndCleanUp(); <a name="l00049"></a>00049 lword total = 0; <a name="l00050"></a>00050 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i!=m_ops.size(); ++i) <a name="l00051"></a>00051 total += m_ops[i].second; <a name="l00052"></a>00052 m_nextTransceiveTime = <a name="l00053"></a>00053 (total < m_maxBytesPerSecond) ? curTime : m_ops.front().first + 1000; <a name="l00054"></a>00054 } <a name="l00055"></a>00055 <a name="l00056"></a>00056 <span class="keywordtype">double</span> LimitedBandwidth::GetCurTimeAndCleanUp() <a name="l00057"></a>00057 { <a name="l00058"></a>00058 <span class="keywordflow">if</span> (!m_maxBytesPerSecond) <a name="l00059"></a>00059 <span class="keywordflow">return</span> 0; <a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="keywordtype">double</span> curTime = m_timer.ElapsedTimeAsDouble(); <a name="l00062"></a>00062 <span class="keywordflow">while</span> (m_ops.size() && (m_ops.front().first + 1000 < curTime)) <a name="l00063"></a>00063 m_ops.pop_front(); <a name="l00064"></a>00064 <span class="keywordflow">return</span> curTime; <a name="l00065"></a>00065 } <a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="keywordtype">void</span> LimitedBandwidth::GetWaitObjects(<a class="code" href="class_wait_object_container.html" title="container of wait objects">WaitObjectContainer</a> &container, <span class="keyword">const</span> <a class="code" href="class_call_stack.html">CallStack</a> &callStack) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 <span class="keywordtype">double</span> nextTransceiveTime = TimeToNextTransceive(); <a name="l00070"></a>00070 <span class="keywordflow">if</span> (nextTransceiveTime) <a name="l00071"></a>00071 container.ScheduleEvent(nextTransceiveTime, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"LimitedBandwidth::GetWaitObjects()"</span>, &callStack)); <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="comment">// *************************************************************</span> <a name="l00075"></a>00075 <a name="l00076"></a><a class="code" href="class_nonblocking_source.html#a942956a5be86ee85268285ccbcc1eda5">00076</a> <span class="keywordtype">size_t</span> <a class="code" href="class_nonblocking_source.html#a942956a5be86ee85268285ccbcc1eda5" title="pump up to maxSize bytes using at most maxTime milliseconds">NonblockingSource::GeneralPump2</a>( <a name="l00077"></a>00077 lword& byteCount, <span class="keywordtype">bool</span> blockingOutput, <a name="l00078"></a>00078 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> maxTime, <span class="keywordtype">bool</span> checkDelimiter, byte delimiter) <a name="l00079"></a>00079 { <a name="l00080"></a>00080 m_blockedBySpeedLimit = <span class="keyword">false</span>; <a name="l00081"></a>00081 <a name="l00082"></a>00082 <span class="keywordflow">if</span> (!GetMaxBytesPerSecond()) <a name="l00083"></a>00083 { <a name="l00084"></a>00084 <span class="keywordtype">size_t</span> ret = DoPump(byteCount, blockingOutput, maxTime, checkDelimiter, delimiter); <a name="l00085"></a>00085 m_doPumpBlocked = (ret != 0); <a name="l00086"></a>00086 <span class="keywordflow">return</span> ret; <a name="l00087"></a>00087 } <a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="keywordtype">bool</span> forever = (maxTime == <a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a>); <a name="l00090"></a>00090 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> timeToGo = maxTime; <a name="l00091"></a>00091 <a class="code" href="class_timer.html" title="high resolution timer">Timer</a> timer(Timer::MILLISECONDS, forever); <a name="l00092"></a>00092 lword maxSize = byteCount; <a name="l00093"></a>00093 byteCount = 0; <a name="l00094"></a>00094 <a name="l00095"></a>00095 timer.StartTimer(); <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keywordflow">while</span> (<span class="keyword">true</span>) <a name="l00098"></a>00098 { <a name="l00099"></a>00099 lword curMaxSize = UnsignedMin(ComputeCurrentTransceiveLimit(), maxSize - byteCount); <a name="l00100"></a>00100 <a name="l00101"></a>00101 <span class="keywordflow">if</span> (curMaxSize || m_doPumpBlocked) <a name="l00102"></a>00102 { <a name="l00103"></a>00103 <span class="keywordflow">if</span> (!forever) timeToGo = SaturatingSubtract(maxTime, timer.ElapsedTime()); <a name="l00104"></a>00104 <span class="keywordtype">size_t</span> ret = DoPump(curMaxSize, blockingOutput, timeToGo, checkDelimiter, delimiter); <a name="l00105"></a>00105 m_doPumpBlocked = (ret != 0); <a name="l00106"></a>00106 <span class="keywordflow">if</span> (curMaxSize) <a name="l00107"></a>00107 { <a name="l00108"></a>00108 NoteTransceive(curMaxSize); <a name="l00109"></a>00109 byteCount += curMaxSize; <a name="l00110"></a>00110 } <a name="l00111"></a>00111 <span class="keywordflow">if</span> (ret) <a name="l00112"></a>00112 <span class="keywordflow">return</span> ret; <a name="l00113"></a>00113 } <a name="l00114"></a>00114 <a name="l00115"></a>00115 <span class="keywordflow">if</span> (maxSize != ULONG_MAX && byteCount >= maxSize) <a name="l00116"></a>00116 <span class="keywordflow">break</span>; <a name="l00117"></a>00117 <a name="l00118"></a>00118 <span class="keywordflow">if</span> (!forever) <a name="l00119"></a>00119 { <a name="l00120"></a>00120 timeToGo = SaturatingSubtract(maxTime, timer.ElapsedTime()); <a name="l00121"></a>00121 <span class="keywordflow">if</span> (!timeToGo) <a name="l00122"></a>00122 <span class="keywordflow">break</span>; <a name="l00123"></a>00123 } <a name="l00124"></a>00124 <a name="l00125"></a>00125 <span class="keywordtype">double</span> waitTime = TimeToNextTransceive(); <a name="l00126"></a>00126 <span class="keywordflow">if</span> (!forever && waitTime > timeToGo) <a name="l00127"></a>00127 { <a name="l00128"></a>00128 m_blockedBySpeedLimit = <span class="keyword">true</span>; <a name="l00129"></a>00129 <span class="keywordflow">break</span>; <a name="l00130"></a>00130 } <a name="l00131"></a>00131 <a name="l00132"></a>00132 <a class="code" href="class_wait_object_container.html" title="container of wait objects">WaitObjectContainer</a> container; <a name="l00133"></a>00133 LimitedBandwidth::GetWaitObjects(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NonblockingSource::GeneralPump2() - speed limit"</span>, 0)); <a name="l00134"></a>00134 container.Wait((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)waitTime); <a name="l00135"></a>00135 } <a name="l00136"></a>00136 <a name="l00137"></a>00137 <span class="keywordflow">return</span> 0; <a name="l00138"></a>00138 } <a name="l00139"></a>00139 <a name="l00140"></a>00140 <span class="keywordtype">size_t</span> NonblockingSource::PumpMessages2(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> &messageCount, <span class="keywordtype">bool</span> blocking) <a name="l00141"></a>00141 { <a name="l00142"></a>00142 <span class="keywordflow">if</span> (messageCount == 0) <a name="l00143"></a>00143 <span class="keywordflow">return</span> 0; <a name="l00144"></a>00144 <a name="l00145"></a>00145 messageCount = 0; <a name="l00146"></a>00146 <a name="l00147"></a>00147 lword byteCount; <a name="l00148"></a>00148 <span class="keywordflow">do</span> { <a name="l00149"></a>00149 byteCount = LWORD_MAX; <a name="l00150"></a>00150 RETURN_IF_NONZERO(Pump2(byteCount, blocking)); <a name="l00151"></a>00151 } <span class="keywordflow">while</span>(byteCount == LWORD_MAX); <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="keywordflow">if</span> (!m_messageEndSent && SourceExhausted()) <a name="l00154"></a>00154 { <a name="l00155"></a>00155 RETURN_IF_NONZERO(<a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()->Put2(NULL, 0, GetAutoSignalPropagation(), <span class="keyword">true</span>)); <a name="l00156"></a>00156 m_messageEndSent = <span class="keyword">true</span>; <a name="l00157"></a>00157 messageCount = 1; <a name="l00158"></a>00158 } <a name="l00159"></a>00159 <span class="keywordflow">return</span> 0; <a name="l00160"></a>00160 } <a name="l00161"></a>00161 <a name="l00162"></a><a class="code" href="class_nonblocking_sink.html#af5da8dc12d42cf6152588ab02de4d336">00162</a> lword <a class="code" href="class_nonblocking_sink.html#af5da8dc12d42cf6152588ab02de4d336" title="flush to device for no more than maxTime milliseconds">NonblockingSink::TimedFlush</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> maxTime, <span class="keywordtype">size_t</span> targetSize) <a name="l00163"></a>00163 { <a name="l00164"></a>00164 m_blockedBySpeedLimit = <span class="keyword">false</span>; <a name="l00165"></a>00165 <a name="l00166"></a>00166 <span class="keywordtype">size_t</span> curBufSize = GetCurrentBufferSize(); <a name="l00167"></a>00167 <span class="keywordflow">if</span> (curBufSize <= targetSize && (targetSize || !EofPending())) <a name="l00168"></a>00168 <span class="keywordflow">return</span> 0; <a name="l00169"></a>00169 <a name="l00170"></a>00170 <span class="keywordflow">if</span> (!GetMaxBytesPerSecond()) <a name="l00171"></a>00171 <span class="keywordflow">return</span> DoFlush(maxTime, targetSize); <a name="l00172"></a>00172 <a name="l00173"></a>00173 <span class="keywordtype">bool</span> forever = (maxTime == <a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a>); <a name="l00174"></a>00174 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> timeToGo = maxTime; <a name="l00175"></a>00175 <a class="code" href="class_timer.html" title="high resolution timer">Timer</a> timer(Timer::MILLISECONDS, forever); <a name="l00176"></a>00176 lword totalFlushed = 0; <a name="l00177"></a>00177 <a name="l00178"></a>00178 timer.StartTimer(); <a name="l00179"></a>00179 <a name="l00180"></a>00180 <span class="keywordflow">while</span> (<span class="keyword">true</span>) <a name="l00181"></a>00181 { <a name="l00182"></a>00182 <span class="keywordtype">size_t</span> flushSize = UnsignedMin(curBufSize - targetSize, ComputeCurrentTransceiveLimit()); <a name="l00183"></a>00183 <span class="keywordflow">if</span> (flushSize || EofPending()) <a name="l00184"></a>00184 { <a name="l00185"></a>00185 <span class="keywordflow">if</span> (!forever) timeToGo = SaturatingSubtract(maxTime, timer.ElapsedTime()); <a name="l00186"></a>00186 <span class="keywordtype">size_t</span> ret = (size_t)DoFlush(timeToGo, curBufSize - flushSize); <a name="l00187"></a>00187 <span class="keywordflow">if</span> (ret) <a name="l00188"></a>00188 { <a name="l00189"></a>00189 NoteTransceive(ret); <a name="l00190"></a>00190 curBufSize -= ret; <a name="l00191"></a>00191 totalFlushed += ret; <a name="l00192"></a>00192 } <a name="l00193"></a>00193 } <a name="l00194"></a>00194 <a name="l00195"></a>00195 <span class="keywordflow">if</span> (curBufSize <= targetSize && (targetSize || !EofPending())) <a name="l00196"></a>00196 <span class="keywordflow">break</span>; <a name="l00197"></a>00197 <a name="l00198"></a>00198 <span class="keywordflow">if</span> (!forever) <a name="l00199"></a>00199 { <a name="l00200"></a>00200 timeToGo = SaturatingSubtract(maxTime, timer.ElapsedTime()); <a name="l00201"></a>00201 <span class="keywordflow">if</span> (!timeToGo) <a name="l00202"></a>00202 <span class="keywordflow">break</span>; <a name="l00203"></a>00203 } <a name="l00204"></a>00204 <a name="l00205"></a>00205 <span class="keywordtype">double</span> waitTime = TimeToNextTransceive(); <a name="l00206"></a>00206 <span class="keywordflow">if</span> (!forever && waitTime > timeToGo) <a name="l00207"></a>00207 { <a name="l00208"></a>00208 m_blockedBySpeedLimit = <span class="keyword">true</span>; <a name="l00209"></a>00209 <span class="keywordflow">break</span>; <a name="l00210"></a>00210 } <a name="l00211"></a>00211 <a name="l00212"></a>00212 <a class="code" href="class_wait_object_container.html" title="container of wait objects">WaitObjectContainer</a> container; <a name="l00213"></a>00213 LimitedBandwidth::GetWaitObjects(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NonblockingSink::TimedFlush() - speed limit"</span>, 0)); <a name="l00214"></a>00214 container.Wait((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)waitTime); <a name="l00215"></a>00215 } <a name="l00216"></a>00216 <a name="l00217"></a>00217 <span class="keywordflow">return</span> totalFlushed; <a name="l00218"></a>00218 } <a name="l00219"></a>00219 <a name="l00220"></a>00220 <span class="keywordtype">bool</span> NonblockingSink::IsolatedFlush(<span class="keywordtype">bool</span> hardFlush, <span class="keywordtype">bool</span> blocking) <a name="l00221"></a>00221 { <a name="l00222"></a>00222 <a class="code" href="class_nonblocking_sink.html#af5da8dc12d42cf6152588ab02de4d336" title="flush to device for no more than maxTime milliseconds">TimedFlush</a>(blocking ? <a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a> : 0); <a name="l00223"></a>00223 <span class="keywordflow">return</span> hardFlush && (!!GetCurrentBufferSize() || EofPending()); <a name="l00224"></a>00224 } <a name="l00225"></a>00225 <a name="l00226"></a>00226 <span class="comment">// *************************************************************</span> <a name="l00227"></a>00227 <a name="l00228"></a>00228 NetworkSource::NetworkSource(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *attachment) <a name="l00229"></a>00229 : <a class="code" href="class_nonblocking_source.html" title="a Source class that can pump from a device for a specified amount of time.">NonblockingSource</a>(attachment), m_buf(1024*16) <a name="l00230"></a>00230 , m_waitingForResult(false), m_outputBlocked(false) <a name="l00231"></a>00231 , m_dataBegin(0), m_dataEnd(0) <a name="l00232"></a>00232 { <a name="l00233"></a>00233 } <a name="l00234"></a>00234 <a name="l00235"></a><a class="code" href="class_network_source.html#a4f8b5b536e6862529ba82c4efd2978ef">00235</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_network_source.html#a4f8b5b536e6862529ba82c4efd2978ef">NetworkSource::GetMaxWaitObjectCount</a>()<span class="keyword"> const</span> <a name="l00236"></a>00236 <span class="keyword"></span>{ <a name="l00237"></a>00237 <span class="keywordflow">return</span> <a class="code" href="class_network_source.html#a4f8b5b536e6862529ba82c4efd2978ef">LimitedBandwidth::GetMaxWaitObjectCount</a>() <a name="l00238"></a>00238 + GetReceiver().<a class="code" href="class_waitable.html#a221cf2a8063487b56b97c27197e16b12" title="maximum number of wait objects that this object can return">GetMaxWaitObjectCount</a>() <a name="l00239"></a>00239 + <a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()-><a class="code" href="class_buffered_transformation.html#a99cd939cda477af4d8e09bf15c692dc2" title="maximum number of wait objects that this object can return">GetMaxWaitObjectCount</a>(); <a name="l00240"></a>00240 } <a name="l00241"></a>00241 <a name="l00242"></a>00242 <span class="keywordtype">void</span> NetworkSource::GetWaitObjects(<a class="code" href="class_wait_object_container.html" title="container of wait objects">WaitObjectContainer</a> &container, <a class="code" href="class_call_stack.html">CallStack</a> <span class="keyword">const</span>& callStack) <a name="l00243"></a>00243 { <a name="l00244"></a>00244 <span class="keywordflow">if</span> (BlockedBySpeedLimit()) <a name="l00245"></a>00245 LimitedBandwidth::GetWaitObjects(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::GetWaitObjects() - speed limit"</span>, &callStack)); <a name="l00246"></a>00246 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!m_outputBlocked) <a name="l00247"></a>00247 { <a name="l00248"></a>00248 <span class="keywordflow">if</span> (m_dataBegin == m_dataEnd) <a name="l00249"></a>00249 AccessReceiver().<a class="code" href="class_waitable.html#a97155f28db33d3dacc69dc453a01609a" title="put wait objects into container">GetWaitObjects</a>(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::GetWaitObjects() - no data"</span>, &callStack)); <a name="l00250"></a>00250 <span class="keywordflow">else</span> <a name="l00251"></a>00251 container.SetNoWait(<a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::GetWaitObjects() - have data"</span>, &callStack)); <a name="l00252"></a>00252 } <a name="l00253"></a>00253 <a name="l00254"></a>00254 <a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()-><a class="code" href="class_buffered_transformation.html#a127ccd1793fdda6548cd1295f848d0f7" title="put wait objects into container">GetWaitObjects</a>(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::GetWaitObjects() - attachment"</span>, &callStack)); <a name="l00255"></a>00255 } <a name="l00256"></a>00256 <a name="l00257"></a>00257 <span class="keywordtype">size_t</span> NetworkSource::DoPump(lword &byteCount, <span class="keywordtype">bool</span> blockingOutput, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> maxTime, <span class="keywordtype">bool</span> checkDelimiter, byte delimiter) <a name="l00258"></a>00258 { <a name="l00259"></a>00259 <a class="code" href="class_network_receiver.html" title="Network Receiver.">NetworkReceiver</a> &receiver = AccessReceiver(); <a name="l00260"></a>00260 <a name="l00261"></a>00261 lword maxSize = byteCount; <a name="l00262"></a>00262 byteCount = 0; <a name="l00263"></a>00263 <span class="keywordtype">bool</span> forever = maxTime == <a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a>; <a name="l00264"></a>00264 <a class="code" href="class_timer.html" title="high resolution timer">Timer</a> timer(Timer::MILLISECONDS, forever); <a name="l00265"></a>00265 <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *t = <a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>(); <a name="l00266"></a>00266 <a name="l00267"></a>00267 <span class="keywordflow">if</span> (m_outputBlocked) <a name="l00268"></a>00268 <span class="keywordflow">goto</span> DoOutput; <a name="l00269"></a>00269 <a name="l00270"></a>00270 <span class="keywordflow">while</span> (<span class="keyword">true</span>) <a name="l00271"></a>00271 { <a name="l00272"></a>00272 <span class="keywordflow">if</span> (m_dataBegin == m_dataEnd) <a name="l00273"></a>00273 { <a name="l00274"></a>00274 <span class="keywordflow">if</span> (receiver.EofReceived()) <a name="l00275"></a>00275 <span class="keywordflow">break</span>; <a name="l00276"></a>00276 <a name="l00277"></a>00277 <span class="keywordflow">if</span> (m_waitingForResult) <a name="l00278"></a>00278 { <a name="l00279"></a>00279 <span class="keywordflow">if</span> (receiver.MustWaitForResult() && <a name="l00280"></a>00280 !receiver.<a class="code" href="class_waitable.html#aded81bbe562f61932306a64e6122a6ca" title="wait on this object">Wait</a>(SaturatingSubtract(maxTime, timer.ElapsedTime()), <a name="l00281"></a>00281 <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::DoPump() - wait receive result"</span>, 0))) <a name="l00282"></a>00282 <span class="keywordflow">break</span>; <a name="l00283"></a>00283 <a name="l00284"></a>00284 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> recvResult = receiver.GetReceiveResult(); <a name="l00285"></a>00285 <span class="preprocessor">#if CRYPTOPP_TRACE_NETWORK</span> <a name="l00286"></a>00286 <span class="preprocessor"></span> OutputDebugString((IntToString((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)<span class="keyword">this</span>) + <span class="stringliteral">": Received "</span> + IntToString(recvResult) + <span class="stringliteral">" bytes\n"</span>).c_str()); <a name="l00287"></a>00287 <span class="preprocessor">#endif</span> <a name="l00288"></a>00288 <span class="preprocessor"></span> m_dataEnd += recvResult; <a name="l00289"></a>00289 m_waitingForResult = <span class="keyword">false</span>; <a name="l00290"></a>00290 <a name="l00291"></a>00291 <span class="keywordflow">if</span> (!receiver.MustWaitToReceive() && !receiver.EofReceived() && m_dataEnd != m_buf.size()) <a name="l00292"></a>00292 <span class="keywordflow">goto</span> ReceiveNoWait; <a name="l00293"></a>00293 } <a name="l00294"></a>00294 <span class="keywordflow">else</span> <a name="l00295"></a>00295 { <a name="l00296"></a>00296 m_dataEnd = m_dataBegin = 0; <a name="l00297"></a>00297 <a name="l00298"></a>00298 <span class="keywordflow">if</span> (receiver.MustWaitToReceive()) <a name="l00299"></a>00299 { <a name="l00300"></a>00300 <span class="keywordflow">if</span> (!receiver.<a class="code" href="class_waitable.html#aded81bbe562f61932306a64e6122a6ca" title="wait on this object">Wait</a>(SaturatingSubtract(maxTime, timer.ElapsedTime()), <a name="l00301"></a>00301 <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::DoPump() - wait receive"</span>, 0))) <a name="l00302"></a>00302 <span class="keywordflow">break</span>; <a name="l00303"></a>00303 <a name="l00304"></a>00304 receiver.<a class="code" href="class_network_receiver.html#a5e1c4db8bc07f0bf5b8d06496162cbe1" title="receive data from network source, returns whether result is immediately available...">Receive</a>(m_buf+m_dataEnd, m_buf.size()-m_dataEnd); <a name="l00305"></a>00305 m_waitingForResult = <span class="keyword">true</span>; <a name="l00306"></a>00306 } <a name="l00307"></a>00307 <span class="keywordflow">else</span> <a name="l00308"></a>00308 { <a name="l00309"></a>00309 ReceiveNoWait: <a name="l00310"></a>00310 m_waitingForResult = <span class="keyword">true</span>; <a name="l00311"></a>00311 <span class="comment">// call Receive repeatedly as long as data is immediately available,</span> <a name="l00312"></a>00312 <span class="comment">// because some receivers tend to return data in small pieces</span> <a name="l00313"></a>00313 <span class="preprocessor">#if CRYPTOPP_TRACE_NETWORK</span> <a name="l00314"></a>00314 <span class="preprocessor"></span> OutputDebugString((IntToString((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)<span class="keyword">this</span>) + <span class="stringliteral">": Receiving "</span> + IntToString(m_buf.size()-m_dataEnd) + <span class="stringliteral">" bytes\n"</span>).c_str()); <a name="l00315"></a>00315 <span class="preprocessor">#endif</span> <a name="l00316"></a>00316 <span class="preprocessor"></span> <span class="keywordflow">while</span> (receiver.<a class="code" href="class_network_receiver.html#a5e1c4db8bc07f0bf5b8d06496162cbe1" title="receive data from network source, returns whether result is immediately available...">Receive</a>(m_buf+m_dataEnd, m_buf.size()-m_dataEnd)) <a name="l00317"></a>00317 { <a name="l00318"></a>00318 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> recvResult = receiver.GetReceiveResult(); <a name="l00319"></a>00319 <span class="preprocessor">#if CRYPTOPP_TRACE_NETWORK</span> <a name="l00320"></a>00320 <span class="preprocessor"></span> OutputDebugString((IntToString((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)<span class="keyword">this</span>) + <span class="stringliteral">": Received "</span> + IntToString(recvResult) + <span class="stringliteral">" bytes\n"</span>).c_str()); <a name="l00321"></a>00321 <span class="preprocessor">#endif</span> <a name="l00322"></a>00322 <span class="preprocessor"></span> m_dataEnd += recvResult; <a name="l00323"></a>00323 <span class="keywordflow">if</span> (receiver.EofReceived() || m_dataEnd > m_buf.size() /2) <a name="l00324"></a>00324 { <a name="l00325"></a>00325 m_waitingForResult = <span class="keyword">false</span>; <a name="l00326"></a>00326 <span class="keywordflow">break</span>; <a name="l00327"></a>00327 } <a name="l00328"></a>00328 } <a name="l00329"></a>00329 } <a name="l00330"></a>00330 } <a name="l00331"></a>00331 } <a name="l00332"></a>00332 <span class="keywordflow">else</span> <a name="l00333"></a>00333 { <a name="l00334"></a>00334 m_putSize = UnsignedMin(m_dataEnd - m_dataBegin, maxSize - byteCount); <a name="l00335"></a>00335 <a name="l00336"></a>00336 <span class="keywordflow">if</span> (checkDelimiter) <a name="l00337"></a>00337 m_putSize = std::find(m_buf+m_dataBegin, m_buf+m_dataBegin+m_putSize, delimiter) - (m_buf+m_dataBegin); <a name="l00338"></a>00338 <a name="l00339"></a>00339 DoOutput: <a name="l00340"></a>00340 <span class="keywordtype">size_t</span> result = t-><a class="code" href="class_buffered_transformation.html#a770bf11f26d4dec66423a7dc2d36843d" title="input multiple bytes that may be modified by callee for blocking or non-blocking...">PutModifiable2</a>(m_buf+m_dataBegin, m_putSize, 0, forever || blockingOutput); <a name="l00341"></a>00341 <span class="keywordflow">if</span> (result) <a name="l00342"></a>00342 { <a name="l00343"></a>00343 <span class="keywordflow">if</span> (t-><a class="code" href="class_waitable.html#aded81bbe562f61932306a64e6122a6ca" title="wait on this object">Wait</a>(SaturatingSubtract(maxTime, timer.ElapsedTime()), <a name="l00344"></a>00344 <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSource::DoPump() - wait attachment"</span>, 0))) <a name="l00345"></a>00345 <span class="keywordflow">goto</span> DoOutput; <a name="l00346"></a>00346 <span class="keywordflow">else</span> <a name="l00347"></a>00347 { <a name="l00348"></a>00348 m_outputBlocked = <span class="keyword">true</span>; <a name="l00349"></a>00349 <span class="keywordflow">return</span> result; <a name="l00350"></a>00350 } <a name="l00351"></a>00351 } <a name="l00352"></a>00352 m_outputBlocked = <span class="keyword">false</span>; <a name="l00353"></a>00353 <a name="l00354"></a>00354 byteCount += m_putSize; <a name="l00355"></a>00355 m_dataBegin += m_putSize; <a name="l00356"></a>00356 <span class="keywordflow">if</span> (checkDelimiter && m_dataBegin < m_dataEnd && m_buf[m_dataBegin] == delimiter) <a name="l00357"></a>00357 <span class="keywordflow">break</span>; <a name="l00358"></a>00358 <span class="keywordflow">if</span> (maxSize != ULONG_MAX && byteCount == maxSize) <a name="l00359"></a>00359 <span class="keywordflow">break</span>; <a name="l00360"></a>00360 <span class="comment">// once time limit is reached, return even if there is more data waiting</span> <a name="l00361"></a>00361 <span class="comment">// but make 0 a special case so caller can request a large amount of data to be</span> <a name="l00362"></a>00362 <span class="comment">// pumped as long as it is immediately available</span> <a name="l00363"></a>00363 <span class="keywordflow">if</span> (maxTime > 0 && timer.ElapsedTime() > maxTime) <a name="l00364"></a>00364 <span class="keywordflow">break</span>; <a name="l00365"></a>00365 } <a name="l00366"></a>00366 } <a name="l00367"></a>00367 <a name="l00368"></a>00368 <span class="keywordflow">return</span> 0; <a name="l00369"></a>00369 } <a name="l00370"></a>00370 <a name="l00371"></a>00371 <span class="comment">// *************************************************************</span> <a name="l00372"></a>00372 <a name="l00373"></a>00373 NetworkSink::NetworkSink(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxBufferSize, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> autoFlushBound) <a name="l00374"></a>00374 : m_maxBufferSize(maxBufferSize), m_autoFlushBound(autoFlushBound) <a name="l00375"></a>00375 , m_needSendResult(false), m_wasBlocked(false), m_eofState(EOF_NONE) <a name="l00376"></a>00376 , m_buffer(STDMIN(16U*1024U+256, maxBufferSize)), m_skipBytes(0) <a name="l00377"></a>00377 , m_speedTimer(<a class="code" href="class_timer.html" title="high resolution timer">Timer</a>::MILLISECONDS), m_byteCountSinceLastTimerReset(0) <a name="l00378"></a>00378 , m_currentSpeed(0), m_maxObservedSpeed(0) <a name="l00379"></a>00379 { <a name="l00380"></a>00380 } <a name="l00381"></a>00381 <a name="l00382"></a><a class="code" href="class_network_sink.html#aa0da4518b7999c27c4a131f71ef06795">00382</a> <span class="keywordtype">float</span> <a class="code" href="class_network_sink.html#aa0da4518b7999c27c4a131f71ef06795" title="compute the current speed of this sink in bytes per second">NetworkSink::ComputeCurrentSpeed</a>() <a name="l00383"></a>00383 { <a name="l00384"></a>00384 <span class="keywordflow">if</span> (m_speedTimer.ElapsedTime() > 1000) <a name="l00385"></a>00385 { <a name="l00386"></a>00386 m_currentSpeed = m_byteCountSinceLastTimerReset * 1000 / m_speedTimer.ElapsedTime(); <a name="l00387"></a>00387 m_maxObservedSpeed = STDMAX(m_currentSpeed, m_maxObservedSpeed * 0.98f); <a name="l00388"></a>00388 m_byteCountSinceLastTimerReset = 0; <a name="l00389"></a>00389 m_speedTimer.StartTimer(); <a name="l00390"></a>00390 <span class="comment">// OutputDebugString(("max speed: " + IntToString((int)m_maxObservedSpeed) + " current speed: " + IntToString((int)m_currentSpeed) + "\n").c_str());</span> <a name="l00391"></a>00391 } <a name="l00392"></a>00392 <span class="keywordflow">return</span> m_currentSpeed; <a name="l00393"></a>00393 } <a name="l00394"></a>00394 <a name="l00395"></a><a class="code" href="class_network_sink.html#a8ee5e7f84b6ceb801ad7fab4c840dad5">00395</a> <span class="keywordtype">float</span> <a class="code" href="class_network_sink.html#a8ee5e7f84b6ceb801ad7fab4c840dad5" title="get the maximum observed speed of this sink in bytes per second">NetworkSink::GetMaxObservedSpeed</a>()<span class="keyword"> const</span> <a name="l00396"></a>00396 <span class="keyword"></span>{ <a name="l00397"></a>00397 lword m = GetMaxBytesPerSecond(); <a name="l00398"></a>00398 <span class="keywordflow">return</span> m ? STDMIN(m_maxObservedSpeed, <span class="keywordtype">float</span>(CRYPTOPP_VC6_INT64 m)) : m_maxObservedSpeed; <a name="l00399"></a>00399 } <a name="l00400"></a>00400 <a name="l00401"></a><a class="code" href="class_network_sink.html#a4c1ccafa0cd1eb8ea8eebf8dbdbcda29">00401</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_network_sink.html#a4c1ccafa0cd1eb8ea8eebf8dbdbcda29">NetworkSink::GetMaxWaitObjectCount</a>()<span class="keyword"> const</span> <a name="l00402"></a>00402 <span class="keyword"></span>{ <a name="l00403"></a>00403 <span class="keywordflow">return</span> <a class="code" href="class_network_sink.html#a4c1ccafa0cd1eb8ea8eebf8dbdbcda29">LimitedBandwidth::GetMaxWaitObjectCount</a>() + GetSender().<a class="code" href="class_waitable.html#a221cf2a8063487b56b97c27197e16b12" title="maximum number of wait objects that this object can return">GetMaxWaitObjectCount</a>(); <a name="l00404"></a>00404 } <a name="l00405"></a>00405 <a name="l00406"></a>00406 <span class="keywordtype">void</span> NetworkSink::GetWaitObjects(<a class="code" href="class_wait_object_container.html" title="container of wait objects">WaitObjectContainer</a> &container, <a class="code" href="class_call_stack.html">CallStack</a> <span class="keyword">const</span>& callStack) <a name="l00407"></a>00407 { <a name="l00408"></a>00408 <span class="keywordflow">if</span> (BlockedBySpeedLimit()) <a name="l00409"></a>00409 LimitedBandwidth::GetWaitObjects(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::GetWaitObjects() - speed limit"</span>, &callStack)); <a name="l00410"></a>00410 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (m_wasBlocked) <a name="l00411"></a>00411 AccessSender().<a class="code" href="class_waitable.html#a97155f28db33d3dacc69dc453a01609a" title="put wait objects into container">GetWaitObjects</a>(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::GetWaitObjects() - was blocked"</span>, &callStack)); <a name="l00412"></a>00412 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!m_buffer.IsEmpty()) <a name="l00413"></a>00413 AccessSender().<a class="code" href="class_waitable.html#a97155f28db33d3dacc69dc453a01609a" title="put wait objects into container">GetWaitObjects</a>(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::GetWaitObjects() - buffer not empty"</span>, &callStack)); <a name="l00414"></a>00414 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (EofPending()) <a name="l00415"></a>00415 AccessSender().<a class="code" href="class_waitable.html#a97155f28db33d3dacc69dc453a01609a" title="put wait objects into container">GetWaitObjects</a>(container, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::GetWaitObjects() - EOF pending"</span>, &callStack)); <a name="l00416"></a>00416 } <a name="l00417"></a>00417 <a name="l00418"></a><a class="code" href="class_network_sink.html#a5827a2d026f19314c777b068ce2b69fb">00418</a> <span class="keywordtype">size_t</span> <a class="code" href="class_network_sink.html#a5827a2d026f19314c777b068ce2b69fb" title="input multiple bytes for blocking or non-blocking processing">NetworkSink::Put2</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">size_t</span> length, <span class="keywordtype">int</span> messageEnd, <span class="keywordtype">bool</span> blocking) <a name="l00419"></a>00419 { <a name="l00420"></a>00420 <span class="keywordflow">if</span> (m_eofState == EOF_DONE) <a name="l00421"></a>00421 { <a name="l00422"></a>00422 <span class="keywordflow">if</span> (length || messageEnd) <a name="l00423"></a>00423 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html" title="base class for all exceptions thrown by Crypto++">Exception</a>(<a class="code" href="class_exception.html#a8e53909d979479380e0274807c074c0aa27d9ee7704100a88e4ae4adcc4072015" title="some error not belong to any of the above categories">Exception::OTHER_ERROR</a>, <span class="stringliteral">"NetworkSink::Put2() being called after EOF had been sent"</span>); <a name="l00424"></a>00424 <a name="l00425"></a>00425 <span class="keywordflow">return</span> 0; <a name="l00426"></a>00426 } <a name="l00427"></a>00427 <a name="l00428"></a>00428 <span class="keywordflow">if</span> (m_eofState > EOF_NONE) <a name="l00429"></a>00429 <span class="keywordflow">goto</span> EofSite; <a name="l00430"></a>00430 <a name="l00431"></a>00431 { <a name="l00432"></a>00432 <span class="keywordflow">if</span> (m_skipBytes) <a name="l00433"></a>00433 { <a name="l00434"></a>00434 assert(length >= m_skipBytes); <a name="l00435"></a>00435 inString += m_skipBytes; <a name="l00436"></a>00436 length -= m_skipBytes; <a name="l00437"></a>00437 } <a name="l00438"></a>00438 <a name="l00439"></a>00439 m_buffer.<a class="code" href="class_buffered_transformation.html#ae70658b0d271f8e114ac6c3cc9774ede" title="input a byte for processing">Put</a>(inString, length); <a name="l00440"></a>00440 <a name="l00441"></a>00441 <span class="keywordflow">if</span> (!blocking || m_buffer.CurrentSize() > m_autoFlushBound) <a name="l00442"></a>00442 <a class="code" href="class_nonblocking_sink.html#af5da8dc12d42cf6152588ab02de4d336" title="flush to device for no more than maxTime milliseconds">TimedFlush</a>(0, 0); <a name="l00443"></a>00443 <a name="l00444"></a>00444 <span class="keywordtype">size_t</span> targetSize = messageEnd ? 0 : m_maxBufferSize; <a name="l00445"></a>00445 <span class="keywordflow">if</span> (blocking) <a name="l00446"></a>00446 <a class="code" href="class_nonblocking_sink.html#af5da8dc12d42cf6152588ab02de4d336" title="flush to device for no more than maxTime milliseconds">TimedFlush</a>(<a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a>, targetSize); <a name="l00447"></a>00447 <a name="l00448"></a>00448 <span class="keywordflow">if</span> (m_buffer.CurrentSize() > targetSize) <a name="l00449"></a>00449 { <a name="l00450"></a>00450 assert(!blocking); <a name="l00451"></a>00451 m_wasBlocked = <span class="keyword">true</span>; <a name="l00452"></a>00452 m_skipBytes += length; <a name="l00453"></a>00453 <span class="keywordtype">size_t</span> blockedBytes = UnsignedMin(length, m_buffer.CurrentSize() - targetSize); <a name="l00454"></a>00454 <span class="keywordflow">return</span> STDMAX<size_t>(blockedBytes, 1); <a name="l00455"></a>00455 } <a name="l00456"></a>00456 <a name="l00457"></a>00457 m_wasBlocked = <span class="keyword">false</span>; <a name="l00458"></a>00458 m_skipBytes = 0; <a name="l00459"></a>00459 } <a name="l00460"></a>00460 <a name="l00461"></a>00461 <span class="keywordflow">if</span> (messageEnd) <a name="l00462"></a>00462 { <a name="l00463"></a>00463 m_eofState = EOF_PENDING_SEND; <a name="l00464"></a>00464 <a name="l00465"></a>00465 EofSite: <a name="l00466"></a>00466 <a class="code" href="class_nonblocking_sink.html#af5da8dc12d42cf6152588ab02de4d336" title="flush to device for no more than maxTime milliseconds">TimedFlush</a>(blocking ? <a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a> : 0, 0); <a name="l00467"></a>00467 <span class="keywordflow">if</span> (m_eofState != EOF_DONE) <a name="l00468"></a>00468 <span class="keywordflow">return</span> 1; <a name="l00469"></a>00469 } <a name="l00470"></a>00470 <a name="l00471"></a>00471 <span class="keywordflow">return</span> 0; <a name="l00472"></a>00472 } <a name="l00473"></a>00473 <a name="l00474"></a>00474 lword NetworkSink::DoFlush(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> maxTime, <span class="keywordtype">size_t</span> targetSize) <a name="l00475"></a>00475 { <a name="l00476"></a>00476 <a class="code" href="class_network_sender.html" title="Network Sender.">NetworkSender</a> &sender = AccessSender(); <a name="l00477"></a>00477 <a name="l00478"></a>00478 <span class="keywordtype">bool</span> forever = maxTime == <a class="code" href="cryptlib_8h.html#a5dc7a255dd813433eeee3152544c2f1d" title="used to represent infinite time">INFINITE_TIME</a>; <a name="l00479"></a>00479 <a class="code" href="class_timer.html" title="high resolution timer">Timer</a> timer(Timer::MILLISECONDS, forever); <a name="l00480"></a>00480 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> totalFlushSize = 0; <a name="l00481"></a>00481 <a name="l00482"></a>00482 <span class="keywordflow">while</span> (<span class="keyword">true</span>) <a name="l00483"></a>00483 { <a name="l00484"></a>00484 <span class="keywordflow">if</span> (m_buffer.CurrentSize() <= targetSize) <a name="l00485"></a>00485 <span class="keywordflow">break</span>; <a name="l00486"></a>00486 <a name="l00487"></a>00487 <span class="keywordflow">if</span> (m_needSendResult) <a name="l00488"></a>00488 { <a name="l00489"></a>00489 <span class="keywordflow">if</span> (sender.MustWaitForResult() && <a name="l00490"></a>00490 !sender.<a class="code" href="class_waitable.html#aded81bbe562f61932306a64e6122a6ca" title="wait on this object">Wait</a>(SaturatingSubtract(maxTime, timer.ElapsedTime()), <a name="l00491"></a>00491 <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::DoFlush() - wait send result"</span>, 0))) <a name="l00492"></a>00492 <span class="keywordflow">break</span>; <a name="l00493"></a>00493 <a name="l00494"></a>00494 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sendResult = sender.GetSendResult(); <a name="l00495"></a>00495 <span class="preprocessor">#if CRYPTOPP_TRACE_NETWORK</span> <a name="l00496"></a>00496 <span class="preprocessor"></span> OutputDebugString((IntToString((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)<span class="keyword">this</span>) + <span class="stringliteral">": Sent "</span> + IntToString(sendResult) + <span class="stringliteral">" bytes\n"</span>).c_str()); <a name="l00497"></a>00497 <span class="preprocessor">#endif</span> <a name="l00498"></a>00498 <span class="preprocessor"></span> m_buffer.<a class="code" href="class_buffered_transformation.html#a0c25529ded99db20ad35ccef3f7234e6" title="discard skipMax bytes from the output buffer">Skip</a>(sendResult); <a name="l00499"></a>00499 totalFlushSize += sendResult; <a name="l00500"></a>00500 m_needSendResult = <span class="keyword">false</span>; <a name="l00501"></a>00501 <a name="l00502"></a>00502 <span class="keywordflow">if</span> (!m_buffer.<a class="code" href="class_byte_queue.html#a9f509e02fb4b1f1587d6b5a001ff43f7" title="returns whether any bytes are currently ready for retrieval">AnyRetrievable</a>()) <a name="l00503"></a>00503 <span class="keywordflow">break</span>; <a name="l00504"></a>00504 } <a name="l00505"></a>00505 <a name="l00506"></a>00506 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> timeOut = maxTime ? SaturatingSubtract(maxTime, timer.ElapsedTime()) : 0; <a name="l00507"></a>00507 <span class="keywordflow">if</span> (sender.MustWaitToSend() && !sender.<a class="code" href="class_waitable.html#aded81bbe562f61932306a64e6122a6ca" title="wait on this object">Wait</a>(timeOut, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::DoFlush() - wait send"</span>, 0))) <a name="l00508"></a>00508 <span class="keywordflow">break</span>; <a name="l00509"></a>00509 <a name="l00510"></a>00510 <span class="keywordtype">size_t</span> contiguousSize = 0; <a name="l00511"></a>00511 <span class="keyword">const</span> byte *block = m_buffer.Spy(contiguousSize); <a name="l00512"></a>00512 <a name="l00513"></a>00513 <span class="preprocessor">#if CRYPTOPP_TRACE_NETWORK</span> <a name="l00514"></a>00514 <span class="preprocessor"></span> OutputDebugString((IntToString((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)<span class="keyword">this</span>) + <span class="stringliteral">": Sending "</span> + IntToString(contiguousSize) + <span class="stringliteral">" bytes\n"</span>).c_str()); <a name="l00515"></a>00515 <span class="preprocessor">#endif</span> <a name="l00516"></a>00516 <span class="preprocessor"></span> sender.Send(block, contiguousSize); <a name="l00517"></a>00517 m_needSendResult = <span class="keyword">true</span>; <a name="l00518"></a>00518 <a name="l00519"></a>00519 <span class="keywordflow">if</span> (maxTime > 0 && timeOut == 0) <a name="l00520"></a>00520 <span class="keywordflow">break</span>; <span class="comment">// once time limit is reached, return even if there is more data waiting</span> <a name="l00521"></a>00521 } <a name="l00522"></a>00522 <a name="l00523"></a>00523 m_byteCountSinceLastTimerReset += totalFlushSize; <a name="l00524"></a>00524 <a class="code" href="class_network_sink.html#aa0da4518b7999c27c4a131f71ef06795" title="compute the current speed of this sink in bytes per second">ComputeCurrentSpeed</a>(); <a name="l00525"></a>00525 <a name="l00526"></a>00526 <span class="keywordflow">if</span> (m_buffer.IsEmpty() && !m_needSendResult) <a name="l00527"></a>00527 { <a name="l00528"></a>00528 <span class="keywordflow">if</span> (m_eofState == EOF_PENDING_SEND) <a name="l00529"></a>00529 { <a name="l00530"></a>00530 sender.SendEof(); <a name="l00531"></a>00531 m_eofState = sender.MustWaitForEof() ? EOF_PENDING_DELIVERY : EOF_DONE; <a name="l00532"></a>00532 } <a name="l00533"></a>00533 <a name="l00534"></a>00534 <span class="keywordflow">while</span> (m_eofState == EOF_PENDING_DELIVERY) <a name="l00535"></a>00535 { <a name="l00536"></a>00536 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> timeOut = maxTime ? SaturatingSubtract(maxTime, timer.ElapsedTime()) : 0; <a name="l00537"></a>00537 <span class="keywordflow">if</span> (!sender.<a class="code" href="class_waitable.html#aded81bbe562f61932306a64e6122a6ca" title="wait on this object">Wait</a>(timeOut, <a class="code" href="class_call_stack.html">CallStack</a>(<span class="stringliteral">"NetworkSink::DoFlush() - wait EOF"</span>, 0))) <a name="l00538"></a>00538 <span class="keywordflow">break</span>; <a name="l00539"></a>00539 <a name="l00540"></a>00540 <span class="keywordflow">if</span> (sender.EofSent()) <a name="l00541"></a>00541 m_eofState = EOF_DONE; <a name="l00542"></a>00542 } <a name="l00543"></a>00543 } <a name="l00544"></a>00544 <a name="l00545"></a>00545 <span class="keywordflow">return</span> totalFlushSize; <a name="l00546"></a>00546 } <a name="l00547"></a>00547 <a name="l00548"></a>00548 <span class="preprocessor">#endif // #ifdef HIGHRES_TIMER_AVAILABLE</span> <a name="l00549"></a>00549 <span class="preprocessor"></span> <a name="l00550"></a>00550 NAMESPACE_END </pre></div></div> <hr size="1"/><address style="text-align: right;"><small>Generated on 9 Dec 2009 for Crypto++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> </body> </html>