Mercurial > hg > openttd
changeset 5918:8fc3143d239c draft
(svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Fri, 02 Feb 2007 23:16:58 +0000 |
parents | ad77d11aeccf |
children | f16dbc00507f |
files | src/network/core/packet.cpp src/network/core/packet.h src/network/core/udp.cpp src/network/network.h src/network/network_client.cpp src/network/network_server.cpp src/network/network_udp.cpp |
diffstat | 7 files changed, 32 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -80,8 +80,16 @@ * sent first. * * So 0x01234567 would be sent as 67 45 23 01. + * + * A bool is sent as a uint8 where zero means false + * and non-zero means true. */ +void Packet::Send_bool(bool data) +{ + this->Send_uint8(data ? 1 : 0); +} + void Packet::Send_uint8(uint8 data) { assert(this->size < sizeof(this->buffer) - sizeof(data)); @@ -133,7 +141,7 @@ /** * Receiving commands * Again, the next couple of functions are endian-safe - * see the comment before NetworkSend_uint8 for more info. + * see the comment before Send_bool for more info. */ @@ -173,6 +181,11 @@ this->pos = sizeof(PacketSize); } +bool Packet::Recv_bool(void) +{ + return this->Recv_uint8() != 0; +} + uint8 Packet::Recv_uint8(void) { uint8 n;
--- a/src/network/core/packet.h +++ b/src/network/core/packet.h @@ -45,6 +45,7 @@ /* Sending/writing of packets */ void PrepareToSend(void); + void Send_bool (bool data); void Send_uint8 (uint8 data); void Send_uint16(uint16 data); void Send_uint32(uint32 data); @@ -56,6 +57,7 @@ void PrepareToRead(void); bool CanReadFromPacket (uint bytes_to_read); + bool Recv_bool (void); uint8 Recv_uint8 (void); uint16 Recv_uint16(void); uint32 Recv_uint32(void);
--- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -190,7 +190,7 @@ 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_bool (info->use_password); p->Send_uint8 (info->clients_max); p->Send_uint8 (info->clients_on); p->Send_uint8 (info->spectators_on); @@ -198,7 +198,7 @@ p->Send_uint16(info->map_width); p->Send_uint16(info->map_height); p->Send_uint8 (info->map_set); - p->Send_uint8 (info->dedicated); + p->Send_bool (info->dedicated); } /** @@ -249,7 +249,7 @@ 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->use_password = p->Recv_bool (); info->clients_max = p->Recv_uint8 (); info->clients_on = p->Recv_uint8 (); info->spectators_on = p->Recv_uint8 (); @@ -261,7 +261,7 @@ info->map_width = p->Recv_uint16(); info->map_height = p->Recv_uint16(); info->map_set = p->Recv_uint8 (); - info->dedicated = (p->Recv_uint8() != 0); + info->dedicated = p->Recv_bool (); if (info->server_lang >= NETWORK_NUM_LANGUAGES) info->server_lang = 0; if (info->map_set >= NETWORK_NUM_LANDSCAPES) info->map_set = 0;
--- a/src/network/network.h +++ b/src/network/network.h @@ -48,7 +48,7 @@ int64 money; // The amount of money the company has int64 income; // How much did the company earned last year uint16 performance; // What was his performance last month? - byte use_password; // 0: No password 1: There is a password + bool use_password; // Is there a password uint16 num_vehicle[NETWORK_VEHICLE_TYPES]; // How many vehicles are there of this type? uint16 num_station[NETWORK_STATION_TYPES]; // How many stations are there of this type? char players[NETWORK_PLAYERS_LENGTH]; // The players that control this company (Name1, name2, ..)
--- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -314,7 +314,7 @@ _network_player_info[current].money = p->Recv_uint64(); _network_player_info[current].income = p->Recv_uint64(); _network_player_info[current].performance = p->Recv_uint16(); - _network_player_info[current].use_password = p->Recv_uint8(); + _network_player_info[current].use_password = p->Recv_bool(); for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) _network_player_info[current].num_vehicle[i] = p->Recv_uint16(); for (i = 0; i < NETWORK_STATION_TYPES; i++) @@ -655,7 +655,7 @@ NetworkAction action = (NetworkAction)p->Recv_uint8(); uint16 index = p->Recv_uint16(); - bool self_send = (p->Recv_uint8() != 0); + bool self_send = p->Recv_bool(); p->Recv_string(msg, MAX_TEXT_MSG_LEN); ci_to = NetworkFindClientInfoFromIndex(index);
--- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -101,7 +101,7 @@ p->Send_uint16(_network_player_info[player->index].performance); /* Send 1 if there is a passord for the company else send 0 */ - p->Send_uint8(StrEmpty(_network_player_info[player->index].password) ? 0 : 1); + p->Send_bool(StrEmpty(_network_player_info[player->index].password)); for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) { p->Send_uint16(_network_player_info[player->index].num_vehicle[i]); @@ -503,7 +503,7 @@ p->Send_uint8 (action); p->Send_uint16(client_index); - p->Send_uint8 (self_send); + p->Send_bool (self_send); p->Send_string(msg); cs->Send_Packet(p);
--- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -124,7 +124,7 @@ packet.Send_uint16(_network_player_info[player->index].performance); /* Send 1 if there is a passord for the company else send 0 */ - packet.Send_uint8 (StrEmpty(_network_player_info[player->index].password) ? 0 : 1); + packet.Send_bool (StrEmpty(_network_player_info[player->index].password)); for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) packet.Send_uint16(_network_player_info[player->index].num_vehicle[i]); @@ -136,8 +136,7 @@ FOR_ALL_CLIENTS(cs) { ci = DEREF_CLIENT_INFO(cs); if (ci->client_playas == player->index) { - /* The uint8 == 1 indicates that a client is following */ - packet.Send_uint8 (1); + packet.Send_bool (true); packet.Send_string(ci->client_name); packet.Send_string(ci->unique_id); packet.Send_uint32(ci->join_date); @@ -146,23 +145,21 @@ /* Also check for the server itself */ ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); if (ci->client_playas == player->index) { - /* The uint8 == 1 indicates that a client is following */ - packet.Send_uint8 (1); + packet.Send_bool (true); packet.Send_string(ci->client_name); packet.Send_string(ci->unique_id); packet.Send_uint32(ci->join_date); } /* Indicates end of client list */ - packet.Send_uint8(0); + packet.Send_bool(false); } /* And check if we have any spectators */ FOR_ALL_CLIENTS(cs) { ci = DEREF_CLIENT_INFO(cs); if (!IsValidPlayer(ci->client_playas)) { - /* The uint8 == 1 indicates that a client is following */ - packet.Send_uint8 (1); + packet.Send_bool (true); packet.Send_string(ci->client_name); packet.Send_string(ci->unique_id); packet.Send_uint32(ci->join_date); @@ -172,15 +169,14 @@ /* Also check for the server itself */ ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); if (!IsValidPlayer(ci->client_playas)) { - /* The uint8 == 1 indicates that a client is following */ - packet.Send_uint8 (1); + packet.Send_bool (true); packet.Send_string(ci->client_name); packet.Send_string(ci->unique_id); packet.Send_uint32(ci->join_date); } /* Indicates end of client list */ - packet.Send_uint8(0); + packet.Send_bool(false); this->SendPacket(&packet, client_addr); }