Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualsmasherprog

Posted 14 June 2012 - 09:40 AM

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids
unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
unsigned short int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int)*2;// 2 is for the size of the string 2 is for the m_id variable, 2 is for the size of the payload
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data+sizeof(packetid), &sizetosend, sizeof(sizetosend));// size of the payload so on the other side, you can copy the data correctly
memcpy(data + sizeof(packetid) +sizeof(sizetosend), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte <img src='http://public.gamedev.net//public/style_emoticons/default/tongue.png' class='bbc_emoticon' alt=':P' />
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));

#3smasherprog

Posted 14 June 2012 - 09:40 AM

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids
unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
unsigned short int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int)*2;// 2 is for the size of the string 2 is for the m_id variable, 2 is for the size of the payload
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data+sizeof(packetid), &packetid, sizeof(sizetosend));// size of the payload so on the other side, you can copy the data correctly
memcpy(data + sizeof(packetid) +sizeof(sizetosend), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte <img src='http://public.gamedev.net//public/style_emoticons/default/tongue.png' class='bbc_emoticon' alt=':P' />
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));

#2smasherprog

Posted 14 June 2012 - 09:38 AM

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids
unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int);// 2 is for the size of the string 2 is for the m_id variable
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data + sizeof(packetid), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte <img src='http://public.gamedev.net//public/style_emoticons/default/tongue.png' class='bbc_emoticon' alt=':P' />
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));

#1smasherprog

Posted 14 June 2012 - 09:37 AM

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , used unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids
unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int);// 2 is for the size of the string 2 is for the m_id variable
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data + sizeof(packetid), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte <img src='http://public.gamedev.net//public/style_emoticons/default/tongue.png' class='bbc_emoticon' alt=':P' />
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));

PARTNERS