Advertisement

Splitting a buffer of data in two sections (the size of the first is known)...

Started by April 11, 2004 10:35 PM
1 comment, last by jacksaccountongamedev 20 years, 10 months ago
I have a problem related to the transfer of data over a network via DirectPlay (Client-Server system). Basically, the computer receives data packets through a connection that vary in size (TCP). The data in a packet is represented using a PBYTE. However, while they may contain different amounts of data, all packets supposedly share a common attribute – the first piece of data in the buffer is a DWORD that defines the ‘type’ of packet. The idea is that I extract the DWORD from the data buffer and then read the remaining data depending on what the DWORD is. Put simply, I have the data buffer (PBYE) stored as pMsg->pReceiveData, and I need to take the first piece of data the size of a DWORD and store it in DWORD Id, and then take the rest of the data and store it in PBYE Data; It is a simple task extracting the DWORD from the buffer (C++):

DWORD Id;
PBYE Data;
memcpy( &Id, pMsg->pReceiveData, sizeof(DWORD) );
How can I go about taking the remaining data and inserting it into Data? I tried the following with no success:


#pragma pack(push, 1) // changes byte alignment to 1

struct TempMessage
{
	DWORD Id;
	PBYTE Data;
};
#pragma pack(pop) // changes byte alignment to 1


TempMessage * temp = (TempMessage*) pMsg->pReceiveData;

//The theory was that by doing this the first piece of data the size of DWORD ends up in

//temp->Id and the rest of it ends up in temp->Data. Predictably this did not work.

//I don’t think it is entirely relevant but here is how I was attempting to send messages:


WCHAR wszData[256];

//strcpy wszData to something…


TempMessage temp;
temp.Id = 10;
temp.Data = (BYTE*) wszData;

DPN_BUFFER_DESC dpnBuffer;
dpnBuffer.pBufferData = (BYTE*) &temp
dpnBuffer.dwBufferSize = sizeof(char)*256 + sizeof(DWORD);

//Send dpnBuffer;


 
Any ideas? This is not exactly a networking specific problem, so if no solutions surface here can help me I shall ask in the General Programming (after this thread has disappeared from the first page)… Thanks for any help. Jackson Allan
Why don''t you just add a size member to your struct and fill it with the size of the data you are sending/receiving?

Here is something I used recently for file I/O: (typed from memory, removing some stuff to fit your scheme, so might be errors...)
struct myChunk{	u8 type;	// Data type being read/wrote	u8 size;	// Size of buffer	BYTE buffer[];	// Data buffer};...u8 rebuf[1024]; //read bufferu8 size;BYTE *dataptr;// read data from file to rebufsize = (myChunk)rebuf->size;dataptr = (myChunk)rebuf->buffer;// at this point we know our data is pointed to by dataptr and is size bytes long


NOTE: that isn''t very good programming style, and probably has symatic errors,but hopefully you get the idea



Drakonite

Shoot Pixels Not People
Shoot Pixels Not People
Advertisement
I do know the size of the data, but I do not see how that helps? I know the size of the whole buffer, and the size of the data in there that I need to extract for the new data buffer is that size minus the size of DWORD.
I tried your suggestion but the compiler game me a sum of errors . To state clearly, what I really need to know how to do is copy memory from an 'offset' inside the buffer, if that makes sense (Notice the capitalized section in the following code):

DWORD Id;
PBYE Data;
memcpy( &Id, pMsg->pReceiveData, sizeof(DWORD) );
memcpy( Data, SIZEOF(DWORD) INTO THE pMsg->pReceiveData, pMsg->pReceiveDataSize - sizeof(DWORD) );
//Need ignore the first piece of data the size of dword and
//start copying from beyond that point

[edited by - jack_1313 on April 12, 2004 6:21:40 AM]

This topic is closed to new replies.

Advertisement