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

Started by
1 comment, last by Carradine 11 years, 7 months ago
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: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com

Advertisement
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
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: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com

This topic is closed to new replies.

Advertisement