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

📄 btsession.h

📁 模拟P2P各种网络环境的,适合新手们的学习,不错的源码.
💻 H
字号:
#ifndef __BTSESSION_H__
#define __BTSESSION_H__
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <list>
#include<vector>
#include "SimEventHandler.h"

class BTTorrent;
class BTPeer;
class BTEvent;
class BTSocket;
class SimEvent;
class BTAlgorithmRechoke;
class BTDocument;
using namespace std;
typedef enum{
	working,
	completed,
	stopped,
	failed
}SessionState;

typedef struct{
	unsigned int index;
	unsigned int peerNum;
}pieceInfo;

class BTSession:public SimEventHandler{
public:
	SessionState 				mState;
	BTDocument* 			mDocument;
	BTTorrent*				mTorrent;
	BTPeer*					mAgent;
	map<BTPeer*, BTSocket*>	mConnections;
	vector<BTPeer*> 			mPeerCandidates;
	set<BTPeer*> 			mUnchokeLists;
	BTEvent*					mNextTrackerAnnouncement;
	vector<list<BTPeer*>*>	mKnowledges;
	BTAlgorithmRechoke* 		mRechoke;
	double					mAmountBetweenSnubDetect;
	list<BTSocket*>			mConnectTimoutList;
	list<BTSocket*>			mRequestTimoutList;
	unsigned int				mOutPutNum;
	vector<unsigned int>           mDownloadInfo;
	bool 					mActive;
	float						mStartTime;
	unsigned int 				mLastFinishedNum;
	bool						mCouldRetire;
	BTEvent*					mTrackerRequest;

public:
	BTSession(BTTorrent* , BTPeer*);
	BTSession(string, BTPeer*);
	~BTSession(void);
	BTPeer* getAgent(void){
		return mAgent;
	};
	BTDocument* getDocument(void){
		return mDocument;
	};
	BTTorrent* getTorrent(void){
		return mTorrent;
	}
	void SetTorrent(BTTorrent* tt){
		mTorrent = tt;
	};
	void addKnowledgePiece(unsigned int index, BTPeer* ptr);
	bool pieceInKnowledge(int index){
		if( mKnowledges[index]->empty())
			return false;
		else
			return true;
	}
	void setAmountBetweenSnubDect(double m){
		mAmountBetweenSnubDetect = m;
	};
	double getAmountBetweenSnubDect(void){
		return mAmountBetweenSnubDetect;
	};
	set<BTPeer*>* getUnchoklist(void){
		return &mUnchokeLists;
	}
	vector<BTPeer*>* getPeerCandidates(void){
		return &mPeerCandidates;
	}
	void addPeerCandidates( BTPeer* peer);
	void removePeerFromCandidates( BTPeer* peer){
		vector<BTPeer*>::iterator aIt = find( mPeerCandidates.begin(), mPeerCandidates.end(), peer);
		if( aIt != mPeerCandidates.end())
			mPeerCandidates.erase(aIt);
	}
	bool inCandidate(BTPeer* peer){
		vector<BTPeer*>::iterator aIt = find( mPeerCandidates.begin(), mPeerCandidates.end(), peer);
		if( aIt != mPeerCandidates.end())
			return true;
		else 
			return false;
	}
	bool inUnchokeList( BTPeer* peer){
		set<BTPeer*>::iterator aIt = mUnchokeLists.find(peer);
		if( aIt != mUnchokeLists.end())
			return true;
		else
			return false;
	}
	void removePeerFromUnchokeList( BTPeer* peer){
		set<BTPeer*>::iterator aIt = mUnchokeLists.find( peer );
		if( aIt != mUnchokeLists.end())
			mUnchokeLists.erase(aIt);
	}
	vector< BTSocket*>* getConnections(void);
	void	removeConnection(BTPeer*);
	void removeConnection( BTSocket*);
	void closeConnection(BTSocket*);
	void queryTracker(void);
	void announceTracker(void);
	void announcePeers(unsigned int );
	void handler(SimEvent*);
	void handleTrackerResponse(BTEvent*);
	void handleConnectionTimeout(BTEvent*);
	void handleSessionTimeout(BTEvent*);
	void handlePeerMessage(BTEvent*);
	void	handlePMBitfield(BTEvent*);
	void handlePMInsterested(BTEvent*);
	void handlePMNotInsterested(BTEvent*);
	void	handlePMChoke(BTEvent*);
	void handlePMUnChoke(BTEvent*);
	void handlePMHave(BTEvent*);
	void handlePMRequest(BTEvent*);
	void handlePMPiece(BTEvent*);
	void handlePMCancel(BTEvent*);
	list<BTPeer*>* selectPeers(vector<BTPeer*>*);
	bool selectPiece(BTPeer *, unsigned int* );
	void connectTo(BTPeer*, bool );
	bool peerInConnections(BTPeer* peer){
		map<BTPeer*, BTSocket*>::iterator aIt = mConnections.find(peer);
		if( aIt != mConnections.end())
			return true;
		else
			return false;

	};
	BTSocket* getConnection(BTPeer* peer);
	BTSocket* addConnection(BTPeer* peer);
	void sendInterested(bool, BTSocket*);
	void sendInterestedTo(bool, BTSocket*);
	void sendChoke(bool, BTSocket*);
	void sendRequest(BTPeer*, int);
	void sendRequestTo(BTPeer*);
	unsigned int countPiecePeers( unsigned int index);
	vector<pieceInfo>* makePieceInfoList(vector< unsigned int>*);
	void handleConnectionTimeout( void);
	void handleRequestTimeout(void);
	void checkTimeout(void);
	void addPieceDownload(unsigned int index);
	void printPieceDownloadInfo( void);
	void removePeerFromKnowledge( BTPeer* peer);
	 
	unsigned int countAvlFileNum(void){
		unsigned int num = mKnowledges[0]->size();
		for( unsigned int i = 0; i< mKnowledges.size(); i++){
			if( mKnowledges[i]->size() ==0 )
				return 0;
			if( mKnowledges[i]->size() < num)
				num = mKnowledges[i]->size();
		}
		return num;

	}
	bool retireWork(void);
	bool ifNoInterestedPeer(void);
	bool ifNoAvailablePeer(void);
	void requestEsp(void);
	void recoverEsp( void );
	bool ifUpload(void);
	bool checkRetire(void);
	unsigned getActiveCon( void ){
		return mUnchokeLists.size();

	};
};
class lessPeerNum{
	public:
	bool operator()( pieceInfo const&, pieceInfo const&)const;
};
class OutlessCompare{
	public:
		bool operator()(BTSocket* const &, BTSocket* const &) const;
};
#endif

⌨️ 快捷键说明

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