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

📄 channel.cpp.svn-base

📁 这是和p2p相关的一份源码
💻 SVN-BASE
📖 第 1 页 / 共 5 页
字号:
// -----------------------------------int	ChanHitList::clearDeadHits(unsigned int timeout){	int cnt=0;	unsigned int ctime = sys->getTime();	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)		{			if ((ctime-hits[i].time) > timeout)				hits[i].init();			else				cnt++;		}	return cnt;}// -----------------------------------void	ChanHitList::deadHit(ChanHit &h){	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip == h.host.ip)			if (hits[i].host.port == h.host.port)				hits[i].init();}// -----------------------------------int	ChanHitList::numHits(){	int cnt=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			cnt++;	return cnt;}// -----------------------------------int	ChanHitList::numListeners(){	int cnt=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			cnt += hits[i].numListeners;	return cnt;}// -----------------------------------int	ChanHitList::numBusy(){	int cnt=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			cnt += hits[i].busy?1:0;	return cnt;}// -----------------------------------int	ChanHitList::numStable(){	int cnt=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			cnt += hits[i].stable?1:0;	return cnt;}// -----------------------------------int	ChanHitList::numFirewalled(){	int cnt=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			cnt += hits[i].firewalled?1:0;	return cnt;}// -----------------------------------int	ChanHitList::closestHit(){	int hop=10000;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			if (hits[i].hops < hop)				hop = hits[i].hops;	return hop;}// -----------------------------------int	ChanHitList::furthestHit(){	int hop=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			if (hits[i].hops > hop)				hop = hits[i].hops;	return hop;}// -----------------------------------unsigned int	ChanHitList::newestHit(){	unsigned int time=0;	for(int i=0; i<MAX_HITS; i++)		if (hits[i].host.ip)			if (hits[i].time > time)				time = hits[i].time;	return time;}// -----------------------------------ChanHit	ChanHitList::getHit(bool allowPush){	int minHops = 100;	unsigned int maxUpTime = 0;	ChanHit ch;	ch.init();	for(int i=0; i<MAX_HITS; i++)		{		ChanHit *c = &hits[i];		if (c->host.ip)			if (!c->busy)				if (c->upTime >= maxUpTime)				{					bool firewallOK;					firewallOK = (allowPush && c->firewalled) || (!c->firewalled) || (c->host.localIP());					if (c->firewalled)						if (((c->hops > chanMgr->maxPushHops) || (c->hops > minHops)))							firewallOK = false;										if (firewallOK)					{						ch = *c;						minHops = c->hops;						maxUpTime = c->upTime;					}				}	}	return ch;}// -----------------------------------void ChanHit::init(){	host.ip = 0;	host.port = 0;	numListeners = 0;	firewalled = busy = stable = false;	hops = 0;	index = 0;	time = upTime = 0;	agentStr[0]=0;	numSkips=0;	packetID.clear();	maxPreviewTime=0;}// -----------------------------------const char *ChanInfo::getTypeStr(TYPE t){	switch (t)	{		case T_OGG: return "OGG";		case T_RAW: return "RAW";		case T_MP3: return "MP3";		case T_MOV: return "MOV";		case T_MPG: return "MPG";		case T_NSV: return "NSV";		case T_WMA: return "WMA";		case T_WMV: return "WMV";		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";		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 		return SP_UNKNOWN;}// -----------------------------------const char *ChanInfo::getTypeExt(TYPE t){	switch(t)	{		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 "stream";	}}// -----------------------------------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,"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::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;}// -----------------------------------void ChanInfo::update(ChanInfo &info){	// only update from chaninfo that has full name etc..	if (info.name.isEmpty())		return;	if (contentType == T_UNKNOWN)	{		bitrate = info.bitrate;		contentType = info.contentType;		name = info.name;	}	track = info.track;	comment = info.comment;	genre = info.genre;	url = info.url;}// -----------------------------------void ChanInfo::initNameID(const char *n){	init();	id.fromStr(n);	if (!id.isSet())		name.set(n);}// -----------------------------------void ChanInfo::init(){	status = S_UNKNOWN;	name.clear();	bitrate = 0;	contentType = T_UNKNOWN;	srcProtocol = SP_UNKNOWN;	id.clear();	url.clear();	genre.clear();	comment.clear();	track.clear();	lastPlay = 0;	numSkips = 0;}// -----------------------------------void ChanInfo::readTrackXML(XML::Node *n){	track.clear();	readXMLString(track.title,n,"title");	readXMLString(track.contact,n,"contact");	readXMLString(track.artist,n,"artist");	readXMLString(track.album,n,"album");	readXMLString(track.genre,n,"genre");}// -----------------------------------unsigned int ChanInfo::getUptime(){	// calculate uptime and cap if requested by settings.	unsigned int upt;	upt = lastPlay?(sys->getTime()-lastPlay):0;	if (chanMgr->maxUptime)		if (upt > chanMgr->maxUptime)			upt = chanMgr->maxUptime;	return upt;}// -----------------------------------XML::Node *ChanInfo::createChannelXML(){	char idStr[64];	String nameHTML = name;	nameHTML.convertTo(String::T_HTML);	String urlHTML = url;	urlHTML.convertTo(String::T_HTML);	String genreHTML = genre;	genreHTML.convertTo(String::T_HTML);	String descHTML = desc;	descHTML.convertTo(String::T_HTML);	String commentHTML;	commentHTML = comment;	commentHTML.convertTo(String::T_HTML);	id.toStr(idStr);	return new XML::Node("channel name=\"%s\" id=\"%s\" bitrate=\"%d\" type=\"%s\" genre=\"%s\" desc=\"%s\" url=\"%s\" uptime=\"%d\" comment=\"%s\" skips=\"%d\"",		nameHTML.cstr(),		idStr,		bitrate,		getTypeStr(contentType),		genreHTML.cstr(),		descHTML.cstr(),		urlHTML.cstr(),		getUptime(),		commentHTML.cstr(),		numSkips		);	}// -----------------------------------XML::Node *ChanInfo::createQueryXML(){	char buf[512];	char idStr[64];	String nameHTML = name;	nameHTML.convertTo(String::T_HTML);	String genreHTML = genre;	genreHTML.convertTo(String::T_HTML);	buf[0]=0;	if (!nameHTML.isEmpty())	{		strcat(buf," name=\"");		strcat(buf,nameHTML.cstr());		strcat(buf,"\"");	}	if (!genreHTML.isEmpty())	{		strcat(buf," genre=\"");		strcat(buf,genreHTML.cstr());		strcat(buf,"\"");	}	if (id.isSet())	{		id.toStr(idStr);		strcat(buf," id=\"");		strcat(buf,idStr);		strcat(buf,"\"");	}			return new XML::Node("channel %s",buf);}// -----------------------------------XML::Node *ChanInfo::createRelayChannelXML(){	char idStr[64];	id.toStr(idStr);	return new XML::Node("channel id=\"%s\" uptime=\"%d\" skips=\"%d\"",		idStr,		getUptime(),		numSkips		);	}// -----------------------------------XML::Node *ChanInfo::createTrackXML(){	String titleHTML,contactHTML,albumHTML,genreHTML,artistHTML;	titleHTML = track.title;	titleHTML.convertTo(String::T_HTML);	artistHTML = track.artist;	artistHTML.convertTo(String::T_HTML);	albumHTML = track.album;	albumHTML.convertTo(String::T_HTML);	genreHTML = track.genre;	genreHTML.convertTo(String::T_HTML);	contactHTML = track.contact;	contactHTML.convertTo(String::T_HTML);	return new XML::Node("track title=\"%s\" artist=\"%s\" album=\"%s\" genre=\"%s\" contact=\"%s\"",		titleHTML.cstr(),		artistHTML.cstr(),		albumHTML.cstr(),		genreHTML.cstr(),		contactHTML.cstr()		);}// -----------------------------------void ChanInfo::init(XML::Node *n){	init();	updateFromXML(n);}// -----------------------------------void ChanInfo::updateFromXML(XML::Node *n){	String typeStr,idStr;	readXMLString(name,n,"name");	readXMLString(genre,n,"genre");	readXMLString(url,n,"url");	readXMLString(desc,n,"desc");	int br = n->findAttrInt("bitrate");	if (br)		bitrate = br;	readXMLString(typeStr,n,"type");	if (!typeStr.isEmpty())		contentType = getTypeFromStr(typeStr.cstr());	readXMLString(idStr,n,"id");	if (!idStr.isEmpty())		id.fromStr(idStr.cstr());	readXMLString(comment,n,"comment");	XML::Node *tn = n->findNode("track");	if (tn)		readTrackXML(tn);}// -----------------------------------void ChanInfo::init(const char *n, GnuID &cid, TYPE tp, int br){	init();	name.set(n);	bitrate = br;	contentType = tp;	id = cid;}// -----------------------------------void ChanInfo::init(const char *fn){	init();	if (fn)		name.set(fn);}// -----------------------------------void PlayList::readASX(Stream &in){	LOG_DEBUG("Reading ASX");	XML xml;	try	{		xml.read(in);	}catch(StreamException &) {} // TODO: eof is NOT handled properly in sockets - always get error at end	if (xml.root)	{		XML::Node *n = xml.root->child;		while (n)		{			if (stricmp("entry",n->getName())==0)			{				XML::Node *rf = n->findNode("ref");				if (rf)				{					char *hr = rf->findAttr("href");					if (hr)					{						addURL(hr,"");						//LOG("asx url %s",hr);					}				}			}			n=n->sibling;		}	}}// -----------------------------------void PlayList::readSCPLS(Stream &in){	char tmp[256];	while (in.readLine(tmp,sizeof(tmp)))	{		if (strnicmp(tmp,"file",4)==0)		{			char *p = strstr(tmp,"=");			if (p)				addURL(p+1,"");		}	}}// -----------------------------------void PlayList::readPLS(Stream &in){	char tmp[256];	while (in.readLine(tmp,sizeof(tmp)))	{		if (tmp[0] != '#')			addURL(tmp,"");	}}// -----------------------------------void PlayList::writeSCPLS(Stream &out){	out.writeLine("[playlist]");	out.writeLine("");	out.writeLine("NumberOfEntries=%d",numURLs);	for(int i=0; i<numURLs; i++)	{		out.writeLine("File%d=%s",i+1,urls[i].cstr());		out.writeLine("Title%d=%s",i+1,titles[i].cstr());		out.writeLine("Length%d=-1",i+1);	}	out.writeLine("Version=2");}// -----------------------------------void PlayList::writePLS(Stream &out){	for(int i=0; i<numURLs; i++)		out.writeLine("%s",urls[i].cstr());}// -----------------------------------void PlayList::writeASX(Stream &out){	out.writeLine("<ASX Version=\"3.0\">");	for(int i=0; i<numURLs; i++)	{		out.writeLine("<ENTRY>");		out.writeLine("<REF href = \"%s\" />",urls[i].cstr());		out.writeLine("</ENTRY>");	}	out.writeLine("</ASX>");}// -----------------------------------void PlayList::addChannels(const char *path, Channel **cl, int num){	String url;	for(int i=0; i<num; i++)	{		char idStr[64];		cl[i]->getIDStr(idStr);		sprintf(url.cstr(),"%s/stream/%s.%s",path,idStr,ChanInfo::getTypeExt(cl[i]->info.contentType));		addURL(url.cstr(),cl[i]->info.name);	}	}// -----------------------------------void PlayList::addChannel(const char *path, ChanInfo &info){	String url;	char idStr[64];	info.id.toStr(idStr);	sprintf(url.cstr(),"%s/stream/%s.%s",path,idStr,ChanInfo::getTypeExt(info.contentType));	addURL(url.cstr(),info.name);}

⌨️ 快捷键说明

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