tcp_content.cpp

Go to the documentation of this file.
00001 /* $Id: tcp_content.cpp 15206 2009-01-22 10:09:56Z 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       return true;
00042 
00043     default:
00044       return false;
00045   }
00046 }
00047 
00048 bool ContentInfo::IsValid() const
00049 {
00050   return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
00051 }
00052 
00053 void NetworkContentSocketHandler::Close()
00054 {
00055   CloseConnection();
00056   if (this->sock == INVALID_SOCKET) return;
00057 
00058   closesocket(this->sock);
00059   this->sock = INVALID_SOCKET;
00060 }
00061 
00066 #define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
00067 
00072 bool NetworkContentSocketHandler::HandlePacket(Packet *p)
00073 {
00074   PacketContentType type = (PacketContentType)p->Recv_uint8();
00075 
00076   switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
00077     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
00078     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
00079     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
00080     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
00081     CONTENT_COMMAND(PACKET_CONTENT_SERVER_INFO);
00082     CONTENT_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
00083     CONTENT_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
00084 
00085     default:
00086       if (this->HasClientQuit()) {
00087         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));
00088       } else {
00089         DEBUG(net, 0, "[tcp/content] received illegal packet from %s:%d", inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
00090       }
00091       return false;
00092   }
00093 }
00094 
00098 void NetworkContentSocketHandler::Recv_Packets()
00099 {
00100   Packet *p;
00101   NetworkRecvStatus res;
00102   while ((p = this->Recv_Packet(&res)) != NULL) {
00103     bool cont = HandlePacket(p);
00104     delete p;
00105     if (!cont) return;
00106   }
00107 }
00108 
00115 #define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
00116 bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \
00117   DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s:%d", \
00118       type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port)); \
00119   return false; \
00120 }
00121 
00122 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
00123 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
00124 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
00125 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
00126 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
00127 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
00128 DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
00129 
00130 #endif /* ENABLE_NETWORK */

Generated on Mon Mar 9 23:33:48 2009 for openttd by  doxygen 1.5.6