# tcp_content.cpp

Go to the documentation of this file.

    00001 /* $Id: tcp_content.cpp 17081 2009-08-06 00:04:00Z rubidium $ */
    00002
    00007 #ifdef ENABLE_NETWORK
    00008
    00009 #include "../../stdafx.h"
    00010 #include "../../debug.h"
    00011 #include "tcp_content.h"
    00012
    00013 ContentInfo::ContentInfo()
    00014 {
    00015   memset(this, 0, sizeof(*this));
    00016 }
    00017
    00018 ContentInfo::~ContentInfo()
    00019 {
    00020   free(this->dependencies);
    00021   free(this->tags);
    00022 }
    00023
    00024 size_t ContentInfo::Size() const
    00025 {
    00026   size_t len = 0;
    00027   for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
    00028
    00029   /* The size is never larger than the content info size plus the size of the
    00030    * tags and dependencies */
    00031   return sizeof(*this) +
    00032       sizeof(this->dependency_count) +
    00033       sizeof(*this->dependencies) * this->dependency_count;
    00034 }
    00035
    00036 bool ContentInfo::IsSelected() const
    00037 {
    00038   switch (this->state) {
    00039     case ContentInfo::SELECTED:
    00040     case ContentInfo::AUTOSELECTED:
    00041     case ContentInfo::ALREADY_HERE:
    00042       return true;
    00043
    00044     default:
    00045       return false;
    00046   }
    00047 }
    00048
    00049 bool ContentInfo::IsValid() const
    00050 {
    00051   return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
    00052 }
    00053
    00054 void NetworkContentSocketHandler::Close()
    00055 {
    00056   CloseConnection();
    00057   if (this->sock == INVALID_SOCKET) return;
    00058
    00059   closesocket(this->sock);
    00060   this->sock = INVALID_SOCKET;
    00061 }
    00062
    00067 #define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
    00068
    00073 bool NetworkContentSocketHandler::HandlePacket(Packet *p)
    00074 {
    00075   PacketContentType type = (PacketContentType)p->Recv_uint8();
    00076
    00077   switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
    00078     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
    00079     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
    00080     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
    00081     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
    00082     CONTENT_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00083     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
    00084     CONTENT_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00085
    00086     default:
    00087       if (this->HasClientQuit()) {
    00088         DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s:%d", type,  inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
    00089       } else {
    00090         DEBUG(net, 0, "[tcp/content] received illegal packet from %s:%d", inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
    00091       }
    00092       return false;
    00093   }
    00094 }
    00095
    00099 void NetworkContentSocketHandler::Recv_Packets()
    00100 {
    00101   Packet *p;
    00102   NetworkRecvStatus res;
    00103   while ((p = this->Recv_Packet(&res)) != NULL) {
    00104     bool cont = HandlePacket(p);
    00105     delete p;
    00106     if (!cont) return;
    00107   }
    00108 }
    00109
    00116 #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
    00117 bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \
    00118   DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s:%d", \
    00119       type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port)); \
    00120   return false; \
    00121 }
    00122
    00123 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
    00124 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
    00125 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
    00126 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
    00127 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00128 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
    00129 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00130
    00131 #endif /* ENABLE_NETWORK */