(svn r15967) -Codechange: do not access NetworkSocketHandler::has_quit directly

This commit is contained in:
rubidium 2009-04-07 18:23:14 +00:00
parent 94a70f8ade
commit a25684b833
7 changed files with 34 additions and 30 deletions

View File

@ -36,10 +36,9 @@ struct Packet;
* SocketHandler for all network sockets in OpenTTD. * SocketHandler for all network sockets in OpenTTD.
*/ */
class NetworkSocketHandler { class NetworkSocketHandler {
public:
/* TODO: make socket & has_quit protected once the TCP stuff
*is in a real class too */
bool has_quit; ///< Whether the current client has quit/send a bad packet bool has_quit; ///< Whether the current client has quit/send a bad packet
public:
/* TODO: make socket protected once the TCP stuff is in a real class too */
SOCKET sock; ///< The socket currently connected to SOCKET sock; ///< The socket currently connected to
public: public:
/** Create a new unbound socket */ /** Create a new unbound socket */
@ -72,6 +71,11 @@ public:
*/ */
bool HasClientQuit() const { return this->has_quit; } bool HasClientQuit() const { return this->has_quit; }
/**
* Reopen the socket so we can send/receive stuff again.
*/
void Reopen() { this->has_quit = false; }
void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf); void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf);
void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf); void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf);
void Send_CompanyInformation(Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats); void Send_CompanyInformation(Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats);

View File

@ -29,7 +29,7 @@ NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection() NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection()
{ {
this->writable = false; this->writable = false;
this->has_quit = true; NetworkSocketHandler::CloseConnection();
/* Free all pending and partially received packets */ /* Free all pending and partially received packets */
while (this->packet_queue != NULL) { while (this->packet_queue != NULL) {

View File

@ -54,7 +54,7 @@ void NetworkUDPSocketHandler::Close()
NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection() NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
{ {
this->has_quit = true; NetworkSocketHandler::CloseConnection();
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
} }
@ -264,7 +264,7 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_
PacketUDPType type; PacketUDPType type;
/* New packet == new client, which has not quit yet */ /* New packet == new client, which has not quit yet */
this->has_quit = false; this->Reopen();
type = (PacketUDPType)p->Recv_uint8(); type = (PacketUDPType)p->Recv_uint8();

View File

@ -274,7 +274,7 @@ static void NetworkClientError(NetworkRecvStatus res, NetworkClientSocket *cs)
/* We just want to close the connection.. */ /* We just want to close the connection.. */
if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) { if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
cs->has_quit = true; cs->CloseConnection();
NetworkCloseClient(cs); NetworkCloseClient(cs);
_networking = false; _networking = false;
@ -434,7 +434,7 @@ void NetworkCloseClient(NetworkClientSocket *cs)
DEBUG(net, 1, "Closed client connection %d", cs->client_id); DEBUG(net, 1, "Closed client connection %d", cs->client_id);
if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) { if (!cs->HasClientQuit() && _network_server && cs->status > STATUS_INACTIVE) {
/* We did not receive a leave message from this client... */ /* We did not receive a leave message from this client... */
char client_name[NETWORK_CLIENT_NAME_LENGTH]; char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkClientSocket *new_cs; NetworkClientSocket *new_cs;
@ -844,7 +844,7 @@ static bool NetworkReceive()
NetworkRecvStatus res; NetworkRecvStatus res;
/* The client already was quiting! */ /* The client already was quiting! */
if (cs->has_quit) return false; if (cs->HasClientQuit()) return false;
res = NetworkClient_ReadPackets(cs); res = NetworkClient_ReadPackets(cs);
if (res != NETWORK_RECV_STATUS_OKAY) { if (res != NETWORK_RECV_STATUS_OKAY) {

View File

@ -359,7 +359,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
{ {
byte company_info_version = p->Recv_uint8(); byte company_info_version = p->Recv_uint8();
if (!MY_CLIENT->has_quit && company_info_version == NETWORK_COMPANY_INFO_VERSION) { if (!MY_CLIENT->HasClientQuit() && company_info_version == NETWORK_COMPANY_INFO_VERSION) {
/* We have received all data... (there are no more packets coming) */ /* We have received all data... (there are no more packets coming) */
if (!p->Recv_bool()) return NETWORK_RECV_STATUS_CLOSE_QUERY; if (!p->Recv_bool()) return NETWORK_RECV_STATUS_CLOSE_QUERY;
@ -403,7 +403,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
p->Recv_string(name, sizeof(name)); p->Recv_string(name, sizeof(name));
if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST; if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
/* Do we receive a change of data? Most likely we changed playas */ /* Do we receive a change of data? Most likely we changed playas */
if (client_id == _network_own_client_id) _network_playas = playas; if (client_id == _network_own_client_id) _network_playas = playas;
@ -513,7 +513,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD)
/* Initialize the password hash salting variables. */ /* Initialize the password hash salting variables. */
_password_game_seed = p->Recv_uint32(); _password_game_seed = p->Recv_uint32();
p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id)); p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
case NETWORK_GAME_PASSWORD: case NETWORK_GAME_PASSWORD:
ShowNetworkNeedPassword(type); ShowNetworkNeedPassword(type);
@ -557,7 +557,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
maptype = p->Recv_uint8(); maptype = p->Recv_uint8();
if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST; if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
/* First packet, init some stuff */ /* First packet, init some stuff */
if (maptype == MAP_PACKET_START) { if (maptype == MAP_PACKET_START) {
@ -577,7 +577,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
* do a division by zero. When the connection is lost, we just return * do a division by zero. When the connection is lost, we just return
* that. If kbytes_total is 0, the packet must be malformed as a * that. If kbytes_total is 0, the packet must be malformed as a
* savegame less than 1 kilobyte is practically impossible. */ * savegame less than 1 kilobyte is practically impossible. */
if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST; if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
if (_network_join_bytes_total == 0) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (_network_join_bytes_total == 0) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
_network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING; _network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
@ -938,7 +938,7 @@ NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientSocket *cs)
while (res == NETWORK_RECV_STATUS_OKAY && (p = cs->Recv_Packet(&res)) != NULL) { while (res == NETWORK_RECV_STATUS_OKAY && (p = cs->Recv_Packet(&res)) != NULL) {
byte type = p->Recv_uint8(); byte type = p->Recv_uint8();
if (type < PACKET_END && _network_client_packet[type] != NULL && !MY_CLIENT->has_quit) { if (type < PACKET_END && _network_client_packet[type] != NULL && !MY_CLIENT->HasClientQuit()) {
res = _network_client_packet[type](p); res = _network_client_packet[type](p);
} else { } else {
res = NETWORK_RECV_STATUS_MALFORMED_PACKET; res = NETWORK_RECV_STATUS_MALFORMED_PACKET;

View File

@ -457,7 +457,7 @@ public:
assert(_network_content_client.sock == INVALID_SOCKET); assert(_network_content_client.sock == INVALID_SOCKET);
_network_content_client.isConnecting = false; _network_content_client.isConnecting = false;
_network_content_client.sock = s; _network_content_client.sock = s;
_network_content_client.has_quit = false; _network_content_client.Reopen();
_network_content_client.OnConnect(true); _network_content_client.OnConnect(true);
} }
}; };

View File

@ -166,7 +166,7 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientSocket *cs, Netw
DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->client_id, str); DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->client_id, str);
} }
cs->has_quit = true; cs->CloseConnection();
/* Make sure the data get's there before we close the connection */ /* Make sure the data get's there before we close the connection */
cs->Send_Packets(); cs->Send_Packets();
@ -668,7 +668,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
client_lang = (NetworkLanguage)p->Recv_uint8(); client_lang = (NetworkLanguage)p->Recv_uint8();
p->Recv_string(unique_id, sizeof(unique_id)); p->Recv_string(unique_id, sizeof(unique_id));
if (cs->has_quit) return; if (cs->HasClientQuit()) return;
/* join another company does not affect these values */ /* join another company does not affect these values */
switch (playas) { switch (playas) {
@ -769,7 +769,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
/* The client was never joined.. so this is impossible, right? /* The client was never joined.. so this is impossible, right?
* Ignore the packet, give the client a warning, and close his connection */ * Ignore the packet, give the client a warning, and close his connection */
if (cs->status < STATUS_AUTH || cs->has_quit) { if (cs->status < STATUS_AUTH || cs->HasClientQuit()) {
SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
return; return;
} }
@ -791,7 +791,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK) DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
{ {
/* Client has the map, now start syncing */ /* Client has the map, now start syncing */
if (cs->status == STATUS_DONE_MAP && !cs->has_quit) { if (cs->status == STATUS_DONE_MAP && !cs->HasClientQuit()) {
char client_name[NETWORK_CLIENT_NAME_LENGTH]; char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkClientSocket *new_cs; NetworkClientSocket *new_cs;
@ -846,7 +846,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
/* The client was never joined.. so this is impossible, right? /* The client was never joined.. so this is impossible, right?
* Ignore the packet, give the client a warning, and close his connection */ * Ignore the packet, give the client a warning, and close his connection */
if (cs->status < STATUS_DONE_MAP || cs->has_quit) { if (cs->status < STATUS_DONE_MAP || cs->HasClientQuit()) {
SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
return; return;
} }
@ -854,7 +854,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
CommandPacket cp; CommandPacket cp;
const char *err = cs->Recv_Command(p, &cp); const char *err = cs->Recv_Command(p, &cp);
if (cs->has_quit) return; if (cs->HasClientQuit()) return;
NetworkClientInfo *ci = cs->GetInfo(); NetworkClientInfo *ci = cs->GetInfo();
@ -946,8 +946,8 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8(); NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
/* The client was never joined.. thank the client for the packet, but ignore it */ /* The client was never joined.. thank the client for the packet, but ignore it */
if (cs->status < STATUS_DONE_MAP || cs->has_quit) { if (cs->status < STATUS_DONE_MAP || cs->HasClientQuit()) {
cs->has_quit = true; cs->CloseConnection();
return; return;
} }
@ -966,7 +966,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
} }
} }
cs->has_quit = true; cs->CloseConnection();
} }
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT) DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
@ -977,8 +977,8 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
char client_name[NETWORK_CLIENT_NAME_LENGTH]; char client_name[NETWORK_CLIENT_NAME_LENGTH];
/* The client was never joined.. thank the client for the packet, but ignore it */ /* The client was never joined.. thank the client for the packet, but ignore it */
if (cs->status < STATUS_DONE_MAP || cs->has_quit) { if (cs->status < STATUS_DONE_MAP || cs->HasClientQuit()) {
cs->has_quit = true; cs->CloseConnection();
return; return;
} }
@ -992,7 +992,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
} }
} }
cs->has_quit = true; cs->CloseConnection();
} }
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK) DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK)
@ -1194,7 +1194,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
p->Recv_string(client_name, sizeof(client_name)); p->Recv_string(client_name, sizeof(client_name));
ci = cs->GetInfo(); ci = cs->GetInfo();
if (cs->has_quit) return; if (cs->HasClientQuit()) return;
if (ci != NULL) { if (ci != NULL) {
/* Display change */ /* Display change */
@ -1552,7 +1552,7 @@ bool NetworkServer_ReadPackets(NetworkClientSocket *cs)
NetworkRecvStatus res; NetworkRecvStatus res;
while ((p = cs->Recv_Packet(&res)) != NULL) { while ((p = cs->Recv_Packet(&res)) != NULL) {
byte type = p->Recv_uint8(); byte type = p->Recv_uint8();
if (type < PACKET_END && _network_server_packet[type] != NULL && !cs->has_quit) { if (type < PACKET_END && _network_server_packet[type] != NULL && !cs->HasClientQuit()) {
_network_server_packet[type](cs, p); _network_server_packet[type](cs, p);
} else { } else {
DEBUG(net, 0, "[server] received invalid packet type %d", type); DEBUG(net, 0, "[server] received invalid packet type %d", type);