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

📄 servmgr.cpp

📁 P2P应用 : Peercast的源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	if (!iniFile.openReadOnly(fn))		saveSettings(fn);	servMgr->numFilters = 0;	showLog = 0;	if (iniFile.openReadOnly(fn))	{		while (iniFile.readNext())		{			// server settings			if (iniFile.isName("serverPort"))				servMgr->serverHost.port = iniFile.getIntValue();			else if (iniFile.isName("autoServe"))				servMgr->autoServe = iniFile.getBoolValue();			else if (iniFile.isName("autoConnect"))				servMgr->autoConnect = iniFile.getBoolValue();			else if (iniFile.isName("icyPassword"))		// depreciated				strcpy(servMgr->password,iniFile.getStrValue());			else if (iniFile.isName("forceIP"))				servMgr->forceIP = iniFile.getStrValue();
			else if (iniFile.isName("isRoot"))				servMgr->isRoot = iniFile.getBoolValue();			else if (iniFile.isName("broadcastID"))
			{				chanMgr->broadcastID.fromStr(iniFile.getStrValue());
				chanMgr->broadcastID.id[0] = PCP_BROADCAST_FLAGS;			// hacky, but we need to fix old clients

			}else if (iniFile.isName("htmlPath"))
				strcpy(servMgr->htmlPath,iniFile.getStrValue());
			else if (iniFile.isName("maxPGNUIncoming"))
				servMgr->maxGnuIncoming = iniFile.getIntValue();
			else if (iniFile.isName("minPGNUIncoming"))
				servMgr->minGnuIncoming = iniFile.getIntValue();

			else if (iniFile.isName("maxControlConnections"))
			{
				servMgr->maxControl = iniFile.getIntValue();

			}			else if (iniFile.isName("maxBitrateOut"))				servMgr->maxBitrateOut = iniFile.getIntValue();
			else if (iniFile.isName("maxStreamsOut"))		// depreciated				servMgr->setMaxRelays(iniFile.getIntValue());
			else if (iniFile.isName("maxRelays"))		
				servMgr->setMaxRelays(iniFile.getIntValue());
			else if (iniFile.isName("maxDirect"))		
				servMgr->maxDirect = iniFile.getIntValue();
			else if (iniFile.isName("maxStreamsPerChannel"))		// depreciated				chanMgr->maxRelaysPerChannel = iniFile.getIntValue();			else if (iniFile.isName("maxRelaysPerChannel"))
				chanMgr->maxRelaysPerChannel = iniFile.getIntValue();
			else if (iniFile.isName("firewallTimeout"))				firewallTimeout = iniFile.getIntValue();			else if (iniFile.isName("forceNormal"))				forceNormal = iniFile.getBoolValue();			else if (iniFile.isName("broadcastMsgInterval"))				chanMgr->broadcastMsgInterval = iniFile.getIntValue();			else if (iniFile.isName("broadcastMsg"))				chanMgr->broadcastMsg.set(iniFile.getStrValue(),String::T_ASCII);			else if (iniFile.isName("hostUpdateInterval"))
				chanMgr->hostUpdateInterval = iniFile.getIntValue();
			else if (iniFile.isName("icyMetaInterval"))				chanMgr->icyMetaInterval = iniFile.getIntValue();
			else if (iniFile.isName("maxServIn"))
				servMgr->maxServIn = iniFile.getIntValue();
			else if (iniFile.isName("chanLog"))				servMgr->chanLog.set(iniFile.getStrValue(),String::T_ASCII);			else if (iniFile.isName("rootMsg"))				rootMsg.set(iniFile.getStrValue());			else if (iniFile.isName("networkID"))				networkID.fromStr(iniFile.getStrValue());			else if (iniFile.isName("authType"))			{				char *t = iniFile.getStrValue();				if (stricmp(t,"cookie")==0)					servMgr->authType = ServMgr::AUTH_COOKIE;				else if (stricmp(t,"http-basic")==0)					servMgr->authType = ServMgr::AUTH_HTTPBASIC;			}else if (iniFile.isName("cookiesExpire"))			{				char *t = iniFile.getStrValue();				if (stricmp(t,"never")==0)					servMgr->cookieList.neverExpire = true;				else if (stricmp(t,"session")==0)					servMgr->cookieList.neverExpire = false;			}			// privacy settings			else if (iniFile.isName("password"))				strcpy(servMgr->password,iniFile.getStrValue());			else if (iniFile.isName("maxUptime"))				chanMgr->maxUptime = iniFile.getIntValue();			// client settings
			else if (iniFile.isName("rootHost"))
			{
				if (!PCP_FORCE_YP)
					servMgr->rootHost = iniFile.getStrValue();

			}else if (iniFile.isName("deadHitAge"))				chanMgr->deadHitAge = iniFile.getIntValue();			else if (iniFile.isName("tryoutDelay"))				servMgr->tryoutDelay = iniFile.getIntValue();			else if (iniFile.isName("refreshHTML"))				refreshHTML = iniFile.getIntValue();			else if (iniFile.isName("relayBroadcast"))			{				servMgr->relayBroadcast = iniFile.getIntValue();				if (servMgr->relayBroadcast < 30)					servMgr->relayBroadcast = 30;			}			else if (iniFile.isName("minBroadcastTTL"))				chanMgr->minBroadcastTTL = iniFile.getIntValue();			else if (iniFile.isName("maxBroadcastTTL"))				chanMgr->maxBroadcastTTL = iniFile.getIntValue();			else if (iniFile.isName("pushTimeout"))				chanMgr->pushTimeout = iniFile.getIntValue();			else if (iniFile.isName("pushTries"))				chanMgr->pushTries = iniFile.getIntValue();			else if (iniFile.isName("maxPushHops"))				chanMgr->maxPushHops = iniFile.getIntValue();			else if (iniFile.isName("autoQuery"))			{				chanMgr->autoQuery = iniFile.getIntValue();				if ((chanMgr->autoQuery < 300) && (chanMgr->autoQuery > 0))					chanMgr->autoQuery = 300;			}			else if (iniFile.isName("queryTTL"))			{				servMgr->queryTTL = iniFile.getIntValue();			}			// debug			else if (iniFile.isName("logDebug"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_DEBUG:0;			else if (iniFile.isName("logErrors"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_ERROR:0;			else if (iniFile.isName("logNetwork"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_NETWORK:0;			else if (iniFile.isName("logChannel"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_CHANNEL:0;			else if (iniFile.isName("pauseLog"))				pauseLog = iniFile.getBoolValue();			else if (iniFile.isName("idleSleepTime"))				sys->idleSleepTime = iniFile.getIntValue();			else if (iniFile.isName("[Server1]"))				allowServer1 = readServerSettings(iniFile,allowServer1);			else if (iniFile.isName("[Server2]"))				allowServer2 = readServerSettings(iniFile,allowServer2);			else if (iniFile.isName("[Filter]"))			{				readFilterSettings(iniFile,filters[numFilters]);				if (numFilters < (MAX_FILTERS-1))					numFilters++;			}			else if (iniFile.isName("[Notify]"))			{				notifyMask = NT_UPGRADE;				while (iniFile.readNext())				{					if (iniFile.isName("[End]"))						break;					else if (iniFile.isName("PeerCast"))						notifyMask |= iniFile.getBoolValue()?NT_PEERCAST:0;					else if (iniFile.isName("Broadcasters"))						notifyMask |= iniFile.getBoolValue()?NT_BROADCASTERS:0;					else if (iniFile.isName("TrackInfo"))						notifyMask |= iniFile.getBoolValue()?NT_TRACKINFO:0;				}			}			else if (iniFile.isName("[RelayChannel]"))			{				ChanInfo info;				bool stayConnected=false;				String sourceURL;				while (iniFile.readNext())				{					if (iniFile.isName("[End]"))						break;					else if (iniFile.isName("name"))						info.name.set(iniFile.getStrValue());					else if (iniFile.isName("id"))						info.id.fromStr(iniFile.getStrValue());					else if (iniFile.isName("sourceType"))						info.srcProtocol = ChanInfo::getProtocolFromStr(iniFile.getStrValue());					else if (iniFile.isName("contentType"))						info.contentType = ChanInfo::getTypeFromStr(iniFile.getStrValue());					else if (iniFile.isName("stayConnected"))						stayConnected = iniFile.getBoolValue();					else if (iniFile.isName("sourceURL"))						sourceURL.set(iniFile.getStrValue());					else if (iniFile.isName("genre"))						info.genre.set(iniFile.getStrValue());					else if (iniFile.isName("contactURL"))						info.url.set(iniFile.getStrValue());					else if (iniFile.isName("bitrate"))						info.bitrate = atoi(iniFile.getStrValue());					else if (iniFile.isName("tracker"))
					{
						ChanHit hit;
						hit.init();
						hit.tracker = true;
						hit.host.fromStrName(iniFile.getStrValue(),DEFAULT_PORT);
						hit.rhost[0] = hit.host;
						hit.rhost[1] = hit.host;
						hit.chanID = info.id;
						hit.recv = true;
						chanMgr->addHit(hit);
					}
				}				if (sourceURL.isEmpty())				{					chanMgr->createRelay(info,stayConnected);				}else				{
					info.bcID = chanMgr->broadcastID;					Channel *c = chanMgr->createChannel(info,NULL);					if (c)						c->startURL(sourceURL.cstr());				}			} else if (iniFile.isName("[Host]"))			{				Host h;				ServHost::TYPE type=ServHost::T_NONE;
				bool firewalled=false;				unsigned int time=0;				while (iniFile.readNext())				{					if (iniFile.isName("[End]"))						break;					else if (iniFile.isName("address"))						h.fromStrIP(iniFile.getStrValue(),DEFAULT_PORT);					else if (iniFile.isName("type"))						type = ServHost::getTypeFromStr(iniFile.getStrValue());					else if (iniFile.isName("time"))						time = iniFile.getIntValue();				}				servMgr->addHost(h,type,time);			} else if (iniFile.isName("[ValidBCID]"))
			{
				BCID *bcid = new BCID();
				while (iniFile.readNext())
				{
					if (iniFile.isName("[End]"))
						break;
					else if (iniFile.isName("id"))
						bcid->id.fromStr(iniFile.getStrValue());
					else if (iniFile.isName("name"))
						bcid->name.set(iniFile.getStrValue());
					else if (iniFile.isName("email"))
						bcid->email.set(iniFile.getStrValue());
					else if (iniFile.isName("url"))
						bcid->url.set(iniFile.getStrValue());
					else if (iniFile.isName("valid"))
						bcid->valid = iniFile.getBoolValue();
				}
				servMgr->addValidBCID(bcid);
			}		}		}	if (!numFilters)		setFilterDefaults();
}

// --------------------------------------------------
unsigned int ServMgr::numStreams(GnuID &cid, Servent::TYPE tp, bool all)
{
	int cnt = 0;
	Servent *sv = servents;
	while (sv)
	{
		if (sv->isConnected())
			if (sv->type == tp)
				if (sv->chanID.isSame(cid))
					if (all || !sv->isPrivate())
						cnt++;
		sv=sv->next;
	}
	return cnt;
}
// --------------------------------------------------
unsigned int ServMgr::numStreams(Servent::TYPE tp, bool all)
{
	int cnt = 0;
	Servent *sv = servents;
	while (sv)
	{
		if (sv->isConnected())
			if (sv->type == tp)
				if (all || !sv->isPrivate())
					cnt++;
		sv=sv->next;
	}
	return cnt;
}
// --------------------------------------------------
bool ServMgr::getChannel(char *str,ChanInfo &info, bool relay)
{
	// remove file extension (only added for winamp)
	//char *ext = strstr(str,".");
	//if (ext) *ext = 0;

	procConnectArgs(str,info);

	Channel *ch;

	ch = chanMgr->findChannelByNameID(info);
	if (ch)
	{

		if (!ch->isPlaying())
		{
			if (relay)
			{
				ch->info.lastPlayStart = 0; // force reconnect 
				ch->info.lastPlayEnd = 0; 
			}else
				return false;
		}
		
		info = ch->info;	// get updated channel info 

		return true;
	}else
	{
		if (relay)
		{
			ch = chanMgr->findAndRelay(info);
			if (ch)
			{
				info = ch->info; //get updated channel info 
				return true;
			}
		}
	}

	return false;
}
// --------------------------------------------------int ServMgr::findChannel(ChanInfo &info){
#if 0	char idStr[64];	info.id.toStr(idStr);	if (info.id.isSet())	{		// if we have an ID then try and connect to known hosts carrying channel.		ServHost sh = getOutgoingServent(info.id);		addOutgoing(sh.host,info.id,true);	}	GnuPacket pack;	XML xml;	XML::Node *n = info.createQueryXML();	xml.setRoot(n);	pack.initFind(NULL,&xml,servMgr->queryTTL);	addReplyID(pack.id);	int cnt = broadcast(pack,NULL);	LOG_NETWORK("Querying network: %s %s - %d servents",info.name.cstr(),idStr,cnt);	return cnt;
#endif
	return 0;}// --------------------------------------------------// add outgoing network connection from string (ip:port format)bool ServMgr::addOutgoing(Host h, GnuID &netid, bool pri){
