OpenTTD
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 
17 typedef std::vector<ContentInfo *> ContentVector;
19 typedef std::vector<const ContentInfo *> ConstContentVector;
20 
24 typedef const ContentInfo * const * ConstContentIterator;
25 
32  virtual void OnConnect(bool success) {}
33 
37  virtual void OnDisconnect() {}
38 
43  virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
44 
50  virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
51 
56  virtual void OnDownloadComplete(ContentID cid) {}
57 
59  virtual ~ContentCallback() {}
60 };
61 
66 protected:
67  typedef std::vector<ContentID> ContentIDList;
68  std::vector<ContentCallback *> callbacks;
69  ContentIDList requested;
71  std::vector<char> http_response;
73 
74  FILE *curFile;
76  bool isConnecting;
77  uint32 lastActivity;
78 
79  friend class NetworkContentConnecter;
80 
81  bool Receive_SERVER_INFO(Packet *p) override;
82  bool Receive_SERVER_CONTENT(Packet *p) override;
83 
84  ContentInfo *GetContent(ContentID cid);
85  void DownloadContentInfo(ContentID cid);
86 
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;
92 
93  void OnFailure() override;
94  void OnReceiveData(const char *data, size_t length) override;
95 
96  bool BeforeDownload();
97  void AfterDownload();
98 
99  void DownloadSelectedContentHTTP(const ContentIDList &content);
100  void DownloadSelectedContentFallback(const ContentIDList &content);
101 public:
103  static const int IDLE_TIMEOUT = 60 * 1000;
104 
107 
108  void Connect();
109  void SendReceive();
110  void Close() override;
111 
112  void RequestContentList(ContentType type);
113  void RequestContentList(uint count, 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 
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);
124 
125  void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
126  void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
127  void CheckDependencyState(ContentInfo *ci);
128 
130  uint Length() const { return (uint)this->infos.size(); }
132  ConstContentIterator Begin() const { return this->infos.data(); }
134  ConstContentIterator Get(uint32 index) const { return this->infos.data() + index; }
136  ConstContentIterator End() const { return this->Begin() + this->Length(); }
137 
138  void Clear();
139 
141  void AddCallback(ContentCallback *cb) { include(this->callbacks, cb); }
143  void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::find(this->callbacks.begin(), this->callbacks.end(), cb)); }
144 };
145 
147 
149 
150 void ShowMissingContentWindow(const struct GRFConfig *list);
151 
152 #endif /* NETWORK_CONTENT_H */
Helper to mark the end of the types.
Definition: tcp_content.h:33
ContentVector infos
All content info we received.
const ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
ConstContentIterator Begin() const
Get the begin of the content inf iterator.
Basic functions to receive and send HTTP TCP packets.
Internal entity of a packet.
Definition: packet.h:40
std::vector< char > http_response
The HTTP response to the requests we&#39;ve been doing.
Socket handler for the content server connection.
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:96
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Callbacks for notifying others about incoming data.
ContentID
Unique identifier for the content.
Definition: tcp_content.h:49
bool isConnecting
Whether we&#39;re connecting.
void ShowMissingContentWindow(const GRFConfig *list)
Show the content list window with all missing grfs from the given list.
virtual void OnDisconnect()
Callback for when the connection got disconnected.
void AddCallback(ContentCallback *cb)
Add a callback to this class.
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
ConstContentIterator End() const
Get the end of the content inf iterator.
std::vector< ContentInfo * > ContentVector
Vector with content info.
Information about GRF, used in the game and (part of it) in savegames.
uint32 lastActivity
The last time there was network activity.
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
ConstContentIterator Get(uint32 index) const
Get the nth position of the content inf iterator.
virtual ~ContentCallback()
Silentium.
ContentIDList requested
ContentIDs we already requested (so we don&#39;t do it again)
Connect to the content server.
Basic functions to receive and send TCP packets to/from the content server.
ContentInfo ** ContentIterator
Iterator for the content vector.
uint Length() const
Get the number of content items we know locally.
ContentType
The values in the enum are important; they are used as database &#39;keys&#39;.
Definition: tcp_content.h:21
void RemoveCallback(ContentCallback *cb)
Remove a callback.
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...
void OnFailure() override
Callback for when the connection attempt failed.
Callback for when the HTTP handler has something to tell us.
Definition: tcp_http.h:18
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
ContentInfo * curInfo
Information about the currently downloaded file.
virtual void OnConnect(bool success)
Callback for when the connection has finished.
int http_response_index
Where we are, in the response, with handling it.
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:40
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
FILE * curFile
Currently downloaded file.
Container for all important information about a piece of content.
Definition: tcp_content.h:54
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< const ContentInfo * > ConstContentVector
Vector with constant content info.