tcp_content.cpp

Go to the documentation of this file.
00001 /* $Id: tcp_content.cpp 21886 2011-01-22 09:53:15Z 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 
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     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
00116     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
00117     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
00118     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
00119     CONTENT_COMMAND(PACKET_CONTENT_SERVER_INFO);
00120     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
00121     CONTENT_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
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 
00152 #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
00153 bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
00154 { \
00155   DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", \
00156       type, this->client_addr.GetAddressAsString()); \
00157   return false; \
00158 }
00159 
00160 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST)
00161 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID)
00162 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID)
00163 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5)
00164 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO)
00165 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_CONTENT)
00166 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT)
00167 
00168 #endif /* ENABLE_NETWORK */

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