0

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?

Ronen Ness
  • 9,923
  • 4
  • 33
  • 50

1 Answers1

2

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.

Steve Huston
  • 1,432
  • 13
  • 20
  • Hi just wondering since you seem to know a little about ACE - whats the main difference between WFMO_Reactor and a normal ACE_Reactor? Because I see both behave pretty much the same as long as you want the WFMO bWaitAll flag false (and I know there's also the lower-handle higher-priority thing in WFMO, but is that it?). Basically I'm trying to figure out if I have any actual reason to use WFMO reactor and not the basic one. Thanks! – Ronen Ness Apr 24 '18 at 07:22
  • There is a very nice discussion of this question in The ACE Programmer's Guide (which I co-authored), section 7.7, pg 181. http://www.riverace.com/acebooks/index.htm#apg. But if you use ACE_Reactor::instance() on Windows, you're getting ACE_WFMO_Reactor. – Steve Huston Apr 25 '18 at 11:57
  • I appreciate your help very much and I'm sure the book is great, but maybe you could give a short answer for it here? Just the key point perhaps, a "spoiler" if you will (since I won't buy a whole book just for a single question). Thanks :) – Ronen Ness Apr 25 '18 at 19:59