(svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.

This commit is contained in:
rubidium
2007-02-01 23:26:44 +00:00
parent 0b4d416e1f
commit 105cd53868
7 changed files with 333 additions and 339 deletions

View File

@@ -99,9 +99,9 @@ void NetworkCoreShutdown(void)
void NetworkSocketHandler::Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf)
{
uint j;
NetworkSend_uint32(p, grf->grfid);
p->Send_uint32(grf->grfid);
for (j = 0; j < sizeof(grf->md5sum); j++) {
NetworkSend_uint8 (p, grf->md5sum[j]);
p->Send_uint8 (grf->md5sum[j]);
}
}
@@ -113,9 +113,9 @@ void NetworkSocketHandler::Send_GRFIdentifier(Packet *p, const GRFIdentifier *gr
void NetworkSocketHandler::Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf)
{
uint j;
grf->grfid = NetworkRecv_uint32(this, p);
grf->grfid = p->Recv_uint32();
for (j = 0; j < sizeof(grf->md5sum); j++) {
grf->md5sum[j] = NetworkRecv_uint8(this, p);
grf->md5sum[j] = p->Recv_uint8();
}
}

View File

@@ -63,7 +63,7 @@ Packet *NetworkSend_Init(PacketType type)
/**
* Writes the packet size from the raw packet from packet->size
*/
void Packet::PrepareToSend()
void Packet::PrepareToSend(void)
{
assert(this->cs == NULL && this->next == NULL);
@@ -82,52 +82,51 @@ void Packet::PrepareToSend()
* So 0x01234567 would be sent as 67 45 23 01.
*/
void NetworkSend_uint8(Packet *packet, uint8 data)
void Packet::Send_uint8(uint8 data)
{
assert(packet->size < sizeof(packet->buffer) - sizeof(data));
packet->buffer[packet->size++] = data;
assert(this->size < sizeof(this->buffer) - sizeof(data));
this->buffer[this->size++] = data;
}
void NetworkSend_uint16(Packet *packet, uint16 data)
void Packet::Send_uint16(uint16 data)
{
assert(packet->size < sizeof(packet->buffer) - sizeof(data));
packet->buffer[packet->size++] = GB(data, 0, 8);
packet->buffer[packet->size++] = GB(data, 8, 8);
assert(this->size < sizeof(this->buffer) - sizeof(data));
this->buffer[this->size++] = GB(data, 0, 8);
this->buffer[this->size++] = GB(data, 8, 8);
}
void NetworkSend_uint32(Packet *packet, uint32 data)
void Packet::Send_uint32(uint32 data)
{
assert(packet->size < sizeof(packet->buffer) - sizeof(data));
packet->buffer[packet->size++] = GB(data, 0, 8);
packet->buffer[packet->size++] = GB(data, 8, 8);
packet->buffer[packet->size++] = GB(data, 16, 8);
packet->buffer[packet->size++] = GB(data, 24, 8);
assert(this->size < sizeof(this->buffer) - sizeof(data));
this->buffer[this->size++] = GB(data, 0, 8);
this->buffer[this->size++] = GB(data, 8, 8);
this->buffer[this->size++] = GB(data, 16, 8);
this->buffer[this->size++] = GB(data, 24, 8);
}
void NetworkSend_uint64(Packet *packet, uint64 data)
void Packet::Send_uint64(uint64 data)
{
assert(packet->size < sizeof(packet->buffer) - sizeof(data));
packet->buffer[packet->size++] = GB(data, 0, 8);
packet->buffer[packet->size++] = GB(data, 8, 8);
packet->buffer[packet->size++] = GB(data, 16, 8);
packet->buffer[packet->size++] = GB(data, 24, 8);
packet->buffer[packet->size++] = GB(data, 32, 8);
packet->buffer[packet->size++] = GB(data, 40, 8);
packet->buffer[packet->size++] = GB(data, 48, 8);
packet->buffer[packet->size++] = GB(data, 56, 8);
assert(this->size < sizeof(this->buffer) - sizeof(data));
this->buffer[this->size++] = GB(data, 0, 8);
this->buffer[this->size++] = GB(data, 8, 8);
this->buffer[this->size++] = GB(data, 16, 8);
this->buffer[this->size++] = GB(data, 24, 8);
this->buffer[this->size++] = GB(data, 32, 8);
this->buffer[this->size++] = GB(data, 40, 8);
this->buffer[this->size++] = GB(data, 48, 8);
this->buffer[this->size++] = GB(data, 56, 8);
}
/**
* Sends a string over the network. It sends out
* the string + '\0'. No size-byte or something.
* @param packet packet to send the string in
* @param data the string to send
*/
void NetworkSend_string(Packet *packet, const char* data)
void Packet::Send_string(const char* data)
{
assert(data != NULL);
assert(packet->size < sizeof(packet->buffer) - strlen(data) - 1);
while ((packet->buffer[packet->size++] = *data++) != '\0') {}
assert(this->size < sizeof(this->buffer) - strlen(data) - 1);
while ((this->buffer[this->size++] = *data++) != '\0') {}
}
@@ -139,14 +138,14 @@ void NetworkSend_string(Packet *packet, const char* data)
/** Is it safe to read from the packet, i.e. didn't we run over the buffer ? */
static inline bool CanReadFromPacket(NetworkSocketHandler *cs, const Packet *packet, const uint bytes_to_read)
bool Packet::CanReadFromPacket(uint bytes_to_read)
{
/* Don't allow reading from a quit client/client who send bad data */
if (cs->HasClientQuit()) return false;
if (this->cs->HasClientQuit()) return false;
/* Check if variable is within packet-size */
if (packet->pos + bytes_to_read > packet->size) {
cs->CloseConnection();
if (this->pos + bytes_to_read > this->size) {
this->cs->CloseConnection();
return false;
}
@@ -156,7 +155,7 @@ static inline bool CanReadFromPacket(NetworkSocketHandler *cs, const Packet *pac
/**
* Reads the packet size from the raw packet and stores it in the packet->size
*/
void Packet::ReadRawPacketSize()
void Packet::ReadRawPacketSize(void)
{
assert(this->cs != NULL && this->next == NULL);
this->size = (PacketSize)this->buffer[0];
@@ -166,7 +165,7 @@ void Packet::ReadRawPacketSize()
/**
* Prepares the packet so it can be read
*/
void Packet::PrepareToRead()
void Packet::PrepareToRead(void)
{
this->ReadRawPacketSize();
@@ -174,59 +173,59 @@ void Packet::PrepareToRead()
this->pos = sizeof(PacketSize);
}
uint8 NetworkRecv_uint8(NetworkSocketHandler *cs, Packet *packet)
uint8 Packet::Recv_uint8(void)
{
uint8 n;
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0;
if (!this->CanReadFromPacket(sizeof(n))) return 0;
n = packet->buffer[packet->pos++];
n = this->buffer[this->pos++];
return n;
}
uint16 NetworkRecv_uint16(NetworkSocketHandler *cs, Packet *packet)
uint16 Packet::Recv_uint16(void)
{
uint16 n;
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0;
if (!this->CanReadFromPacket(sizeof(n))) return 0;
n = (uint16)packet->buffer[packet->pos++];
n += (uint16)packet->buffer[packet->pos++] << 8;
n = (uint16)this->buffer[this->pos++];
n += (uint16)this->buffer[this->pos++] << 8;
return n;
}
uint32 NetworkRecv_uint32(NetworkSocketHandler *cs, Packet *packet)
uint32 Packet::Recv_uint32(void)
{
uint32 n;
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0;
if (!this->CanReadFromPacket(sizeof(n))) return 0;
n = (uint32)packet->buffer[packet->pos++];
n += (uint32)packet->buffer[packet->pos++] << 8;
n += (uint32)packet->buffer[packet->pos++] << 16;
n += (uint32)packet->buffer[packet->pos++] << 24;
n = (uint32)this->buffer[this->pos++];
n += (uint32)this->buffer[this->pos++] << 8;
n += (uint32)this->buffer[this->pos++] << 16;
n += (uint32)this->buffer[this->pos++] << 24;
return n;
}
uint64 NetworkRecv_uint64(NetworkSocketHandler *cs, Packet *packet)
uint64 Packet::Recv_uint64(void)
{
uint64 n;
if (!CanReadFromPacket(cs, packet, sizeof(n))) return 0;
if (!this->CanReadFromPacket(sizeof(n))) return 0;
n = (uint64)packet->buffer[packet->pos++];
n += (uint64)packet->buffer[packet->pos++] << 8;
n += (uint64)packet->buffer[packet->pos++] << 16;
n += (uint64)packet->buffer[packet->pos++] << 24;
n += (uint64)packet->buffer[packet->pos++] << 32;
n += (uint64)packet->buffer[packet->pos++] << 40;
n += (uint64)packet->buffer[packet->pos++] << 48;
n += (uint64)packet->buffer[packet->pos++] << 56;
n = (uint64)this->buffer[this->pos++];
n += (uint64)this->buffer[this->pos++] << 8;
n += (uint64)this->buffer[this->pos++] << 16;
n += (uint64)this->buffer[this->pos++] << 24;
n += (uint64)this->buffer[this->pos++] << 32;
n += (uint64)this->buffer[this->pos++] << 40;
n += (uint64)this->buffer[this->pos++] << 48;
n += (uint64)this->buffer[this->pos++] << 56;
return n;
}
/** Reads a string till it finds a '\0' in the stream */
void NetworkRecv_string(NetworkSocketHandler *cs, Packet *p, char *buffer, size_t size)
void Packet::Recv_string(char *buffer, size_t size)
{
PacketSize pos;
char *bufp = buffer;
@@ -234,17 +233,17 @@ void NetworkRecv_string(NetworkSocketHandler *cs, Packet *p, char *buffer, size_
/* Don't allow reading from a closed socket */
if (cs->HasClientQuit()) return;
pos = p->pos;
while (--size > 0 && pos < p->size && (*buffer++ = p->buffer[pos++]) != '\0') {}
pos = this->pos;
while (--size > 0 && pos < this->size && (*buffer++ = this->buffer[pos++]) != '\0') {}
if (size == 0 || pos == p->size) {
if (size == 0 || pos == this->size) {
*buffer = '\0';
/* If size was sooner to zero then the string in the stream
* skip till the \0, so than packet can be read out correctly for the rest */
while (pos < p->size && p->buffer[pos] != '\0') pos++;
while (pos < this->size && this->buffer[pos] != '\0') pos++;
pos++;
}
p->pos = pos;
this->pos = pos;
str_validate(bufp);
}

View File

@@ -42,25 +42,28 @@ public:
Packet(NetworkSocketHandler *cs);
Packet(PacketType type);
void PrepareToSend();
/* Sending/writing of packets */
void PrepareToSend(void);
void ReadRawPacketSize();
void PrepareToRead();
void Send_uint8 (uint8 data);
void Send_uint16(uint16 data);
void Send_uint32(uint32 data);
void Send_uint64(uint64 data);
void Send_string(const char* data);
/* Reading/receiving of packets */
void ReadRawPacketSize(void);
void PrepareToRead(void);
bool CanReadFromPacket (uint bytes_to_read);
uint8 Recv_uint8 (void);
uint16 Recv_uint16(void);
uint32 Recv_uint32(void);
uint64 Recv_uint64(void);
void Recv_string(char* buffer, size_t size);
};
Packet *NetworkSend_Init(PacketType type);
void NetworkSend_uint8 (Packet *packet, uint8 data);
void NetworkSend_uint16(Packet *packet, uint16 data);
void NetworkSend_uint32(Packet *packet, uint32 data);
void NetworkSend_uint64(Packet *packet, uint64 data);
void NetworkSend_string(Packet *packet, const char* data);
uint8 NetworkRecv_uint8 (NetworkSocketHandler *cs, Packet *packet);
uint16 NetworkRecv_uint16(NetworkSocketHandler *cs, Packet *packet);
uint32 NetworkRecv_uint32(NetworkSocketHandler *cs, Packet *packet);
uint64 NetworkRecv_uint64(NetworkSocketHandler *cs, Packet *packet);
void NetworkRecv_string(NetworkSocketHandler *cs, Packet *packet, char* buffer, size_t size);
#endif /* ENABLE_NETWORK */

View File

@@ -146,7 +146,7 @@ void NetworkUDPSocketHandler::ReceivePackets()
*/
void NetworkUDPSocketHandler::Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info)
{
NetworkSend_uint8 (p, NETWORK_GAME_INFO_VERSION);
p->Send_uint8 (NETWORK_GAME_INFO_VERSION);
/*
* Please observe the order.
@@ -169,7 +169,7 @@ void NetworkUDPSocketHandler::Send_NetworkGameInfo(Packet *p, const NetworkGameI
for (c = info->grfconfig; c != NULL; c = c->next) {
if (!HASBIT(c->flags, GCF_STATIC)) count++;
}
NetworkSend_uint8 (p, count); // Send number of GRFs
p->Send_uint8 (count); // Send number of GRFs
/* Send actual GRF Identifications */
for (c = info->grfconfig; c != NULL; c = c->next) {
@@ -178,27 +178,27 @@ void NetworkUDPSocketHandler::Send_NetworkGameInfo(Packet *p, const NetworkGameI
}
/* NETWORK_GAME_INFO_VERSION = 3 */
NetworkSend_uint32(p, info->game_date);
NetworkSend_uint32(p, info->start_date);
p->Send_uint32(info->game_date);
p->Send_uint32(info->start_date);
/* NETWORK_GAME_INFO_VERSION = 2 */
NetworkSend_uint8 (p, info->companies_max);
NetworkSend_uint8 (p, info->companies_on);
NetworkSend_uint8 (p, info->spectators_max);
p->Send_uint8 (info->companies_max);
p->Send_uint8 (info->companies_on);
p->Send_uint8 (info->spectators_max);
/* NETWORK_GAME_INFO_VERSION = 1 */
NetworkSend_string(p, info->server_name);
NetworkSend_string(p, info->server_revision);
NetworkSend_uint8 (p, info->server_lang);
NetworkSend_uint8 (p, info->use_password);
NetworkSend_uint8 (p, info->clients_max);
NetworkSend_uint8 (p, info->clients_on);
NetworkSend_uint8 (p, info->spectators_on);
NetworkSend_string(p, info->map_name);
NetworkSend_uint16(p, info->map_width);
NetworkSend_uint16(p, info->map_height);
NetworkSend_uint8 (p, info->map_set);
NetworkSend_uint8 (p, info->dedicated);
p->Send_string(info->server_name);
p->Send_string(info->server_revision);
p->Send_uint8 (info->server_lang);
p->Send_uint8 (info->use_password);
p->Send_uint8 (info->clients_max);
p->Send_uint8 (info->clients_on);
p->Send_uint8 (info->spectators_on);
p->Send_string(info->map_name);
p->Send_uint16(info->map_width);
p->Send_uint16(info->map_height);
p->Send_uint8 (info->map_set);
p->Send_uint8 (info->dedicated);
}
/**
@@ -210,7 +210,7 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i
{
static const Date MAX_DATE = ConvertYMDToDate(MAX_YEAR, 11, 31); // December is month 11
info->game_info_version = NetworkRecv_uint8(this, p);
info->game_info_version = p->Recv_uint8();
/*
* Please observe the order.
@@ -224,7 +224,7 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i
case 4: {
GRFConfig **dst = &info->grfconfig;
uint i;
uint num_grfs = NetworkRecv_uint8(this, p);
uint num_grfs = p->Recv_uint8();
for (i = 0; i < num_grfs; i++) {
GRFConfig *c = CallocT<GRFConfig>(1);
@@ -237,31 +237,31 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i
}
} /* Fallthrough */
case 3:
info->game_date = clamp(NetworkRecv_uint32(this, p), 0, MAX_DATE);
info->start_date = clamp(NetworkRecv_uint32(this, p), 0, MAX_DATE);
info->game_date = clamp(p->Recv_uint32(), 0, MAX_DATE);
info->start_date = clamp(p->Recv_uint32(), 0, MAX_DATE);
/* Fallthrough */
case 2:
info->companies_max = NetworkRecv_uint8 (this, p);
info->companies_on = NetworkRecv_uint8 (this, p);
info->spectators_max = NetworkRecv_uint8 (this, p);
info->companies_max = p->Recv_uint8 ();
info->companies_on = p->Recv_uint8 ();
info->spectators_max = p->Recv_uint8 ();
/* Fallthrough */
case 1:
NetworkRecv_string(this, p, info->server_name, sizeof(info->server_name));
NetworkRecv_string(this, p, info->server_revision, sizeof(info->server_revision));
info->server_lang = NetworkRecv_uint8 (this, p);
info->use_password = (NetworkRecv_uint8 (this, p) != 0);
info->clients_max = NetworkRecv_uint8 (this, p);
info->clients_on = NetworkRecv_uint8 (this, p);
info->spectators_on = NetworkRecv_uint8 (this, p);
p->Recv_string(info->server_name, sizeof(info->server_name));
p->Recv_string(info->server_revision, sizeof(info->server_revision));
info->server_lang = p->Recv_uint8 ();
info->use_password = (p->Recv_uint8 () != 0);
info->clients_max = p->Recv_uint8 ();
info->clients_on = p->Recv_uint8 ();
info->spectators_on = p->Recv_uint8 ();
if (info->game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
info->game_date = NetworkRecv_uint16(this, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
info->start_date = NetworkRecv_uint16(this, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
info->game_date = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
info->start_date = p->Recv_uint16() + DAYS_TILL_ORIGINAL_BASE_YEAR;
}
NetworkRecv_string(this, p, info->map_name, sizeof(info->map_name));
info->map_width = NetworkRecv_uint16(this, p);
info->map_height = NetworkRecv_uint16(this, p);
info->map_set = NetworkRecv_uint8 (this, p);
info->dedicated = (NetworkRecv_uint8(this, p) != 0);
p->Recv_string(info->map_name, sizeof(info->map_name));
info->map_width = p->Recv_uint16();
info->map_height = p->Recv_uint16();
info->map_set = p->Recv_uint8 ();
info->dedicated = (p->Recv_uint8() != 0);
if (info->server_lang >= NETWORK_NUM_LANGUAGES) info->server_lang = 0;
if (info->map_set >= NETWORK_NUM_LANDSCAPES) info->map_set = 0;
@@ -286,7 +286,7 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i
/* New packet == new client, which has not quit yet */
this->has_quit = false;
type = (PacketUDPType)NetworkRecv_uint8(this, p);
type = (PacketUDPType)p->Recv_uint8();
switch (this->HasClientQuit() ? PACKET_UDP_END : type) {
UDP_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);