2009-01-17 16:53:32 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/** @file network_content.h Part of the network protocol handling content distribution. */
|
|
|
|
|
|
|
|
#ifndef NETWORK_CONTENT_H
|
|
|
|
#define NETWORK_CONTENT_H
|
|
|
|
|
|
|
|
#include "core/tcp_content.h"
|
2010-02-03 18:42:23 +00:00
|
|
|
#include "core/tcp_http.h"
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/** Vector with content info */
|
|
|
|
typedef SmallVector<ContentInfo *, 16> ContentVector;
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Vector with constant content info */
|
2009-01-20 16:51:55 +00:00
|
|
|
typedef SmallVector<const ContentInfo *, 16> ConstContentVector;
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/** Iterator for the content vector */
|
|
|
|
typedef ContentInfo **ContentIterator;
|
2011-05-04 20:24:23 +00:00
|
|
|
/** Iterator for the constant content vector */
|
2009-01-20 16:51:55 +00:00
|
|
|
typedef const ContentInfo * const * ConstContentIterator;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/** Callbacks for notifying others about incoming data */
|
|
|
|
struct ContentCallback {
|
2009-01-20 16:51:55 +00:00
|
|
|
/**
|
|
|
|
* Callback for when the connection has finished
|
|
|
|
* @param success whether the connection was made or that we failed to make it
|
|
|
|
*/
|
|
|
|
virtual void OnConnect(bool success) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for when the connection got disconnected.
|
|
|
|
*/
|
|
|
|
virtual void OnDisconnect() {}
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/**
|
|
|
|
* We received a content info.
|
|
|
|
* @param ci the content info
|
|
|
|
*/
|
2009-01-20 16:51:55 +00:00
|
|
|
virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We have progress in the download of a file
|
|
|
|
* @param ci the content info of the file
|
|
|
|
* @param bytes the number of bytes downloaded since the previous call
|
|
|
|
*/
|
2011-03-06 10:11:59 +00:00
|
|
|
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We have finished downloading a file
|
|
|
|
* @param cid the ContentID of the downloaded file
|
|
|
|
*/
|
|
|
|
virtual void OnDownloadComplete(ContentID cid) {}
|
|
|
|
|
|
|
|
/** Silentium */
|
|
|
|
virtual ~ContentCallback() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket handler for the content server connection
|
|
|
|
*/
|
2010-02-03 18:42:23 +00:00
|
|
|
class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
|
2009-01-17 16:53:32 +00:00
|
|
|
protected:
|
2011-05-04 20:24:23 +00:00
|
|
|
typedef SmallVector<ContentID, 4> ContentIDList; ///< List of content IDs to (possibly) select.
|
2009-01-17 16:53:32 +00:00
|
|
|
SmallVector<ContentCallback *, 2> callbacks; ///< Callbacks to notify "the world"
|
2010-02-03 17:12:19 +00:00
|
|
|
ContentIDList requested; ///< ContentIDs we already requested (so we don't do it again)
|
2009-01-20 16:51:55 +00:00
|
|
|
ContentVector infos; ///< All content info we received
|
2010-02-03 18:42:23 +00:00
|
|
|
SmallVector<char, 1024> http_response; ///< The HTTP response to the requests we've been doing
|
|
|
|
int http_response_index; ///< Where we are, in the response, with handling it
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
FILE *curFile; ///< Currently downloaded file
|
|
|
|
ContentInfo *curInfo; ///< Information about the currently downloaded file
|
2009-01-20 16:51:55 +00:00
|
|
|
bool isConnecting; ///< Whether we're connecting
|
|
|
|
uint32 lastActivity; ///< The last time there was network activity
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
friend class NetworkContentConnecter;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2019-03-24 16:24:06 +00:00
|
|
|
bool Receive_SERVER_INFO(Packet *p) override;
|
|
|
|
bool Receive_SERVER_CONTENT(Packet *p) override;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
ContentInfo *GetContent(ContentID cid);
|
|
|
|
void DownloadContentInfo(ContentID cid);
|
|
|
|
|
2019-03-24 16:24:06 +00:00
|
|
|
void OnConnect(bool success) override;
|
|
|
|
void OnDisconnect() override;
|
|
|
|
void OnReceiveContentInfo(const ContentInfo *ci) override;
|
|
|
|
void OnDownloadProgress(const ContentInfo *ci, int bytes) override;
|
|
|
|
void OnDownloadComplete(ContentID cid) override;
|
2010-02-03 17:15:35 +00:00
|
|
|
|
2019-03-24 16:24:06 +00:00
|
|
|
void OnFailure() override;
|
|
|
|
void OnReceiveData(const char *data, size_t length) override;
|
2010-02-03 18:42:23 +00:00
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
bool BeforeDownload();
|
|
|
|
void AfterDownload();
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
void DownloadSelectedContentHTTP(const ContentIDList &content);
|
|
|
|
void DownloadSelectedContentFallback(const ContentIDList &content);
|
2009-01-17 16:53:32 +00:00
|
|
|
public:
|
2009-01-20 16:51:55 +00:00
|
|
|
/** The idle timeout; when to close the connection because it's idle. */
|
|
|
|
static const int IDLE_TIMEOUT = 60 * 1000;
|
|
|
|
|
|
|
|
ClientNetworkContentSocketHandler();
|
|
|
|
~ClientNetworkContentSocketHandler();
|
|
|
|
|
|
|
|
void Connect();
|
|
|
|
void SendReceive();
|
2019-03-24 16:24:06 +00:00
|
|
|
void Close() override;
|
2009-01-20 16:51:55 +00:00
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
void RequestContentList(ContentType type);
|
|
|
|
void RequestContentList(uint count, const ContentID *content_ids);
|
|
|
|
void RequestContentList(ContentVector *cv, bool send_md5sum = true);
|
|
|
|
|
2010-02-03 18:42:23 +00:00
|
|
|
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
|
2009-01-20 16:51:55 +00:00
|
|
|
|
|
|
|
void Select(ContentID cid);
|
|
|
|
void Unselect(ContentID cid);
|
|
|
|
void SelectAll();
|
2009-01-20 21:05:13 +00:00
|
|
|
void SelectUpgrade();
|
2009-01-20 16:51:55 +00:00
|
|
|
void UnselectAll();
|
|
|
|
void ToggleSelectedState(const ContentInfo *ci);
|
|
|
|
|
|
|
|
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
|
|
|
|
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
|
|
|
|
void CheckDependencyState(ContentInfo *ci);
|
|
|
|
|
|
|
|
/** Get the number of content items we know locally. */
|
|
|
|
uint Length() const { return this->infos.Length(); }
|
|
|
|
/** Get the begin of the content inf iterator. */
|
|
|
|
ConstContentIterator Begin() const { return this->infos.Begin(); }
|
|
|
|
/** Get the nth position of the content inf iterator. */
|
|
|
|
ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
|
|
|
|
/** Get the end of the content inf iterator. */
|
|
|
|
ConstContentIterator End() const { return this->infos.End(); }
|
2011-01-22 09:53:15 +00:00
|
|
|
|
2009-01-23 10:20:29 +00:00
|
|
|
void Clear();
|
2009-01-20 16:51:55 +00:00
|
|
|
|
|
|
|
/** Add a callback to this class */
|
|
|
|
void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
|
|
|
|
/** Remove a callback */
|
|
|
|
void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
|
2009-01-17 16:53:32 +00:00
|
|
|
};
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
extern ClientNetworkContentSocketHandler _network_content_client;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2015-12-10 18:28:01 +00:00
|
|
|
void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type1 = CONTENT_TYPE_END, ContentType type2 = CONTENT_TYPE_END);
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2011-12-09 21:49:52 +00:00
|
|
|
void ShowMissingContentWindow(const struct GRFConfig *list);
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
#endif /* NETWORK_CONTENT_H */
|