Jump to content
SubSpace Forum Network

Recommended Posts

Posted
My struct:
       typedef struct SEncryptionRequestPacket{
           Uint8 type;     //0x00
           Uint8 subtype;  //0x01
           Uint32 key;
           Uint16 version; //0x0001
       }TEncryptionRequestPacket;

And its use:

void CSSConnection::SendEncryptionRequest(){
   Log("SendEncryptionRequest");
ClientPackets::TEncryptionRequestPacket packet;

packet.type=0x00;
packet.subtype=0x01;
packet.key=-(rand() % 0x7FFFFFFF);
packet.version=0x0001;

   //<debug>
   char buff[11];
   Log(((string("Sending ")+itoa(sizeof(ClientPackets::TEncryptionRequestPacket),buff,10))+" bytes").c_str());
   //</debug>

   socket->Send((const char*)&packet,sizeof(ClientPackets::TEncryptionRequestPacket));
}

My problem: For some reason sizeof returns 12 rather than the 8 I would expect... Now why is that?

Posted

Thanks :D

 

I am experiencing some trouble though. As I understand it, what I want is:

#pargma pack(nopack)

But that is apparently not supported in gcc. I get the following warning:

 

warning: unknown action 'nopack' for '#pragma pack' - ignored

 

I can reduce the size to 10 by using

#pargma pack(1)

but that is still not the size I would expect.

 

I could, however, just ignore the extra bytes when sending my packets, but how should I the go about filling a struct with data from an incomming packet? What should I do about the extra bytes in the struct?

Posted

Your processor has a 32 bit word (4bytes). So no matter if you specify a 1 byte variable, that variable is still going to be placed in a memory space that is 4 bytes long but the variable will be treated as if the memory space was only 1 byte large. The reason for even having 1 btye variables is for backwards compatability, old processors were 8 and 16 bits, programs built on those processors should still work on 32 and 64 bit chips.

 

Sorry i couldnt help you with the packets stuff, i dont know enough about how winsock works

Posted

Oh so thats what pack is all about. Now it actualy makes sense smile.gif

My only worry is if I will be able to fill in my values like this:

char *cdata=GetExcitingDataFromTheServer();
TEncryptionRequestPacket pdata;
memcpy(&pdata,cdata,sizeof(TEncryptionRequestPacket));

Guest
This topic is now closed to further replies.
×
×
  • Create New...