Improve the code by static code analysis [2/3]: Performance
[amule.git] / src / amule.h
bloba0e027c45a024b7fde698b523cd7454da245660d
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) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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.
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
27 #ifndef AMULE_H
28 #define AMULE_H
31 #include <wx/app.h> // Needed for wxApp
32 #include <wx/intl.h> // Needed for wxLocale
35 #include "Types.h" // Needed for int32, uint16 and uint64
36 #include <map>
37 #ifndef __WXMSW__
38 #include <signal.h>
39 // #include <wx/unix/execute.h>
40 #endif // __WXMSW__
43 class CAbstractFile;
44 class CKnownFile;
45 class ExternalConn;
46 class CamuleDlg;
47 class CPreferences;
48 class CDownloadQueue;
49 class CUploadQueue;
50 class CServerConnect;
51 class CSharedFileList;
52 class CServer;
53 class CFriend;
54 class CMD4Hash;
55 class CServerList;
56 class CListenSocket;
57 class CClientList;
58 class CKnownFileList;
59 class CCanceledFileList;
60 class CSearchList;
61 class CClientCreditsList;
62 class CFriendList;
63 class CClientUDPSocket;
64 class CIPFilter;
65 class UploadBandwidthThrottler;
66 class CAsioService;
67 #ifdef ENABLE_UPNP
68 class CUPnPControlPoint;
69 class CUPnPPortMapping;
70 #endif
71 class CStatistics;
72 class wxSocketEvent;
73 class wxCommandEvent;
74 class wxCloseEvent;
75 class wxFFileOutputStream;
76 class CTimer;
77 class CTimerEvent;
78 class wxSingleInstanceChecker;
79 class CHashingEvent;
80 class CMuleInternalEvent;
81 class CCompletionEvent;
82 class CAllocFinishedEvent;
83 class wxExecuteData;
84 class CLoggingEvent;
87 namespace MuleNotify {
88 class CMuleGUIEvent;
92 using MuleNotify::CMuleGUIEvent;
95 #ifdef AMULE_DAEMON
96 #define AMULE_APP_BASE wxAppConsole
97 #define CORE_TIMER_PERIOD 300
98 #else
99 #define AMULE_APP_BASE wxApp
100 #define CORE_TIMER_PERIOD 100
101 #endif
103 #define CONNECTED_ED2K (1<<0)
104 #define CONNECTED_KAD_NOT (1<<1)
105 #define CONNECTED_KAD_OK (1<<2)
106 #define CONNECTED_KAD_FIREWALLED (1<<3)
109 // Base class common to amule, aamuled and amulegui
110 class CamuleAppCommon
112 private:
113 // Used to detect a previous running instance of aMule
114 wxSingleInstanceChecker* m_singleInstance;
116 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
117 protected:
118 wxString FullMuleVersion;
119 wxString OSDescription;
120 wxString OSType;
121 bool enable_daemon_fork;
122 bool ec_config;
123 bool m_skipConnectionDialog;
124 bool m_geometryEnabled;
125 wxString m_geometryString;
126 wxString m_logFile;
127 wxString m_appName;
128 wxString m_PidFile;
130 bool InitCommon(int argc, wxChar ** argv);
131 void RefreshSingleInstanceChecker();
132 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
133 public:
134 wxString ConfigDir;
135 wxString m_configFile;
137 CamuleAppCommon();
138 ~CamuleAppCommon();
139 void AddLinksFromFile();
140 // URL functions
141 wxString CreateMagnetLink(const CAbstractFile *f);
142 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
143 // Who am I ?
144 #ifdef AMULE_DAEMON
145 bool IsDaemon() const { return true; }
146 #else
147 bool IsDaemon() const { return false; }
148 #endif
150 #ifdef CLIENT_GUI
151 bool IsRemoteGui() const { return true; }
152 #else
153 bool IsRemoteGui() const { return false; }
154 #endif
156 const wxString& GetMuleAppName() const { return m_appName; }
157 const wxString GetFullMuleVersion() const;
160 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
162 private:
163 enum APPState {
164 APP_STATE_RUNNING = 0,
165 APP_STATE_SHUTTINGDOWN,
166 APP_STATE_STARTING
169 public:
170 CamuleApp();
171 virtual ~CamuleApp();
173 virtual bool OnInit();
174 int OnExit();
175 #if wxUSE_ON_FATAL_EXCEPTION
176 void OnFatalException();
177 #endif
178 bool ReinitializeNetwork(wxString *msg);
180 // derived classes may override those
181 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
183 // Socket handlers
184 void ListenSocketHandler(wxSocketEvent& event);
185 void ServerSocketHandler(wxSocketEvent& event);
186 void UDPSocketHandler(wxSocketEvent& event);
188 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
190 // Barry - To find out if app is running or shutting/shut down
191 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
192 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
194 // Check ED2K and Kademlia state
195 bool IsFirewalled() const;
196 // Are we connected to at least one network?
197 bool IsConnected() const;
198 // Connection to ED2K
199 bool IsConnectedED2K() const;
201 // What about Kad? Is it running?
202 bool IsKadRunning() const;
203 // Connection to Kad
204 bool IsConnectedKad() const;
205 // Check Kad state (TCP)
206 bool IsFirewalledKad() const;
207 // Check Kad state (UDP)
208 bool IsFirewalledKadUDP() const;
209 // Check Kad state (LAN mode)
210 bool IsKadRunningInLanMode() const;
211 // Kad stats
212 uint32 GetKadUsers() const;
213 uint32 GetKadFiles() const;
214 uint32 GetKadIndexedSources() const;
215 uint32 GetKadIndexedKeywords() const;
216 uint32 GetKadIndexedNotes() const;
217 uint32 GetKadIndexedLoad() const;
218 // True IP of machine
219 uint32 GetKadIPAdress() const;
220 // Buddy status
221 uint8 GetBuddyStatus() const;
222 uint32 GetBuddyIP() const;
223 uint32 GetBuddyPort() const;
225 // Check if we should callback this client
226 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
228 // Misc functions
229 void OnlineSig(bool zero = false);
230 void Localize_mule();
231 void Trigger_New_version(wxString newMule);
233 // shakraw - new EC code using wxSocketBase
234 ExternalConn* ECServerHandler;
236 // return current (valid) public IP or 0 if unknown
237 // If ignorelocal is true, don't use m_localip
238 uint32 GetPublicIP(bool ignorelocal = false) const;
239 void SetPublicIP(const uint32 dwIP);
241 uint32 GetED2KID() const;
242 uint32 GetID() const;
244 // Other parts of the interface and such
245 CPreferences* glob_prefs;
246 CDownloadQueue* downloadqueue;
247 CUploadQueue* uploadqueue;
248 CServerConnect* serverconnect;
249 CSharedFileList* sharedfiles;
250 CServerList* serverlist;
251 CListenSocket* listensocket;
252 CClientList* clientlist;
253 CKnownFileList* knownfiles;
254 CCanceledFileList* canceledfiles;
255 CSearchList* searchlist;
256 CClientCreditsList* clientcredits;
257 CFriendList* friendlist;
258 CClientUDPSocket* clientudp;
259 CStatistics* m_statistics;
260 CIPFilter* ipfilter;
261 UploadBandwidthThrottler* uploadBandwidthThrottler;
262 CAsioService* m_AsioService;
263 #ifdef ENABLE_UPNP
264 CUPnPControlPoint* m_upnp;
265 std::vector<CUPnPPortMapping> m_upnpMappings;
266 #endif
267 wxLocale m_locale;
269 void ShutDown();
271 wxString GetLog(bool reset = false);
272 wxString GetServerLog(bool reset = false);
273 wxString GetDebugLog(bool reset = false);
275 bool AddServer(CServer *srv, bool fromUser = false);
276 void AddServerMessageLine(wxString &msg);
277 #ifdef __DEBUG__
278 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
279 #endif
280 void SetOSFiles(const wxString& new_path);
282 const wxString& GetOSType() const { return OSType; }
284 void ShowUserCount();
286 void ShowConnectionState(bool forceUpdate = false);
288 void StartKad();
289 void StopKad();
291 /** Bootstraps kad from the specified IP (must be in hostorder). */
292 void BootstrapKad(uint32 ip, uint16 port);
293 /** Updates the nodes.dat file from the specified url. */
294 void UpdateNotesDat(const wxString& str);
297 void DisconnectED2K();
299 bool CryptoAvailable() const;
301 protected:
303 #ifdef __WXDEBUG__
305 * Handles asserts in a thread-safe manner.
307 virtual void OnAssertFailure(const wxChar* file, int line,
308 const wxChar* func, const wxChar* cond, const wxChar* msg);
309 #endif
311 void OnUDPDnsDone(CMuleInternalEvent& evt);
312 void OnSourceDnsDone(CMuleInternalEvent& evt);
313 void OnServerDnsDone(CMuleInternalEvent& evt);
315 void OnTCPTimer(CTimerEvent& evt);
316 void OnCoreTimer(CTimerEvent& evt);
318 void OnFinishedHashing(CHashingEvent& evt);
319 void OnFinishedAICHHashing(CHashingEvent& evt);
320 void OnFinishedCompletion(CCompletionEvent& evt);
321 void OnFinishedAllocation(CAllocFinishedEvent& evt);
322 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
323 void OnHashingShutdown(CMuleInternalEvent&);
324 void OnNotifyEvent(CMuleGUIEvent& evt);
326 void SetTimeOnTransfer();
328 APPState m_app_state;
330 wxString m_emulesig_path;
331 wxString m_amulesig_path;
333 uint32 m_dwPublicIP;
335 long webserver_pid;
337 wxString server_msg;
339 CTimer* core_timer;
341 private:
342 virtual void OnUnhandledException();
344 void CheckNewVersion(uint32 result);
346 uint32 m_localip;
350 #ifndef AMULE_DAEMON
353 class CamuleGuiBase {
354 public:
355 CamuleGuiBase();
356 virtual ~CamuleGuiBase();
358 wxString m_FrameTitle;
359 CamuleDlg* amuledlg;
360 int m_FileDetailDialogActive;
362 bool CopyTextToClipboard( wxString strText );
363 void ResetTitle();
365 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
366 virtual int ShowAlert(wxString msg, wxString title, int flags);
368 void AddGuiLogLine(const wxString& line);
369 protected:
371 * This list is used to contain log messages that are to be displayed
372 * on the GUI, when it is currently impossible to do so. This is in order
373 * to allows us to queue messages till after the dialog has been created.
375 std::list<wxString> m_logLines;
379 #ifndef CLIENT_GUI
382 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
385 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
387 int OnExit();
388 bool OnInit();
390 public:
392 virtual int ShowAlert(wxString msg, wxString title, int flags);
394 void ShutDown(wxCloseEvent &evt);
396 wxString GetLog(bool reset = false);
397 wxString GetServerLog(bool reset = false);
398 void AddServerMessageLine(wxString &msg);
399 DECLARE_EVENT_TABLE()
403 DECLARE_APP(CamuleGuiApp)
404 extern CamuleGuiApp *theApp;
407 #else /* !CLIENT_GUI */
410 #include "amule-remote-gui.h"
413 #endif // CLIENT_GUI
416 #define CALL_APP_DATA_LOCK
419 #else /* ! AMULE_DAEMON */
421 // wxWidgets 2.8 requires special code for event handling and sockets.
422 // 2.9 doesn't, so standard event loop and sockets can be used
424 // Windows: aMuled compiles with 2.8 (without the special code),
425 // but works only with 2.9
427 #if !wxCHECK_VERSION(2, 9, 0)
428 // wx 2.8 needs a hand-made event loop in any case
429 #define AMULED28_EVENTLOOP
431 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
433 #ifdef HAVE_CONFIG_H
434 # include "config.h" // defines ASIO_SOCKETS
435 #endif
437 #ifndef ASIO_SOCKETS
438 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
439 #ifndef __WXMSW__
440 #define AMULED28_SOCKETS
441 #endif
442 #endif
443 #endif
445 #ifdef AMULED28_SOCKETS
446 #include <wx/socket.h>
448 class CSocketSet;
451 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
453 private:
454 CSocketSet *m_in_set, *m_out_set;
456 wxMutex m_lock;
457 public:
458 CAmuledGSocketFuncTable();
460 void AddSocket(GSocket *socket, GSocketEvent event);
461 void RemoveSocket(GSocket *socket, GSocketEvent event);
462 void RunSelect();
464 virtual bool OnInit();
465 virtual void OnExit();
466 virtual bool CanUseEventLoop();
467 virtual bool Init_Socket(GSocket *socket);
468 virtual void Destroy_Socket(GSocket *socket);
469 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
470 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
471 virtual void Enable_Events(GSocket *socket);
472 virtual void Disable_Events(GSocket *socket);
476 #endif // AMULED28_SOCKETS
478 // no AppTraits used on Windows
479 #ifndef __WXMSW__
481 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
483 #include <wx/apptrait.h>
485 class CDaemonAppTraits : public wxConsoleAppTraits
487 private:
488 struct sigaction m_oldSignalChildAction;
489 struct sigaction m_newSignalChildAction;
491 #ifdef AMULED28_SOCKETS
492 CAmuledGSocketFuncTable *m_table;
493 wxMutex m_lock;
494 std::list<wxObject *> m_sched_delete;
495 public:
496 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
497 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
498 virtual void ScheduleForDestroy(wxObject *object);
499 virtual void RemoveFromPendingDelete(wxObject *object);
501 void DeletePending();
502 #else // AMULED28_SOCKETS
503 public:
504 CDaemonAppTraits();
505 #endif // !AMULED28_SOCKETS
507 virtual int WaitForChild(wxExecuteData& execData);
509 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
510 virtual wxStandardPathsBase& GetStandardPaths();
511 #endif
514 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
515 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
516 #endif // __WXMSW__
519 class CamuleDaemonApp : public CamuleApp
521 private:
522 #ifdef AMULED28_EVENTLOOP
523 bool m_Exit;
524 #endif
525 #ifdef AMULED28_SOCKETS
526 CAmuledGSocketFuncTable *m_table;
527 #endif
528 bool OnInit();
529 int OnRun();
530 int OnExit();
532 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
534 #ifndef __WXMSW__
535 struct sigaction m_oldSignalChildAction;
536 struct sigaction m_newSignalChildAction;
537 public:
538 wxAppTraits *CreateTraits();
539 #endif // __WXMSW__
541 public:
543 #ifdef AMULED28_EVENTLOOP
544 CamuleDaemonApp();
546 void ExitMainLoop() { m_Exit = true; }
547 #endif
549 bool CopyTextToClipboard(wxString strText);
551 virtual int ShowAlert(wxString msg, wxString title, int flags);
553 DECLARE_EVENT_TABLE()
556 DECLARE_APP(CamuleDaemonApp)
557 extern CamuleDaemonApp *theApp;
559 #endif /* ! AMULE_DAEMON */
561 #endif // AMULE_H
562 // File_checked_for_headers