# network_content.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef NETWORK_CONTENT_H
    00013 #define NETWORK_CONTENT_H
    00014
    00015 #include "core/tcp_content.h"
    00016 #include "core/tcp_http.h"
    00017
    00018 #if defined(ENABLE_NETWORK)
    00019
    00021 typedef SmallVector<ContentInfo *, 16> ContentVector;
    00022 typedef SmallVector<const ContentInfo *, 16> ConstContentVector;
    00023
    00025 typedef ContentInfo **ContentIterator;
    00026 typedef const ContentInfo * const * ConstContentIterator;
    00027
    00029 struct ContentCallback {
    00034   virtual void OnConnect(bool success) {}
    00035
    00039   virtual void OnDisconnect() {}
    00040
    00045   virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
    00046
    00052   virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
    00053
    00058   virtual void OnDownloadComplete(ContentID cid) {}
    00059
    00061   virtual ~ContentCallback() {}
    00062 };
    00063
    00067 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
    00068 protected:
    00069   typedef SmallVector<ContentID, 4> ContentIDList;
    00070   SmallVector<ContentCallback *, 2> callbacks;
    00071   ContentIDList requested;
    00072   ContentVector infos;
    00073   SmallVector<char, 1024> http_response;
    00074   int http_response_index;
    00075
    00076   FILE *curFile;
    00077   ContentInfo *curInfo;
    00078   bool isConnecting;
    00079   uint32 lastActivity;
    00080
    00081   friend class NetworkContentConnecter;
    00082
    00083   DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
    00084   DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
    00085
    00086   ContentInfo *GetContent(ContentID cid);
    00087   void DownloadContentInfo(ContentID cid);
    00088
    00089   void OnConnect(bool success);
    00090   void OnDisconnect();
    00091   void OnReceiveContentInfo(const ContentInfo *ci);
    00092   void OnDownloadProgress(const ContentInfo *ci, int bytes);
    00093   void OnDownloadComplete(ContentID cid);
    00094
    00095   void OnFailure();
    00096   void OnReceiveData(const char *data, size_t length);
    00097
    00098   bool BeforeDownload();
    00099   void AfterDownload();
    00100
    00101   void DownloadSelectedContentHTTP(const ContentIDList &content);
    00102   void DownloadSelectedContentFallback(const ContentIDList &content);
    00103 public:
    00105   static const int IDLE_TIMEOUT = 60 * 1000;
    00106
    00107   ClientNetworkContentSocketHandler();
    00108   ~ClientNetworkContentSocketHandler();
    00109
    00110   void Connect();
    00111   void SendReceive();
    00112   void Close();
    00113
    00114   void RequestContentList(ContentType type);
    00115   void RequestContentList(uint count, const ContentID *content_ids);
    00116   void RequestContentList(ContentVector *cv, bool send_md5sum = true);
    00117
    00118   void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
    00119
    00120   void Select(ContentID cid);
    00121   void Unselect(ContentID cid);
    00122   void SelectAll();
    00123   void SelectUpgrade();
    00124   void UnselectAll();
    00125   void ToggleSelectedState(const ContentInfo *ci);
    00126
    00127   void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
    00128   void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
    00129   void CheckDependencyState(ContentInfo *ci);
    00130
    00132   uint Length() const { return this->infos.Length(); }
    00134   ConstContentIterator Begin() const { return this->infos.Begin(); }
    00136   ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
    00138   ConstContentIterator End() const { return this->infos.End(); }
    00139
    00140   void Clear();
    00141
    00143   void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
    00145   void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
    00146 };
    00147
    00148 extern ClientNetworkContentSocketHandler _network_content_client;
    00149
    00150 void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type = CONTENT_TYPE_END);
    00151
    00152 #else
    00153 static inline void ShowNetworkContentListWindow() {}
    00154 #endif /* ENABLE_NETWORK */
    00155
    00156 #endif /* NETWORK_CONTENT_H */