⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 engine.h

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 H
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program 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; either version 2
of the License, or (at your option) any later version.

This program 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.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#ifndef EngineH
#define EngineH

#include <windows.h>
#include <list>
#include <queue>

#include "Config.h"
#include "istring.h"
#include "CriticalSection.h"
#include "GiftCommand.h"
#include "Searching.h"
#include "Downloading.h"
#include "Uploading.h"
#include "Sharing.h"
#include "Networks.h"
#include "User.h"
#include "Hash.h"
#include "GiftLauncher.h"
//---------------------------------------------------------------------------

namespace KCeasyEngine {

using std::list;
using std::queue;

// Callback

static const UINT WM_ENGINE_CALLBACK = WM_USER + 0x200;

typedef enum
{
    CbcNoop = 0x00,         // never sent
    CbcDebug,               // Data1 is 0 for client and 1 for server packet, Data2 points to std::string containing the packet
    CbcGiftLog,             // Data1 points to std::string containing one or more lines of giFT's stdout loggind, Data2 is NULL
    // online status
    CbcStateChange,         // Data1 is TEngine::TState, Data2 is 1 if giFT crashed, 0 otherwise
    CbcNetworkError,        // Data1 points to std::string containing error description, Data2 is NULL
    // network stats
    CbcNetworksUpdate,      // Data1 points to TNetworks, Data2 is NULL
    // searching
    CbcNewSearch,           // Data1 points to TSearch, Data2 is NULL
    CbcSearchBegin,         // Data1 points to TSearch, Data2 is NULL
    CbcSearchResult,        // Data1 points to TSearch, Data2 to list<TSearchResult*> (if Data2 is NULL search finished)
    CbcSearchEnd,           // Data1 points to TSearch, Data2 is NULL
    CbcSearchRemoved,       // Data1 points to TSearch, Data2 is NULL
    // downloading
    CbcNewDownload,         // Data1 points to TDownload, Data2 is NULL
    CbcDownloadUpdate,      // Data1 points to TDownload, Data2 is NULL
    CbcDownloadRemoved,     // Data1 points to TDownload, Data2 is NULL
    CbcDownloadAddSource,   // Data1 points to TDownload, Data2 points to TDlSource
    CbcDownloadDelSource,   // Data1 points to TDownload, Data2 points to TDlSource
    // uploading
    CbcNewUpload,           // Data1 points to TUpload, Data2 is NULL
    CbcUploadUpdate,        // Data1 points to TUpload, Data2 is NULL
    CbcUploadRemoved,       // Data1 points to TUpload, Data2 is NULL
    // sharing
    CbcSharesSyncUpdate,            // Data1 points to TShares, Data2 is number of currently hashing file
    CbcSharesHiddenUpdate,          // Data1 points to TShares, Data2 is NULL
    CbcSharesDownloadDirsUpdate,    // Data1 points to TShares, Data2 is NULL
    CbcSharedDirsUpdate,            // Data1 points to TShares, Data2 is 1 if this was sent in response to AddDir/RemoveDir/SetDirs, 0 otherwise

    CbcSharesAddFile,       // Data1 points to TShares, Data2 points to TSharedFile
    CbcSharesDelFile,       // Data1 points to TShares, Data2 points to TSharedFile (if Data2 is NULL all files are removed)
    CbcSharesChangeFile,    // Data1 points to TShares, Data2 points to TSharedFile

} TCallbackCode;

class TCallbackInfo
{
public:
    TCallbackInfo() : Code(CbcNoop), Data1(NULL), Data2(NULL) {}
    TCallbackInfo(TCallbackCode c, void* d1 = NULL, void* d2 = NULL) : Code(c), Data1(d1), Data2(d2) {}

    TCallbackCode Code;
    void* Data1;                 // command specific data
    void* Data2;                 // command specific data
};

// Engine

class TSearchResult;

class TEngine
{
friend class TSearch;
friend class TDownload;
friend class TUpload;
friend class TShares;
friend class TNetworks;
friend DWORD WINAPI g_SocketThreadFunc(void* data);
public:
    typedef list<TSearch*>::iterator TSearchIterator;
    typedef list<TDownload*>::iterator TDownloadIterator;
    typedef list<TUpload*>::iterator TUploadIterator;
    typedef enum
    {
        Offline = 0x00,
        Connecting,
        LaunchingGift,
        LaunchFailed,
        LaunchSucceeded,
        ConnectFailed,
        Online,
        Disconnecting
    } TState;

    TEngine(const char* NClientName = NULL, const char* NClientVersion = NULL);
    ~TEngine();

    void Init();
    bool GiftInstalled() { return GiftLauncher->IsInstalled(); }
    bool IsUsingLocalGift() { return UsingLocalGift; }
    TGiftLauncher* GetLauncher() { return GiftLauncher; }
    const string& GetGiftPath() { return GiftLauncher->GetGiftPath(); }
    TFileConfig* GetGiftConf() { return GiftLauncher->GetGiftConf(); }

    bool TurnOnline(); // starts and connects to internal giFT, returns false if giFT is not installed
    void TurnOnline(const string& Host, unsigned short Port); // connects to remote gift
    void TurnOffline();
    unsigned int GetUptime(); // returns number of seconds we are/were online

