Discussion:
unknown
1970-01-01 00:00:00 UTC
Permalink
<div><br></div><div>2. I had tried this before, but ran into threading issu=
e, it made my app crash</div><div><br></div><div>3. So you suggest to use a=
global mutex to make sure when I am closing one socket, no async_read or a=
snyc_write operation happens on it in another thread.</div>


<div><br></div><div>4. I need to apply this mutex to all my read/write oper=
ations like this:</div><div>Sign is one connected object in my server app:<=
/div><div><br></div><div><div>void Sign::DoWrite(boost::shared_ptr&lt;Messa=
ge&gt; msg) {</div>


<div>=C2=A0 write_msgs_.push_back(msg);</div><div>=C2=A0 if (write_msgs_.si=
ze() =3D=3D 1) {</div><div>=C2=A0 =C2=A0 boost::shared_ptr&lt;vector&lt;cha=
r&gt; &gt; data(new vector&lt;char&gt;(400, 0));</div><div>=C2=A0 =C2=A0 ms=
g-&gt;Write(*data);</div><div>


=C2=A0 =C2=A0=C2=A0</div><div>=C2=A0 =C2=A0 clientsPoolMutex.lock();</div><=
div>=C2=A0 =C2=A0 async_write(socket,</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0=
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 strand_.wrap(=
bind(&amp;Sign::AfterSend, shared_from_this(), _1, data)));</div>


<div>=C2=A0 }</div><div>}</div></div><div><br></div><div><br></div></div><d=
iv class=3D"gmail_extra"><div class=3D""><br clear=3D"all"><div><div dir=3D=
"ltr">Dean Chen=C2=A0<br>Best regards<br><a href=3D"http://blog.csdn.net/cs=
freebird" target=3D"_blank">http://blog.csdn.net/csfreebird</a></div>


</div>
<br><br></div><div><div class=3D"h5"><div class=3D"gmail_quote">On Wed, Apr=
2, 2014 at 12:02 AM, Slav <span dir=3D"ltr">&lt;<a href=3D"mailto:slavmfm@=
gmail.com" target=3D"_blank">***@gmail.com</a>&gt;</span> wrote:<br><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #=
ccc solid;padding-left:1ex">


<div dir=3D"ltr"><div><div><div><div><div><div><div>To safely work with con=
nections asio::strand can be used:<br><br></div><div>=C2=A0=C2=A0=C2=A0 //c=
reated once when io_service is created:<br></div>=C2=A0=C2=A0=C2=A0 asio::s=
trand strand( io_service );<br>



=C2=A0=C2=A0=C2=A0 <br>=C2=A0=C2=A0=C2=A0 ...<br>=C2=A0=C2=A0=C2=A0 <br></d=
iv>=C2=A0=C2=A0=C2=A0 //at any moment connection must be &quot;touched&quot=
;:<br></div><div>=C2=A0=C2=A0=C2=A0 //no parallel async_read/write or handl=
e_accept() can be called since we&#39;re within asio::strand, but your serv=
er can start using clients so lock them in that case:<br>



</div>=C2=A0=C2=A0=C2=A0 clientsPoolMutex.lock();<br><br></div>=C2=A0=C2=A0=
=C2=A0 for each client in clients<br>=C2=A0=C2=A0=C2=A0 {<br></div><div>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 //multiple harmless errors can aris=
e (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 serv=
er if not catch them:<br>



</div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 asio::error_code shut=
downErrorCode;<br></div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 asi=
o::error_code closeErrorCode;<br></div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 <br></div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 //sh=
ould be kicked due to being too old:<br></div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 if ( client-&gt;lastMessageReceived &lt; currentTime - ( 60=
* 60 ) )<br>



=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 {<br></div>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 client-&gt;socket.shutdown( a=
sio::socket_base::shutdown_both, shutdownErrorCode );<br></div>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 client-&gt;socket.cl=
ose( closeErrorCode );<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br>=
=C2=A0=C2=A0=C2=A0 }<br><div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 <br>



</div></div></div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_qu=
ote">2014-04-01 17:51 GMT+04:00 Dean Chen <span dir=3D"ltr">&lt;<a href=3D"=
mailto:***@gmail.com" target=3D"_blank">***@gmail.com</a>&gt;=
</span>:<div>


<div><br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">Hi, Slav:<br>
=C2=A0 Thanks for your response. I used to create one thread to close the<b=
r>
=C2=A0 connection if it&#39;s timeout. But this cause my process to crash. =
Because I<br>
=C2=A0 cannot close one socket in another thread when there is another<br>
=C2=A0 async_write or async_read operation on it.<br>
Accordin to this post:<br>
=C2=A0 <a href=3D"http://web.archiveorange.com/archive/v/Q0J4VefPMc2v8QYvcV=
r3" target=3D"_blank">http://web.archiveorange.com/archive/v/Q0J4VefPMc2v8Q=
YvcVr3</a><br>
<br>
Most likely you have a threading issue in your program where you close a<br=
=C2=A0socket from one thread while simultaneously starting another async<br=
=C2=A0operation on the same socket from another thread. If you are sure thi=
s is<br>
=C2=A0not the case, please attach a small, complete program that exhibits t=
he<br>
=C2=A0problem. Thanks.<br>
<br>
=C2=A0 Could you teach me your way, how to close the socket safely?<br>
<div><div><br>
Slav &lt;<a href=3D"mailto:***@gmail.com" target=3D"_blank">***@gma=
il.com</a>&gt; writes:<br>
<br>
&gt; I had the same problem. Each day ~15000 users was connecting the<br>
&gt; server but some of connections wasn&#39;t closed, so when ~500000<br>
&gt; connections was hanging (some of them was live), server must be<br>
&gt; restarted - it happened like each month-two.<br>
&gt; Timers will significantly slow your server down, indeed. As was<br>
&gt; answered to me in this mailing lists, it should be solved with<br>
&gt; periodic iteration through all connections to kill any which hang too<=
br>
&gt; much depending your inner server logic.<br>
&gt; For my server I kick everyone who didn&#39;t sent anything to server f=
or<br>
&gt; an hour. Kicking process is being invoked each 5 minutes. Iteration<br=
&gt; through 10000 connections is lightning fast: takes less time than<br>
&gt; human can measure (so less than 80 milliseconds) so it&#39;s possible =
to<br>
&gt; run it each second but not needed.<br>
&gt;<br>
&gt; 2014-04-01 16:46 GMT+04:00 =E9=99=88=E6=8A=92 &lt;<a href=3D"mailto:cs=
***@gmail.com" target=3D"_blank">***@gmail.com</a>&gt;:<br>
&gt;<br>
&gt;<br>
&gt; =C2=A0 =C2=A0 Hi,<br>
&gt; =C2=A0 =C2=A0 My server needs to handle thousands of TCP connections. =
But I find<br>
&gt; =C2=A0 =C2=A0 many connections are not closed because async_read metho=
d blocks,<br>
&gt; =C2=A0 =C2=A0 it&#39;s weird!<br>
&gt; =C2=A0 =C2=A0 Today my server cannot work because there are too many T=
CP<br>
&gt; =C2=A0 =C2=A0 connections, I have to restart OS.<br>
&gt; =C2=A0 =C2=A0 I read a few articles which describes how to use dead li=
ne timer<br>
&gt; =C2=A0 =C2=A0 with async_read, but I have some sad experience, thousan=
ds of<br>
&gt; =C2=A0 =C2=A0 timers will slow down my server.<br>
&gt; =C2=A0 =C2=A0 See my question on stackoverflow:<br>
&gt; =C2=A0 =C2=A0 <a href=3D"http://stackoverflow.com/questions/22777835/i=
s-boostasio-asyn-read-" target=3D"_blank">http://stackoverflow.com/question=
s/22777835/is-boostasio-asyn-read-</a><br>
&gt; =C2=A0 =C2=A0 with-timer-a-good-idea<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; =C2=A0 =C2=A0 My question is:<br>
&gt; =C2=A0 =C2=A0 Why does my async_read blocks?<br>
&gt; =C2=A0 =C2=A0 Can I use dead line timer in this case?<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; =C2=A0 =C2=A0 Dean Chen<br>
&gt; =C2=A0 =C2=A0 Best regards<br>
&gt; =C2=A0 =C2=A0 <a href=3D"http://blog.csdn.net/csfreebird" target=3D"_b=
lank">http://blog.csdn.net/csfreebird</a><br>
&gt;<br>
&gt; =C2=A0 =C2=A0 --------------------------------------------------------=
-----------<br>
&gt; =C2=A0 =C2=A0 -----------<br>
&gt;<br>
&gt; =C2=A0 =C2=A0 _______________________________________________<br>
&gt; =C2=A0 =C2=A0 asio-users mailing list<br>
&gt; =C2=A0 =C2=A0 <a href=3D"mailto:asio-***@lists.sourceforge.net" targ=
et=3D"_blank">asio-***@lists.sourceforge.net</a><br>
&gt; =C2=A0 =C2=A0 <a href=3D"https://lists.sourceforge.net/lists/listinfo/=
asio-users" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/=
asio-users</a><br>
&gt; =C2=A0 =C2=A0 _______________________________________________<br>
&gt; =C2=A0 =C2=A0 Using Asio? List your project at<br>
&gt; =C2=A0 =C2=A0 <a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" t=
arget=3D"_blank">http://think-async.com/Asio/WhoIsUsingAsio</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; ----------------------------------------------------------------------=
--------<br>
&gt; _______________________________________________<br>
&gt; asio-users mailing list<br>
&gt; <a href=3D"mailto:asio-***@lists.sourceforge.net" target=3D"_blank">=
asio-***@lists.sourceforge.net</a><br>
&gt; <a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" ta=
rget=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a>=
<br>
&gt; _______________________________________________<br>
&gt; Using Asio? List your project at<br>
&gt; <a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blan=
k">http://think-async.com/Asio/WhoIsUsingAsio</a><br>
<br>
---------------------------------------------------------------------------=
---<br>
_______________________________________________<br>
asio-users mailing list<br>
<a href=3D"mailto:asio-***@lists.sourceforge.net" target=3D"_blank">asio-=
***@lists.sourceforge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" target=
=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a><br>
_______________________________________________<br>
Using Asio? List your project at<br>
<a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blank">ht=
tp://think-async.com/Asio/WhoIsUsingAsio</a><br>
</div></div></blockquote></div></div></div><br></div>
<br>-----------------------------------------------------------------------=
-------<br>
<br>_______________________________________________<br>
asio-users mailing list<br>
<a href=3D"mailto:asio-***@lists.sourceforge.net" target=3D"_blank">asio-=
***@lists.sourceforge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/asio-users" target=
=3D"_blank">https://lists.sourceforge.net/lists/listinfo/asio-users</a><br>
_______________________________________________<br>
Using Asio? List your project at<br>
<a href=3D"http://think-async.com/Asio/WhoIsUsingAsio" target=3D"_blank">ht=
tp://think-async.com/Asio/WhoIsUsingAsio</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br></div>

--047d7bf0dac6d95e9a04f605879c--

Loading...