Discussion:
[asio-users] Best way to store a message to be broadcast
Darren Cook
2012-02-21 02:47:08 UTC
Permalink
I asked this question on stackoverflow:
http://stackoverflow.com/q/9324695/841830

Short version: what is the best way to (compose and) store a message to
be broadcast with async_write()? I have something working, but there
seems to be a lot of copying going on. I can see lots of choices in
asio, but I'm hoping for guidance on which is best in this case (and why).

Thanks!
Darren

P.S. If you hate StackOverflow, I don't mind answering here too.
--
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)
Green, Cliff
2012-02-21 06:31:24 UTC
Permalink
Take a look at the "shared_const_buffer" class in the Asio example / documentation. It's a fairly simple buf class that wraps a shared pointer and provides appropriate typedefs for the Asio buffer classes.

It will allow you to create one buffer that is used in multiple async writes, and when the last one finishes, the buffer will be destroyed "automagically" (the ref count of the shared pointer goes to 0).

It's relatively efficient and doesn't require any extra bookkeeping.

Cliff

-----Original Message-----
From: Darren Cook [mailto:***@dcook.org]
Sent: Monday, February 20, 2012 6:47 PM
To: asio-***@lists.sourceforge.net
Subject: [asio-users] Best way to store a message to be broadcast

I asked this question on stackoverflow:
http://stackoverflow.com/q/9324695/841830

Short version: what is the best way to (compose and) store a message to
be broadcast with async_write()? I have something working, but there
seems to be a lot of copying going on. I can see lots of choices in
asio, but I'm hoping for guidance on which is best in this case (and why).

Thanks!
Darren

P.S. If you hate StackOverflow, I don't mind answering here too.
--
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Darren Cook
2012-02-22 00:23:34 UTC
Permalink
Post by Green, Cliff
Post by Darren Cook
http://stackoverflow.com/q/9324695/841830
Take a look at the "shared_const_buffer" class in the Asio example /
documentation.
Thanks Cliff.
http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/buffers/reference_counted.cpp
Post by Green, Cliff
It's a fairly simple buf class that wraps a shared
pointer and provides appropriate typedefs for the Asio buffer
classes.
It will allow you to create one buffer that is used in multiple async
writes, and when the last one finishes, the buffer will be destroyed
"automagically" (the ref count of the shared pointer goes to 0).
It's relatively efficient and doesn't require any extra bookkeeping.
As you say it is quite short. I couldn't quite work out what this gains
me, compared to directly using a shared_ptr<std::string> as I do at the
moment?

Darren
--
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)
Green, Cliff
2012-02-22 08:11:51 UTC
Permalink
You may already be doing the equivalent - I briefly read through your code, but didn't study it in detail.

I use the shared_const_buffer so that there is never any extra copying of the buffer, even for multiple outstanding async writes (of the same data) to multiple clients (which I think is what you're doing). Once the buffer is created, it is shared among all the async writes. The Asio buffer, used in the async write call, doesn't copy the data - it is only a tuple that stores the equivalent of "pointer, size" (which is why it is imperative that the buffer lasts for the duration of the async write).

Maybe you were asking something different. :)

Cliff


-----Original Message-----
From: Darren Cook [mailto:***@dcook.org]
Sent: Tuesday, February 21, 2012 4:24 PM
To: asio-***@lists.sourceforge.net
Subject: Re: [asio-users] Best way to store a message to be broadcast
Post by Green, Cliff
Post by Darren Cook
http://stackoverflow.com/q/9324695/841830
Take a look at the "shared_const_buffer" class in the Asio example /
documentation.
Thanks Cliff.
http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/buffers/reference_counted.cpp
Post by Green, Cliff
It's a fairly simple buf class that wraps a shared
pointer and provides appropriate typedefs for the Asio buffer
classes.
It will allow you to create one buffer that is used in multiple async
writes, and when the last one finishes, the buffer will be destroyed
"automagically" (the ref count of the shared pointer goes to 0).
It's relatively efficient and doesn't require any extra bookkeeping.
As you say it is quite short. I couldn't quite work out what this gains
me, compared to directly using a shared_ptr<std::string> as I do at the
moment?

Darren
--
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Loading...