OpenTTD
network_content.h
Go to the documentation of this file.
1 /* $Id: network_content.h 27468 2015-12-10 18:28:01Z zuu $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef NETWORK_CONTENT_H
13 #define NETWORK_CONTENT_H
14 
15 #include "core/tcp_content.h"
16 #include "core/tcp_http.h"
17 
18 #if defined(ENABLE_NETWORK)
19 
24 
28 typedef const ContentInfo * const * ConstContentIterator;
29 
36  virtual void OnConnect(bool success) {}
37 
41  virtual void OnDisconnect() {}
42 
47  virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
48 
54  virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
55 
60  virtual void OnDownloadComplete(ContentID cid) {}
61 
63  virtual ~ContentCallback() {}
64 };
65 
70 protected:
73  ContentIDList requested;
77 
78  FILE *curFile;
80  bool isConnecting;
81  uint32 lastActivity;
82 
83  friend class NetworkContentConnecter;
84 
85  virtual bool Receive_SERVER_INFO(Packet *p);
86  virtual bool Receive_SERVER_CONTENT(Packet *p);
87 
88  ContentInfo *GetContent(ContentID cid);
89  void DownloadContentInfo(ContentID cid);
90 
91  void OnConnect(bool success);
92  void OnDisconnect();
93  void OnReceiveContentInfo(const ContentInfo *ci);
94  void OnDownloadProgress(const ContentInfo *ci, int bytes);
95  void OnDownloadComplete(ContentID cid);
96 
97  void OnFailure();
98  void OnReceiveData(const char *data, size_t length);
99 
100  bool BeforeDownload();
101  void AfterDownload();
102 
103  void DownloadSelectedContentHTTP(const ContentIDList &content);
104  void DownloadSelectedContentFallback(const ContentIDList &content);
105 public:
107  static const int IDLE_TIMEOUT = 60 * 1000;
108 
111 
112  void Connect();
113  void SendReceive();
114  void Close();
115 
116  void RequestContentList(ContentType type);
117  void RequestContentList(uint count, const ContentID *content_ids);
118  void RequestContentList(ContentVector *cv, bool send_md5sum = true);
119 
120  void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
121 
122  void Select(ContentID cid);
123  void Unselect(ContentID cid);
124  void SelectAll();
125  void SelectUpgrade();
126  void UnselectAll();
127  void ToggleSelectedState(const ContentInfo *ci);
128 
129  void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
130  void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
131  void CheckDependencyState(ContentInfo *ci);
132 
134  uint Length() const { return this->infos.Length(); }
136  ConstContentIterator Begin() const { return this->infos.Begin(); }
138  ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
140  ConstContentIterator End() const { return this->infos.End(); }
141 
142  void Clear();
143 
145  void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
147  void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
148 };
149 
151 
153 
154 void ShowMissingContentWindow(const struct GRFConfig *list);
155 
156 #else
157 static inline void ShowNetworkContentListWindow() {}
158 #endif /* ENABLE_NETWORK */
159 
160 #endif /* NETWORK_CONTENT_H */
Helper to mark the end of the types.
Definition: tcp_content.h:37
ContentVector infos
All content info we received.
const ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
SmallVector< ContentInfo *, 16 > ContentVector
Vector with content info.
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:44
Socket handler for the content server connection.
SmallVector< char, 1024 > http_response
The HTTP response to the requests we&#39;ve been doing.
virtual void OnFailure()
Callback for when the connection attempt failed.
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:100
Callbacks for notifying others about incoming data.
void ShowNetworkContentListWindow(ContentVector *cv=NULL, ContentType type1=CONTENT_TYPE_END, ContentType type2=CONTENT_TYPE_END)
Show the content list window with a given set of content.
const T * Begin() const
Get the pointer to the first item (const)
SmallVector< ContentCallback *, 2 > callbacks
Callbacks to notify "the world".
ContentID
Unique identifier for the content.
Definition: tcp_content.h:53
const T * End() const
Get the pointer behind the last valid item (const)
SmallVector< ContentID, 4 > ContentIDList
List of content IDs to (possibly) select.
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.
ConstContentIterator End() const
Get the end of the content inf iterator.
uint Length() const
Get the number of items in the list.
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)
SmallVector< const ContentInfo *, 16 > ConstContentVector
Vector with constant content info.
Connect to the content server.
const T * Find(const T &item) const
Search for the first occurrence of an item.
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:25
void RemoveCallback(ContentCallback *cb)
Remove a callback.
Callback for when the HTTP handler has something to tell us.
Definition: tcp_http.h:22
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
void Erase(T *item)
Removes given item from this vector.
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.
const T * Get(uint index) const
Get the pointer to item "number" (const)
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:44
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:58