network_command.cpp

Go to the documentation of this file.
00001 /* $Id: network_command.cpp 21890 2011-01-22 14:52:20Z rubidium $ */
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 
00012 #ifdef ENABLE_NETWORK
00013 
00014 #include "../stdafx.h"
00015 #include "network_admin.h"
00016 #include "network_client.h"
00017 #include "network_server.h"
00018 #include "../command_func.h"
00019 #include "../company_func.h"
00020 #include "../settings_type.h"
00021 
00023 static CommandCallback * const _callback_table[] = {
00024   /* 0x00 */ NULL,
00025   /* 0x01 */ CcBuildPrimaryVehicle,
00026   /* 0x02 */ CcBuildAirport,
00027   /* 0x03 */ CcBuildBridge,
00028   /* 0x04 */ CcBuildCanal,
00029   /* 0x05 */ CcBuildDocks,
00030   /* 0x06 */ CcFoundTown,
00031   /* 0x07 */ CcBuildRoadTunnel,
00032   /* 0x08 */ CcBuildRailTunnel,
00033   /* 0x09 */ CcBuildWagon,
00034   /* 0x0A */ CcRoadDepot,
00035   /* 0x0B */ CcRailDepot,
00036   /* 0x0C */ CcPlaceSign,
00037   /* 0x0D */ CcPlaySound10,
00038   /* 0x0E */ CcPlaySound1D,
00039   /* 0x0F */ CcPlaySound1E,
00040   /* 0x10 */ CcStation,
00041   /* 0x11 */ CcTerraform,
00042 #ifdef ENABLE_AI
00043   /* 0x12 */ CcAI,
00044 #else
00045   /* 0x12 */ NULL,
00046 #endif /* ENABLE_AI */
00047   /* 0x13 */ CcCloneVehicle,
00048   /* 0x14 */ CcGiveMoney,
00049   /* 0x15 */ CcCreateGroup,
00050   /* 0x16 */ CcFoundRandomTown,
00051   /* 0x17 */ CcRoadStop,
00052   /* 0x18 */ CcBuildIndustry,
00053   /* 0x19 */ CcStartStopVehicle,
00054 };
00055 
00061 void CommandQueue::Append(CommandPacket *p)
00062 {
00063   CommandPacket *add = MallocT<CommandPacket>(1);
00064   *add = *p;
00065   add->next = NULL;
00066   if (this->first == NULL) {
00067     this->first = add;
00068   } else {
00069     this->last->next = add;
00070   }
00071   this->last = add;
00072   this->count++;
00073 }
00074 
00080 CommandPacket *CommandQueue::Pop(bool ignore_paused)
00081 {
00082   CommandPacket **prev = &this->first;
00083   CommandPacket *ret = this->first;
00084   if (ignore_paused && _pause_mode != PM_UNPAUSED) {
00085     while (ret != NULL && !IsCommandAllowedWhilePaused(ret->cmd)) {
00086       prev = &ret->next;
00087       ret = ret->next;
00088     }
00089   }
00090   if (ret != NULL) {
00091     *prev = ret->next;
00092     this->count--;
00093   }
00094   return ret;
00095 }
00096 
00102 CommandPacket *CommandQueue::Peek(bool ignore_paused)
00103 {
00104   if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
00105 
00106   for (CommandPacket *p = this->first; p != NULL; p = p->next) {
00107     if (IsCommandAllowedWhilePaused(p->cmd)) return p;
00108   }
00109   return NULL;
00110 }
00111 
00113 void CommandQueue::Free()
00114 {
00115   CommandPacket *cp;
00116   while ((cp = this->Pop()) != NULL) {
00117     free(cp);
00118   }
00119   assert(this->count == 0);
00120 }
00121 
00123 static CommandQueue _local_wait_queue;
00125 static CommandQueue _local_execution_queue;
00126 
00137 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
00138 {
00139   assert((cmd & CMD_FLAGS_MASK) == 0);
00140 
00141   CommandPacket c;
00142   c.company  = company;
00143   c.tile     = tile;
00144   c.p1       = p1;
00145   c.p2       = p2;
00146   c.cmd      = cmd;
00147   c.callback = callback;
00148 
00149   strecpy(c.text, (text != NULL) ? text : "", lastof(c.text));
00150 
00151   if (_network_server) {
00152     /* If we are the server, we queue the command in our 'special' queue.
00153      *   In theory, we could execute the command right away, but then the
00154      *   client on the server can do everything 1 tick faster than others.
00155      *   So to keep the game fair, we delay the command with 1 tick
00156      *   which gives about the same speed as most clients.
00157      */
00158     c.frame = _frame_counter_max + 1;
00159     c.my_cmd = true;
00160 
00161     _local_wait_queue.Append(&c);
00162     return;
00163   }
00164 
00165   c.frame = 0; // The client can't tell which frame, so just make it 0
00166 
00167   /* Clients send their command to the server and forget all about the packet */
00168   MyClient::SendCommand(&c);
00169 }
00170 
00180 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
00181 {
00182   for (CommandPacket *p = _local_execution_queue.Peek(); p != NULL; p = p->next) {
00183     CommandPacket c = *p;
00184     c.callback = 0;
00185     cs->outgoing_queue.Append(&c);
00186   }
00187 }
00188 
00192 void NetworkExecuteLocalCommandQueue()
00193 {
00194   assert(IsLocalCompany());
00195 
00196   CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
00197 
00198   CommandPacket *cp;
00199   while ((cp = queue.Peek()) != NULL) {
00200     /* The queue is always in order, which means
00201      * that the first element will be executed first. */
00202     if (_frame_counter < cp->frame) break;
00203 
00204     if (_frame_counter > cp->frame) {
00205       /* If we reach here, it means for whatever reason, we've already executed
00206        * past the command we need to execute. */
00207       error("[net] Trying to execute a packet in the past!");
00208     }
00209 
00210     /* We can execute this command */
00211     _current_company = cp->company;
00212     cp->cmd |= CMD_NETWORK_COMMAND;
00213     DoCommandP(cp, cp->my_cmd);
00214 
00215     queue.Pop();
00216     free(cp);
00217   }
00218 
00219   /* Local company may have changed, so we should not restore the old value */
00220   _current_company = _local_company;
00221 }
00222 
00226 void NetworkFreeLocalCommandQueue()
00227 {
00228   _local_execution_queue.Free();
00229 }
00230 
00236 static void DistributeCommandPacket(CommandPacket cp, const NetworkClientSocket *owner)
00237 {
00238   CommandCallback *callback = cp.callback;
00239   cp.frame = _frame_counter_max + 1;
00240 
00241   NetworkClientSocket *cs;
00242   FOR_ALL_CLIENT_SOCKETS(cs) {
00243     if (cs->status >= NetworkClientSocket::STATUS_MAP) {
00244       /* Callbacks are only send back to the client who sent them in the
00245        *  first place. This filters that out. */
00246       cp.callback = (cs != owner) ? NULL : callback;
00247       cp.my_cmd = (cs == owner);
00248       cs->outgoing_queue.Append(&cp);
00249     }
00250   }
00251 
00252   cp.callback = (cs != owner) ? NULL : callback;
00253   cp.my_cmd = (cs == owner);
00254   _local_execution_queue.Append(&cp);
00255 }
00256 
00262 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
00263 {
00264   int to_go = _settings_client.network.commands_per_frame;
00265 
00266   CommandPacket *cp;
00267   while (--to_go >= 0 && (cp = queue->Pop(true)) != NULL) {
00268     DistributeCommandPacket(*cp, owner);
00269     NetworkAdminCmdLogging(owner, cp);
00270     free(cp);
00271   }
00272 }
00273 
00274 void NetworkDistributeCommands()
00275 {
00276   /* First send the server's commands. */
00277   DistributeQueue(&_local_wait_queue, NULL);
00278 
00279   /* Then send the queues of the others. */
00280   NetworkClientSocket *cs;
00281   FOR_ALL_CLIENT_SOCKETS(cs) {
00282     DistributeQueue(&cs->incoming_queue, cs);
00283   }
00284 }
00285 
00292 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
00293 {
00294   cp->company = (CompanyID)p->Recv_uint8();
00295   cp->cmd     = p->Recv_uint32();
00296   cp->p1      = p->Recv_uint32();
00297   cp->p2      = p->Recv_uint32();
00298   cp->tile    = p->Recv_uint32();
00299   p->Recv_string(cp->text, lengthof(cp->text));
00300 
00301   byte callback = p->Recv_uint8();
00302 
00303   if (!IsValidCommand(cp->cmd))               return "invalid command";
00304   if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "offline only command";
00305   if ((cp->cmd & CMD_FLAGS_MASK) != 0)        return "invalid command flag";
00306   if (callback > lengthof(_callback_table))   return "invalid callback";
00307 
00308   cp->callback = _callback_table[callback];
00309   return NULL;
00310 }
00311 
00317 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
00318 {
00319   p->Send_uint8 (cp->company);
00320   p->Send_uint32(cp->cmd);
00321   p->Send_uint32(cp->p1);
00322   p->Send_uint32(cp->p2);
00323   p->Send_uint32(cp->tile);
00324   p->Send_string(cp->text);
00325 
00326   byte callback = 0;
00327   while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
00328     callback++;
00329   }
00330 
00331   if (callback == lengthof(_callback_table)) {
00332     DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", cp->callback);
00333     callback = 0; // _callback_table[0] == NULL
00334   }
00335   p->Send_uint8 (callback);
00336 }
00337 
00338 #endif /* ENABLE_NETWORK */

Generated on Fri Feb 4 20:53:42 2011 for OpenTTD by  doxygen 1.6.1