4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file network_content.h Part of the network protocol handling content distribution. */
12 #ifndef NETWORK_CONTENT_H
13 #define NETWORK_CONTENT_H
15 #include "core/tcp_content.h"
16 #include "core/tcp_http.h"
18 #if defined(ENABLE_NETWORK)
20 /** Vector with content info */
21 typedef SmallVector
<ContentInfo
*, 16> ContentVector
;
22 /** Vector with constant content info */
23 typedef SmallVector
<const ContentInfo
*, 16> ConstContentVector
;
25 /** Iterator for the content vector */
26 typedef ContentInfo
**ContentIterator
;
27 /** Iterator for the constant content vector */
28 typedef const ContentInfo
* const * ConstContentIterator
;
30 /** Callbacks for notifying others about incoming data */
31 struct ContentCallback
{
33 * Callback for when the connection has finished
34 * @param success whether the connection was made or that we failed to make it
36 virtual void OnConnect(bool success
) {}
39 * Callback for when the connection got disconnected.
41 virtual void OnDisconnect() {}
44 * We received a content info.
45 * @param ci the content info
47 virtual void OnReceiveContentInfo(const ContentInfo
*ci
) {}
50 * We have progress in the download of a file
51 * @param ci the content info of the file
52 * @param bytes the number of bytes downloaded since the previous call
54 virtual void OnDownloadProgress(const ContentInfo
*ci
, int bytes
) {}
57 * We have finished downloading a file
58 * @param cid the ContentID of the downloaded file
60 virtual void OnDownloadComplete(ContentID cid
) {}
63 virtual ~ContentCallback() {}
67 * Socket handler for the content server connection
69 class ClientNetworkContentSocketHandler
: public NetworkContentSocketHandler
, ContentCallback
, HTTPCallback
{
71 typedef SmallVector
<ContentID
, 4> ContentIDList
; ///< List of content IDs to (possibly) select.
72 SmallVector
<ContentCallback
*, 2> callbacks
; ///< Callbacks to notify "the world"
73 ContentIDList requested
; ///< ContentIDs we already requested (so we don't do it again)
74 ContentVector infos
; ///< All content info we received
75 SmallVector
<char, 1024> http_response
; ///< The HTTP response to the requests we've been doing
76 int http_response_index
; ///< Where we are, in the response, with handling it
78 FILE *curFile
; ///< Currently downloaded file
79 ContentInfo
*curInfo
; ///< Information about the currently downloaded file
80 bool isConnecting
; ///< Whether we're connecting
81 uint32 lastActivity
; ///< The last time there was network activity
83 friend class NetworkContentConnecter
;
85 virtual bool Receive_SERVER_INFO(Packet
*p
);
86 virtual bool Receive_SERVER_CONTENT(Packet
*p
);
88 ContentInfo
*GetContent(ContentID cid
);
89 void DownloadContentInfo(ContentID cid
);
91 void OnConnect(bool success
);
93 void OnReceiveContentInfo(const ContentInfo
*ci
);
94 void OnDownloadProgress(const ContentInfo
*ci
, int bytes
);
95 void OnDownloadComplete(ContentID cid
);
98 void OnReceiveData(const char *data
, size_t length
);
100 bool BeforeDownload();
101 void AfterDownload();
103 void DownloadSelectedContentHTTP(const ContentIDList
&content
);
104 void DownloadSelectedContentFallback(const ContentIDList
&content
);
106 /** The idle timeout; when to close the connection because it's idle. */
107 static const int IDLE_TIMEOUT
= 60 * 1000;
109 ClientNetworkContentSocketHandler();
110 ~ClientNetworkContentSocketHandler();
116 void RequestContentList(ContentType type
);
117 void RequestContentList(uint count
, const ContentID
*content_ids
);
118 void RequestContentList(ContentVector
*cv
, bool send_md5sum
= true);
120 void DownloadSelectedContent(uint
&files
, uint
&bytes
, bool fallback
= false);
122 void Select(ContentID cid
);
123 void Unselect(ContentID cid
);
125 void SelectUpgrade();
127 void ToggleSelectedState(const ContentInfo
*ci
);
129 void ReverseLookupDependency(ConstContentVector
&parents
, const ContentInfo
*child
) const;
130 void ReverseLookupTreeDependency(ConstContentVector
&tree
, const ContentInfo
*child
) const;
131 void CheckDependencyState(ContentInfo
*ci
);
133 /** Get the number of content items we know locally. */
134 uint
Length() const { return this->infos
.Length(); }
135 /** Get the begin of the content inf iterator. */
136 ConstContentIterator
Begin() const { return this->infos
.Begin(); }
137 /** Get the nth position of the content inf iterator. */
138 ConstContentIterator
Get(uint32 index
) const { return this->infos
.Get(index
); }
139 /** Get the end of the content inf iterator. */
140 ConstContentIterator
End() const { return this->infos
.End(); }
144 /** Add a callback to this class */
145 void AddCallback(ContentCallback
*cb
) { this->callbacks
.Include(cb
); }
146 /** Remove a callback */
147 void RemoveCallback(ContentCallback
*cb
) { this->callbacks
.Erase(this->callbacks
.Find(cb
)); }
150 extern ClientNetworkContentSocketHandler _network_content_client
;
152 void ShowNetworkContentListWindow(ContentVector
*cv
= NULL
, ContentType type
= CONTENT_TYPE_END
);
154 void ShowMissingContentWindow(const struct GRFConfig
*list
);
157 static inline void ShowNetworkContentListWindow() {}
158 #endif /* ENABLE_NETWORK */
160 #endif /* NETWORK_CONTENT_H */