OpenTTD Source: src/network/network_content.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
20 using ContentVector = std::vector<std::unique_ptr<ContentInfo>>;
22 using ConstContentVector = std::vector<const ContentInfo *>;
23
25 struct ContentCallback {
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
63 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
64protected:
65 using ContentIDList = std::vector<ContentID>;
66 std::vector<ContentCallback *> callbacks;
67 ContentIDList requested;
68 ContentIDList queued;
69 ContentVector infos;
70 std::unordered_multimap<ContentID, ContentID> reverse_dependency_map;
71 std::vector<char> http_response;
72 int http_response_index = -2;
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
85 ContentInfo *GetContent(ContentID cid) const;
86 void DownloadContentInfo(ContentID cid);
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);
102 void DownloadSelectedContentFallback(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
112 void RequestContentList(ContentType type);
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);
117 void RequestQueuedContentInfo();
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
144extern ClientNetworkContentSocketHandler _network_content_client;
145
146void ShowNetworkContentListWindow(ContentVector *cv = nullptr, ContentType type1 = CONTENT_TYPE_END, ContentType type2 = CONTENT_TYPE_END);
147
148void ShowMissingContentWindow(const GRFConfigList &list);
149
150#endif /* NETWORK_CONTENT_H */
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition network_content.h:63
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition network_content.cpp:879
ClientNetworkContentSocketHandler::OnConnect
void OnConnect(bool success) override
Callback for when the connection has finished.
Definition network_content.cpp:1054
ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
Definition network_content.cpp:322
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition network_content.cpp:292
ClientNetworkContentSocketHandler::RemoveCallback
void RemoveCallback(ContentCallback *cb)
Remove a callback.
Definition network_content.h:141
ClientNetworkContentSocketHandler::ToggleSelectedState
void ToggleSelectedState(const ContentInfo &ci)
Toggle the state of a content info and check its dependencies.
Definition network_content.cpp:898
ClientNetworkContentSocketHandler::IDLE_TIMEOUT
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
Definition network_content.h:105
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
Definition network_content.cpp:579
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition network_content.cpp:845
ClientNetworkContentSocketHandler::OnReceiveContentInfo
void OnReceiveContentInfo(const ContentInfo &ci) override
We received a content info.
Definition network_content.cpp:1073
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition network_content.h:72
ClientNetworkContentSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition network_content.cpp:736
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
Definition network_content.h:67
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition network_content.cpp:890
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition network_content.cpp:175
ClientNetworkContentSocketHandler::is_connecting
bool is_connecting
Whether we're connecting.
Definition network_content.h:76
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition network_content.h:71
ClientNetworkContentSocketHandler::ReverseLookupTreeDependency
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
Reverse lookup the dependencies of all parents over a given child.
Definition network_content.cpp:934
ClientNetworkContentSocketHandler::queued
ContentIDList queued
ContentID queue to be requested.
Definition network_content.h:68
ClientNetworkContentSocketHandler::OnDownloadProgress
void OnDownloadProgress(const ContentInfo &ci, int bytes) override
We have progress in the download of a file.
Definition network_content.cpp:1083
ClientNetworkContentSocketHandler::DownloadContentInfo
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
Definition network_content.cpp:792
ClientNetworkContentSocketHandler::Receive_SERVER_INFO
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...
Definition network_content.cpp:86
ClientNetworkContentSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the content server and when that's the case hand...
Definition network_content.cpp:761
ClientNetworkContentSocketHandler::OnDownloadComplete
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
Definition network_content.cpp:1092
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition network_content.h:69
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition network_content.cpp:559
ClientNetworkContentSocketHandler::ContentIDList
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
Definition network_content.h:65
ClientNetworkContentSocketHandler::GetContent
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
Definition network_content.cpp:832
ClientNetworkContentSocketHandler::AddCallback
void AddCallback(ContentCallback *cb)
Add a callback to this class.
Definition network_content.h:139
ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT
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...
Definition network_content.cpp:450
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition network_content.cpp:723
ClientNetworkContentSocketHandler::reverse_dependency_map
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
Definition network_content.h:70
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition network_content.cpp:494
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(const ContentInfo &ci)
Check the dependencies (recursively) of this content info.
Definition network_content.cpp:956
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition network_content.cpp:1044
ClientNetworkContentSocketHandler::RequestQueuedContentInfo
void RequestQueuedContentInfo()
Send a content request for queued content info download.
Definition network_content.cpp:805
ClientNetworkContentSocketHandler::cur_file
std::optional< FileHandle > cur_file
Currently downloaded file.
Definition network_content.h:74
ClientNetworkContentSocketHandler::last_activity
std::chrono::steady_clock::time_point last_activity
The last time there was network activity.
Definition network_content.h:78
ClientNetworkContentSocketHandler::Info
auto Info() const
Get a read-only view of content info for iterating externally.
Definition network_content.h:134
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition network_content.cpp:521
ClientNetworkContentSocketHandler::ReverseLookupDependency
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo &child) const
Reverse lookup the dependencies of (direct) parents over a given child.
Definition network_content.cpp:920
ClientNetworkContentSocketHandler::IsCancelled
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
Definition network_content.cpp:553
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition network_content.h:66
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition network_content.cpp:338
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition network_content.cpp:858
ClientNetworkContentSocketHandler::Cancel
void Cancel()
Cancel the current download.
Definition network_content.cpp:751
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition network_content.cpp:1064
ClientNetworkContentSocketHandler::cur_info
std::unique_ptr< ContentInfo > cur_info
Information about the currently downloaded file.
Definition network_content.h:75
ClientNetworkContentSocketHandler::is_cancelled
bool is_cancelled
Whether the download has been cancelled.
Definition network_content.h:77
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition network_content.cpp:868
NetworkContentConnecter
Connect to the content server.
Definition network_content.cpp:695
NetworkContentSocketHandler
Base socket handler for all Content TCP sockets.
Definition tcp_content.h:22
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition container_func.hpp:24
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:23
http.h
Basic functions to send and receive HTTP packets.
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition network_content.cpp:47
ShowNetworkContentListWindow
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.
Definition network_content_gui.cpp:1117
ContentVector
std::vector< std::unique_ptr< ContentInfo > > ContentVector
Vector with content info.
Definition network_content.h:20
ConstContentVector
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
Definition network_content.h:22
ShowMissingContentWindow
void ShowMissingContentWindow(const GRFConfigList &list)
Show the content list window with all missing grfs from the given list.
Definition newgrf_gui.cpp:1505
ContentCallback
Callbacks for notifying others about incoming data.
Definition network_content.h:25
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress(const ContentInfo &ci, int bytes)
We have progress in the download of a file.
Definition network_content.h:48
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition network_content.h:35
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo(const ContentInfo &ci)
We received a content info.
Definition network_content.h:41
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
Definition network_content.h:54
ContentCallback::OnConnect
virtual void OnConnect(bool success)
Callback for when the connection has finished.
Definition network_content.h:30
ContentCallback::~ContentCallback
virtual ~ContentCallback()=default
Silentium.
ContentInfo
Container for all important information about a piece of content.
Definition tcp_content_type.h:56
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition http.h:20
Packet
Internal entity of a packet.
Definition packet.h:43
tcp_content.h
Basic functions to receive and send TCP packets to/from the content server.
ContentID
uint32_t ContentID
Unique identifier for the content.
Definition tcp_content_type.h:51
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition tcp_content_type.h:21
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition tcp_content_type.h:33