unknown
1970-01-01 00:00:00 UTC
--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's beyond your question so I possibly shouldn'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've introduced strand but never used it :). So it should look like this:<br><br></div><div> Server::Server()<br> {<br>
</div><div><div> //created once when io_service is created:<br></div> asio::strand strand( io_service );<br>
<br> ...<br> <br> //at any moment connection must be "touched":<br></div> strand.dispatch( boost::bind( &Server::ExpelIdlers, this ) );<br> }<br><div><div><div><br></div>
<div> void Server::ExpelIdlers()<br> {<br></div><div> for each client in clients<br> {<br><div>
//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> asio::error_code shutdownErrorCode;<br></div><div> asio::error_code closeErrorCode;<br></div><div> </div><div> //should be kicked due to being too old:<br></div> if ( client->lastMessageReceived < currentTime - ( 60 * 60 ) )<br>
{<br> client->socket.shutdown( asio::socket_base::shutdown_<div>both, shutdownErrorCode );<br></div> client->socket.close( closeErrorCode );<br> }<br> }<br> }<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"><<a href="mailto:***@qq.com" target="_blank">***@qq.com</a>></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 <span style="font-family:Verdana;line-height:1.5">strand</span><span style="font-family:Verdana;line-height:1.5"> .</span></div> <div><br></div><div><div>But if you access those <span style="line-height:1.5">connections/sessions from </span><span style="line-height:1.5">multiple</span><span style="line-height:1.5"> threads, you should use</span><span style="line-height:1.5"> Slav's </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">------------------ ???? ------------------</div><div style="font-size:12px;background:#efefef;padding:8px"><div><b>???:</b> "??";<<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>>;</div> <div><b>????:</b> 2014?4?2?(???) ??9:51</div><div><b>???:</b> "<a href="mailto:asio-***@lists.sourceforge.net" target="_blank">asio-***@lists.sourceforge.net</a>"<<a href="mailto:asio-***@lists.sourceforge.net" target="_blank">asio-***@lists.sourceforge.net</a>>; <u></u></div> <div></div><div><b>??:</b> 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. </div> <div class="gmail_extra"><br clear="all"><div><div dir="ltr">Dean Chen <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"><<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>></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>
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's beyond your question so I possibly shouldn'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've introduced strand but never used it :). So it should look like this:<br><br></div><div> Server::Server()<br> {<br>
</div><div><div> //created once when io_service is created:<br></div> asio::strand strand( io_service );<br>
<br> ...<br> <br> //at any moment connection must be "touched":<br></div> strand.dispatch( boost::bind( &Server::ExpelIdlers, this ) );<br> }<br><div><div><div><br></div>
<div> void Server::ExpelIdlers()<br> {<br></div><div> for each client in clients<br> {<br><div>
//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> asio::error_code shutdownErrorCode;<br></div><div> asio::error_code closeErrorCode;<br></div><div> </div><div> //should be kicked due to being too old:<br></div> if ( client->lastMessageReceived < currentTime - ( 60 * 60 ) )<br>
{<br> client->socket.shutdown( asio::socket_base::shutdown_<div>both, shutdownErrorCode );<br></div> client->socket.close( closeErrorCode );<br> }<br> }<br> }<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"><<a href="mailto:***@qq.com" target="_blank">***@qq.com</a>></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 <span style="font-family:Verdana;line-height:1.5">strand</span><span style="font-family:Verdana;line-height:1.5"> .</span></div> <div><br></div><div><div>But if you access those <span style="line-height:1.5">connections/sessions from </span><span style="line-height:1.5">multiple</span><span style="line-height:1.5"> threads, you should use</span><span style="line-height:1.5"> Slav's </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">------------------ ???? ------------------</div><div style="font-size:12px;background:#efefef;padding:8px"><div><b>???:</b> "??";<<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>>;</div> <div><b>????:</b> 2014?4?2?(???) ??9:51</div><div><b>???:</b> "<a href="mailto:asio-***@lists.sourceforge.net" target="_blank">asio-***@lists.sourceforge.net</a>"<<a href="mailto:asio-***@lists.sourceforge.net" target="_blank">asio-***@lists.sourceforge.net</a>>; <u></u></div> <div></div><div><b>??:</b> 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. </div> <div class="gmail_extra"><br clear="all"><div><div dir="ltr">Dean Chen <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"><<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>></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>