network_content.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef NETWORK_CONTENT_H
00013 #define NETWORK_CONTENT_H
00014
00015 #include "core/tcp_content.h"
00016
00017 #if defined(ENABLE_NETWORK)
00018
00020 typedef SmallVector<ContentInfo *, 16> ContentVector;
00021 typedef SmallVector<const ContentInfo *, 16> ConstContentVector;
00022
00024 typedef ContentInfo **ContentIterator;
00025 typedef const ContentInfo * const * ConstContentIterator;
00026
00028 struct ContentCallback {
00033 virtual void OnConnect(bool success) {}
00034
00038 virtual void OnDisconnect() {}
00039
00044 virtual void OnReceiveContentInfo(const ContentInfo *ci) {}
00045
00051 virtual void OnDownloadProgress(const ContentInfo *ci, uint bytes) {}
00052
00057 virtual void OnDownloadComplete(ContentID cid) {}
00058
00060 virtual ~ContentCallback() {}
00061 };
00062
00066 class ClientNetworkContentSocketHandler : public NetworkContentSocketHandler, ContentCallback {
00067 protected:
00068 SmallVector<ContentCallback *, 2> callbacks;
00069 SmallVector<ContentID, 4> requested;
00070 ContentVector infos;
00071
00072 FILE *curFile;
00073 ContentInfo *curInfo;
00074 bool isConnecting;
00075 uint32 lastActivity;
00076
00077 friend class NetworkContentConnecter;
00078
00079 DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_INFO);
00080 DECLARE_CONTENT_RECEIVE_COMMAND(PACKET_CONTENT_SERVER_CONTENT);
00081
00082 ContentInfo *GetContent(ContentID cid);
00083 void DownloadContentInfo(ContentID cid);
00084
00085 void OnConnect(bool success);
00086 void OnDisconnect();
00087 void OnReceiveContentInfo(const ContentInfo *ci);
00088 void OnDownloadProgress(const ContentInfo *ci, uint bytes);
00089 void OnDownloadComplete(ContentID cid);
00090 public:
00092 static const int IDLE_TIMEOUT = 60 * 1000;
00093
00094 ClientNetworkContentSocketHandler();
00095 ~ClientNetworkContentSocketHandler();
00096
00097 void Connect();
00098 void SendReceive();
00099 void Close();
00100
00101 void RequestContentList(ContentType type);
00102 void RequestContentList(uint count, const ContentID *content_ids);
00103 void RequestContentList(ContentVector *cv, bool send_md5sum = true);
00104
00105 void DownloadSelectedContent(uint &files, uint &bytes);
00106
00107 void Select(ContentID cid);
00108 void Unselect(ContentID cid);
00109 void SelectAll();
00110 void SelectUpgrade();
00111 void UnselectAll();
00112 void ToggleSelectedState(const ContentInfo *ci);
00113
00114 void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const;
00115 void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const;
00116 void CheckDependencyState(ContentInfo *ci);
00117
00119 uint Length() const { return this->infos.Length(); }
00121 ConstContentIterator Begin() const { return this->infos.Begin(); }
00123 ConstContentIterator Get(uint32 index) const { return this->infos.Get(index); }
00125 ConstContentIterator End() const { return this->infos.End(); }
00127 void Clear();
00128
00130 void AddCallback(ContentCallback *cb) { this->callbacks.Include(cb); }
00132 void RemoveCallback(ContentCallback *cb) { this->callbacks.Erase(this->callbacks.Find(cb)); }
00133 };
00134
00135 extern ClientNetworkContentSocketHandler _network_content_client;
00136
00137 void ShowNetworkContentListWindow(ContentVector *cv = NULL, ContentType type = CONTENT_TYPE_END);
00138
00139 #else
00140 static inline void ShowNetworkContentListWindow() {}
00141 #endif
00142
00143 #endif