miteshpandey
December 8th, 2004, 10:24 PM
I have a socket application and both the client and the server use the
following structure to store message.
struct MyMsg
{
CString m_strText;
CString m_strPlayerName;
int m_BitmapIndex;
};
The socket uses the following class to read and write.
class DataBlock {
public:
//// Public interface
// Ctors and dtor
DataBlock() :
pcData(0),
nBytes(0) { }
DataBlock(char* data, int bytes) :
pcData(data),
nBytes(bytes) { }
DataBlock(const DataBlock& db) :
pcData(db.pcData),
nBytes(db.nBytes) { }
virtual ~DataBlock() { }
// Makes a shallow copy of rhs, since this class's semantics say
// that the object's creator owns the data block, and that we just
// refer to that data.
//
// NOTE:
// This should work no differently than the default operator=,
// but we want to allow subclasses to be able to redefine it
// polymorphically.
virtual DataBlock& operator=(const DataBlock& rhs);
//// Public data
// Pointer to the data
char* pcData;
// Number of bytes that pcData points to
int nBytes;
};
My question is this:
How do I convert MyMsg to DataBlock.
In the sample program DataBlock is used like this:
// Set up output buffers and such
DataBlock DBOut(20);
ostrstream outs(DBOut.pcData, DBOut.nBytes);
// Send 10 packets on this connection
int i;
for (i = 0; i < 10; i++) {
outs.seekp(0);
outs << "Hello #" << (i + 1) << ends;
DBOut.nBytes = strlen(DBOut.pcData) + 1;
ClientConnection.Write(DBOut);
}
DataBlock is returned when the socket reads.
DataBlock *pDB = ClientConnection.Read();
Please help. How do I do the conversions.
following structure to store message.
struct MyMsg
{
CString m_strText;
CString m_strPlayerName;
int m_BitmapIndex;
};
The socket uses the following class to read and write.
class DataBlock {
public:
//// Public interface
// Ctors and dtor
DataBlock() :
pcData(0),
nBytes(0) { }
DataBlock(char* data, int bytes) :
pcData(data),
nBytes(bytes) { }
DataBlock(const DataBlock& db) :
pcData(db.pcData),
nBytes(db.nBytes) { }
virtual ~DataBlock() { }
// Makes a shallow copy of rhs, since this class's semantics say
// that the object's creator owns the data block, and that we just
// refer to that data.
//
// NOTE:
// This should work no differently than the default operator=,
// but we want to allow subclasses to be able to redefine it
// polymorphically.
virtual DataBlock& operator=(const DataBlock& rhs);
//// Public data
// Pointer to the data
char* pcData;
// Number of bytes that pcData points to
int nBytes;
};
My question is this:
How do I convert MyMsg to DataBlock.
In the sample program DataBlock is used like this:
// Set up output buffers and such
DataBlock DBOut(20);
ostrstream outs(DBOut.pcData, DBOut.nBytes);
// Send 10 packets on this connection
int i;
for (i = 0; i < 10; i++) {
outs.seekp(0);
outs << "Hello #" << (i + 1) << ends;
DBOut.nBytes = strlen(DBOut.pcData) + 1;
ClientConnection.Write(DBOut);
}
DataBlock is returned when the socket reads.
DataBlock *pDB = ClientConnection.Read();
Please help. How do I do the conversions.