OpenTTD Source 20250613-master-ga1786fa1f4
network_content.h
Go to the documentation of this file.
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 */
7
10#ifndef NETWORK_CONTENT_H
11#define NETWORK_CONTENT_H
12
13#include <ranges>
14#include "core/tcp_content.h"
15#include "core/http.h"
16#include <unordered_map>
17#include "../core/container_func.hpp"
18
20using ContentVector = std::vector<std::unique_ptr<ContentInfo>>;
22using ConstContentVector = std::vector<const ContentInfo *>;
23
30 virtual void OnConnect([[maybe_unused]] bool success) {}
31
35 virtual void OnDisconnect() {}
36
41 virtual void OnReceiveContentInfo([[maybe_unused]] const ContentInfo &ci) {}
42
48 virtual void OnDownloadProgress([[maybe_unused]] const ContentInfo &ci, [[maybe_unused]] int bytes) {}
49
54 virtual void OnDownloadComplete([[maybe_unused]] ContentID cid) {}
55
57 virtual ~ContentCallback() = default;
58};
59
64protected:
65 using ContentIDList = std::vector<ContentID>;
66 std::vector<ContentCallback *> callbacks;
70 std::unordered_multimap<ContentID, ContentID> reverse_dependency_map;
71 std::vector<char> http_response;
73
74 std::optional<FileHandle> cur_file;
75 std::unique_ptr<ContentInfo> cur_info;
76 bool is_connecting = false;
77 bool is_cancelled = false;
78 std::chrono::steady_clock::time_point last_activity = std::chrono::steady_clock::now();
79
80 friend class NetworkContentConnecter;
81
82 bool Receive_SERVER_INFO(Packet &p) override;
83 bool Receive_SERVER_CONTENT(Packet &p) override;
84
87
88 void OnConnect(bool success) override;
89 void OnDisconnect() override;
90 void OnReceiveContentInfo(const ContentInfo &ci) override;
91 void OnDownloadProgress(const ContentInfo &ci, int bytes) override;
92 void OnDownloadComplete(ContentID cid) override;
93
94 void OnFailure() override;
95 void OnReceiveData(std::unique_ptr<char[]> data, size_t length) override;
96 bool IsCancelled() const override;
97
98 bool BeforeDownload();
99 void AfterDownload();
100
101 void DownloadSelectedContentHTTP(const ContentIDList &content);
103public:
105 static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60);
106
107 void Connect();
108 void SendReceive();
109 NetworkRecvStatus CloseConnection(bool error = true) override;
110 void Cancel();
111
113 void RequestContentList(std::span<const ContentID> content_ids);
114 void RequestContentList(ContentVector *cv, bool send_md5sum = true);
115
116 void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
118
119 void Select(ContentID cid);
120 void Unselect(ContentID cid);
121 void SelectAll();
122 void SelectUpgrade();
123 void UnselectAll();
124 void ToggleSelectedState(const ContentInfo &ci);
125
126 void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo &child) const;
127 void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
128 void CheckDependencyState(const ContentInfo &ci);
129
134 auto Info() const { return this->infos | std::views::transform([](const auto &ci) -> const ContentInfo & { return *ci; }); }
135
136 void Clear();
137
139 void AddCallback(ContentCallback *cb) { include(this->callbacks, cb); }
141 void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::ranges::find(this->callbacks, cb)); }
142};
143
145
147
148void ShowMissingContentWindow(const GRFConfigList &list);
149
150#endif /* NETWORK_CONTENT_H */
Socket handler for the content server connection.
void SelectUpgrade()
Select everything that's an update for something we've got.
void OnConnect(bool success) override
Callback for when the connection has finished.
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
void RemoveCallback(ContentCallback *cb)
Remove a callback.
void ToggleSelectedState(const ContentInfo &ci)
Toggle the state of a content info and check its dependencies.
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
void Select(ContentID cid)
Select a specific content id.
void OnReceiveContentInfo(const ContentInfo &ci) override
We received a content info.
int http_response_index
Where we are, in the response, with handling it.
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
void UnselectAll()
Unselect everything that we've not downloaded so far.
void RequestContentList(ContentType type)
Request the content list for the given type.
bool is_connecting
Whether we're connecting.
std::vector< char > http_response
The HTTP response to the requests we've been doing.
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
Reverse lookup the dependencies of all parents over a given child.
ContentIDList queued
ContentID queue to be requested.
void OnDownloadProgress(const ContentInfo &ci, int bytes) override
We have progress in the download of a file.
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
bool Receive_SERVER_INFO(Packet &p) override
Server sending list of content info: uint8_t type (invalid ID == does not exist) uint32_t id uint32_t...
void SendReceive()
Check whether we received/can send some data from/to the content server and when that's the case hand...
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
ContentVector infos
All content info we received.
void OnFailure() override
An error has occurred and the connection has been closed.
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
void AddCallback(ContentCallback *cb)
Add a callback to this class.
bool Receive_SERVER_CONTENT(Packet &p) override
Server sending list of content info: uint32_t unique id uint32_t file size (0 == does not exist) stri...
void Connect()
Connect with the content server.
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
bool BeforeDownload()
Handle the opening of the file before downloading.
void CheckDependencyState(const ContentInfo &ci)
Check the dependencies (recursively) of this content info.
void Clear()
Clear all downloaded content information.
void RequestQueuedContentInfo()
Send a content request for queued content info download.
std::optional< FileHandle > cur_file
Currently downloaded file.
std::chrono::steady_clock::time_point last_activity
The last time there was network activity.
auto Info() const
Get a read-only view of content info for iterating externally.
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo &child) const
Reverse lookup the dependencies of (direct) parents over a given child.
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
void Unselect(ContentID cid)
Unselect a specific content id.
void Cancel()
Cancel the current download.
void OnDisconnect() override
Callback for when the connection got disconnected.
std::unique_ptr< ContentInfo > cur_info
Information about the currently downloaded file.
bool is_cancelled
Whether the download has been cancelled.
void SelectAll()
Select everything we can select.
Connect to the content server.
Base socket handler for all Content TCP sockets.
Definition tcp_content.h:22
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:23
Basic functions to send and receive HTTP packets.
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
void ShowNetworkContentListWindow(ContentVector *cv=nullptr, ContentType type1=CONTENT_TYPE_END, ContentType type2=CONTENT_TYPE_END)
Show the content list window with a given set of content.
std::vector< std::unique_ptr< ContentInfo > > ContentVector
Vector with content info.
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
void ShowMissingContentWindow(const GRFConfigList &list)
Show the content list window with all missing grfs from the given list.
Callbacks for notifying others about incoming data.
virtual void OnDownloadProgress(const ContentInfo &ci, int bytes)
We have progress in the download of a file.
virtual void OnDisconnect()
Callback for when the connection got disconnected.
virtual void OnReceiveContentInfo(const ContentInfo &ci)
We received a content info.
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
virtual void OnConnect(bool success)
Callback for when the connection has finished.
virtual ~ContentCallback()=default
Silentium.
Container for all important information about a piece of content.
Callback for when the HTTP handler has something to tell us.
Definition http.h:20
Internal entity of a packet.
Definition packet.h:43
Basic functions to receive and send TCP packets to/from the content server.
uint32_t ContentID
Unique identifier for the content.
ContentType
The values in the enum are important; they are used as database 'keys'.
@ CONTENT_TYPE_END
Helper to mark the end of the types.