Casting / converting

19 views
Skip to first unread message

Gal Anonim

unread,
Jan 7, 2014, 6:06:38 AM1/7/14
to lib...@googlegroups.com
What is the best practice for casting/converting data ?
I mean:

string st = "1121212";
ting::Buffer buff(st.c_str(),st.size());
sock.send(buf);

or
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());

ting::StaticBuffer<char *> data2;
data2=(char*)cstr;
sock.send(data2);

This does not work, i'm asking. What is the best way to do this.
Also. How to  make serialization of an object and send it over network?


Now i was able to send data like that:

ting::StaticBuffer<ting::u8, 100> data;
for(int x=0;x<10;x++) {
    data[x]=buf[x];
    }
sock.Send(xx2);

What if my data is longer than 10? I need to cut text in blocks? What id my data is shorter than 10? I'm sending empty bytes??

Thank you,
Rafal
 

Gal Anonim

unread,
Jan 7, 2014, 6:19:08 AM1/7/14
to lib...@googlegroups.com
I ment:

ting::StaticBuffer<ting::u8, 10> data;

Gall Anonim

unread,
Jan 7, 2014, 8:13:17 AM1/7/14
to lib...@googlegroups.com
I have one working cast like method.

        string str = "test 12";
        ting::u8* strU8 =  new ting::u8(str.size());
        for(int x=0;x<str.size();x++) {
            strU8[x]=str[x];
            }
        ting::Buffer<ting::u8> data2(strU8,str.size());
        sock.Send(data2);

Is there a chance to do this faster, better?
Rafal

Gall Anonim

unread,
Jan 7, 2014, 8:19:35 AM1/7/14
to lib...@googlegroups.com
Second, better version:

        char str[60] = "test 12 \n Kokosy";
        ting::u8* strU8 =  new ting::u8(sizeof(str));
        strU8=(ting::u8*)str;
        ting::Buffer<ting::u8> data2(strU8,sizeof(str));
        sock.Send(data2);

or

        string str = "test 12 \n Kokosy";
        ting::u8* strU8 =  new ting::u8(sizeof(str));
        strU8=(ting::u8*)str.c_str();
        ting::Buffer<ting::u8> data2(strU8,sizeof(str));
        sock.Send(data2);

Thanks

Gall Anonim

unread,
Jan 7, 2014, 8:44:19 AM1/7/14
to lib...@googlegroups.com
Sorry for spamming, I wish this will be useful for someone.

With strings best to use is:
string str = "test 12 \n Kokosy \n asdasdasdasd";
ting::u8* strU8 =  new ting::u8(str.length());
strU8=(ting::u8*)str.c_str();
ting::Buffer<ting::u8> data2(strU8,str.length());
sock.Send(data2);


Length() counts chars, size() counts bytes, so use length.
Ty,
Rafal

Ivan Gagis

unread,
Jan 10, 2014, 6:48:34 AM1/10/14
to lib...@googlegroups.com
Hi!

sorry for late reply, had a lack of time.

sock.Send() does not take ownership of passed in buffer, so, after calling the method, the buffer does not have to remain existing.
Because of that, the bes would be to do the very first thing you proposed, but you probably have to cast char to ting::u8:

string st = "1121212";
ting::Buffer<ting::u8> buf(reinterpret_cast<ting::u8*>(st.c_str()), st.size());
sock.send(buf);

and you need to use size() because you are sending bytes over the network, not characters.

Serializing the object is a thing with is out of ting library scope, you have to do it yourself before sending over the network. The essence of serialization is that you need to "pack" all relevant data of the object to ting::Buffer<ting::u8> which can later be sent. And on the other end, after receiving the bytes, you have to "unpack" the date and fill in the fields of your object. That is the idea. This "packing" and "unpacking" you generally have to do by hand.

Hope that helps.

Br,
Ivan




вторник, 7 января 2014 г., 15:44:19 UTC+2 пользователь Gall Anonim написал:
Reply all
Reply to author
Forward
0 new messages