Sign in to follow this  

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

This topic is 1910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 1910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this