Jump to content

  • Log In with Google      Sign In   
  • Create Account


Copying partial data from/to a class, any problems?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Carradine   Members   -  Reputation: 260

Like
0Likes
Like

Posted 19 September 2012 - 04:26 PM

I have my game running as a client/server architecture and I am sending data from one to the other. I am now optimizing the data I send over the network and I am currently doing this:

I have a class set up like so:

[source lang="cpp"]class temp{ int a; BOOL b; int c; foo * d1; foo2 * d2; CString eStr; CString fStr;};[/source]
And the identical class on both the client and server.
I am sending variables a,b, and c as one struct, and I send the other CString data in their own packets for now.
Initially, I was sending each variable over as a packet, now I am sending data over in structs. I was sending these "base" data types (a,b,c) over as a struct by putting them in their own struct like so:

[source lang="cpp"]class temp{ struct _baseValues { int a; BOOL b; int c; } foo * d1; foo2 * d2; Cstring eStr; CString fStr;};[/source]

And sending it over in a char array like so:

[source lang="cpp"]LPTSTR aStr = convStr.GetBufferSetLength(sizeof(temp::_baseValues));memcpy( (void*)aStr, (char*)pStr, sizeof(temp::_baseValues) );[/source]
However my question is that is it simpler for me to ignore creating another struct for my base values, and just send the data over in the original class setup (the first class in this post ) and copying it over like so:

[source lang="cpp"]LPTSTR aStr = convStr.GetBufferSetLength(12);memcpy( (void*)aStr, (char*)pStr, 12 );[/source]

and reading it in on the client like this:
[source lang="cpp"]memcpy( (void*)tempObj, (LPCTSTR)packetCharStr, 12 );[/source]
Everything works fine, but my question is, except for the fact I have to enter a manual size for the data sizes for the conversions, could this cause any problems? Aside I have to watch the order of which I place members in the class, and not worry about endian problems from different operating systems, is this acceptable? Or could I have some random memory problems doing this?

I can go back to putting the base values (a,b,c) back into a struct, but this does seem a bit simpler, and I won't have to do a large code overhaul.

Thanks
--------------------------Vantage - 2006, 2008, 2009 Independent Games Festival Submission http://www.crystaldragon.com

Sponsor:

#2 Trienco   Crossbones+   -  Reputation: 2138

Like
0Likes
Like

Posted 19 September 2012 - 10:57 PM

So you have a version that automatically handles alignment, padding, changing the content of the structs, etc... and one version with tons of magic numbers that is most likely going to break with the slightest code change or even just compiling for 64bit and is a maintenance nightmare.

Don't confuse "it's less typing" with "it's easier".

Apart from that, all the cstring and memory fiddling would make me extremely nervous and look for a safer way to do it. Especially abusing "strings" as raw data buffers.

For situations where classes need to save/send themselves they usually know how to serialize themselves. At least it's a keyword that should provide plenty of common approaches.
f@dzhttp://festini.device-zero.de

#3 Carradine   Members   -  Reputation: 260

Like
0Likes
Like

Posted 20 September 2012 - 12:53 AM

I knew that when I was doing this it was feeling a bit "dirty" but i couldn't quite put my finger on it. Thanks.
--------------------------Vantage - 2006, 2008, 2009 Independent Games Festival Submission http://www.crystaldragon.com




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS