OpenTTD Source  13.2.1
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/tcp_http.h"
15 #include <unordered_map>
16 
18 typedef std::vector<ContentInfo *> ContentVector;
20 typedef std::vector<const ContentInfo *> ConstContentVector;
21 
25 typedef const ContentInfo * const * ConstContentIterator;
26 
33  virtual void OnConnect(bool success) {}
34 
38  virtual void OnDisconnect() {}
39 
44  virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
45 
51  virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
52 
57  virtual void OnDownloadComplete(ContentID cid) {}
58 
60  virtual ~ContentCallback() {}
61 };
62 
67 protected:
68  typedef std::vector<ContentID> ContentIDList;
69  std::vector<ContentCallback *> callbacks;
72  std::unordered_multimap<ContentID, ContentID> reverse_dependency_map;
73  std::vector<char> http_response;
75 
76  FILE *curFile;
78  bool isConnecting;
79  std::chrono::steady_clock::time_point lastActivity;
80 
81  friend class NetworkContentConnecter;
82 
83  bool Receive_SERVER_INFO(Packet *p) override;
84  bool Receive_SERVER_CONTENT(Packet *p) override;
85 
86  ContentInfo *GetContent(ContentID cid) const;
88 
89  void OnConnect(bool success) override;
90  void OnDisconnect() override;
91  void OnReceiveContentInfo(const ContentInfo *ci) override;
92  void OnDownloadProgress(const ContentInfo *ci, int bytes) override;
93  void OnDownloadComplete(ContentID cid) override;
94 
95  void OnFailure() override;
96  void OnReceiveData(const char *data, size_t length) override;
97 
98  bool BeforeDownload();
99  void AfterDownload();
100 
101  void DownloadSelectedContentHTTP(const ContentIDList &content);
102  void DownloadSelectedContentFallback(const ContentIDList &content);
103 public:
105  static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60);
106 
109 
110  void Connect();
111  void SendReceive();
112  NetworkRecvStatus CloseConnection(bool error = true) override;
113 
114  void RequestContentList(ContentType type);
115  void RequestContentList(uint count, const ContentID *content_ids);
116  void RequestContentList(ContentVector *cv, bool send_md5sum = true);
117 
118  void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
119 
120  void Select(ContentID cid);
121  void Unselect(ContentID cid);
122  void SelectAll();
123  void SelectUpgrade();
124  void UnselectAll();
125  void ToggleSelectedState(const ContentInfo *ci);
126 
127  void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
128  void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
130 
132  uint Length() const { return (uint)this->infos.size(); }
134  ConstContentIterator Begin() const { return this->infos.data(); }
136  ConstContentIterator Get(uint32 index) const { return this->infos.data() + index; }
138  ConstContentIterator End() const { return this->Begin() + this->Length(); }
139 
140  void Clear();
141 
143  void AddCallback(ContentCallback *cb) { include(this->callbacks, cb); }
145  void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::find(this->callbacks.begin(), this->callbacks.end(), cb)); }
146 };
147 
149 
151 
152 void ShowMissingContentWindow(const struct GRFConfig *list);
153 
154 #endif /* NETWORK_CONTENT_H */
ContentCallback
Callbacks for notifying others about incoming data.
Definition: network_content.h:28
ClientNetworkContentSocketHandler::OnConnect
void OnConnect(bool success) override
Callback for when the connection has finished.
Definition: network_content.cpp:1078
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:943
ClientNetworkContentSocketHandler::End
ConstContentIterator End() const
Get the end of the content inf iterator.
Definition: network_content.h:138
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:818
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition: tcp_http.h:18
tcp_http.h
ClientNetworkContentSocketHandler::Length
uint Length() const
Get the number of content items we know locally.
Definition: network_content.h:132
ClientNetworkContentSocketHandler::lastActivity
std::chrono::steady_clock::time_point lastActivity
The last time there was network activity.
Definition: network_content.h:79
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition: network_content.h:74
_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:1126
ContentCallback::OnConnect
virtual void OnConnect(bool success)
Callback for when the connection has finished.
Definition: network_content.h:33
ContentVector
std::vector< ContentInfo * > ContentVector
Vector with content info.
Definition: network_content.h:18
ShowMissingContentWindow
void ShowMissingContentWindow(const GRFConfig *list)
Show the content list window with all missing grfs from the given list.
Definition: newgrf_gui.cpp:1547
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
Definition: network_content.cpp:979
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition: network_content.h:66
ClientNetworkContentSocketHandler::reverse_dependency_map
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
Definition: network_content.h:72
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
Definition: network_content.h:57
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content_type.h:28
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition: network_content.cpp:1067
ClientNetworkContentSocketHandler::isConnecting
bool isConnecting
Whether we're connecting.
Definition: network_content.h:78
ConstContentIterator
const typedef ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
Definition: network_content.h:25
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition: tcp_content_type.h:16
ClientNetworkContentSocketHandler::curInfo
ContentInfo * curInfo
Information about the currently downloaded file.
Definition: network_content.h:77
ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
Definition: network_content.cpp:345
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition: network_content.cpp:190
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
Definition: network_content.h:44
tcp_content.h
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition: network_content.cpp:1088
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:155
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition: network_content.h:71
ClientNetworkContentSocketHandler::DownloadContentInfo
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
Definition: network_content.cpp:841
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
Definition: network_content.cpp:755
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition: network_content.cpp:913
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:50
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(const char *data, size_t length) override
We're receiving data.
Definition: network_content.cpp:616
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition: network_content.cpp:881
ContentIterator
ContentInfo ** ContentIterator
Iterator for the content vector.
Definition: network_content.h:23
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_content.cpp:596
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::GetContent
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
Definition: network_content.cpp:855
ClientNetworkContentSocketHandler::curFile
FILE * curFile
Currently downloaded file.
Definition: network_content.h:76
ClientNetworkContentSocketHandler::OnDownloadComplete
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
Definition: network_content.cpp:1116
ClientNetworkContentSocketHandler::OnDownloadProgress
void OnDownloadProgress(const ContentInfo *ci, int bytes) override
We have progress in the download of a file.
Definition: network_content.cpp:1107
ClientNetworkContentSocketHandler::RemoveCallback
void RemoveCallback(ContentCallback *cb)
Remove a callback.
Definition: network_content.h:145
Packet
Internal entity of a packet.
Definition: packet.h:44
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition: network_content.cpp:310
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition: network_content.h:73
ClientNetworkContentSocketHandler::ContentIDList
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
Definition: network_content.h:68
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:957
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition: network_content.cpp:792
ClientNetworkContentSocketHandler::ToggleSelectedState
void ToggleSelectedState(const ContentInfo *ci)
Toggle the state of a content info and check its dependencies.
Definition: network_content.cpp:921
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:45
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition: network_content.h:69
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition: network_content.cpp:868
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
Definition: network_content.h:70
NetworkContentConnecter
Connect to the content server.
Definition: network_content.cpp:764
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition: network_content.cpp:372
ClientNetworkContentSocketHandler::AddCallback
void AddCallback(ContentCallback *cb)
Add a callback to this class.
Definition: network_content.h:143
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
ClientNetworkContentSocketHandler::OnReceiveContentInfo
void OnReceiveContentInfo(const ContentInfo *ci) override
We received a content info.
Definition: network_content.cpp:1097
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition: network_content.cpp:891
ClientNetworkContentSocketHandler::Get
ConstContentIterator Get(uint32 index) const
Get the nth position of the content inf iterator.
Definition: network_content.h:136
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition: network_content.cpp:537
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 id uint32 file_s...
Definition: network_content.cpp:52
ClientNetworkContentSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition: network_content.cpp:802
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition: network_content.cpp:902
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition: network_content.cpp:562
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
Definition: network_content.cpp:744
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition: network_content.h:38
ContentCallback::~ContentCallback
virtual ~ContentCallback()
Silentium.
Definition: network_content.h:60
ConstContentVector
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
Definition: network_content.h:20
ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT
bool Receive_SERVER_CONTENT(Packet *p) override
Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string f...
Definition: network_content.cpp:497
ClientNetworkContentSocketHandler::Begin
ConstContentIterator Begin() const
Get the begin of the content inf iterator.
Definition: network_content.h:134
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
Definition: network_content.h:51