OpenTTD: src/network/network_content.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network_content.h
Go to the documentation of this file.
1 /* $Id: network_content.h 23465 2011-12-09 21:49:52Z yexo $ */
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 type = 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 */