#if 0	if (h.ip)	{		if (!findServent(h.ip,h.port,netid))		{			Servent *sv = allocServent();			if (sv)			{				if (pri)					sv->priorityConnect = true;				sv->networkID = netid;				sv->initOutgoing(h,Servent::T_OUTGOING);				return true;			}		}	}#endif
	return false;}// --------------------------------------------------
Servent *ServMgr::findConnection(Servent::TYPE t,GnuID &sid)
{
	Servent *sv = servents;
	while (sv)
	{
		if (sv->isConnected())
			if (sv->type == t)
				if (sv->remoteID.isSame(sid))				
					return sv;
		sv=sv->next;
	}
	return NULL;
}

// --------------------------------------------------void ServMgr::procConnectArgs(char *str,ChanInfo &info){	char arg[MAX_CGI_LEN];	char curr[MAX_CGI_LEN];
	char *args = strstr(str,"?");
	if (args)
		*args++=0;

	info.initNameID(str);
	if (args)	{		while (args=nextCGIarg(args,curr,arg))		{
			LOG_DEBUG("cmd: %s, arg: %s",curr,arg);
			if (strcmp(curr,"sip")==0)			// sip - add network connection to client with channel
			{				Host h;				h.fromStrName(arg,DEFAULT_PORT);				if (addOutgoing(h,servMgr->networkID,true))					LOG_NETWORK("Added connection: %s",arg);			}else if (strcmp(curr,"pip")==0)			// pip - add private network connection to client with channel
			{				Host h;				h.fromStrName(arg,DEFAULT_PORT);				if (addOutgoing(h,info.id,true))					LOG_NETWORK("Added private connection: %s",arg);			}else if (strcmp(curr,"ip")==0)
			// ip - add hit
			{
				Host h;
				h.fromStrName(arg,DEFAULT_PORT);
				ChanHit hit;
				hit.init();
				hit.host = h; 
				hit.rhost[0] = h;
				hit.rhost[1].init();
				hit.chanID = info.id;
				hit.recv = true;

				chanMgr->addHit(hit);
			}else if (strcmp(curr,"tip")==0)
			// tip - add tracker hit
			{
				Host h;
				h.fromStrName(arg,DEFAULT_PORT);
				chanMgr->addHit(h,info.id,true);
			}
		}	}}// --------------------------------------------------bool ServMgr::start(){
	char idStr[64];


	const char *priv;
