summaryrefslogtreecommitdiff
path: root/src/network/network_content.h
blob: 610288c437df88f8ac9c62b5583a7d5b1dd6d2cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file network_content.h Part of the network protocol handling content distribution. */

#ifndef NETWORK_CONTENT_H
#define NETWORK_CONTENT_H

#if defined(ENABLE_NETWORK)

#include "core/tcp_content.h"
#include "../core/smallvec_type.hpp"

/** Vector with content info */
typedef SmallVector<ContentInfo *, 16> ContentVector;
typedef SmallVector<const ContentInfo *, 16> ConstContentVector;

/** Iterator for the content vector */
typedef ContentInfo **ContentIterator;
typedef const ContentInfo * const * ConstContentIterator;

/** Callbacks for notifying others about incoming data */
struct ContentCallback {
	/**
	 * Callback for when the connection has finished
	 * @param success whether the connection was made or that we failed to make it
	 */
	virtual void OnConnect(bool success) {}

	/**
	 * Callback for when the connection got disconnected.
	 */
	virtual void OnDisconnect() {}

	/**
	 * We received a content info.
	 * @param ci the content info
	 */
	virtual void OnReceiveContentInfo(const ContentInfo *ci) {}

	/**
	 * We have progress in the download of a file
	 * @param ci the content info of the file
	 * @param bytes the number of bytes downloaded since the previous call
	 */
	virtual void OnDownloadProgress(const ContentInfo *ci, uint bytes) {}

	/**
	 * We have finished downloading a file
	 * @param cid the ContentID of the downloaded file
	 */
	virtual void OnDownloadComplete(ContentID cid) {}

	/** Silentium */
	virtual ~ContentCallback() {}
};

/**
 * Socket handler for the content server connection
 */
class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback {
protected:
	SmallVector<ContentCallback *, 2> callbacks; ///< Callbacks to notify "the world"
	SmallVector<ContentID, 4> requested;         ///< ContentIDs we already requested (so we don't do it again)
	ContentVector infos;                         ///< All content info we received

	FILE *curFile;        ///< Currently downloaded file
	ContentInfo *curInfo; ///< Information about the currently downloaded file
	bool isConnecting;    ///< Whether we're connecting
	uint32 lastActivity;  ///< The last time there was network activity

	friend class NetworkContentConnecter;

	DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
	DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);

	ContentInfo *GetContent(ContentID cid);
	void DownloadContentInfo(ContentID cid);

	void OnConnect(bool success);
	void OnDisconnect();
	void OnReceiveContentInfo(const ContentInfo *ci);
	void OnDownloadProgress(const ContentInfo *ci, uint bytes);
	void OnDownloadComplete(ContentID cid);
public:
	/** The idle timeout; when to close the connection because it's idle. */
	static const int IDLE_TIMEOUT = 60 * 1000;

	ClientNetworkContentSocketHandler();
	~ClientNetworkContentSocketHandler();

	void Connect();
	void SendReceive();
	void Close();

	void RequestContentList(ContentType type);
	void RequestContentList(uint count, const ContentID *content_ids);
	void RequestContentList(ContentVector *cv, bool send_md5sum = true);

	void DownloadSelectedContent(uint &files, uint &bytes);

	void Select(ContentID cid);
	void Unselect(ContentID cid);
	void SelectAll();
	void SelectUpgrade();
	void UnselectAll();
	void ToggleSelectedState(const ContentInfo *ci);

	void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
	void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
	void CheckDependencyState(ContentInfo *ci);

	/** Get the number of content items we know locally. */
	uint Length() const { return this->infos.Length(); }
	/** Get the begin of the content inf iterator. */
	ConstContentIterator Begin() const { return this->infos.Begin(); }
	/** Get the nth position of the content inf iterator. */
	ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
	/** Get the end of the content inf iterator. */
	ConstContentIterator End() const { return this->infos.End(); }
	/** Clear all downloaded content information. */
	void Clear();

	/** Add a callback to this class */
	void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
	/** Remove a callback */
	void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
};

extern ClientNetworkContentSocketHandler _network_content_client;

void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type = CONTENT_TYPE_END);

#else
static inline void ShowNetworkContentListWindow() {}
#endif /* ENABLE_NETWORK */

#endif /* NETWORK_CONTENT_H */