~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/network/core/tcp_content.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: tcp_content.cpp 15206 2009-01-22 10:09:56Z rubidium $ */
 
2
 
 
3
/**
 
4
 * @file tcp_content.cpp Basic functions to receive and send Content packets.
 
5
 */
 
6
 
 
7
#ifdef ENABLE_NETWORK
 
8
 
 
9
#include "../../stdafx.h"
 
10
#include "../../debug.h"
 
11
#include "tcp_content.h"
 
12
 
 
13
ContentInfo::ContentInfo()
 
14
{
 
15
        memset(this, 0, sizeof(*this));
 
16
}
 
17
 
 
18
ContentInfo::~ContentInfo()
 
19
{
 
20
        free(this->dependencies);
 
21
        free(this->tags);
 
22
}
 
23
 
 
24
size_t ContentInfo::Size() const
 
25
{
 
26
        size_t len = 0;
 
27
        for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
 
28
 
 
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;
 
34
}
 
35
 
 
36
bool ContentInfo::IsSelected() const
 
37
{
 
38
        switch (this->state) {
 
39
                case ContentInfo::SELECTED:
 
40
                case ContentInfo::AUTOSELECTED:
 
41
                        return true;
 
42
 
 
43
                default:
 
44
                        return false;
 
45
        }
 
46
}
 
47
 
 
48
bool ContentInfo::IsValid() const
 
49
{
 
50
        return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
 
51
}
 
52
 
 
53
void NetworkContentSocketHandler::Close()
 
54
{
 
55
        CloseConnection();
 
56
        if (this->sock == INVALID_SOCKET) return;
 
57
 
 
58
        closesocket(this->sock);
 
59
        this->sock = INVALID_SOCKET;
 
60
}
 
61
 
 
62
/**
 
63
 * Defines a simple (switch) case for each network packet
 
64
 * @param type the packet type to create the case for
 
65
 */
 
66
#define CONTENT_COMMAND(type) case type: return this->NetworkPacketReceive_ ## type ## _command(p); break;
 
67
 
 
68
/**
 
69
 * Handle an incoming packets by sending it to the correct function.
 
70
 * @param p the received packet
 
71
 */
 
72
bool NetworkContentSocketHandler::HandlePacket(Packet *p)
 
73
{
 
74
        PacketContentType type = (PacketContentType)p->Recv_uint8();
 
75
 
 
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);
 
84
 
 
85
                default:
 
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));
 
88
                        } else {
 
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));
 
90
                        }
 
91
                        return false;
 
92
        }
 
93
}
 
94
 
 
95
/**
 
96
 * Receive a packet at UDP level
 
97
 */
 
98
void NetworkContentSocketHandler::Recv_Packets()
 
99
{
 
100
        Packet *p;
 
101
        NetworkRecvStatus res;
 
102
        while ((p = this->Recv_Packet(&res)) != NULL) {
 
103
                bool cont = HandlePacket(p);
 
104
                delete p;
 
105
                if (!cont) return;
 
106
        }
 
107
}
 
108
 
 
109
/**
 
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
 
114
 */
 
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)); \
 
119
        return false; \
 
120
}
 
121
 
 
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);
 
129
 
 
130
#endif /* ENABLE_NETWORK */