locked
Timeout for reading from a StreamSocket? RRS feed

Answers

  • I finally found a document that had the answers I needed:

    http://msdn.microsoft.com/en-us/library/windows/apps/dd984117.aspx

    My final solution was roughly as follows:

    cancellation_token_source cts;
    mutex m_cancel;
    condition_variable cv;
    bool ready = false;
    
    unique_lock<mutex> lk(m_cancel);
    
    // start the read
    auto t = create_task(reader->LoadAsync(256), cts.get_token());
    t.then([&](unsigned int n)
    {
    	// read succeeded, notify the timeout condition variable
    	unique_lock<mutex> lk(m_cancel);
    
    	ready = true;
    	cv.notify_one();
    });
    
    if(!cv.wait_for(lk, chrono::milliseconds(1000), [&]{ return ready; }))
    {
    	// timeout condition variable timed out, cancel the read task
    	cts.cancel();
    	t.wait();
    	
    	{
    		// simulate a read of 0 bytes, when timeout occurs to close the connection
    		unique_lock<mutex> lk_data(m_data);
    		data.clear();
    		didRead = true;
    	}
    
    	return;
    }
    
    {
    	unique_lock<mutex> lk_data(m_data);
    	
    	size_t bytesRead = reader->UnconsumedBufferLength;
    	
    	for(size_t i = 0; i < bytesRead; ++i)
    		data.push_back(reader->ReadByte()); // vector<unsigned char> data
    	
    	didRead = true;
    }


    • Edited by Nick_00389294 Wednesday, October 23, 2013 3:05 AM
    • Marked as answer by Nick_00389294 Wednesday, October 23, 2013 3:05 AM
    Wednesday, October 23, 2013 3:04 AM

All replies

  • You are referring to window store apps, not windows phone apps, and while the syntax looks the same, the underlaying results are actually different.

    You can implement the timeout yourself, by calling the Close method after a set amount of time passed. You can measure this using a timer.


    • Edited by mcosmin Sunday, October 20, 2013 9:26 AM
    Sunday, October 20, 2013 9:26 AM
  • I finally found a document that had the answers I needed:

    http://msdn.microsoft.com/en-us/library/windows/apps/dd984117.aspx

    My final solution was roughly as follows:

    cancellation_token_source cts;
    mutex m_cancel;
    condition_variable cv;
    bool ready = false;
    
    unique_lock<mutex> lk(m_cancel);
    
    // start the read
    auto t = create_task(reader->LoadAsync(256), cts.get_token());
    t.then([&](unsigned int n)
    {
    	// read succeeded, notify the timeout condition variable
    	unique_lock<mutex> lk(m_cancel);
    
    	ready = true;
    	cv.notify_one();
    });
    
    if(!cv.wait_for(lk, chrono::milliseconds(1000), [&]{ return ready; }))
    {
    	// timeout condition variable timed out, cancel the read task
    	cts.cancel();
    	t.wait();
    	
    	{
    		// simulate a read of 0 bytes, when timeout occurs to close the connection
    		unique_lock<mutex> lk_data(m_data);
    		data.clear();
    		didRead = true;
    	}
    
    	return;
    }
    
    {
    	unique_lock<mutex> lk_data(m_data);
    	
    	size_t bytesRead = reader->UnconsumedBufferLength;
    	
    	for(size_t i = 0; i < bytesRead; ++i)
    		data.push_back(reader->ReadByte()); // vector<unsigned char> data
    	
    	didRead = true;
    }


    • Edited by Nick_00389294 Wednesday, October 23, 2013 3:05 AM
    • Marked as answer by Nick_00389294 Wednesday, October 23, 2013 3:05 AM
    Wednesday, October 23, 2013 3:04 AM