mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-12 18:40:29 +00:00
(svn r21027) -Change/Fix: under some circumstances the file handle of the downloaded savegame wouldn't be closed, and validity of the handled wasn't checked in all cases
This commit is contained in:
parent
c5015bb5bd
commit
744bc2a614
@ -40,7 +40,7 @@
|
|||||||
* Create a new socket for the client side of the game connection.
|
* Create a new socket for the client side of the game connection.
|
||||||
* @param s The socket to connect with.
|
* @param s The socket to connect with.
|
||||||
*/
|
*/
|
||||||
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
|
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s), download_file(NULL)
|
||||||
{
|
{
|
||||||
assert(ClientNetworkGameSocketHandler::my_client == NULL);
|
assert(ClientNetworkGameSocketHandler::my_client == NULL);
|
||||||
ClientNetworkGameSocketHandler::my_client = this;
|
ClientNetworkGameSocketHandler::my_client = this;
|
||||||
@ -51,6 +51,9 @@ ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
|
|||||||
{
|
{
|
||||||
assert(ClientNetworkGameSocketHandler::my_client == this);
|
assert(ClientNetworkGameSocketHandler::my_client == this);
|
||||||
ClientNetworkGameSocketHandler::my_client = NULL;
|
ClientNetworkGameSocketHandler::my_client = NULL;
|
||||||
|
|
||||||
|
/* If for whatever reason the handle isn't closed, do it now. */
|
||||||
|
if (this->download_file != NULL) fclose(this->download_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkRecvStatus ClientNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
|
NetworkRecvStatus ClientNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
|
||||||
@ -660,8 +663,6 @@ DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_WAIT)
|
|||||||
|
|
||||||
DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP)
|
DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP)
|
||||||
{
|
{
|
||||||
static FILE *file_pointer;
|
|
||||||
|
|
||||||
byte maptype;
|
byte maptype;
|
||||||
|
|
||||||
maptype = p->Recv_uint8();
|
maptype = p->Recv_uint8();
|
||||||
@ -670,8 +671,9 @@ DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP)
|
|||||||
|
|
||||||
/* First packet, init some stuff */
|
/* First packet, init some stuff */
|
||||||
if (maptype == MAP_PACKET_START) {
|
if (maptype == MAP_PACKET_START) {
|
||||||
file_pointer = FioFOpenFile("network_client.tmp", "wb", AUTOSAVE_DIR);
|
if (this->download_file != NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
|
||||||
if (file_pointer == NULL) {
|
this->download_file = FioFOpenFile("network_client.tmp", "wb", AUTOSAVE_DIR);
|
||||||
|
if (this->download_file == NULL) {
|
||||||
_switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
|
_switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
|
||||||
return NETWORK_RECV_STATUS_SAVEGAME;
|
return NETWORK_RECV_STATUS_SAVEGAME;
|
||||||
}
|
}
|
||||||
@ -696,20 +698,25 @@ DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP)
|
|||||||
return NETWORK_RECV_STATUS_OKAY;
|
return NETWORK_RECV_STATUS_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->download_file == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
|
||||||
|
|
||||||
if (maptype == MAP_PACKET_NORMAL) {
|
if (maptype == MAP_PACKET_NORMAL) {
|
||||||
/* We are still receiving data, put it to the file */
|
/* We are still receiving data, put it to the file */
|
||||||
if (fwrite(p->buffer + p->pos, 1, p->size - p->pos, file_pointer) != (size_t)(p->size - p->pos)) {
|
if (fwrite(p->buffer + p->pos, 1, p->size - p->pos, this->download_file) != (size_t)(p->size - p->pos)) {
|
||||||
_switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
|
_switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
|
||||||
|
fclose(this->download_file);
|
||||||
|
this->download_file = NULL;
|
||||||
return NETWORK_RECV_STATUS_SAVEGAME;
|
return NETWORK_RECV_STATUS_SAVEGAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
_network_join_bytes = ftell(file_pointer);
|
_network_join_bytes = ftell(this->download_file);
|
||||||
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
|
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if this was the last packet */
|
/* Check if this was the last packet */
|
||||||
if (maptype == MAP_PACKET_END) {
|
if (maptype == MAP_PACKET_END) {
|
||||||
fclose(file_pointer);
|
fclose(this->download_file);
|
||||||
|
this->download_file = NULL;
|
||||||
|
|
||||||
_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
|
_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
|
||||||
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
|
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
|
||||||
|
@ -18,6 +18,9 @@
|
|||||||
|
|
||||||
/** Class for handling the client side of the game connection. */
|
/** Class for handling the client side of the game connection. */
|
||||||
class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
|
class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
|
||||||
|
private:
|
||||||
|
FILE *download_file; ///< Handle used for downloading the savegame.
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend void NetworkExecuteLocalCommandQueue();
|
friend void NetworkExecuteLocalCommandQueue();
|
||||||
friend void NetworkClose(bool close_admins);
|
friend void NetworkClose(bool close_admins);
|
||||||
|
Loading…
Reference in New Issue
Block a user