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/smallvec_type.hpp"
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, uint bytes) {}
00053 
00058   virtual void OnDownloadComplete(ContentID cid) {}
00059 
00061   virtual ~ContentCallback() {}
00062 };
00063 
00067 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback {
00068 protected:
00069   SmallVector<ContentCallback *, 2> callbacks; 
00070   SmallVector<ContentID, 4> requested;         
00071   ContentVector infos;                         
00072 
00073   FILE *curFile;        
00074   ContentInfo *curInfo; 
00075   bool isConnecting;    
00076   uint32 lastActivity;  
00077 
00078   friend class NetworkContentConnecter;
00079 
00080   DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
00081   DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
00082 
00083   ContentInfo *GetContent(ContentID cid);
00084   void DownloadContentInfo(ContentID cid);
00085 
00086   void OnConnect(bool success);
00087   void OnDisconnect();
00088   void OnReceiveContentInfo(const ContentInfo *ci);
00089   void OnDownloadProgress(const ContentInfo *ci, uint bytes);
00090   void OnDownloadComplete(ContentID cid);
00091 public:
00093   static const int IDLE_TIMEOUT = 60 * 1000;
00094 
00095   ClientNetworkContentSocketHandler();
00096   ~ClientNetworkContentSocketHandler();
00097 
00098   void Connect();
00099   void SendReceive();
00100   void Close();
00101 
00102   void RequestContentList(ContentType type);
00103   void RequestContentList(uint count, const ContentID *content_ids);
00104   void RequestContentList(ContentVector *cv, bool send_md5sum = true);
00105 
00106   void DownloadSelectedContent(uint &files, uint &bytes);
00107 
00108   void Select(ContentID cid);
00109   void Unselect(ContentID cid);
00110   void SelectAll();
00111   void SelectUpgrade();
00112   void UnselectAll();
00113   void ToggleSelectedState(const ContentInfo *ci);
00114 
00115   void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
00116   void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
00117   void CheckDependencyState(ContentInfo *ci);
00118 
00120   uint Length() const { return this->infos.Length(); }
00122   ConstContentIterator Begin() const { return this->infos.Begin(); }
00124   ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
00126   ConstContentIterator End() const { return this->infos.End(); }
00128   void Clear();
00129 
00131   void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
00133   void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
00134 };
00135 
00136 extern ClientNetworkContentSocketHandler _network_content_client;
00137 
00138 void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type = CONTENT_TYPE_END);
00139 
00140 #else
00141 static inline void ShowNetworkContentListWindow() {}
00142 #endif /* ENABLE_NETWORK */
00143 
00144 #endif /* NETWORK_CONTENT_H */

Generated on Sat Dec 26 20:06:02 2009 for OpenTTD by  doxygen 1.5.6