network_content.h

Go to the documentation of this file.
00001 /* $Id: network_content.h 15221 2009-01-23 10:20:29Z rubidium $ */
00002 
00005 #ifndef NETWORK_CONTENT_H
00006 #define NETWORK_CONTENT_H
00007 
00008 #if defined(ENABLE_NETWORK)
00009 
00010 #include "core/tcp_content.h"
00011 #include "../core/smallvec_type.hpp"
00012 
00014 typedef SmallVector<ContentInfo *, 16> ContentVector;
00015 typedef SmallVector<const ContentInfo *, 16> ConstContentVector;
00016 
00018 typedef ContentInfo **ContentIterator;
00019 typedef const ContentInfo * const * ConstContentIterator;
00020 
00022 struct ContentCallback {
00027   virtual void OnConnect(bool success) {}
00028 
00032   virtual void OnDisconnect() {}
00033 
00038   virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
00039 
00045   virtual void OnDownloadProgress(const ContentInfo *ci, uint bytes) {}
00046 
00051   virtual void OnDownloadComplete(ContentID cid) {}
00052 
00054   virtual ~ContentCallback() {}
00055 };
00056 
00060 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback {
00061 protected:
00062   SmallVector<ContentCallback *, 2> callbacks; 
00063   SmallVector<ContentID, 4> requested;         
00064   ContentVector infos;                         
00065 
00066   FILE *curFile;        
00067   ContentInfo *curInfo; 
00068   bool isConnecting;    
00069   uint32 lastActivity;  
00070 
00071   friend class NetworkContentConnecter;
00072 
00073   DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
00074   DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
00075 
00076   ContentInfo *GetContent(ContentID cid);
00077   void DownloadContentInfo(ContentID cid);
00078 
00079   void OnConnect(bool success);
00080   void OnDisconnect();
00081   void OnReceiveContentInfo(const ContentInfo *ci);
00082   void OnDownloadProgress(const ContentInfo *ci, uint bytes);
00083   void OnDownloadComplete(ContentID cid);
00084 public:
00086   static const int IDLE_TIMEOUT = 60 * 1000;
00087 
00088   ClientNetworkContentSocketHandler();
00089   ~ClientNetworkContentSocketHandler();
00090 
00091   void Connect();
00092   void SendReceive();
00093   void Close();
00094 
00095   void RequestContentList(ContentType type);
00096   void RequestContentList(uint count, const ContentID *content_ids);
00097   void RequestContentList(ContentVector *cv, bool send_md5sum = true);
00098 
00099   void DownloadSelectedContent(uint &files, uint &bytes);
00100 
00101   void Select(ContentID cid);
00102   void Unselect(ContentID cid);
00103   void SelectAll();
00104   void SelectUpgrade();
00105   void UnselectAll();
00106   void ToggleSelectedState(const ContentInfo *ci);
00107 
00108   void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
00109   void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
00110   void CheckDependencyState(ContentInfo *ci);
00111 
00113   uint Length() const { return this->infos.Length(); }
00115   ConstContentIterator Begin() const { return this->infos.Begin(); }
00117   ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
00119   ConstContentIterator End() const { return this->infos.End(); }
00121   void Clear();
00122 
00124   void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
00126   void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
00127 };
00128 
00129 extern ClientNetworkContentSocketHandler _network_content_client;
00130 
00131 void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type = CONTENT_TYPE_END);
00132 
00133 #else
00134 static inline void ShowNetworkContentListWindow() {}
00135 #endif /* ENABLE_NETWORK */
00136 
00137 #endif /* NETWORK_CONTENT_H */

Generated on Mon Jun 8 23:04:04 2009 for OpenTTD by  doxygen 1.5.6