Nothing to see here. Really.
[amule.git] / src / ClientRef.h
blob7e3ccb8755fb8bc620a8f5f6abf62a6ab9ae0d74
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2003-2011 Stu Redman ( sturedman@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef CLIENTREF_H
27 #define CLIENTREF_H
30 // Clients are stored in many places. To prevent problems when a client gets deleted
31 // and later a pointer to a deleted client is referenced, clients are now stored in
32 // this class only. It uses reference counting in the client which deletes the client only
33 // after the last reference has been unlinked.
34 // Also the class is used as abstraction layer for the CUpDownClient class.
37 #include <list>
38 #include <set>
39 #include "Types.h"
41 class CUpDownClient;
42 class CKnownFile;
43 class CPartFile;
44 class CFriend;
45 class BitVector;
46 class CMD4Hash;
48 #ifdef __DEBUG__
49 #define DEBUG_ZOMBIE_CLIENTS
50 #endif
52 #ifdef DEBUG_ZOMBIE_CLIENTS
53 #define LINKED_FROM , wxString from
54 #define CLIENT_DEBUGSTRING(a) , wxT(a)
55 #define CCLIENTREF(a, b) CClientRef(a, b)
56 #else
57 #define LINKED_FROM
58 #define CLIENT_DEBUGSTRING(a)
59 #define CCLIENTREF(a, b) CClientRef(a)
60 #endif
62 class CClientRef {
64 CUpDownClient * m_client;
65 #ifdef DEBUG_ZOMBIE_CLIENTS
66 wxString m_from;
67 #endif
69 public:
70 CClientRef() { m_client = NULL; }
71 CClientRef(const CClientRef&);
72 CClientRef(CUpDownClient * client LINKED_FROM);
73 CClientRef(class CEC_UpDownClient_Tag *);
74 ~CClientRef() { Unlink(); }
75 CClientRef& operator = (const CClientRef& ref)
77 #ifdef DEBUG_ZOMBIE_CLIENTS
78 m_from = wxT("assigned from ") + ref.m_from;
79 Link(ref.m_client, m_from);
80 #else
81 Link(ref.m_client);
82 #endif
83 return *this;
87 // For sets and maps
88 bool operator < (const CClientRef & other) const { return m_client < other.m_client; }
89 bool operator == (const CClientRef & other) const { return m_client == other.m_client; }
90 bool operator != (const CClientRef & other) const { return m_client != other.m_client; }
92 void swap(CClientRef & other)
94 CUpDownClient * c = m_client;
95 m_client = other.m_client;
96 other.m_client = c;
97 #ifdef DEBUG_ZOMBIE_CLIENTS
98 m_from.swap(other.m_from);
99 #endif
102 void Link(CUpDownClient * client LINKED_FROM);
103 void Unlink();
104 bool IsLinked() const { return m_client != NULL; }
106 // Return client, no matter if it was deleted
107 CUpDownClient * GetClient() const { return m_client; }
108 // Check if client was deleted, if yes unlink and return zero
109 CUpDownClient * GetClientChecked();
110 // Check if client was deleted, if yes unlink
111 CClientRef& GetRef();
113 void Safe_Delete();
115 // Wrapper methods
116 void ClearUploadFileID() const; // only wrapper const
117 uint32 ECID() const;
118 bool ExtProtocolAvailable() const;
119 uint16 GetAvailablePartCount() const;
120 const wxString& GetClientFilename() const;
121 const wxString& GetClientModString() const;
122 const wxString& GetClientOSInfo() const;
123 uint8 GetClientSoft() const;
124 const wxString& GetClientVerString() const;
125 uint64 GetDownloadedTotal() const;
126 uint8 GetDownloadState() const;
127 CFriend* GetFriend() const;
128 bool GetFriendSlot() const;
129 wxString GetFullIP() const;
130 uint16 GetKadPort() const;
131 float GetKBpsDown() const;
132 uint32 GetIP() const;
133 uint16 GetLastDownloadingPart() const;
134 uint16 GetNextRequestedPart() const;
135 uint8 GetObfuscationStatus() const;
136 uint16 GetOldRemoteQueueRank() const;
137 const BitVector& GetPartStatus() const;
138 uint16 GetRemoteQueueRank() const;
139 CPartFile* GetRequestFile() const;
140 uint32 GetScore() const;
141 double GetScoreRatio() const;
142 uint32 GetServerIP() const;
143 const wxString GetServerName() const;
144 uint16 GetServerPort() const;
145 const wxString& GetSoftStr() const;
146 const wxString& GetSoftVerStr() const;
147 int GetSourceFrom() const; // ESourceFrom
148 wxString GetSecureIdentTextStatus() const;
149 uint64 GetTransferredDown() const;
150 uint64 GetTransferredUp() const;
151 uint16 GetUDPPort() const;
152 uint32 GetUploadDatarate() const;
153 uint64 GetUploadedTotal() const;
154 const CKnownFile* GetUploadFile() const;
155 uint16 GetUploadQueueWaitingPosition() const;
156 uint8 GetUploadState() const;
157 size_t GetUpPartCount() const;
158 const CMD4Hash& GetUserHash() const;
159 uint32 GetUserIDHybrid() const;
160 const wxString& GetUserName() const;
161 uint16_t GetUserPort() const;
162 uint32 GetVersion() const;
163 bool HasDisabledSharedFiles() const;
164 bool HasLowID() const;
165 bool IsBadGuy() const;
166 bool IsFriend() const;
167 bool IsIdentified() const;
168 bool IsRemoteQueueFull() const;
169 bool IsUpPartAvailable(uint16 iPart) const;
170 void RequestSharedFileList() const;
171 void SetCommentDirty(bool bDirty = true) const; // only wrapper const
172 void SetFriend(CFriend* newfriend) const; // only wrapper const
173 void SetFriendSlot(bool bNV) const; // only wrapper const
174 bool SwapToAnotherFile(bool bIgnoreNoNeeded, bool ignoreSuspensions, bool bRemoveCompletely, CPartFile* toFile = NULL) const; // only wrapper const
178 // efficient std::swap for it
179 namespace std
181 template<> inline void swap(CClientRef& a, CClientRef& b)
183 a.swap(b);
188 typedef std::list<CClientRef> CClientRefList;
189 typedef std::set<CClientRef> CClientRefSet;
192 #endif