1
/* $Id: tcp_content.cpp 15206 2009-01-22 10:09:56Z rubidium $ */
4
* @file tcp_content.cpp Basic functions to receive and send Content packets.
9
#include "../../stdafx.h"
10
#include "../../debug.h"
11
#include "tcp_content.h"
13
ContentInfo::ContentInfo()
15
memset(this, 0, sizeof(*this));
18
ContentInfo::~ContentInfo()
20
free(this->dependencies);
24
size_t ContentInfo::Size() const
27
for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
29
/* The size is never larger than the content info size plus the size of the
30
* tags and dependencies */
31
return sizeof(*this) +
32
sizeof(this->dependency_count) +
33
sizeof(*this->dependencies) * this->dependency_count;
36
bool ContentInfo::IsSelected() const
38
switch (this->state) {
39
case ContentInfo::SELECTED:
40
case ContentInfo::AUTOSELECTED:
48
bool ContentInfo::IsValid() const
50
return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
53
void NetworkContentSocketHandler::Close()
56
if (this->sock == INVALID_SOCKET) return;
58
closesocket(this->sock);
59
this->sock = INVALID_SOCKET;
63
* Defines a simple (switch) case for each network packet
64
* @param type the packet type to create the case for
66
#define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
69
* Handle an incoming packets by sending it to the correct function.
70
* @param p the received packet
72
bool NetworkContentSocketHandler::HandlePacket(Packet *p)
74
PacketContentType type = (PacketContentType)p->Recv_uint8();
76
switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
77
CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
78
CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
79
CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
80
CONTENT_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
81
CONTENT_COMMAND(PACKET_CONTENT_SERVER_INFO);
82
CONTENT_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
83
CONTENT_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
86
if (this->HasClientQuit()) {
87
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));
89
DEBUG(net, 0, "[tcp/content] received illegal packet from %s:%d", inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
96
* Receive a packet at UDP level
98
void NetworkContentSocketHandler::Recv_Packets()
101
NetworkRecvStatus res;
102
while ((p = this->Recv_Packet(&res)) != NULL) {
103
bool cont = HandlePacket(p);
110
* Create stub implementations for all receive commands that only
111
* show a warning that the given command is not available for the
112
* socket where the packet came from.
113
* @param type the packet type to create the stub for
115
#define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
116
bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \
117
DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s:%d", \
118
type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port)); \
122
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_LIST);
123
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_ID);
124
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID);
125
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5);
126
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
127
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_CLIENT_CONTENT);
128
DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
130
#endif /* ENABLE_NETWORK */