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

📄 channel.cpp

📁 peercast的源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		String timeStr;
		timeStr.setFromStopwatch(upTime);
		strcpy(buf,timeStr.cstr());
	}else if (var == "update")
	{
		String timeStr;
		if (timeStr)
			timeStr.setFromStopwatch(sys->getTime()-time);
		else
			timeStr.set("-");
		strcpy(buf,timeStr.cstr());
	}else if (var == "isFirewalled")
		sprintf(buf,"%d",firewalled?1:0);
	else if (var == "version")
		sprintf(buf,"%d",version);
	else
		return false;

	out.writeString(buf);
	return true;
}

// -----------------------------------
int ChanHitList::getTotalListeners()
{
	int cnt=0;
	ChanHit *h = hit;
	while (h)
	{
		if (h->host.ip)
			cnt+=h->numListeners;
		h=h->next;
	}
	return cnt;
}
// -----------------------------------
int ChanHitList::getTotalRelays()
{
	int cnt=0;
	ChanHit *h = hit;
	while (h)
	{
		if (h->host.ip)
			cnt+=h->numRelays;
		h=h->next;
	}
	return cnt;
}
// -----------------------------------
int ChanHitList::getTotalFirewalled()
{
	int cnt=0;
	ChanHit *h = hit;
	while (h)
	{
		if (h->host.ip)
			if (h->firewalled)
				cnt++;
		h=h->next;
	}
	return cnt;
}

// -----------------------------------
int ChanHitList::contactTrackers(bool connected, int numl, int nums, int uptm)
{
	return 0;
}

// -----------------------------------
ChanHit *ChanHitList::deleteHit(ChanHit *ch)
{
	ChanHit *c = hit,*prev=NULL;
	while (c)
	{
		if (c == ch)
		{
			ChanHit *next = c->next;
			if (prev)
				prev->next = next;
			else
				hit = next;

			delete c;

			return next;
		}
		prev=c;
		c=c->next;
	}

	return NULL;
}// -----------------------------------ChanHit *ChanHitList::addHit(ChanHit &h){	char ip0str[64],ip1str[64];
	h.rhost[0].toStr(ip0str);
	h.rhost[1].toStr(ip1str);
	LOG_DEBUG("Add hit: %s/%s",ip0str,ip1str);

	// dont add our own hits
	if (servMgr->sessionID.isSame(h.sessionID))
		return NULL;

	lastHitTime = sys->getTime();	h.time = lastHitTime;
	ChanHit *ch = hit;	while (ch)
	{
		if ((ch->rhost[0].ip == h.rhost[0].ip) && (ch->rhost[0].port == h.rhost[0].port))			if (((ch->rhost[1].ip == h.rhost[1].ip) && (ch->rhost[1].port == h.rhost[1].port)) || (!ch->rhost[1].isValid()))
			{				if (!ch->dead)
				{
					ChanHit *next = ch->next;
					*ch = h;
					ch->next = next;
					return ch;
				}			}
		ch=ch->next;
	}
	// clear hits with same session ID (IP may have changed)
	if (h.sessionID.isSet())
	{
		ChanHit *ch = hit;
		while (ch)
		{
			if (ch->host.ip)
				if (ch->sessionID.isSame(h.sessionID))
				{
					ch = deleteHit(ch);
					continue;						
				}
			ch=ch->next;
		}
	}


	// else add new hit
	{		ChanHit *ch = new ChanHit();
		*ch = h;
		ch->chanID = info.id;
		ch->next = hit;
		hit = ch;
		return ch;
	}	return NULL;}// -----------------------------------int	ChanHitList::clearDeadHits(unsigned int timeout, bool clearTrackers){	int cnt=0;	unsigned int ctime = sys->getTime();

	ChanHit *ch = hit;	while (ch)
	{		if (ch->host.ip)		{
			if (ch->dead || ((ctime-ch->time) > timeout) && (clearTrackers || (!clearTrackers & !ch->tracker)))
			{
				ch = deleteHit(ch);
				continue;
			}else
				cnt++;		}
		ch = ch->next;
	}	return cnt;}// -----------------------------------void	ChanHitList::deadHit(ChanHit &h){	char ip0str[64],ip1str[64];
	h.rhost[0].toStr(ip0str);
	h.rhost[1].toStr(ip1str);
	LOG_DEBUG("Dead hit: %s/%s",ip0str,ip1str);

	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip)			if (ch->rhost[0].isSame(h.rhost[0]) && ch->rhost[1].isSame(h.rhost[1]))
			{
				ch->dead = true;
			}
		ch = ch->next;
	}}// -----------------------------------
