Files @ r23483:3733e6b8ff17
Branch filter:

Location: cpp/openttd-patchpack/source/src/network/network_client.h

Patric Stout
Remove: ENABLE_NETWORK switch

This switch has been a pain for years. Often disabling broke
compilation, as no developer compiles OpenTTD without, neither do
any of our official binaries.

Additionaly, it has grown so hugely in our codebase, that it
clearly shows that the current solution was a poor one. 350+
instances of "#ifdef ENABLE_NETWORK" were in the code, of which
only ~30 in the networking code itself. The rest were all around
the code to do the right thing, from GUI to NewGRF.

A more proper solution would be to stub all the functions, and
make sure the rest of the code can simply assume network is
available. This was also partially done, and most variables were
correct if networking was disabled. Despite that, often the #ifdefs
were still used.

With the recent removal of DOS, there is also no platform anymore
which we support where networking isn't working out-of-the-box.

All in all, it is time to remove the ENABLE_NETWORK switch. No
replacement is planned, but if you feel we really need this option,
we welcome any Pull Request which implements this in a way that
doesn't crawl through the code like this diff shows we used to.
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file network_client.h Client part of the network protocol. */

#ifndef NETWORK_CLIENT_H
#define NETWORK_CLIENT_H

#include "network_internal.h"

/** Class for handling the client side of the game connection. */
class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
private:
	struct PacketReader *savegame; ///< Packet reader for reading the savegame.
	byte token;                    ///< The token we need to send back to the server to prove we're the right client.

	/** Status of the connection with the server. */
	enum ServerStatus {
		STATUS_INACTIVE,      ///< The client is not connected nor active.
		STATUS_COMPANY_INFO,  ///< We are trying to get company information.
		STATUS_JOIN,          ///< We are trying to join a server.
		STATUS_NEWGRFS_CHECK, ///< Last action was checking NewGRFs.
		STATUS_AUTH_GAME,     ///< Last action was requesting game (server) password.
		STATUS_AUTH_COMPANY,  ///< Last action was requesting company password.
		STATUS_AUTHORIZED,    ///< The client is authorized at the server.
		STATUS_MAP_WAIT,      ///< The client is waiting as someone else is downloading the map.
		STATUS_MAP,           ///< The client is downloading the map.
		STATUS_ACTIVE,        ///< The client is active within in the game.
		STATUS_END,           ///< Must ALWAYS be on the end of this list!! (period)
	};

	ServerStatus status; ///< Status of the connection with the server.

protected:
	friend void NetworkExecuteLocalCommandQueue();
	friend void NetworkClose(bool close_admins);
	static ClientNetworkGameSocketHandler *my_client; ///< This is us!

	virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
	virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);

	static NetworkRecvStatus SendNewGRFsOk();
	static NetworkRecvStatus SendGetMap();
	static NetworkRecvStatus SendMapOk();
	void CheckConnection();
public:
	ClientNetworkGameSocketHandler(SOCKET s);
	~ClientNetworkGameSocketHandler();

	NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
	void ClientError(NetworkRecvStatus res);

	static NetworkRecvStatus SendCompanyInformationQuery();

	static NetworkRecvStatus SendJoin();
	static NetworkRecvStatus SendCommand(const CommandPacket *cp);
	static NetworkRecvStatus SendError(NetworkErrorCode errorno);
	static NetworkRecvStatus SendQuit();
	static NetworkRecvStatus SendAck();

	static NetworkRecvStatus SendGamePassword(const char *password);
	static NetworkRecvStatus SendCompanyPassword(const char *password);

	static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data);
	static NetworkRecvStatus SendSetPassword(const char *password);
	static NetworkRecvStatus SendSetName(const char *name);
	static NetworkRecvStatus SendRCon(const char *password, const char *command);
	static NetworkRecvStatus SendMove(CompanyID company, const char *password);

	static bool IsConnected();

	static void Send();
	static bool Receive();
	static bool GameLoop();
};

/** Helper to make the code look somewhat nicer. */
typedef ClientNetworkGameSocketHandler MyClient;

void NetworkClient_Connected();
void NetworkClientSetCompanyPassword(const char *password);

extern CompanyID _network_join_as;

extern const char *_network_join_server_password;
extern const char *_network_join_company_password;

#endif /* NETWORK_CLIENT_H */