    TState GetState() { return State; }
    bool IsOnline() { return (GetState() == Online); }
    bool IsConnecting() { return (GetState() == Connecting || GetState() == LaunchingGift || GetState() == LaunchSucceeded); }
    bool IsDisconnecting() { return (GetState() == Disconnecting); }
    bool IsOffline() { return (!IsOnline() && !IsConnecting() && !IsDisconnecting()); }
    const string& GetGiftHost() { return GiftHost; }
    unsigned short GetGiftPort() { return GiftPort; }

    void SetCallbackWnd(HWND CbWnd) { CallbackWindow = CbWnd; }
    HWND GetCallbackWnd() { return CallbackWindow; }

    void SetDebug(bool NDebug) { Debug = NDebug; }
    bool GetDebug() { return Debug; }
    void SetGiftLogging(bool Logging) { GiftLogging = Logging; }
    bool GetGiftLogging() { return GiftLogging; }

    // banned words and file extensions
    list<string> GetBannedWords();
    void SetBannedWords(const list<string>& Words);
    list<string> GetBannedExtensions();
    void SetBannedExtensions(const list<string>& Extensions);

    // searching
    void LockSearches() { SearchesCritSec.Enter(); }
    void ReleaseSearches() { SearchesCritSec.Leave(); }
    TSearchIterator GetSearchesBegin() { return Searches.begin(); }
    TSearchIterator GetSearchesEnd() { return Searches.end(); }
    TSearch* NewSearch(const string& Query, TSearchRealm Realm = SRAny, const string& Network = "", const TMetaData& MetaData = TMetaData(), bool BanWords = false, bool BanExtensions = false, bool Intern = false);
    TSearch* NewSearch(const THashSet* Hashes, const string& Network = "", bool Intern = false);
    bool RemoveSearch(TSearch* Search);
    int GetActiveSearchesCount(const string& Network = "");
    int GetActiveHashSearchesCount(const string& Network = "");

    // downloading
    void LockDownloads() { DownloadsCritSec.Enter(); }
    void ReleaseDownloads() { DownloadsCritSec.Leave(); }
    TDownloadIterator GetDownloadsBegin() { return Downloads.begin(); }
    TDownloadIterator GetDownloadsEnd() { return Downloads.end(); }
    int GetDownloadsCount() { return Downloads.size(); };
    TDownload* NewDownload(const TSearchResult* SearchResult, const string& SaveFileName = string());
    TDownload* NewDownload(const THashSet* Hashes, const string& Network, const string& SaveFileName);
    bool RemoveDownload(TDownload* Download);
    TDownload* GetDownloadByHashes(const THashSet* Hashes);

    // uploading
    void LockUploads() { UploadsCritSec.Enter(); }
    void ReleaseUploads() { UploadsCritSec.Leave(); }
    TUploadIterator GetUploadsBegin() { return Uploads.begin(); }
    TUploadIterator GetUploadsEnd() { return Uploads.end(); }
    int GetUploadsCount() { return Uploads.size(); };
    bool RemoveUpload(TUpload* Upload);
    TUpload* GetUploadByHashes(const THashSet* Hashes);

    // sharing
    TShares* GetShares() { return Shares; }

    // networks
    TNetworks* GetNetworks() { return Networks; }

    void* GetUData() { return UData; }
    void SetUData(void* NUData) { UData = NUData; }

private:
    void* UData;

    string ClientName;
    string ClientVersion;

    void Reset();
    void SocketThreadFunc();
    HANDLE SocketThreadHandle;
    void TimerFunc();
    int AutoSearchCountdown;
    bool ExitSocketThread;
    bool Debug;
    bool GiftLogging;
    string GiftLog;
    TState State;
    void SetState(TState NState, bool NotifyUI = false, bool GiftCrashed = false);
    unsigned int ConnectTime, DisconnectTime; // time in seconds we last connected/disconnected

    // gift
    TGiftLauncher* GiftLauncher;
    bool UsingLocalGift;
    string GiftHost;
    unsigned short GiftPort;
    bool ReadGiftLog();

    void QueueCommand(TGiftCommand* Cmd);
    queue<TGiftCommand*> OutQueue;
    TCriticalSection OutQueueCritSec;

    void DispatchCommand(TGiftCommand* Cmd);
    bool SendCallback(TCallbackCode Code, void* Param1 = NULL, void* Param2 = NULL);
    HWND CallbackWindow;

    static const unsigned int MinGiftId = 1;
    static const unsigned int MaxGiftId = 32767;
    unsigned int NextGiftId;
    unsigned int GetNextGiftId();

    // result containing these words/file extensions are ignored
    list<string> BannedWordsList;
    list<string> BannedExtensionsList;

    list<TSearch*> Searches;
    TCriticalSection SearchesCritSec;

    list<TDownload*> Downloads;
    TCriticalSection DownloadsCritSec;

    list<TUpload*> Uploads;
    TCriticalSection UploadsCritSec;

    TShares* Shares;

    TNetworks* Networks;
};

} // namespace KCeasyEngine

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -