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

OpenTTD Source 14.0-RC3

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 "core/tcp_content.h"
14 #include "core/http.h"
15 #include <unordered_map>
16 #include "../core/container_func.hpp"
17
19 typedef std::vector<ContentInfo *> ContentVector;
21 typedef std::vector<const ContentInfo *> ConstContentVector;
22
24 typedef ContentInfo **ContentIterator;
26 typedef const ContentInfo * const * ConstContentIterator;
27
29 struct ContentCallback {
34 virtual void OnConnect([[maybe_unused]] bool success) {}
35
39 virtual void OnDisconnect() {}
40
45 virtual void OnReceiveContentInfo([[maybe_unused]] const ContentInfo *ci) {}
46
52 virtual void OnDownloadProgress([[maybe_unused]] const ContentInfo *ci, [[maybe_unused]] int bytes) {}
53
58 virtual void OnDownloadComplete([[maybe_unused]] ContentID cid) {}
59
61 virtual ~ContentCallback() = default;
62 };
63
67 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
68 protected:
69 typedef std::vector<ContentID> ContentIDList;
70 std::vector<ContentCallback *> callbacks;
71 ContentIDList requested;
72 ContentVector infos;
73 std::unordered_multimap<ContentID, ContentID> reverse_dependency_map;
74 std::vector<char> http_response;
75 int http_response_index;
76
77 FILE *curFile;
78 ContentInfo *curInfo;
79 bool isConnecting;
80 bool isCancelled;
81 std::chrono::steady_clock::time_point lastActivity;
82
83 friend class NetworkContentConnecter;
84
85 bool Receive_SERVER_INFO(Packet &p) override;
86 bool Receive_SERVER_CONTENT(Packet &p) override;
87
88 ContentInfo *GetContent(ContentID cid) const;
89 void DownloadContentInfo(ContentID cid);
90
91 void OnConnect(bool success) override;
92 void OnDisconnect() override;
93 void OnReceiveContentInfo(const ContentInfo *ci) override;
94 void OnDownloadProgress(const ContentInfo *ci, int bytes) override;
95 void OnDownloadComplete(ContentID cid) override;
96
97 void OnFailure() override;
98 void OnReceiveData(std::unique_ptr<char[]> data, size_t length) override;
99 bool IsCancelled() const override;
100
101 bool BeforeDownload();
102 void AfterDownload();
103
104 void DownloadSelectedContentHTTP(const ContentIDList &content);
105 void DownloadSelectedContentFallback(const ContentIDList &content);
106 public:
108 static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60);
109
110 ClientNetworkContentSocketHandler();
111 ~ClientNetworkContentSocketHandler();
112
113 void Connect();
114 void SendReceive();
115 NetworkRecvStatus CloseConnection(bool error = true) override;
116 void Cancel();
117
118 void RequestContentList(ContentType type);
119 void RequestContentList(uint count, const ContentID *content_ids);
120 void RequestContentList(ContentVector *cv, bool send_md5sum = true);
121
122 void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
123
124 void Select(ContentID cid);
125 void Unselect(ContentID cid);
126 void SelectAll();
127 void SelectUpgrade();
128 void UnselectAll();
129 void ToggleSelectedState(const ContentInfo *ci);
130
131 void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
132 void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
133 void CheckDependencyState(ContentInfo *ci);
134
136 uint Length() const { return (uint)this->infos.size(); }
138 ConstContentIterator Begin() const { return this->infos.data(); }
140 ConstContentIterator Get(uint32_t index) const { return this->infos.data() + index; }
142 ConstContentIterator End() const { return this->Begin() + this->Length(); }
143
144 void Clear();
145
147 void AddCallback(ContentCallback *cb) { include(this->callbacks, cb); }
149 void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::find(this->callbacks.begin(), this->callbacks.end(), cb)); }
150 };
151
152 extern ClientNetworkContentSocketHandler _network_content_client;
153
154 void ShowNetworkContentListWindow(ContentVector *cv = nullptr, ContentType type1 = CONTENT_TYPE_END, ContentType type2 = CONTENT_TYPE_END);
155
156 void ShowMissingContentWindow(const struct GRFConfig *list);
157
158 #endif /* NETWORK_CONTENT_H */
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress([[maybe_unused]] const ContentInfo *ci, [[maybe_unused]] int bytes)
We have progress in the download of a file.
Definition: network_content.h:52
ContentCallback
Callbacks for notifying others about incoming data.
Definition: network_content.h:29
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo([[maybe_unused]] const ContentInfo *ci)
We received a content info.
Definition: network_content.h:45
ContentCallback::OnConnect
virtual void OnConnect([[maybe_unused]] bool success)
Callback for when the connection has finished.
Definition: network_content.h:34
NetworkContentSocketHandler
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:22
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:949
ClientNetworkContentSocketHandler::End
ConstContentIterator End() const
Get the end of the content inf iterator.
Definition: network_content.h:142
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:823
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition: http.h:20
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
Definition: network_content.cpp:605
ClientNetworkContentSocketHandler::Length
uint Length() const
Get the number of content items we know locally.
Definition: network_content.h:136
ClientNetworkContentSocketHandler::lastActivity
std::chrono::steady_clock::time_point lastActivity
The last time there was network activity.
Definition: network_content.h:81
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition: network_content.h:75
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:480
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition: network_content.cpp:35
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:1133
ContentVector
std::vector< ContentInfo * > ContentVector
Vector with content info.
Definition: network_content.h:19
ShowMissingContentWindow
void ShowMissingContentWindow(const GRFConfig *list)
Show the content list window with all missing grfs from the given list.
Definition: newgrf_gui.cpp:1570
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
Definition: network_content.cpp:985
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition: network_content.h:67
ClientNetworkContentSocketHandler::reverse_dependency_map
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
Definition: network_content.h:73
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content_type.h:30
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition: network_content.cpp:1073
ClientNetworkContentSocketHandler::isConnecting
bool isConnecting
Whether we're connecting.
Definition: network_content.h:79
ConstContentIterator
const typedef ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
Definition: network_content.h:26
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition: tcp_content_type.h:18
ClientNetworkContentSocketHandler::curInfo
ContentInfo * curInfo
Information about the currently downloaded file.
Definition: network_content.h:78
ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
Definition: network_content.cpp:339
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition: network_content.cpp:189
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
tcp_content.h
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition: network_content.cpp:1094
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:147
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition: network_content.h:72
ClientNetworkContentSocketHandler::DownloadContentInfo
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
Definition: network_content.cpp:847
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
Definition: network_content.cpp:748
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition: network_content.cpp:919
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:52
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition: network_content.cpp:887
ClientNetworkContentSocketHandler::isCancelled
bool isCancelled
Whether the download has been cancelled.
Definition: network_content.h:80
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete([[maybe_unused]] ContentID cid)
We have finished downloading a file.
Definition: network_content.h:58
ContentIterator
ContentInfo ** ContentIterator
Iterator for the content vector.
Definition: network_content.h:24
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_content.cpp:584
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:108
ClientNetworkContentSocketHandler::GetContent
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
Definition: network_content.cpp:861
ClientNetworkContentSocketHandler::curFile
FILE * curFile
Currently downloaded file.
Definition: network_content.h:77
ClientNetworkContentSocketHandler::RemoveCallback
void RemoveCallback(ContentCallback *cb)
Remove a callback.
Definition: network_content.h:149
Packet
Internal entity of a packet.
Definition: packet.h:42
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition: network_content.cpp:309
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition: network_content.h:74
ClientNetworkContentSocketHandler::Receive_SERVER_INFO
bool Receive_SERVER_INFO(Packet &p) override
Server sending list of content info: byte type (invalid ID == does not exist) uint32_t id uint32_t fi...
Definition: network_content.cpp:52
ClientNetworkContentSocketHandler::IsCancelled
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
Definition: network_content.cpp:578
ClientNetworkContentSocketHandler::ContentIDList
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
Definition: network_content.h:69
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:963
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition: network_content.cpp:785
ClientNetworkContentSocketHandler::ToggleSelectedState
void ToggleSelectedState(const ContentInfo *ci)
Toggle the state of a content info and check its dependencies.
Definition: network_content.cpp:927
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
ContentID
ContentID
Unique identifier for the content.
Definition: tcp_content_type.h:47
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition: network_content.h:70
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition: network_content.cpp:874
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
Definition: network_content.h:71
NetworkContentConnecter
Connect to the content server.
Definition: network_content.cpp:757
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition: network_content.cpp:355
ClientNetworkContentSocketHandler::AddCallback
void AddCallback(ContentCallback *cb)
Add a callback to this class.
Definition: network_content.h:147
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition: network_content.cpp:897
ClientNetworkContentSocketHandler::Cancel
void Cancel()
Cancel the current download.
Definition: network_content.cpp:813
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition: network_content.cpp:520
ClientNetworkContentSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition: network_content.cpp:798
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition: network_content.cpp:908
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition: network_content.cpp:545
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
Definition: network_content.cpp:736
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition: network_content.h:39
ConstContentVector
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
Definition: network_content.h:21
http.h
ClientNetworkContentSocketHandler::Begin
ConstContentIterator Begin() const
Get the begin of the content inf iterator.
Definition: network_content.h:138
ClientNetworkContentSocketHandler::Get
ConstContentIterator Get(uint32_t index) const
Get the nth position of the content inf iterator.
Definition: network_content.h:140
ContentCallback::~ContentCallback
virtual ~ContentCallback()=default
Silentium.