# tcp_game.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00014 #ifdef ENABLE_NETWORK
    00015
    00016 #include "../../stdafx.h"
    00017
    00018 #include "../network.h"
    00019 #include "../network_internal.h"
    00020 #include "../../debug.h"
    00021
    00022 #include "table/strings.h"
    00023
    00028 NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s)
    00029 {
    00030   this->sock              = s;
    00031   this->last_frame        = _frame_counter;
    00032   this->last_frame_server = _frame_counter;
    00033   this->last_packet       = _realtime_tick;
    00034 }
    00035
    00043 NetworkRecvStatus NetworkGameSocketHandler::CloseConnection(bool error)
    00044 {
    00045   /* Clients drop back to the main menu */
    00046   if (!_network_server && _networking) {
    00047     _switch_mode = SM_MENU;
    00048     _networking = false;
    00049     extern StringID _switch_mode_errorstr;
    00050     _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
    00051
    00052     return NETWORK_RECV_STATUS_CONN_LOST;
    00053   }
    00054
    00055   return this->CloseConnection(error ? NETWORK_RECV_STATUS_SERVER_ERROR : NETWORK_RECV_STATUS_CONN_LOST);
    00056 }
    00057
    00058
    00063 #define GAME_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
    00064
    00070 NetworkRecvStatus NetworkGameSocketHandler::HandlePacket(Packet *p)
    00071 {
    00072   PacketGameType type = (PacketGameType)p->Recv_uint8();
    00073
    00074   this->last_packet = _realtime_tick;
    00075
    00076   switch (this->HasClientQuit() ? PACKET_END : type) {
    00077     GAME_COMMAND(PACKET_SERVER_FULL)
    00078     GAME_COMMAND(PACKET_SERVER_BANNED)
    00079     GAME_COMMAND(PACKET_CLIENT_JOIN)
    00080     GAME_COMMAND(PACKET_SERVER_ERROR)
    00081     GAME_COMMAND(PACKET_CLIENT_COMPANY_INFO)
    00082     GAME_COMMAND(PACKET_SERVER_COMPANY_INFO)
    00083     GAME_COMMAND(PACKET_SERVER_CLIENT_INFO)
    00084     GAME_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD)
    00085     GAME_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD)
    00086     GAME_COMMAND(PACKET_CLIENT_GAME_PASSWORD)
    00087     GAME_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD)
    00088     GAME_COMMAND(PACKET_SERVER_WELCOME)
    00089     GAME_COMMAND(PACKET_CLIENT_GETMAP)
    00090     GAME_COMMAND(PACKET_SERVER_WAIT)
    00091     GAME_COMMAND(PACKET_SERVER_MAP_BEGIN)
    00092     GAME_COMMAND(PACKET_SERVER_MAP_SIZE)
    00093     GAME_COMMAND(PACKET_SERVER_MAP_DATA)
    00094     GAME_COMMAND(PACKET_SERVER_MAP_DONE)
    00095     GAME_COMMAND(PACKET_CLIENT_MAP_OK)
    00096     GAME_COMMAND(PACKET_SERVER_JOIN)
    00097     GAME_COMMAND(PACKET_SERVER_FRAME)
    00098     GAME_COMMAND(PACKET_SERVER_SYNC)
    00099     GAME_COMMAND(PACKET_CLIENT_ACK)
    00100     GAME_COMMAND(PACKET_CLIENT_COMMAND)
    00101     GAME_COMMAND(PACKET_SERVER_COMMAND)
    00102     GAME_COMMAND(PACKET_CLIENT_CHAT)
    00103     GAME_COMMAND(PACKET_SERVER_CHAT)
    00104     GAME_COMMAND(PACKET_CLIENT_SET_PASSWORD)
    00105     GAME_COMMAND(PACKET_CLIENT_SET_NAME)
    00106     GAME_COMMAND(PACKET_CLIENT_QUIT)
    00107     GAME_COMMAND(PACKET_CLIENT_ERROR)
    00108     GAME_COMMAND(PACKET_SERVER_QUIT)
    00109     GAME_COMMAND(PACKET_SERVER_ERROR_QUIT)
    00110     GAME_COMMAND(PACKET_SERVER_SHUTDOWN)
    00111     GAME_COMMAND(PACKET_SERVER_NEWGAME)
    00112     GAME_COMMAND(PACKET_SERVER_RCON)
    00113     GAME_COMMAND(PACKET_CLIENT_RCON)
    00114     GAME_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)
    00115     GAME_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
    00116     GAME_COMMAND(PACKET_SERVER_MOVE)
    00117     GAME_COMMAND(PACKET_CLIENT_MOVE)
    00118     GAME_COMMAND(PACKET_SERVER_COMPANY_UPDATE)
    00119     GAME_COMMAND(PACKET_SERVER_CONFIG_UPDATE)
    00120
    00121     default:
    00122       this->CloseConnection();
    00123
    00124       if (this->HasClientQuit()) {
    00125         DEBUG(net, 0, "[tcp/game] received invalid packet type %d from client %d", type, this->client_id);
    00126       } else {
    00127         DEBUG(net, 0, "[tcp/game] received illegal packet from client %d", this->client_id);
    00128       }
    00129       return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00130   }
    00131 }
    00132
    00140 NetworkRecvStatus NetworkGameSocketHandler::ReceivePackets()
    00141 {
    00142   Packet *p;
    00143   while ((p = this->ReceivePacket()) != NULL) {
    00144     NetworkRecvStatus res = HandlePacket(p);
    00145     delete p;
    00146     if (res != NETWORK_RECV_STATUS_OKAY) return res;
    00147   }
    00148
    00149   return NETWORK_RECV_STATUS_OKAY;
    00150 }
    00151
    00158 #define DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(type) \
    00159 NetworkRecvStatus NetworkGameSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
    00160 { \
    00161   DEBUG(net, 0, "[tcp/game] received illegal packet type %d from client %d", \
    00162       type, this->client_id); \
    00163   return NETWORK_RECV_STATUS_MALFORMED_PACKET; \
    00164 }
    00165
    00166 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_FULL)
    00167 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_BANNED)
    00168 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
    00169 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
    00170 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO)
    00171 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
    00172 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
    00173 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD)
    00174 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD)
    00175 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_GAME_PASSWORD)
    00176 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD)
    00177 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
    00178 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
    00179 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_WAIT)
    00180 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_BEGIN)
    00181 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_SIZE)
    00182 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_DATA)
    00183 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_DONE)
    00184 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
    00185 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
    00186 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
    00187 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_SYNC)
    00188 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_ACK)
    00189 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
    00190 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_COMMAND)
    00191 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
    00192 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
    00193 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
    00194 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
    00195 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
    00196 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
    00197 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
    00198 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
    00199 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN)
    00200 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME)
    00201 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_RCON)
    00202 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_RCON)
    00203 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)
    00204 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
    00205 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MOVE)
    00206 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_MOVE)
    00207 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_UPDATE)
    00208 DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CONFIG_UPDATE)
    00209
    00210 #endif /* ENABLE_NETWORK */