# tcp_content.cpp

Go to the documentation of this file.

    00001 /* $Id: tcp_content.cpp 20095 2010-07-08 19:59:13Z 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
    00019 ContentInfo::ContentInfo()
    00020 {
    00021   memset(this, 0, sizeof(*this));
    00022 }
    00023
    00024 ContentInfo::~ContentInfo()
    00025 {
    00026   free(this->dependencies);
    00027   free(this->tags);
    00028 }
    00029
    00034 void ContentInfo::TransferFrom(ContentInfo *other)
    00035 {
    00036   if (other != this) {
    00037     free(this->dependencies);
    00038     free(this->tags);
    00039     memcpy(this, other, sizeof(ContentInfo));
    00040     other->dependencies = NULL;
    00041     other->tags = NULL;
    00042   }
    00043 }
    00044
    00045 size_t ContentInfo::Size() const
    00046 {
    00047   size_t len = 0;
    00048   for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
    00049
    00050   /* The size is never larger than the content info size plus the size of the
    00051    * tags and dependencies */
    00052   return sizeof(*this) +
    00053       sizeof(this->dependency_count) +
    00054       sizeof(*this->dependencies) * this->dependency_count;
    00055 }
    00056
    00057 bool ContentInfo::IsSelected() const
    00058 {
    00059   switch (this->state) {
    00060     case ContentInfo::SELECTED:
    00061     case ContentInfo::AUTOSELECTED:
    00062     case ContentInfo::ALREADY_HERE:
    00063       return true;
    00064
    00065     default:
    00066       return false;
    00067   }
    00068 }
    00069
    00070 bool ContentInfo::IsValid() const
    00071 {
    00072   return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
    00073 }
    00074
    00075 void NetworkContentSocketHandler::Close()
    00076 {
    00077   CloseConnection();
    00078   if (this->sock == INVALID_SOCKET) return;
    00079
    00080   closesocket(this->sock);
    00081   this->sock = INVALID_SOCKET;
    00082 }
    00083
    00088 #define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
    00089
    00094 bool NetworkContentSocketHandler::HandlePacket(Packet *p)
    00095 {
    00096   PacketContentType type = (PacketContentType)p->Recv_uint8();
    00097
    00098   switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
    00099     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
    00100     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
    00101     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
    00102     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
    00103     CONTENT_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00104     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
    00105     CONTENT_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00106
    00107     default:
    00108       if (this->HasClientQuit()) {
    00109         DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s", type, this->client_addr.GetAddressAsString());
    00110       } else {
    00111         DEBUG(net, 0, "[tcp/content] received illegal packet from %s", this->client_addr.GetAddressAsString());
    00112       }
    00113       return false;
    00114   }
    00115 }
    00116
    00120 void NetworkContentSocketHandler::Recv_Packets()
    00121 {
    00122   Packet *p;
    00123   while ((p = this->Recv_Packet()) != NULL) {
    00124     bool cont = HandlePacket(p);
    00125     delete p;
    00126     if (!cont) return;
    00127   }
    00128 }
    00129
    00136 #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
    00137 bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
    00138 { \
    00139   DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", \
    00140       type, this->client_addr.GetAddressAsString()); \
    00141   return false; \
    00142 }
    00143
    00144 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
    00145 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
    00146 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
    00147 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
    00148 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00149 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
    00150 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00151
    00152 #endif /* ENABLE_NETWORK */