# 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
    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
    00030 size_t ContentInfo::Size() const
    00031 {
    00032   size_t len = 0;
    00033   for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
    00034
    00035   /* The size is never larger than the content info size plus the size of the
    00036    * tags and dependencies */
    00037   return sizeof(*this) +
    00038       sizeof(this->dependency_count) +
    00039       sizeof(*this->dependencies) * this->dependency_count;
    00040 }
    00041
    00042 bool ContentInfo::IsSelected() const
    00043 {
    00044   switch (this->state) {
    00045     case ContentInfo::SELECTED:
    00046     case ContentInfo::AUTOSELECTED:
    00047     case ContentInfo::ALREADY_HERE:
    00048       return true;
    00049
    00050     default:
    00051       return false;
    00052   }
    00053 }
    00054
    00055 bool ContentInfo::IsValid() const
    00056 {
    00057   return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
    00058 }
    00059
    00060 void NetworkContentSocketHandler::Close()
    00061 {
    00062   CloseConnection();
    00063   if (this->sock == INVALID_SOCKET) return;
    00064
    00065   closesocket(this->sock);
    00066   this->sock = INVALID_SOCKET;
    00067 }
    00068
    00073 #define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
    00074
    00079 bool NetworkContentSocketHandler::HandlePacket(Packet *p)
    00080 {
    00081   PacketContentType type = (PacketContentType)p->Recv_uint8();
    00082
    00083   switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
    00084     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
    00085     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
    00086     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
    00087     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
    00088     CONTENT_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00089     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
    00090     CONTENT_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00091
    00092     default:
    00093       if (this->HasClientQuit()) {
    00094         DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s", type, this->client_addr.GetAddressAsString());
    00095       } else {
    00096         DEBUG(net, 0, "[tcp/content] received illegal packet from %s", this->client_addr.GetAddressAsString());
    00097       }
    00098       return false;
    00099   }
    00100 }
    00101
    00105 void NetworkContentSocketHandler::Recv_Packets()
    00106 {
    00107   Packet *p;
    00108   while ((p = this->Recv_Packet()) != NULL) {
    00109     bool cont = HandlePacket(p);
    00110     delete p;
    00111     if (!cont) return;
    00112   }
    00113 }
    00114
    00121 #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
    00122 bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
    00123 { \
    00124   DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", \
    00125       type, this->client_addr.GetAddressAsString()); \
    00126   return false; \
    00127 }
    00128
    00129 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
    00130 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
    00131 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
    00132 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
    00133 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00134 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
    00135 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00136
    00137 #endif /* ENABLE_NETWORK */