Discussion:
unknown
1970-01-01 00:00:00 UTC
Permalink
--089e0158ab5c1aa96d04f60c99c1
Content-Type: text/html; charset=GB2312
Content-Transfer-Encoding: quoted-printable

<div dir="ltr"><div><div><div>Mutex is needed just to embrace data in case your server is multithreaded - it&#39;s beyond your question so I possibly shouldn&#39;t add it.<br></div>The whole work done here is by the strand, which ensures that function is being invoked within the same thread as any others async_read/write/accept handlers.<br>
</div>Now I see I forgot to show that the for(;;) code must be issued within strand, sorry. I&#39;ve introduced strand but never used it :). So it should look like this:<br><br></div><div>&nbsp;&nbsp;&nbsp; Server::Server()<br>&nbsp;&nbsp;&nbsp; {<br>
</div><div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //created once when io_service is created:<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; asio::strand strand( io_service );<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; //at any moment connection must be &quot;touched&quot;:<br></div>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; strand.dispatch( boost::bind( &amp;Server::ExpelIdlers, this ) );<br>&nbsp;&nbsp;&nbsp; }<br><div><div><div><br></div>
<div>&nbsp;&nbsp;&nbsp; void Server::ExpelIdlers()<br>&nbsp;&nbsp;&nbsp; {<br></div><div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for each client in clients<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//multiple harmless errors can arise (connection was already closed,
connection was forced to be close and so on) - just ignoring them is
fine, but usage of overloaded function which do not accept error codes
field can throw exceptions which can kill your server if not catch them:<br> </div><div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; asio::error_code shutdownErrorCode;<br></div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; asio::error_code closeErrorCode;<br></div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </div><div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //should be kicked due to being too old:<br></div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( client-&gt;lastMessageReceived &lt; currentTime - ( 60 * 60 ) )<br>
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client-&gt;socket.shutdown( asio::socket_base::shutdown_<div>both, shutdownErrorCode );<br></div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client-&gt;socket.close( closeErrorCode );<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br> </div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-04-02 7:18 GMT+04:00 ??? <span dir="ltr">&lt;<a href="mailto:***@qq.com" target="_blank">***@qq.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div>No global mutex, strand should enough.</div><div><br></div><div>Just push all function call of same socket to the same&nbsp;<span style="font-family:Verdana;line-height:1.5">strand</span><span style="font-family:Verdana;line-height:1.5">&nbsp;.</span></div> <div><br></div><div><div>But if you access those&nbsp;<span style="line-height:1.5">connections/sessions from&nbsp;</span><span style="line-height:1.5">multiple</span><span style="line-height:1.5">&nbsp;threads, you should use</span><span style="line-height:1.5">&nbsp;Slav&#39;s&nbsp;</span><span style="line-height:1.5">global mutex</span></div> <div><br></div><div style="font-size:12px;font-family:Arial Narrow;padding:2px 0 2px 0">------------------&nbsp;????&nbsp;------------------</div><div style="font-size:12px;background:#efefef;padding:8px"><div><b>???:</b>&nbsp;&quot;??&quot;;&lt;<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>&gt;;</div> <div><b>????:</b>&nbsp;2014?4?2?(???) ??9:51</div><div><b>???:</b>&nbsp;&quot;<a href="mailto:asio-***@lists.sourceforge.net" target="_blank">asio-***@lists.sourceforge.net</a>&quot;&lt;<a href="mailto:asio-***@lists.sourceforge.net" target="_blank">asio-***@lists.sourceforge.net</a>&gt;; <u></u></div> <div></div><div><b>??:</b>&nbsp;Re: [asio-users] Can I use dead_line_timer with async_read togetherwhen keeping thousands of TCP connections?</div></div><div><div class="h5"><div><br></div><div dir="ltr">But If all read/write operations need to get the global mutex, it might be another performance issue.&nbsp;</div> <div class="gmail_extra"><br clear="all"><div><div dir="ltr">Dean Chen&nbsp;<br>Best regards<br><a href="http://blog.csdn.net/csfreebird" target="_blank">http://blog.csdn.net/csfreebird</a></div> </div> <br><br><div class="gmail_quote">On Wed, Apr 2, 2014 at 9:40 AM, 陈抒 <span dir="ltr">&lt;<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div dir="ltr">Hi, Slav:<div>&nbsp;

Loading...