Your description roughly corresponds with what I Already implemented. The
socket. As far as I know you cannot call those 2 functions on same socket
though while in progress even if in same ioservice thread. My solution
think might be caused by this design.
Post by Yuri TimenkovHi,
I'd suggest you create a class for session or transaction. It should
1) Endpoint of client
2) timer to handle cases when response couldn't be received.
3) socket for nested/dependent request (if required).
4) buffer for sending nested request/reading response and sending response
back to client.
5) reference to server socket for sending response (obviously)
I also suggest making this class derived form
boost::enable_shared_from_this and use
boost::bind(&Session::handle_something, shared_from_this(), <placeholders>)
for asio handler. This will ensure proper life time of all objects.
So your main server makes async_rececive_from it it's own receive buffer
and endpoint (preferably stored as members, same as above). When data comes
to your server socket, the async_recv_from handler creates (with
boost::make_shared<>) new session object, initializing it with data and
endpoint, and calls method which issues nested operation (beware that
shared_from_this() doesn't work from constructor). Or you may encapsulate
this into function (with semantics like async_read_until).
I think the rest is obvious: in this start() method you parse incoming
request, prepare outgoing request, async_send and then async_recv it, then
async_send_back_to_client. After final async_send_to handler completes
your object will be automatically disposed since references stored only in
binder objects.
In your timer handler your should close socket for nested request and send
error back to client. Or you may try to re-send request or employ any other
logic.
If you also have single socket for outgoing requests then you need to
store these sessions in some kind of map and use same "central" server to
dispatch incoming responses.
It's really not that hard, you just have to get used to this async
approach :)
Best wishes,
Yuri
On Wed, Sep 5, 2012 at 6:48 PM, Gheorghe Marinca <
Post by Gheorghe MarincaHow would you think on designing a sigle threaded udp server that listens
on a port and reply's to clients using same port at a later time after
processing their requests (so that while processing one request it can
still replay back to another client)
I was thinking on a mechanism where I use udp::socket::receive_from() in
peek mode when I think there is another time to check if datagrams requests
are queued up. If confirmation is received that a datagram is available I
read it synchronously and give it for processing on another thread to say.
In my single ioService thread then I check if responses to be sent to the
clients are available and send them blocking with udp::socket_send_to()
The problem that using this approach I end up eating 100% CPU time in
that ioService thread because I either peek in a sync way for a datagram
(1), I receive sync a datagram (2) or send sync responses to clients (3).
If no incoming datagrams are available or no responses to be sent back I
just loop burning up CPU.
Regards
-Ghita
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio