Boost.ASIO "deasynchronisation"
Hello,
I'm developing networking library that provides RTMP-like networking using
Boost.ASIO library. My problem is that any synchronous-access function
breaks into many little functions that do some part of job and invoke async
operation with "next" function as a handler, it makes the code really ugly
and obfuscating.
E.g. void hadshake that reads N bytes from peer, responds with those bytes +
same count of other bytes, reads N bytes back and checks that they are the
same. It breaks into:
void start_handshake_wait();
void handle_handshake_wait(const boost::system::error_code&, std::size_t,
buffer_ptr &);
void handle_handshake_wait_write_dup(..same as above..);
void handle_handshake_wait_write_mine(...);
void handle_handshake_wait_read_mine_check(...);
Every function uses boost::bind, so for some quite simple operation a lot of
code is produced. Receiving a chunk of data breaks into:
void read_header_head(); // header size depends on first byte
void parse_header_head();
void parse_header_body();
void parse_chunk_body();
The only idea I came to is to use some tricks like coroutines. (Sorry for
big listings)
template<class T>
class Context : public boost::enable_shared_from_this< Context<T> >
{
public:
typedef boost::shared_ptr
Igore Dmit. wrote:
My problem is that any synchronous-access function breaks into many little functions that do some part of job and invoke async operation with "next" function as a handler, it makes the code really ugly and obfuscating.
I know exactly what you mean. We've started using the proposed Boost.Coroutine library (not yet official, a Google Summer of Code effort plus some bug fixes). You might be interested that the original author specifically targets use with ASIO. http://www.crystalclearsoftware.com/soc/coroutine/index.html http://www.crystalclearsoftware.com/soc/coroutine/coroutine/asio.html http://www.boostpro.com/vault/index.php?action=downloadfile&filename=boost-coroutine-2009-12-01.tar.gz&directory=Concurrent%20Programming& A couple of the bundled tests are built around ASIO.
"Igore" == Igore Dmit
writes:
Igore> My problem is that any synchronous-access function breaks into Igore> many little functions that do some part of job and invoke async Igore> operation with "next" function as a handler, it makes the code Igore> really ugly and obfuscating.
"Nat" == Nat Goodspeed
writes:
Nat> I know exactly what you mean. We've started using the proposed Nat> Boost.Coroutine library (not yet official, a Google Summer of Nat> Code effort plus some bug fixes). You might be interested that Nat> the original author specifically targets use with ASIO. The main author of ASIO itself is also a big proponent of using coroutines, and he demonstrates some tricks for using them cleanly on top of ASIO: http://blog.think-async.com/2009/07/wife-says-i-cant-believe-it-works.html http://blog.think-async.com/2009/08/secret-sauce-revealed.html http://blog.think-async.com/2009/08/composed-operations-coroutines-and-code.... One more option to investigate... Happy Hacking, t.
Anthony Foiani wrote:
The main author of ASIO itself is also a big proponent of using coroutines, and he demonstrates some tricks for using them cleanly on top of ASIO:
http://blog.think-async.com/2009/07/wife-says-i-cant-believe-it- works.html http://blog.think-async.com/2009/08/secret-sauce-revealed.html http://blog.think-async.com/2009/08/composed-operations-coroutines-and- code.html
Also see the new http example #4 in Boost 1.42, Cheers, Rutger
participants (4)
-
Anthony Foiani
-
Igore Dmit.
-
Nat Goodspeed
-
Rutger ter Borg