Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] sync receive_from with timeout
From: Michael Dehmlow (dehmlowm_at_[hidden])
Date: 2009-05-08 14:19:23


Perhaps... that was a bit over simplified as follows is better:

while(!timeout.elapsed())
{
             boost::system::error_code ec;
             boost::asio::socket_base::non_blocking_io command(true);
             socket.io_control(command);
             bytesRead = _socket->read_some(boost::asio::buffer(data,
dataBufferSize), ec);
                    
             if(error&&boost::asio::error::would_block!=error)
             {
                   //there is some other error the client has disconnected;
             }
             else if bytes read>0)
             {
                   //I have some data
                   _myDataDelegate.processData(data,bytesRead);
             }
             else
             {
                   //No data for now lets take a break
                   boost::this_thread::sleep(
boost::posix_time::milliseconds(_myReadInterval));
             }
 }

Stephan Menzel-3 wrote:
>
> Hi Michael,
>
>> while(!timeout.elapsed())
>> {
>> boost::system::error_code ec;
>> boost::asio::socket_base::non_blocking_io command(true);
>> socket.io_control(command);
>> bytesRead = _socket->read_some(boost::asio::buffer(data,
>> dataBufferSize), ec);
>>
>> if(error&&boost::asio::error::would_block!=error)
>> {
>> //there is some other error the client has
>> disconnected;
>> }
>> else
>> {
>> //I must have got some data or theres no data yet check
>> bytesRead for that
>> }
>> boost::this_thread::sleep(
>> boost::posix_time::milliseconds(100));
>> }
>> No extra threads necessary.
>
> Indeed. But the way I see it, you gotta be aware that each of you reads
> will take at least 100 mils, right? In my scenario this is not really
> affordable.
>
> Or am I missing something here? The first read will never have something
> ready in a reliable way, right? Nice try though...
>
> Stephan
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>

-- 
View this message in context: http://www.nabble.com/-asio--sync-receive_from-with-timeout-tp22326399p23451095.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net