You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@qpid.apache.org by Jonathan Robie <jo...@redhat.com> on 2008/10/02 21:12:40 UTC
Writing binary data into messages (feedback?)
Three people have asked me how to write binary data into messages, so I
decided to give it a try. Here are two methods that make sense to me -
is this how we intend people to do it?
Jonathan
// Method 1: create string, write to message
// Let's format it all in a string buffer
std::string s(100, 0);
// Write some stuff into the string
memcpy(&s[0], &i, sizeof(i));
memcpy(&s[4], &f, sizeof(f));
// Put it in the message
message.setData(s);
// Method 2: write directly to message
// Send the message as per usual
Message msg;
msg.getData().resize(100);
memcpy(&msg.getData()[0], &i, sizeof(i));
memcpy(&msg.getData()[4], &f, sizeof(f));
Re: Writing binary data into messages (feedback?)
Posted by Alan Conway <ac...@redhat.com>.
On Fri, 2008-10-03 at 02:29 +0530, Danushka Menikkumbura wrote:
> > Hmmmm, that's too bad. I'd like to avoid creating a buffer and then
> > copying it over ("minimize the movement of data"). Is there a better
> > way to write binary data directly into the message contents without
> > creating a buffer first?
> Jonathan,
>
> When I go through the code (framing::TransferContent) I can clearly see
> that the current implementation can not handle binary data.
>
> At the moment "data" is a std::string and the method "setData" assigns
> the incoming string value to this std::string variable directly. When it
> comes to binary data, we can see null terminators in the middle and
> hence assigning the binary data stream to a std::string will definitely
> truncate the data. So what we will have in the message is just a part of
> the original binary data.
It will NOT truncate the data if you use the correct assignment
functions. std::string is perfectly capable of holding strings
containing null obviously any of the c-string funtions taking a char*
will not work but those taking a char* and a length will work fine.
E.g.
std::string bad("abc\0def"); // Will contain just "abc"
std::string good("abc\0def", 7); // Will contain "abc\0def"
> So, as a solution for this, we need to have two class level variables to
> keep binary data.
>
> 1. void* data;
> 2. long dataLength;
That is exactly what std::string contains (plus a reference count
typically for copy-on-write)
> These two are enough to say all about our binary data dump. So,
> obviously we need to have separate methods to get and set binary data.
>
> We may make use of these changes to keep string data as well so that we
> can drop the current std::string variable.
>
> I can send you a patch if we are happy with this.
A simple pointer+length is not sufficient here - the thing that's
missing is memory management policy. I agree that std::string's policy
is not ideal since it may impose an extra copy of data in some cases,
but if add other options they need to have well defined, automatable
memory management policies. If messages are sent asynchronously the
application has no way of knowing exactly when Qpid is finished with the
data, e.g. via an (optional) user supplied deletion callback (similar to
boost::shared_ptr)
Re: Writing binary data into messages (feedback?)
Posted by Danushka Menikkumbura <da...@wso2.com>.
> It is true that if you were to try to convert a std::string containing
> the nul char to a c style char* string then it would be truncated at the
> first 0.
>
Andrew,
Yes!. Correct. This happened some time back when I was working on an
application which was supposed to send binary data over wire. There of
course what I received was a C string and hence the parsing stopped
when it reached the first NULL. But even that could be handled safely if
we give the length as the second parameter when we construct the
std::string object.
Danushka
--
Danushka Menikkumbura
Technical Lead, WSO2 Inc.
blog : http://danushka-menikkumbura.blogspot.com/
http://wso2.com/ - "The Open Source SOA Company"
Re: Writing binary data into messages (feedback?)
Posted by Andrew Stitcher <as...@redhat.com>.
On Fri, 2008-10-03 at 02:29 +0530, Danushka Menikkumbura wrote:
> ...
> At the moment "data" is a std::string and the method "setData" assigns
> the incoming string value to this std::string variable directly. When it
> comes to binary data, we can see null terminators in the middle and
> hence assigning the binary data stream to a std::string will definitely
> truncate the data.
Where do you get the idea that std::string cannot handle embedded nul
characters?
Essentially the C++ std::string is a blob of bytes and a length so it is
perfectly capable of holding nul characters. The C++ standard talks
about "a sequence consisting of a varying number of arbitrary char-like
objects" [note it doesn't restrict the contents of those objects to be
non-null].
You might ask why do we have strings then and not just a vector<char> or
vector<wchar> and the real answer is only history (std::string came
first)
It is true that if you were to try to convert a std::string containing
the nul char to a c style char* string then it would be truncated at the
first 0.
> So what we will have in the message is just a part of
> the original binary data.
>
> So, as a solution for this, we need to have two class level variables to
> keep binary data.
>
> 1. void* data;
> 2. long dataLength;
>
> These two are enough to say all about our binary data dump. So,
> obviously we need to have separate methods to get and set binary data.
>
> We may make use of these changes to keep string data as well so that we
> can drop the current std::string variable.
>
> I can send you a patch if we are happy with this.
>
> Danushka
>
Re: Writing binary data into messages (feedback?)
Posted by Danushka Menikkumbura <da...@wso2.com>.
> Hmmmm, that's too bad. I'd like to avoid creating a buffer and then
> copying it over ("minimize the movement of data"). Is there a better
> way to write binary data directly into the message contents without
> creating a buffer first?
Jonathan,
When I go through the code (framing::TransferContent) I can clearly see
that the current implementation can not handle binary data.
At the moment "data" is a std::string and the method "setData" assigns
the incoming string value to this std::string variable directly. When it
comes to binary data, we can see null terminators in the middle and
hence assigning the binary data stream to a std::string will definitely
truncate the data. So what we will have in the message is just a part of
the original binary data.
So, as a solution for this, we need to have two class level variables to
keep binary data.
1. void* data;
2. long dataLength;
These two are enough to say all about our binary data dump. So,
obviously we need to have separate methods to get and set binary data.
We may make use of these changes to keep string data as well so that we
can drop the current std::string variable.
I can send you a patch if we are happy with this.
Danushka
--
Danushka Menikkumbura
Technical Lead, WSO2 Inc.
blog : http://danushka-menikkumbura.blogspot.com/
http://wso2.com/ - "The Open Source SOA Company"
Re: Writing binary data into messages (feedback?)
Posted by William Henry <wh...@redhat.com>.
Hi Jonathan,
A user sent me this example:
float *tmp = new float[nx*ny];
string stmp(tmp,nx*ny*sizeof(float));
message.setData(stmp);
He had questions about deep versus shallow copies. I'll look at the docs.
William
----- "Jonathan Robie" <jo...@redhat.com> wrote:
> Danushka Menikkumbura wrote:
> > As far as I can see, this is wrong due to 3 reasons.
> >
> > 1. You can not resize "data" since it is a const.
> > 2. Resizing a string is costly.
> > 3. Copying stuff in to "data" alone is not sufficient as it is
> > necessary to set the content length in the header as well.
>
> Hmmmm, that's too bad. I'd like to avoid creating a buffer and then
> copying it over ("minimize the movement of data"). Is there a better
> way
> to write binary data directly into the message contents without
> creating
> a buffer first?
>
> Jonathan
Re: Writing binary data into messages (feedback?)
Posted by Jonathan Robie <jo...@redhat.com>.
Danushka Menikkumbura wrote:
> As far as I can see, this is wrong due to 3 reasons.
>
> 1. You can not resize "data" since it is a const.
> 2. Resizing a string is costly.
> 3. Copying stuff in to "data" alone is not sufficient as it is
> necessary to set the content length in the header as well.
Hmmmm, that's too bad. I'd like to avoid creating a buffer and then
copying it over ("minimize the movement of data"). Is there a better way
to write binary data directly into the message contents without creating
a buffer first?
Jonathan
Re: Writing binary data into messages (feedback?)
Posted by Alan Conway <ac...@redhat.com>.
On Fri, 2008-10-03 at 01:37 +0530, Danushka Menikkumbura wrote:
> > // Method 2: write directly to message
> >
> > // Send the message as per usual
> >
> > Message msg;
> > msg.getData().resize(100);
> >
> > memcpy(&msg.getData()[0], &i, sizeof(i));
> > memcpy(&msg.getData()[4], &f, sizeof(f));
> As far as I can see, this is wrong due to 3 reasons.
>
> 1. You can not resize "data" since it is a const.
There are const and non-const overrides of getData() so you can do this.
> 2. Resizing a string is costly.
It costs approximately an allocation and a copy of the original data, if
the original string is empty then it's really just an allocation.
> 3. Copying stuff in to "data" alone is not sufficient as it is necessary
> to set the content length in the header as well.
Qpid should set the content length automatically IMO, not sure if it
does though - worth checking & JIRAing/fixing if need be.
Re: Writing binary data into messages (feedback?)
Posted by Danushka Menikkumbura <da...@wso2.com>.
> // Method 2: write directly to message
>
> // Send the message as per usual
>
> Message msg;
> msg.getData().resize(100);
>
> memcpy(&msg.getData()[0], &i, sizeof(i));
> memcpy(&msg.getData()[4], &f, sizeof(f));
As far as I can see, this is wrong due to 3 reasons.
1. You can not resize "data" since it is a const.
2. Resizing a string is costly.
3. Copying stuff in to "data" alone is not sufficient as it is necessary
to set the content length in the header as well.
Danushka
--
Danushka Menikkumbura
Technical Lead, WSO2 Inc.
blog : http://danushka-menikkumbura.blogspot.com/
http://wso2.com/ - "The Open Source SOA Company"