# 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 #include "../../error.h"
    00022
    00023 #include "table/strings.h"
    00024
    00029 NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s)
    00030 {
    00031   this->sock              = s;
    00032   this->last_frame        = _frame_counter;
    00033   this->last_frame_server = _frame_counter;
    00034   this->last_packet       = _realtime_tick;
    00035 }
    00036
    00044 NetworkRecvStatus NetworkGameSocketHandler::CloseConnection(bool error)
    00045 {
    00046   /* Clients drop back to the main menu */
    00047   if (!_network_server && _networking) {
    00048     _switch_mode = SM_MENU;
    00049     _networking = false;
    00050     ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION, INVALID_STRING_ID, WL_CRITICAL);
    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     case PACKET_SERVER_FULL:                  return this->Receive_SERVER_FULL(p);
    00078     case PACKET_SERVER_BANNED:                return this->Receive_SERVER_BANNED(p);
    00079     case PACKET_CLIENT_JOIN:                  return this->Receive_CLIENT_JOIN(p);
    00080     case PACKET_SERVER_ERROR:                 return this->Receive_SERVER_ERROR(p);
    00081     case PACKET_CLIENT_COMPANY_INFO:          return this->Receive_CLIENT_COMPANY_INFO(p);
    00082     case PACKET_SERVER_COMPANY_INFO:          return this->Receive_SERVER_COMPANY_INFO(p);
    00083     case PACKET_SERVER_CLIENT_INFO:           return this->Receive_SERVER_CLIENT_INFO(p);
    00084     case PACKET_SERVER_NEED_GAME_PASSWORD:    return this->Receive_SERVER_NEED_GAME_PASSWORD(p);
    00085     case PACKET_SERVER_NEED_COMPANY_PASSWORD: return this->Receive_SERVER_NEED_COMPANY_PASSWORD(p);
    00086     case PACKET_CLIENT_GAME_PASSWORD:         return this->Receive_CLIENT_GAME_PASSWORD(p);
    00087     case PACKET_CLIENT_COMPANY_PASSWORD:      return this->Receive_CLIENT_COMPANY_PASSWORD(p);
    00088     case PACKET_SERVER_WELCOME:               return this->Receive_SERVER_WELCOME(p);
    00089     case PACKET_CLIENT_GETMAP:                return this->Receive_CLIENT_GETMAP(p);
    00090     case PACKET_SERVER_WAIT:                  return this->Receive_SERVER_WAIT(p);
    00091     case PACKET_SERVER_MAP_BEGIN:             return this->Receive_SERVER_MAP_BEGIN(p);
    00092     case PACKET_SERVER_MAP_SIZE:              return this->Receive_SERVER_MAP_SIZE(p);
    00093     case PACKET_SERVER_MAP_DATA:              return this->Receive_SERVER_MAP_DATA(p);
    00094     case PACKET_SERVER_MAP_DONE:              return this->Receive_SERVER_MAP_DONE(p);
    00095     case PACKET_CLIENT_MAP_OK:                return this->Receive_CLIENT_MAP_OK(p);
    00096     case PACKET_SERVER_JOIN:                  return this->Receive_SERVER_JOIN(p);
    00097     case PACKET_SERVER_FRAME:                 return this->Receive_SERVER_FRAME(p);
    00098     case PACKET_SERVER_SYNC:                  return this->Receive_SERVER_SYNC(p);
    00099     case PACKET_CLIENT_ACK:                   return this->Receive_CLIENT_ACK(p);
    00100     case PACKET_CLIENT_COMMAND:               return this->Receive_CLIENT_COMMAND(p);
    00101     case PACKET_SERVER_COMMAND:               return this->Receive_SERVER_COMMAND(p);
    00102     case PACKET_CLIENT_CHAT:                  return this->Receive_CLIENT_CHAT(p);
    00103     case PACKET_SERVER_CHAT:                  return this->Receive_SERVER_CHAT(p);
    00104     case PACKET_CLIENT_SET_PASSWORD:          return this->Receive_CLIENT_SET_PASSWORD(p);
    00105     case PACKET_CLIENT_SET_NAME:              return this->Receive_CLIENT_SET_NAME(p);
    00106     case PACKET_CLIENT_QUIT:                  return this->Receive_CLIENT_QUIT(p);
    00107     case PACKET_CLIENT_ERROR:                 return this->Receive_CLIENT_ERROR(p);
    00108     case PACKET_SERVER_QUIT:                  return this->Receive_SERVER_QUIT(p);
    00109     case PACKET_SERVER_ERROR_QUIT:            return this->Receive_SERVER_ERROR_QUIT(p);
    00110     case PACKET_SERVER_SHUTDOWN:              return this->Receive_SERVER_SHUTDOWN(p);
    00111     case PACKET_SERVER_NEWGAME:               return this->Receive_SERVER_NEWGAME(p);
    00112     case PACKET_SERVER_RCON:                  return this->Receive_SERVER_RCON(p);
    00113     case PACKET_CLIENT_RCON:                  return this->Receive_CLIENT_RCON(p);
    00114     case PACKET_SERVER_CHECK_NEWGRFS:         return this->Receive_SERVER_CHECK_NEWGRFS(p);
    00115     case PACKET_CLIENT_NEWGRFS_CHECKED:       return this->Receive_CLIENT_NEWGRFS_CHECKED(p);
    00116     case PACKET_SERVER_MOVE:                  return this->Receive_SERVER_MOVE(p);
    00117     case PACKET_CLIENT_MOVE:                  return this->Receive_CLIENT_MOVE(p);
    00118     case PACKET_SERVER_COMPANY_UPDATE:        return this->Receive_SERVER_COMPANY_UPDATE(p);
    00119     case PACKET_SERVER_CONFIG_UPDATE:         return this->Receive_SERVER_CONFIG_UPDATE(p);
    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
    00157 NetworkRecvStatus NetworkGameSocketHandler::ReceiveInvalidPacket(PacketGameType type)
    00158 {
    00159   DEBUG(net, 0, "[tcp/game] received illegal packet type %d from client %d", type, this->client_id);
    00160   return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00161 }
    00162
    00163 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_FULL); }
    00164 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_BANNED(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_BANNED); }
    00165 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_JOIN); }
    00166 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_ERROR(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_ERROR); }
    00167 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_COMPANY_INFO(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_COMPANY_INFO); }
    00168 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_COMPANY_INFO); }
    00169 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_CLIENT_INFO); }
    00170 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSWORD(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_NEED_GAME_PASSWORD); }
    00171 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_NEED_COMPANY_PASSWORD); }
    00172 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_GAME_PASSWORD); }
    00173 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_COMPANY_PASSWORD); }
    00174 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_WELCOME(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_WELCOME); }
    00175 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_GETMAP); }
    00176 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_WAIT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_WAIT); }
    00177 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_BEGIN); }
    00178 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_SIZE); }
    00179 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_MAP_DATA(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_DATA); }
    00180 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_DONE); }
    00181 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_MAP_OK); }
    00182 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_JOIN(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_JOIN); }
    00183 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_FRAME(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_FRAME); }
    00184 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_SYNC(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_SYNC); }
    00185 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_ACK(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_ACK); }
    00186 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_COMMAND); }
    00187 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_COMMAND); }
    00188 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_CHAT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_CHAT); }
    00189 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_CHAT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_CHAT); }
    00190 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_SET_PASSWORD); }
    00191 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_SET_NAME(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_SET_NAME); }
    00192 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_QUIT); }
    00193 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_ERROR); }
    00194 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_QUIT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_QUIT); }
    00195 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_ERROR_QUIT); }
    00196 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_SHUTDOWN(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_SHUTDOWN); }
    00197 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_NEWGAME(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_NEWGAME); }
    00198 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_RCON(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_RCON); }
    00199 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_RCON(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_RCON); }
    00200 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_CHECK_NEWGRFS); }
    00201 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_NEWGRFS_CHECKED); }
    00202 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_MOVE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_MOVE); }
    00203 NetworkRecvStatus NetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CLIENT_MOVE); }
    00204 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_COMPANY_UPDATE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_COMPANY_UPDATE); }
    00205 NetworkRecvStatus NetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE(Packet *p) { return this->ReceiveInvalidPacket(PACKET_SERVER_CONFIG_UPDATE); }
    00206
    00207 #endif /* ENABLE_NETWORK */