void	ChanHitList::delHit(ChanHit &h)
{
	char ip0str[64],ip1str[64];
	h.rhost[0].toStr(ip0str);
	h.rhost[1].toStr(ip1str);
	LOG_DEBUG("Del hit: %s/%s",ip0str,ip1str);

	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip)
			if (ch->rhost[0].isSame(h.rhost[0]) && ch->rhost[1].isSame(h.rhost[1]))
			{
				ch=deleteHit(ch);
				continue;
			}
		ch = ch->next;
	}
}
// -----------------------------------int	ChanHitList::numHits(){	int cnt=0;	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)
			cnt++;
		ch = ch->next;
	}	return cnt;}
// -----------------------------------int	ChanHitList::numListeners(){	int cnt=0;	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)			cnt += ch->numListeners;
		ch=ch->next;
	}	return cnt;}// -----------------------------------
int	ChanHitList::numRelays()
{
	int cnt=0;
	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)
			cnt += ch->numRelays;
		ch=ch->next;
	}

	return cnt;
}

// -----------------------------------
int	ChanHitList::numTrackers()
{
	int cnt=0;
	ChanHit *ch = hit;
	while (ch)
	{
		if ((ch->host.ip && !ch->dead) && (ch->tracker))
			cnt++;
		ch=ch->next;
	}
	return cnt;
}
// -----------------------------------int	ChanHitList::numFirewalled(){	int cnt=0;	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)
			cnt += ch->firewalled?1:0;		ch=ch->next;
	}	return cnt;}// -----------------------------------int	ChanHitList::closestHit(){	unsigned int hop=10000;	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)			if (ch->numHops < hop)				hop = ch->numHops;		ch=ch->next;
	}
	return hop;}// -----------------------------------int	ChanHitList::furthestHit(){	unsigned int hop=0;	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)			if (ch->numHops > hop)				hop = ch->numHops;		ch=ch->next;
	}
	return hop;}// -----------------------------------unsigned int	ChanHitList::newestHit(){	unsigned int time=0;	ChanHit *ch = hit;
	while (ch)
	{
		if (ch->host.ip && !ch->dead)			if (ch->time > time)				time = ch->time;		ch=ch->next;
	}
	return time;}// -----------------------------------int ChanHitList::pickHits(ChanHitSearch &chs)
{
	ChanHit best,*bestP=NULL;
	best.init();
	best.numHops = 255;
	best.time = 0;

	unsigned int ctime = sys->getTime();

	ChanHit *c = hit;
	while (c)
	{		if (c->host.ip && !c->dead)
		{
			if (!chs.excludeID.isSame(c->sessionID))
			if ((chs.waitDelay==0) || ((ctime-c->lastContact) >= chs.waitDelay))
			if ((c->numHops<best.numHops))	// (c->time>=best.time))
			if (c->relay || (!c->relay && chs.useBusyRelays))
			if (c->cin || (!c->cin && chs.useBusyControls))
			{

				if (chs.trackersOnly && c->tracker)
				{
					if (chs.matchHost.ip)
					{
						if ((c->rhost[0].ip == chs.matchHost.ip) && c->rhost[1].isValid())
						{
							bestP = c;
							best = *c;
							best.host = best.rhost[1];	// use lan ip
						}
					}else if (c->firewalled == chs.useFirewalled)
					{
						bestP = c;
						best = *c;
						best.host = best.rhost[0];			// use wan ip
					}
				}else if (!chs.trackersOnly && !c->tracker)
				{					if (chs.matchHost.ip)
					{
						if ((c->rhost[0].ip == chs.matchHost.ip) && c->rhost[1].isValid())
						{
							bestP = c;
							best = *c;
							best.host = best.rhost[1];	// use lan ip
						}
					}else if (c->firewalled == chs.useFirewalled)
					{
						bestP = c;
						best = *c;
						best.host = best.rhost[0];			// use wan ip
					}
				}
			}
		}
		c=c->next;	}


	if (bestP)
	{
		if (chs.numResults < ChanHitSearch::MAX_RESULTS)
		{
			if (chs.waitDelay)
				bestP->lastContact = ctime;
			chs.best[chs.numResults++] = best;
			return 1;
		}

	}

	return 0;
}// -----------------------------------const char *ChanInfo::getTypeStr(TYPE t){	switch (t)	{		case T_RAW: return "RAW";		case T_MP3: return "MP3";		case T_OGG: return "OGG";		case T_OGM: return "OGM";
		case T_WMA: return "WMA";		case T_MOV: return "MOV";		case T_MPG: return "MPG";		case T_NSV: return "NSV";		case T_WMV: return "WMV";		case T_PLS: return "PLS";		case T_ASX: return "ASX";		default: return "UNKNOWN";	}}// -----------------------------------const char *ChanInfo::getProtocolStr(PROTOCOL t){	switch (t)	{		case SP_PEERCAST: return "PEERCAST";		case SP_HTTP: return "HTTP";		case SP_FILE: return "FILE";		case SP_MMS: return "MMS";		case SP_PCP: return "PCP";
		default: return "UNKNOWN";	}}// -----------------------------------ChanInfo::PROTOCOL ChanInfo::getProtocolFromStr(const char *str){	if (stricmp(str,"PEERCAST")==0)		return SP_PEERCAST;	else if (stricmp(str,"HTTP")==0)		return SP_HTTP;	else if (stricmp(str,"FILE")==0)		return SP_FILE;	else if (stricmp(str,"MMS")==0)		return SP_MMS;	else if (stricmp(str,"PCP")==0)
		return SP_PCP;
	else 		return SP_UNKNOWN;}// -----------------------------------const char *ChanInfo::getTypeExt(TYPE t){	switch(t)	{		case ChanInfo::T_OGM:
		case ChanInfo::T_OGG:			return ".ogg";		case ChanInfo::T_MP3:			return ".mp3";		case ChanInfo::T_MOV:			return ".mov";		case ChanInfo::T_NSV:			return ".nsv";		case ChanInfo::T_WMV:			return ".wmv";		case ChanInfo::T_WMA:			return ".wma";		default:			return "";	}}// -----------------------------------ChanInfo::TYPE ChanInfo::getTypeFromStr(const char *str){	if (stricmp(str,"MP3")==0)		return T_MP3;	else if (stricmp(str,"OGG")==0)		return T_OGG;	else if (stricmp(str,"OGM")==0)
		return T_OGM;
	else if (stricmp(str,"RAW")==0)		return T_RAW;	else if (stricmp(str,"NSV")==0)		return T_NSV;	else if (stricmp(str,"WMA")==0)		return T_WMA;	else if (stricmp(str,"WMV")==0)		return T_WMV;	else if (stricmp(str,"PLS")==0)		return T_PLS;	else if (stricmp(str,"M3U")==0)		return T_PLS;	else if (stricmp(str,"ASX")==0)		return T_ASX;	else 		return T_UNKNOWN;}// -----------------------------------
bool	ChanInfo::matchNameID(ChanInfo &inf)
{
	if (inf.id.isSet())
		if (id.isSame(inf.id))
			return true;

	if (!inf.name.isEmpty())
		if (name.contains(inf.name))
			return true;

	return false;
}
// -----------------------------------bool	ChanInfo::match(ChanInfo &inf){	bool matchAny=true;	if (inf.status != S_UNKNOWN)	{		if (status != inf.status)			return false;	}	if (inf.bitrate != 0)	{		if (bitrate == inf.bitrate)			return true;		matchAny = false;	}	if (inf.id.isSet())	{		if (id.isSame(inf.id))			return true;		matchAny = false;	}	if (inf.contentType != T_UNKNOWN)	{		if (contentType == inf.contentType)			return true;		matchAny = false;	}	if (!inf.name.isEmpty())	{		if (name.contains(inf.name))			return true;		matchAny = false;	}	if (!inf.genre.isEmpty())	{		if (genre.contains(inf.genre))			return true;		matchAny = false;	}	return matchAny;}// -----------------------------------
bool TrackInfo::update(TrackInfo &inf)
{
	bool changed = false;

	if (!contact.isSame(inf.contact))
	{
		contact = inf.contact;
		changed = true;
	}

	if (!title.isSame(inf.title))
	{
		title = inf.title;
		changed = true;
	}

	if (!artist.isSame(inf.artist))
	{
		artist = inf.artist;
		changed = true;
	}

	if (!album.isSame(inf.album))
	{
		album = inf.album;
		changed = true;
	}

	if (!genre.isSame(inf.genre))
	{
		genre = inf.genre;
		changed = true;
	}


	return changed;
}

// -----------------------------------bool ChanInfo::update(ChanInfo &info){	bool changed = false;



	// check valid id
	if (!info.id.isSet())
		return false;

	// only update from chaninfo that has full name etc..	if (info.name.isEmpty())		return false;
	// check valid broadcaster key
	if (bcID.isSet())
	{
		if (!bcID.isSame(info.bcID))
		{
			LOG_ERROR("ChanInfo BC key not valid");
			return false;
		}
	}else
	{
		bcID = info.bcID;
	}


	if (bitrate != info.bitrate)
	{
		bitrate = info.bitrate;
		changed = true;
	}

	if (contentType != info.contentType)
	{
		cont

⌨️ 快捷键说明

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