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
21 typedef SmallVector<ContentInfo *, 16> ContentVector;
23 typedef SmallVector<const ContentInfo *, 16> ConstContentVector;
24
26 typedef ContentInfo **ContentIterator;
28 typedef const ContentInfo * const * ConstContentIterator;
29
31 struct ContentCallback {
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
69 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
70 protected:
71 typedef SmallVector<ContentID, 4> ContentIDList;
72 SmallVector<ContentCallback *, 2> callbacks;
73 ContentIDList requested;
74 ContentVector infos;
75 SmallVector<char, 1024> http_response;
76 int http_response_index;
77
78 FILE *curFile;
79 ContentInfo *curInfo;
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
109 ClientNetworkContentSocketHandler();
110 ~ClientNetworkContentSocketHandler();
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
150 extern ClientNetworkContentSocketHandler _network_content_client;
151
152 void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type1 = CONTENT_TYPE_END, ContentType type2 = CONTENT_TYPE_END);
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 */
CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content.h:37
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition: network_content.h:74
ConstContentIterator
const ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
Definition: network_content.h:28
ContentVector
SmallVector< ContentInfo *, 16 > ContentVector
Vector with content info.
Definition: network_content.h:21
ClientNetworkContentSocketHandler::Begin
ConstContentIterator Begin() const
Get the begin of the content inf iterator.
Definition: network_content.h:136
tcp_http.h
Basic functions to receive and send HTTP TCP packets.
Packet
Internal entity of a packet.
Definition: packet.h:44
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition: network_content.h:69
ClientNetworkContentSocketHandler::http_response
SmallVector< char, 1024 > http_response
The HTTP response to the requests we&#39;ve been doing.
Definition: network_content.h:75
NetworkContentConnecter::OnFailure
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: network_content.cpp:732
NetworkContentSocketHandler
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:100
ContentCallback
Callbacks for notifying others about incoming data.
Definition: network_content.h:31
ShowNetworkContentListWindow
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.
Definition: network_content_gui.cpp:1144
SmallVector::Begin
const T * Begin() const
Get the pointer to the first item (const)
Definition: smallvec_type.hpp:272
SmallVector< ContentInfo *, 16 >
ClientNetworkContentSocketHandler::callbacks
SmallVector< ContentCallback *, 2 > callbacks
Callbacks to notify "the world".
Definition: network_content.h:72
ContentID
ContentID
Unique identifier for the content.
Definition: tcp_content.h:53
SmallVector::End
const T * End() const
Get the pointer behind the last valid item (const)
Definition: smallvec_type.hpp:292
ClientNetworkContentSocketHandler::ContentIDList
SmallVector< ContentID, 4 > ContentIDList
List of content IDs to (possibly) select.
Definition: network_content.h:71
ClientNetworkContentSocketHandler::isConnecting
bool isConnecting
Whether we&#39;re connecting.
Definition: network_content.h:80
ShowMissingContentWindow
void ShowMissingContentWindow(const GRFConfig *list)
Show the content list window with all missing grfs from the given list.
Definition: newgrf_gui.cpp:1554
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition: network_content.h:41
ClientNetworkContentSocketHandler::AddCallback
void AddCallback(ContentCallback *cb)
Add a callback to this class.
Definition: network_content.h:145
ClientNetworkContentSocketHandler::End
ConstContentIterator End() const
Get the end of the content inf iterator.
Definition: network_content.h:140
SmallVector::Length
uint Length() const
Get the number of items in the list.
Definition: smallvec_type.hpp:262
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:153
ClientNetworkContentSocketHandler::lastActivity
uint32 lastActivity
The last time there was network activity.
Definition: network_content.h:81
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
Definition: network_content.h:54
ClientNetworkContentSocketHandler::Get
ConstContentIterator Get(uint32 index) const
Get the nth position of the content inf iterator.
Definition: network_content.h:138
ContentCallback::~ContentCallback
virtual ~ContentCallback()
Silentium.
Definition: network_content.h:63
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don&#39;t do it again)
Definition: network_content.h:73
ConstContentVector
SmallVector< const ContentInfo *, 16 > ConstContentVector
Vector with constant content info.
Definition: network_content.h:23
NetworkContentConnecter
Connect to the content server.
Definition: network_content.cpp:724
SmallVector::Find
const T * Find(const T &item) const
Search for the first occurrence of an item.
Definition: smallvec_type.hpp:167
tcp_content.h
Basic functions to receive and send TCP packets to/from the content server.
ContentIterator
ContentInfo ** ContentIterator
Iterator for the content vector.
Definition: network_content.h:26
ClientNetworkContentSocketHandler::Length
uint Length() const
Get the number of content items we know locally.
Definition: network_content.h:134
ContentType
ContentType
The values in the enum are important; they are used as database &#39;keys&#39;.
Definition: tcp_content.h:25
ClientNetworkContentSocketHandler::RemoveCallback
void RemoveCallback(ContentCallback *cb)
Remove a callback.
Definition: network_content.h:147
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition: tcp_http.h:22
SmallVector::Include
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
Definition: smallvec_type.hpp:250
SmallVector::Erase
void Erase(T *item)
Removes given item from this vector.
Definition: smallvec_type.hpp:223
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
Definition: network_content.h:47
ClientNetworkContentSocketHandler::curInfo
ContentInfo * curInfo
Information about the currently downloaded file.
Definition: network_content.h:79
ContentCallback::OnConnect
virtual void OnConnect(bool success)
Callback for when the connection has finished.
Definition: network_content.h:36
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition: network_content.h:76
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
Definition: network_content.h:60
SmallVector::Get
const T * Get(uint index) const
Get the pointer to item "number" (const)
Definition: smallvec_type.hpp:313
TCPConnecter::Connect
void Connect()
The actual connection function.
Definition: tcp_connect.cpp:44
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition: network_content.cpp:35
ClientNetworkContentSocketHandler::curFile
FILE * curFile
Currently downloaded file.
Definition: network_content.h:78
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content.h:58