#if PRIVATE_BROADCASTER
	priv = "(private)";
#else
	priv = "";
#endif
	LOG_DEBUG("Peercast %s, %s %s",PCX_VERSTRING,peercastApp->getClientTypeOS(),priv);

	sessionID.toStr(idStr);
	LOG_DEBUG("SessionID: %s",idStr);

	chanMgr->broadcastID.toStr(idStr);
	LOG_DEBUG("BroadcastID: %s",idStr);

	checkForceIP();

	serverThread.func = ServMgr::serverProc;	if (!sys->startThread(&serverThread))		return false;
	idleThread.func = ServMgr::idleProc;	if (!sys->startThread(&idleThread))		return false;	return true;}// --------------------------------------------------int ServMgr::clientProc(ThreadInfo *thread){
#if 0	thread->lock();	GnuID netID;	netID = servMgr->networkID;	while(thread->active)	{		if (servMgr->autoConnect)		{			if (servMgr->needConnections() || servMgr->forceLookup)			{				if (servMgr->needHosts() || servMgr->forceLookup)				{					// do lookup to find some hosts					Host lh;					lh.fromStrName(servMgr->connectHost,DEFAULT_PORT);					if (!servMgr->findServent(lh.ip,lh.port,netID))					{						Servent *sv = servMgr->allocServent();						if (sv)						{							LOG_DEBUG("Lookup: %s",servMgr->connectHost);							sv->networkID = netID;							sv->initOutgoing(lh,Servent::T_LOOKUP);							servMgr->forceLookup = false;						}					}				}				for(int i=0; i<MAX_TRYOUT; i++)				{					if (servMgr->outUsedFull())						break;					if (servMgr->tryFull())

⌨️ 快捷键说明

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