⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readthrottlefilter.html

📁 MINA+API 关于MINA的API
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="360" href="#360">360</a>             <strong class="jxr_keyword">boolean</strong> suspendedRead;<a name="361" href="#361">361</a>             <strong class="jxr_keyword">synchronized</strong> (state) {<a name="362" href="#362">362</a>                 suspendedRead = state.suspendedRead;<a name="363" href="#363">363</a>             }<a name="364" href="#364">364</a>             <a name="365" href="#365">365</a>             <em class="jxr_comment">// Suppress resumeRead() if read is suspended by this filter.</em><a name="366" href="#366">366</a>             <strong class="jxr_keyword">if</strong> (suspendedRead) {<a name="367" href="#367">367</a>                 trafficMask = trafficMask.and(TrafficMask.WRITE);<a name="368" href="#368">368</a>             }<a name="369" href="#369">369</a>         }<a name="370" href="#370">370</a>         <a name="371" href="#371">371</a>         nextFilter.filterSetTrafficMask(session, trafficMask);<a name="372" href="#372">372</a>     }<a name="373" href="#373">373</a> <a name="374" href="#374">374</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottleFilter.html">EnterFilter</a> <strong class="jxr_keyword">extends</strong> <a href="../../../../../org/apache/mina/common/IoFilterAdapter.html">IoFilterAdapter</a> {<a name="375" href="#375">375</a>         @Override<a name="376" href="#376">376</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> onPreRemove(<a name="377" href="#377">377</a>                 <a href="../../../../../org/apache/mina/common/IoFilterChain.html">IoFilterChain</a> parent, String name, <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter) <strong class="jxr_keyword">throws</strong> Exception {<a name="378" href="#378">378</a>             <em class="jxr_comment">// Remove the exit filter together.</em><a name="379" href="#379">379</a>             <strong class="jxr_keyword">try</strong> {<a name="380" href="#380">380</a>                 parent.remove(ReadThrottleFilter.<strong class="jxr_keyword">this</strong>);<a name="381" href="#381">381</a>             } <strong class="jxr_keyword">catch</strong> (Exception e) {<a name="382" href="#382">382</a>                 <em class="jxr_comment">// Ignore.</em><a name="383" href="#383">383</a>             }<a name="384" href="#384">384</a>         }<a name="385" href="#385">385</a>         <a name="386" href="#386">386</a>         @Override<a name="387" href="#387">387</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> onPostRemove(<a name="388" href="#388">388</a>                 <a href="../../../../../org/apache/mina/common/IoFilterChain.html">IoFilterChain</a> parent, String name, <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter) <strong class="jxr_keyword">throws</strong> Exception {<a name="389" href="#389">389</a>             parent.getSession().removeAttribute(STATE);<a name="390" href="#390">390</a>         }<a name="391" href="#391">391</a>     <a name="392" href="#392">392</a>         @Override<a name="393" href="#393">393</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> messageReceived(<a name="394" href="#394">394</a>                 <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter, <a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session, Object message) <strong class="jxr_keyword">throws</strong> Exception {<a name="395" href="#395">395</a>             enter(session, estimateSize(message));<a name="396" href="#396">396</a>             nextFilter.messageReceived(session, message);<a name="397" href="#397">397</a>         }<a name="398" href="#398">398</a>     }<a name="399" href="#399">399</a>     <a name="400" href="#400">400</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">int</strong> estimateSize(Object message) {<a name="401" href="#401">401</a>         <strong class="jxr_keyword">int</strong> size = messageSizeEstimator.estimateSize(message);<a name="402" href="#402">402</a>         <strong class="jxr_keyword">if</strong> (size &lt; 0) {<a name="403" href="#403">403</a>             <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalStateException(<a name="404" href="#404">404</a>                     MessageSizeEstimator.<strong class="jxr_keyword">class</strong>.getSimpleName() + <span class="jxr_string">" returned "</span> +<a name="405" href="#405">405</a>                     <span class="jxr_string">"a negative value ("</span> + size + <span class="jxr_string">"): "</span> + message);<a name="406" href="#406">406</a>         }<a name="407" href="#407">407</a>         <strong class="jxr_keyword">return</strong> size;<a name="408" href="#408">408</a>     }<a name="409" href="#409">409</a> <a name="410" href="#410">410</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">void</strong> enter(<a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session, <strong class="jxr_keyword">int</strong> size) {<a name="411" href="#411">411</a>         <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottleFilter.html">State</a> state = getState(session);<a name="412" href="#412">412</a> <a name="413" href="#413">413</a>         <strong class="jxr_keyword">int</strong> globalBufferSize = ReadThrottleFilter.globalBufferSize.addAndGet(size);<a name="414" href="#414">414</a>         <strong class="jxr_keyword">int</strong> serviceBufferSize = increaseServiceBufferSize(session.getService(), size);<a name="415" href="#415">415</a> <a name="416" href="#416">416</a>         <strong class="jxr_keyword">int</strong> maxGlobalBufferSize = <strong class="jxr_keyword">this</strong>.maxGlobalBufferSize;<a name="417" href="#417">417</a>         <strong class="jxr_keyword">int</strong> maxServiceBufferSize = <strong class="jxr_keyword">this</strong>.maxServiceBufferSize;<a name="418" href="#418">418</a>         <strong class="jxr_keyword">int</strong> maxSessionBufferSize = <strong class="jxr_keyword">this</strong>.maxSessionBufferSize;<a name="419" href="#419">419</a>         <a name="420" href="#420">420</a>         <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottlePolicy.html">ReadThrottlePolicy</a> policy = getPolicy();<a name="421" href="#421">421</a>         <a name="422" href="#422">422</a>         <strong class="jxr_keyword">boolean</strong> enforcePolicy = false;<a name="423" href="#423">423</a>         <strong class="jxr_keyword">int</strong> sessionBufferSize;<a name="424" href="#424">424</a>         <strong class="jxr_keyword">synchronized</strong> (state) {<a name="425" href="#425">425</a>             sessionBufferSize = (state.sessionBufferSize += size);<a name="426" href="#426">426</a>             <strong class="jxr_keyword">if</strong> ((maxSessionBufferSize != 0 &amp;&amp; sessionBufferSize &gt;= maxSessionBufferSize) ||<a name="427" href="#427">427</a>                 (maxServiceBufferSize != 0 &amp;&amp; serviceBufferSize &gt;= maxServiceBufferSize) ||<a name="428" href="#428">428</a>                 (maxGlobalBufferSize  != 0 &amp;&amp; globalBufferSize  &gt;= maxGlobalBufferSize)) {<a name="429" href="#429">429</a>                 enforcePolicy = <strong class="jxr_keyword">true</strong>;<a name="430" href="#430">430</a>                 <strong class="jxr_keyword">switch</strong> (policy) {<a name="431" href="#431">431</a>                 <strong class="jxr_keyword">case</strong> EXCEPTION:<a name="432" href="#432">432</a>                 <strong class="jxr_keyword">case</strong> BLOCK:<a name="433" href="#433">433</a>                     state.suspendedRead = <strong class="jxr_keyword">true</strong>;<a name="434" href="#434">434</a>                 }<a name="435" href="#435">435</a>             }<a name="436" href="#436">436</a>         }<a name="437" href="#437">437</a> <a name="438" href="#438">438</a>         <strong class="jxr_keyword">if</strong> (logger.isDebugEnabled()) {<a name="439" href="#439">439</a>             logger.debug(getMessage(session, <span class="jxr_string">"  Entered - "</span>));<a name="440" href="#440">440</a>         }<a name="441" href="#441">441</a>         <a name="442" href="#442">442</a>         <strong class="jxr_keyword">if</strong> (enforcePolicy) {<a name="443" href="#443">443</a>             <strong class="jxr_keyword">switch</strong> (policy) {<a name="444" href="#444">444</a>             <strong class="jxr_keyword">case</strong> CLOSE:<a name="445" href="#445">445</a>                 log(session, state);<a name="446" href="#446">446</a>                 session.close();<a name="447" href="#447">447</a>                 raiseException(session);<a name="448" href="#448">448</a>                 <strong class="jxr_keyword">break</strong>;<a name="449" href="#449">449</a>             <strong class="jxr_keyword">case</strong> EXCEPTION:<a name="450" href="#450">450</a>                 suspend(session, state, logger);<a name="451" href="#451">451</a>                 raiseException(session);<a name="452" href="#452">452</a>                 <strong class="jxr_keyword">break</strong>;<a name="453" href="#453">453</a>             <strong class="jxr_keyword">case</strong> BLOCK:<a name="454" href="#454">454</a>                 suspend(session, state, logger);<a name="455" href="#455">455</a>                 <strong class="jxr_keyword">break</strong>;<a name="456" href="#456">456</a>             <strong class="jxr_keyword">case</strong> LOG:<a name="457" href="#457">457</a>                 log(session, state);<a name="458" href="#458">458</a>                 <strong class="jxr_keyword">break</strong>;<a name="459" href="#459">459</a>             }<a name="460" href="#460">460</a>         }<a name="461" href="#461">461</a>     }<a name="462" href="#462">462</a> <a name="463" href="#463">463</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">void</strong> suspend(<a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session, <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottleFilter.html">State</a> state, Logger logger) {<a name="464" href="#464">464</a>         log(session, state);<a name="465" href="#465">465</a>         session.suspendRead();<a name="466" href="#466">466</a>         <strong class="jxr_keyword">if</strong> (logger.isDebugEnabled()) {<a name="467" href="#467">467</a>             logger.debug(getMessage(session, <span class="jxr_string">"Suspended - "</span>));<a name="468" href="#468">468</a>         }<a name="469" href="#469">469</a>     }<a name="470" href="#470">470</a>     <a name="471" href="#471">471</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">void</strong> exit(<a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session, <strong class="jxr_keyword">int</strong> size) {<a name="472" href="#472">472</a>         <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottleFilter.html">State</a> state = getState(session);<a name="473" href="#473">473</a> <a name="474" href="#474">474</a>         <strong class="jxr_keyword">int</strong> globalBufferSize = ReadThrottleFilter.globalBufferSize.addAndGet(-size);<a name="475" href="#475">475</a>         <strong class="jxr_keyword">if</strong> (globalBufferSize &lt; 0) {<a name="476" href="#476">476</a>             <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalStateException(<span class="jxr_string">"globalBufferSize: "</span> + globalBufferSize);<a name="477" href="#477">477</a>         }<a name="478" href="#478">478</a>         <a name="479" href="#479">479</a>         <strong class="jxr_keyword">int</strong> serviceBufferSize = increaseServiceBufferSize(session.getService(), -size);<a name="480" href="#480">480</a>         <strong class="jxr_keyword">if</strong> (serviceBufferSize &lt; 0) {<a name="481" href="#481">481</a>             <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalStateException(<span class="jxr_string">"serviceBufferSize: "</span> + serviceBufferSize);<a name="482" href="#482">482</a>         }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -