Boost logo

Boost Users :

Subject: [Boost-users] boost::asio handler based on received data
From: Abdo Haji-Ali (abdo.haji.ali_at_[hidden])
Date: 2009-02-16 12:19:32


Greetings,

A server that I'm communicating with sends two types of messages
identified by an ID.
The first message type is sent automatically on specific events. The
second type is sent in response to commands.
I want to provide an interface that notifies the user when events
occur (asynchronously) and at the same time enable performing
commands. My first implementation did this (pseudocode):

// In initialization
socket.async_receive(buffer, event_handler);

// In send_command
socket.send(command_buffer);
socket.receive_async(response_buffer, user_response_handler);

event_handler() {
    user_event_handler();
    socket.async_receive(buffer, event_handler);
}

The problem here is that 'event_handler' might (or will?) receive the
response message. An additional problem is that these messages have no
specific order. In other words, the following sequence might occur:
- Send command (A)
- Send command (B)
- Receive event notification
- Receive response (A)
- Receive response (B)

A possible solution might be the following:

// In initialization
socket.async_receive(buffer, msg_handler);

// In send_command
socket.send(command_buffer);
handlers_queue.enqueue(user_command_handler);
socket.receive_async(response_buffer, msg_handler); // Line(3)

msg_handler() {
    if (event) user_event_handler();
    else handlers_queue.dequeue() (); // Call first user handler
    socket.async_receive(buffer, msg_handler);
}

I want to know if boost::asio provides a better way to solve this
problem. I'm particularly interested if writing a custom io service
can help me in this situation.

Thanks you,

-- 
Abdo Haji-Ali
Programmer
In|Framez

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