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;
00023 typedef SmallVector<const ContentInfo *, 16> ConstContentVector;
00024 
00026 typedef ContentInfo **ContentIterator;
00028 typedef const ContentInfo * const * ConstContentIterator;
00029 
00031 struct ContentCallback {
00036   virtual void OnConnect(bool success) {}
00037 
00041   virtual void OnDisconnect() {}
00042 
00047   virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
00048 
00054   virtual void OnDownloadProgress(const ContentInfo *ci, int bytes) {}
00055 
00060   virtual void OnDownloadComplete(ContentID cid) {}
00061 
00063   virtual ~ContentCallback() {}
00064 };
00065 
00069 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback, HTTPCallback {
00070 protected:
00071   typedef SmallVector<ContentID, 4> ContentIDList; 
00072   SmallVector<ContentCallback *, 2> callbacks; 
00073   ContentIDList requested;                     
00074   ContentVector infos;                         
00075   SmallVector<char, 1024> http_response;       
00076   int http_response_index;                     
00077 
00078   FILE *curFile;        
00079   ContentInfo *curInfo; 
00080   bool isConnecting;    
00081   uint32 lastActivity;  
00082 
00083   friend class NetworkContentConnecter;
00084 
00085   virtual bool Receive_SERVER_INFO(Packet *p);
00086   virtual bool Receive_SERVER_CONTENT(Packet *p);
00087 
00088   ContentInfo *GetContent(ContentID cid);
00089   void DownloadContentInfo(ContentID cid);
00090 
00091   void OnConnect(bool success);
00092   void OnDisconnect();
00093   void OnReceiveContentInfo(const ContentInfo *ci);
00094   void OnDownloadProgress(const ContentInfo *ci, int bytes);
00095   void OnDownloadComplete(ContentID cid);
00096 
00097   void OnFailure();
00098   void OnReceiveData(const char *data, size_t length);
00099 
00100   bool BeforeDownload();
00101   void AfterDownload();
00102 
00103   void DownloadSelectedContentHTTP(const ContentIDList &content);
00104   void DownloadSelectedContentFallback(const ContentIDList &content);
00105 public:
00107   static const int IDLE_TIMEOUT = 60 * 1000;
00108 
00109   ClientNetworkContentSocketHandler();
00110   ~ClientNetworkContentSocketHandler();
00111 
00112   void Connect();
00113   void SendReceive();
00114   void Close();
00115 
00116   void RequestContentList(ContentType type);
00117   void RequestContentList(uint count, const ContentID *content_ids);
00118   void RequestContentList(ContentVector *cv, bool send_md5sum = true);
00119 
00120   void DownloadSelectedContent(uint &files, uint &bytes, bool fallback = false);
00121 
00122   void Select(ContentID cid);
00123   void Unselect(ContentID cid);
00124   void SelectAll();
00125   void SelectUpgrade();
00126   void UnselectAll();
00127   void ToggleSelectedState(const ContentInfo *ci);
00128 
00129   void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
00130   void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
00131   void CheckDependencyState(ContentInfo *ci);
00132 
00134   uint Length() const { return this->infos.Length(); }
00136   ConstContentIterator Begin() const { return this->infos.Begin(); }
00138   ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
00140   ConstContentIterator End() const { return this->infos.End(); }
00141 
00142   void Clear();
00143 
00145   void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
00147   void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
00148 };
00149 
00150 extern ClientNetworkContentSocketHandler _network_content_client;
00151 
00152 void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type = CONTENT_TYPE_END);
00153 
00154 #else
00155 static inline void ShowNetworkContentListWindow() {}
00156 #endif /* ENABLE_NETWORK */
00157 
00158 #endif /* NETWORK_CONTENT_H */

Generated on Fri May 27 04:19:44 2011 for OpenTTD by  doxygen 1.6.1