tcp_content.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 #include "tcp_content.h"
00018 
00020 ContentInfo::ContentInfo()
00021 {
00022   memset(this, 0, sizeof(*this));
00023 }
00024 
00026 ContentInfo::~ContentInfo()
00027 {
00028   free(this->dependencies);
00029   free(this->tags);
00030 }
00031 
00036 void ContentInfo::TransferFrom(ContentInfo *other)
00037 {
00038   if (other != this) {
00039     free(this->dependencies);
00040     free(this->tags);
00041     memcpy(this, other, sizeof(ContentInfo));
00042     other->dependencies = NULL;
00043     other->tags = NULL;
00044   }
00045 }
00046 
00051 size_t ContentInfo::Size() const
00052 {
00053   size_t len = 0;
00054   for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
00055 
00056   /* The size is never larger than the content info size plus the size of the
00057    * tags and dependencies */
00058   return sizeof(*this) +
00059       sizeof(this->dependency_count) +
00060       sizeof(*this->dependencies) * this->dependency_count;
00061 }
00062 
00067 bool ContentInfo::IsSelected() const
00068 {
00069   switch (this->state) {
00070     case ContentInfo::SELECTED:
00071     case ContentInfo::AUTOSELECTED:
00072     case ContentInfo::ALREADY_HERE:
00073       return true;
00074 
00075     default:
00076       return false;
00077   }
00078 }
00079 
00084 bool ContentInfo::IsValid() const
00085 {
00086   return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
00087 }
00088 
00089 void NetworkContentSocketHandler::Close()
00090 {
00091   CloseConnection();
00092   if (this->sock == INVALID_SOCKET) return;
00093 
00094   closesocket(this->sock);
00095   this->sock = INVALID_SOCKET;
00096 }
00097 
00102 #define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
00103 
00110 bool NetworkContentSocketHandler::HandlePacket(Packet *p)
00111 {
00112   PacketContentType type = (PacketContentType)p->Recv_uint8();
00113 
00114   switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
00115     case PACKET_CONTENT_CLIENT_INFO_LIST:      return this->Receive_CLIENT_INFO_LIST(p);
00116     case PACKET_CONTENT_CLIENT_INFO_ID:        return this->Receive_CLIENT_INFO_ID(p);
00117     case PACKET_CONTENT_CLIENT_INFO_EXTID:     return this->Receive_CLIENT_INFO_EXTID(p);
00118     case PACKET_CONTENT_CLIENT_INFO_EXTID_MD5: return this->Receive_CLIENT_INFO_EXTID_MD5(p);
00119     case PACKET_CONTENT_SERVER_INFO:           return this->Receive_SERVER_INFO(p);
00120     case PACKET_CONTENT_CLIENT_CONTENT:        return this->Receive_CLIENT_CONTENT(p);
00121     case PACKET_CONTENT_SERVER_CONTENT:        return this->Receive_SERVER_CONTENT(p);
00122 
00123     default:
00124       if (this->HasClientQuit()) {
00125         DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s", type, this->client_addr.GetAddressAsString());
00126       } else {
00127         DEBUG(net, 0, "[tcp/content] received illegal packet from %s", this->client_addr.GetAddressAsString());
00128       }
00129       return false;
00130   }
00131 }
00132 
00136 void NetworkContentSocketHandler::ReceivePackets()
00137 {
00138   Packet *p;
00139   while ((p = this->ReceivePacket()) != NULL) {
00140     bool cont = this->HandlePacket(p);
00141     delete p;
00142     if (!cont) return;
00143   }
00144 }
00145 
00146 
00152 bool NetworkContentSocketHandler::ReceiveInvalidPacket(PacketContentType type)
00153 {
00154   DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", type, this->client_addr.GetAddressAsString());
00155   return false;
00156 }
00157 
00158 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_LIST(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_LIST); }
00159 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_ID(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_ID); }
00160 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_EXTID); }
00161 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID_MD5(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5); }
00162 bool NetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_SERVER_INFO); }
00163 bool NetworkContentSocketHandler::Receive_CLIENT_CONTENT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_CONTENT); }
00164 bool NetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p) { return this->ReceiveInvalidPacket(PACKET_CONTENT_SERVER_CONTENT); }
00165 
00166 #endif /* ENABLE_NETWORK */

Generated on Sun Jun 5 04:19:57 2011 for OpenTTD by  doxygen 1.6.1