Ronen Ness
Ronen Ness

Reputation: 10740

Using WaitForMultipleObjects() with ACE_SOCK_Stream - get event only when there's data

Is it possible to use WaitForMultipleObjects() with ACE_SOCK_Stream, and make it return only when there's data to read from it?

I tried to following:

    // set some params
    DWORD handlesCount = 1;
    DWORD timeoutMs = 5 * 1000;
    HANDLE* handles = new HANDLE[handlesCount]; 
    handles[0] = sock_stream.get_handle();

    while (true) {
        int ret = WaitForMultipleObjects(handlesCount, handles, false, timeoutMs);
        std::cout << "Result: " << ret << std::endl;

But the WaitForMultipleObjects() returns immediately the socket stream index, indicating that its ready (it prints 0 in an endless loop).

The socket is accepted via a ACE_SOCK_Acceptor (ACE_SOCK_Acceptor->accept()).

How do I make WaitForMultipleObjects() wait until the socket has data to read?

Upvotes: 0

Views: 164

Answers (1)

Steve Huston
Steve Huston

Reputation: 1590

The socket handle is not suitable for use in WFMO. You should use WSAEventSelect to associate the desired event(s) with an event handle that's registered with WFMO.

Since you are also familiar with ACE, you can check the source code for ace/WFMO_Reactor.cpp, register_handler() method to see a use-case and how it works with WFMO.

Upvotes: 2

Related Questions