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

📄 servmgr.cpp

📁 这是和p2p相关的一份源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ------------------------------------------------// File : servmgr.cpp// Date: 4-apr-2002// Author: giles// Desc: //		Management class for handling multiple servent connections.//// (c) 2002 peercast.org// ------------------------------------------------// 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.// ------------------------------------------------#include <stdlib.h>#include "servent.h"#include "servmgr.h"#include "inifile.h"#include "stats.h"#include "peercast.h"static int debugtest=0;// -----------------------------------ServMgr::ServMgr(){	int i;	for(i=0; i<MAX_SERVENTS; i++)		servents[i].init();	authType = AUTH_COOKIE;	cookieList.init();	startTime = sys->getTime();	allowServer1 = Servent::ALLOW_ALL;	allowServer2 = Servent::ALLOW_BROADCAST;	clearHostCache();	password[0]=0;	allowGnutella = false;	maxBitrate = MAX_BITRATE;	maxIncoming = MAX_INCOMING;	maxOutgoing = MAX_OUTGOING;	maxTryout = MAX_TRYOUT;	minConnected = MIN_CONNECTED;	maxStreams = 0;	refreshHTML = 5;	notifyMask = 0xffff;	tryoutDelay = 10;	relayBroadcast = 300;	serventBandwidth = 1000;	numVersions = 0;	sessionID.generate();	isRoot = false;	forceIP[0]	= 0;		// force server IP	strcpy(connectHost,"connect1.peercast.org");	serverHost.fromStr("127.0.0.1:7144");	network.set("peercast");	firewalled = FW_UNKNOWN;			allowDirect = true;	autoConnect = true;	forceLookup = true;	autoServe = true;	forceNormal = false;	replyIDs.init(500);	// 500 active packets	totalStreams = 0;	firewallTimeout = 30;	pauseLog = false;	showLog = 0;	shutdownTimer = 0;	downloadURL[0] = 0;	rootMsg.clear();	restartServer=false;	setFilterDefaults();}// -----------------------------------void	ServMgr::addVersion(unsigned int ver){	for(int i=0; i<numVersions; i++)		if (clientVersions[i] == ver)		{			clientCounts[i]++;			return;		}	if (numVersions < MAX_VERSIONS)	{		clientVersions[numVersions] = ver;		clientCounts[numVersions] = 1;		numVersions++;	}}// -----------------------------------void ServMgr::setFilterDefaults(){	numFilters = 0;	filters[numFilters].host.fromStr("255.255.255.255");	filters[numFilters].flags = ServFilter::F_ALLOW;	numFilters++;	filters[numFilters].host.fromStr("127.0.0.1");	filters[numFilters].flags = ServFilter::F_PRIVATE|ServFilter::F_ALLOW;	numFilters++;}// -----------------------------------void	ServMgr::setPassiveSearch(unsigned int t){//	if ((t > 0) && (t < 60))//		t = 60;//	passiveSearch = t;}// -----------------------------------void ServMgr::addHost(Host &h, bool force){	if (!h.isValid() || h.loopbackIP())		return;	if (!force)		for(int i=0; i<MAX_HOSTCACHE; i++)			if (hostCache[i].ip == h.ip)				if (hostCache[i].port == h.port)					return;	char str[64];	h.toStr(str);	LOG_NETWORK("New host: %s",str);	h.value = 0;	// make sure dead count is zero	hostCache[writeHostPtr] = h;	readHostPtr = writeHostPtr;		// set read ptr to get this host next	writeHostPtr = (writeHostPtr+1)%MAX_HOSTCACHE;}// -----------------------------------void ServMgr::deadHost(Host &h){	for(int i=0; i<MAX_HOSTCACHE; i++)		if (hostCache[i].ip == h.ip)			if (hostCache[i].port == h.port)				hostCache[i].init();}// -----------------------------------void ServMgr::clearHostCache(){	memset(hostCache,0,MAX_HOSTCACHE*sizeof(Host));	readHostPtr=writeHostPtr=0;}	// -----------------------------------unsigned int ServMgr::numHosts(){	unsigned int cnt = 0;	for(int i=0; i<MAX_HOSTCACHE; i++)		if (hostCache[i].ip != 0)			if (hostCache[i].port != 0)				cnt++;	return cnt;}// -----------------------------------Host ServMgr::getHost(){	Host h(0,0);		for(int i=0; i<MAX_HOSTCACHE; i++)	{		h = hostCache[readHostPtr];		readHostPtr = (readHostPtr-1)%MAX_HOSTCACHE;		if (h.ip != 0)			break;	}	return h;}// -----------------------------------int ServMgr::getNewestHosts(Host *hl,int max,Host &rh){	int p = (writeHostPtr > 0) ? writeHostPtr-1 : MAX_HOSTCACHE-1;	int cnt=0;	for(int i=0; i<MAX_HOSTCACHE; i++)	{		Host *h = &hostCache[p];		if (!((rh.globalIP() && !h->globalIP()) || rh.isSame(*h) || (h->ip==0)))		{			hl[cnt++] = *h;			if (cnt >= max)				break;		}		if (p>0)			p--;		else			p = (writeHostPtr > 0) ? writeHostPtr-1 : MAX_HOSTCACHE-1;	}	return cnt;}// -----------------------------------Servent *ServMgr::findServent(unsigned int ip, unsigned short port){	lock.on();	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->type != Servent::T_NONE)		{			Host h = s->getHost();			if ((h.ip == ip) && (h.port == port))			{				lock.off();				return s;			}		}	}	lock.off();	return NULL;}// -----------------------------------Servent *ServMgr::findServent(Servent::TYPE t){	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->type == t)			return s;	}	return NULL;}// -----------------------------------Servent *ServMgr::allocServent(){	lock.on();	Servent *s = findServent(Servent::T_NONE);	if (s)		s->type = Servent::T_ALLOCATED;	lock.off();	if (!s) 		return NULL;	return s;}// -----------------------------------unsigned int ServMgr::numConnected(int type){	unsigned int cnt=0;	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->status == Servent::S_CONNECTED)			if (s->type == type)				cnt++;	}	return cnt;}// -----------------------------------unsigned int ServMgr::numUsed(int type){	unsigned int cnt=0;	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->type == type)			cnt++;	}	return cnt;}// -----------------------------------unsigned int ServMgr::numStreams(bool all){	unsigned int cnt=0;	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->status == Servent::S_CONNECTED)			if (s->type == Servent::T_STREAM)				if (all || !s->isPrivate())					cnt++;	}	return cnt;}// -----------------------------------unsigned int ServMgr::totalOutput(bool all){	unsigned int tot = 0;	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->status == Servent::S_CONNECTED)			if (s->type == Servent::T_STREAM)				if (all || !s->isPrivate())					if (s->sock)						tot += s->sock->bytesOutPerSec;	}	return tot;}// -----------------------------------unsigned int ServMgr::numOutgoing(){	int cnt=0;	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if ((s->type == Servent::T_INCOMING) ||		    (s->type == Servent::T_OUTGOING)) 			cnt++;	}	return cnt;}// -----------------------------------bool ServMgr::seenPacket(GnuPacket &p){	for(int i=0; i<MAX_SERVENTS; i++)	{		Servent *s = &servents[i];		if (s->status == Servent::S_CONNECTED)			if (s->seenIDs.contains(p.id))				return true;	}	return false;}// -----------------------------------int ServMgr::broadcast(GnuPacket &pack,Servent *src){	int cnt=0;	if (pack.ttl)	{		for(int i=0; i<MAX_SERVENTS; i++)		{			Servent *s = &servents[i];			if (s != src)				if (s->type != Servent::T_STREAM)					if (s->status == Servent::S_CONNECTED)						if (!s->seenIDs.contains(pack.id))							if (s->outputPacket(pack,false))								cnt++;		}	}	LOG_NETWORK("broadcast: %s (%d) to %d servents",GNU_FUNC_STR(pack.func),pack.ttl,cnt);	return cnt;}// -----------------------------------int ServMgr::route(GnuPacket &pack, GnuID &routeID, Servent *src){	int cnt=0;	if (pack.ttl)	{		for(int i=0; i<MAX_SERVENTS; i++)		{			Servent *s = &servents[i];			if (s != src)				if (s->type != Servent::T_STREAM)					if (s->status == Servent::S_CONNECTED)						if (!s->seenIDs.contains(pack.id))							if (s->seenIDs.contains(routeID))								if (s->outputPacket(pack,true))									cnt++;		}	}	LOG_NETWORK("route: %s (%d) to %d servents",GNU_FUNC_STR(pack.func),pack.ttl,cnt);	return cnt;}// -----------------------------------void ServMgr::setFirewall(FW_STATE state){	if (firewalled != state)	{		char *str;		switch (state)		{			case FW_ON:				str = "ON";				break;			case FW_OFF:				str = "OFF";				break;			case FW_UNKNOWN:			default:				str = "UNKNOWN";				break;		}		LOG_DEBUG("Firewall is set to %s",str);		firewalled = state;	}}// -----------------------------------bool ServMgr::isFiltered(int fl, Host &h){	for(int i=0; i<numFilters; i++)		if (filters[i].flags & fl)			if (h.isMemberOf(filters[i].host))				return true;	return false;}#if 0// -----------------------------------bool ServMgr::canServeHost(Host &h){	if (server)	{		Host sh = server->getHost();		if (sh.globalIP() || (sh.localIP() && h.localIP()))			return true;			}	return false;}#endif// --------------------------------------------------void writeServerSettings(IniFile &iniFile, unsigned int a){	iniFile.writeBoolValue("allowHTML",a & Servent::ALLOW_HTML);	iniFile.writeBoolValue("allowData",a & Servent::ALLOW_DATA);	iniFile.writeBoolValue("allowServent",a & Servent::ALLOW_SERVENT);	iniFile.writeBoolValue("allowBroadcast",a & Servent::ALLOW_BROADCAST);}// --------------------------------------------------void writeFilterSettings(IniFile &iniFile, ServFilter &f){	char ipstr[64];	f.host.IPtoStr(ipstr);	iniFile.writeStrValue("ip",ipstr);	iniFile.writeBoolValue("private",f.flags & ServFilter::F_PRIVATE);	iniFile.writeBoolValue("ban",f.flags & ServFilter::F_BAN);	iniFile.writeBoolValue("allow",f.flags & ServFilter::F_ALLOW);}// --------------------------------------------------void ServMgr::saveSettings(const char *fn){	IniFile iniFile;	if (!iniFile.openWriteReplace(fn))	{		LOG_ERROR("Unable to open ini file");	}else{		LOG("Saving settings to:  %s",fn);		iniFile.writeSection("Server");		iniFile.writeIntValue("serverPort",servMgr->serverHost.port);		iniFile.writeBoolValue("autoServe",servMgr->autoServe);		iniFile.writeStrValue("icyPassword",servMgr->password);		iniFile.writeStrValue("forceIP",servMgr->forceIP);		iniFile.writeBoolValue("isRoot",servMgr->isRoot);		iniFile.writeIntValue("maxBitrate",servMgr->maxBitrate);		iniFile.writeIntValue("maxStreams",servMgr->maxStreams);		iniFile.writeIntValue("maxStreamsPerChannel",chanMgr->maxStreamsPerChannel);		iniFile.writeIntValue("maxIncoming",servMgr->maxIncoming);		iniFile.writeIntValue("maxOutgoing",servMgr->maxOutgoing);		iniFile.writeIntValue("maxTryout",servMgr->maxTryout);		iniFile.writeIntValue("minConnected",servMgr->minConnected);		iniFile.writeIntValue("serventBandwidth",servMgr->serventBandwidth);		iniFile.writeIntValue("firewallTimeout",firewallTimeout);		iniFile.writeBoolValue("forceNormal",forceNormal);		iniFile.writeStrValue("rootMsg",rootMsg.cstr());		iniFile.writeStrValue("authType",servMgr->authType==ServMgr::AUTH_COOKIE?"cookie":"http-basic");		iniFile.writeStrValue("cookiesExpire",servMgr->cookieList.neverExpire==true?"never":"session");		iniFile.writeStrValue("network",servMgr->network.cstr());		iniFile.writeSection("Client");		iniFile.writeBoolValue("autoConnect",servMgr->autoConnect);		iniFile.writeStrValue("lookupHost",servMgr->connectHost);		iniFile.writeIntValue("tryoutDelay",servMgr->tryoutDelay);		iniFile.writeIntValue("refreshHTML",refreshHTML);		iniFile.writeIntValue("relayBroadcast",servMgr->relayBroadcast);		iniFile.writeIntValue("broadcastTTL",chanMgr->broadcastTTL);		iniFile.writeIntValue("pushTries",chanMgr->pushTries);		iniFile.writeIntValue("pushTimeout",chanMgr->pushTimeout);		iniFile.writeIntValue("maxPushHops",chanMgr->maxPushHops);				iniFile.writeSection("Notify");			iniFile.writeBoolValue("PeerCast",notifyMask&NT_PEERCAST);			iniFile.writeBoolValue("Broadcasters",notifyMask&NT_BROADCASTERS);			iniFile.writeBoolValue("TrackInfo",notifyMask&NT_TRACKINFO);		iniFile.writeLine("[End]");		int i;		for(i=0; i<servMgr->numFilters; i++)		{			iniFile.writeSection("Filter");				writeFilterSettings(iniFile,servMgr->filters[i]);			iniFile.writeLine("[End]");		}		iniFile.writeSection("Server1");			writeServerSettings(iniFile,allowServer1);		iniFile.writeLine("[End]");		iniFile.writeSection("Server2");			writeServerSettings(iniFile,allowServer2);		iniFile.writeLine("[End]");		iniFile.writeSection("Broadcast");		iniFile.writeIntValue("broadcastMsgInterval",chanMgr->broadcastMsgInterval);		iniFile.writeStrValue("broadcastMsg",chanMgr->broadcastMsg.cstr());		iniFile.writeIntValue("icyMetaInterval",chanMgr->icyMetaInterval);		char idStr[64];		chanMgr->broadcastID.toStr(idStr);		iniFile.writeStrValue("broadcastID",idStr);				iniFile.writeIntValue("deadHitAge",chanMgr->deadHitAge);		iniFile.writeSection("Debug");		iniFile.writeBoolValue("logDebug",(showLog&(1<<LogBuffer::T_DEBUG))!=0);		iniFile.writeBoolValue("logErrors",(showLog&(1<<LogBuffer::T_ERROR))!=0);		iniFile.writeBoolValue("logNetwork",(showLog&(1<<LogBuffer::T_NETWORK))!=0);		iniFile.writeBoolValue("logChannel",(showLog&(1<<LogBuffer::T_CHANNEL))!=0);		iniFile.writeBoolValue("pauseLog",pauseLog);		iniFile.writeIntValue("idleSleepTime",sys->idleSleepTime);		for(i=0; i<ChanMgr::MAX_CHANNELS; i++)		{			char idstr[64];			Channel *c = &chanMgr->channels[i];			if (c->isActive())				if ( (c->type == Channel::T_RELAY) ||					 ((c->type == Channel::T_BROADCAST) && (c->srcType == Channel::SRC_URL)))				{					c->getIDStr(idstr);					iniFile.writeSection("RelayChannel");					iniFile.writeStrValue("name",c->getName());					iniFile.writeStrValue("genre",c->info.genre.cstr());					iniFile.writeStrValue("type",ChanInfo::getTypeStr(c->info.contentType));					iniFile.writeStrValue("contactURL",c->info.url.cstr());					iniFile.writeStrValue("id",idstr);					iniFile.writeBoolValue("stayConnected",c->stayConnected);					if (!c->sourceURL.isEmpty())						iniFile.writeStrValue("sourceURL",c->sourceURL.cstr());					iniFile.writeLine("[End]");				}		}				iniFile.writeSection("HostCache");		char str[64];		for(i=0; i<ServMgr::MAX_SERVENTS; i++)			if (servMgr->servents[i].type == Servent::T_OUTGOING)				if (servMgr->servents[i].status == Servent::S_CONNECTED)				{					Host h = servMgr->servents[i].getHost();					h.toStr(str);					iniFile.writeLine(str);

⌨️ 快捷键说明

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