Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / network / network_content.h
blobb74308a100cacb19272a0c87dd852d9cf4d6ca9c
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file network_content.h Part of the network protocol handling content distribution. */
10 #ifndef NETWORK_CONTENT_H
11 #define NETWORK_CONTENT_H
13 #include "core/tcp_content.h"
14 #include "core/tcp_http.h"
16 /** Vector with content info */
17 typedef std::vector<ContentInfo *> ContentVector;
18 /** Vector with constant content info */
19 typedef std::vector<const ContentInfo *> ConstContentVector;
21 /** Iterator for the content vector */
22 typedef ContentInfo **ContentIterator;
23 /** Iterator for the constant content vector */
24 typedef const ContentInfo * const * ConstContentIterator;
26 /** Callbacks for notifying others about incoming data */
27 struct ContentCallback {
28 /**
29 * Callback for when the connection has finished
30 * @param success whether the connection was made or that we failed to make it
32 virtual void OnConnect(bool success) {}
34 /**
35 * Callback for when the connection got disconnected.
37 virtual void OnDisconnect() {}
39 /**
40 * We received a content info.
41 * @param ci the content info
43 virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
45 /**
46 * We have progress in the download of a file
47 * @param ci the content info of the file
48 * @param bytes the number of bytes downloaded since the previous call
50 virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
52 /**
53 * We have finished downloading a file
54 * @param cid the ContentID of the downloaded file
56 virtual void OnDownloadComplete(ContentID cid) {}
58 /** Silentium */
59 virtual ~ContentCallback() {}
62 /**
63 * Socket handler for the content server connection
65 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
66 protected:
67 typedef std::vector<ContentID> ContentIDList; ///< List of content IDs to (possibly) select.
68 std::vector<ContentCallback *> callbacks; ///< Callbacks to notify "the world"
69 ContentIDList requested; ///< ContentIDs we already requested (so we don't do it again)
70 ContentVector infos; ///< All content info we received
71 std::vector<char> http_response; ///< The HTTP response to the requests we've been doing
72 int http_response_index; ///< Where we are, in the response, with handling it
74 FILE *curFile; ///< Currently downloaded file
75 ContentInfo *curInfo; ///< Information about the currently downloaded file
76 bool isConnecting; ///< Whether we're connecting
77 std::chrono::steady_clock::time_point lastActivity; ///< The last time there was network activity
79 friend class NetworkContentConnecter;
81 bool Receive_SERVER_INFO(Packet *p) override;
82 bool Receive_SERVER_CONTENT(Packet *p) override;
84 ContentInfo *GetContent(ContentID cid);
85 void DownloadContentInfo(ContentID cid);
87 void OnConnect(bool success) override;
88 void OnDisconnect() override;
89 void OnReceiveContentInfo(const ContentInfo *ci) override;
90 void OnDownloadProgress(const ContentInfo *ci, int bytes) override;
91 void OnDownloadComplete(ContentID cid) override;
93 void OnFailure() override;
94 void OnReceiveData(const char *data, size_t length) override;
96 bool BeforeDownload();
97 void AfterDownload();
99 void DownloadSelectedContentHTTP(const ContentIDList &content);
100 void DownloadSelectedContentFallback(const ContentIDList &content);
101 public:
102 /** The idle timeout; when to close the connection because it's idle. */
103 static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60);
105 ClientNetworkContentSocketHandler();
106 ~ClientNetworkContentSocketHandler();
108 void Connect();
109 void SendReceive();
110 NetworkRecvStatus CloseConnection(bool error = true) override;
112 void RequestContentList(ContentType type);
113 void RequestContentList(uint count, const ContentID *content_ids);
114 void RequestContentList(ContentVector *cv, bool send_md5sum = true);
116 void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
118 void Select(ContentID cid);
119 void Unselect(ContentID cid);
120 void SelectAll();
121 void SelectUpgrade();
122 void UnselectAll();
123 void ToggleSelectedState(const ContentInfo *ci);
125 void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
126 void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
127 void CheckDependencyState(ContentInfo *ci);
129 /** Get the number of content items we know locally. */
130 uint Length() const { return (uint)this->infos.size(); }
131 /** Get the begin of the content inf iterator. */
132 ConstContentIterator Begin() const { return this->infos.data(); }
133 /** Get the nth position of the content inf iterator. */
134 ConstContentIterator Get(uint32 index) const { return this->infos.data() + index; }
135 /** Get the end of the content inf iterator. */
136 ConstContentIterator End() const { return this->Begin() + this->Length(); }
138 void Clear();
140 /** Add a callback to this class */
141 void AddCallback(ContentCallback *cb) { include(this->callbacks, cb); }
142 /** Remove a callback */
143 void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::find(this->callbacks.begin(), this->callbacks.end(), cb)); }
146 extern ClientNetworkContentSocketHandler _network_content_client;
148 void ShowNetworkContentListWindow(ContentVector *cv = nullptr, ContentType type1 = CONTENT_TYPE_END, ContentType type2 = CONTENT_TYPE_END);
150 void ShowMissingContentWindow(const struct GRFConfig *list);
152 #endif /* NETWORK_CONTENT_H */