📄 servmgr.cpp
字号:
// ------------------------------------------------// 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(ServHost::T_NONE); password[0]=0; allowGnutella = false; maxBitrate = MAX_BITRATE; maxIncoming = MAX_INCOMING; maxOutgoing = MAX_OUTGOING; maxTryout = MAX_TRYOUT; minConnected = MIN_CONNECTED; maxStreams = 0; maxPreviewTime = MAX_PREVIEWTIME; maxPreviewWait = MAX_PREVIEWWAIT; 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",DEFAULT_PORT); 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",0); filters[numFilters].flags = ServFilter::F_NETWORK; numFilters++; filters[numFilters].host.fromStr("127.0.0.1",0); filters[numFilters].flags = ServFilter::F_PRIVATE|ServFilter::F_NETWORK|ServFilter::F_DIRECT; numFilters++;}// -----------------------------------void ServMgr::setPassiveSearch(unsigned int t){// if ((t > 0) && (t < 60))// t = 60;// passiveSearch = t;}// -----------------------------------bool ServMgr::seenHost(Host &h, ServHost::TYPE type,unsigned int time){ time = sys->getTime()-time; for(int i=0; i<MAX_HOSTCACHE; i++) if (hostCache[i].type == type) if (hostCache[i].host.ip == h.ip) if (hostCache[i].time >= time) return true; return false;}// -----------------------------------void ServMgr::addHost(Host &h, ServHost::TYPE type, unsigned int time, GnuID &netid){ int i;// if (!h.isValid() || h.loopbackIP()) if (!h.isValid()) return; ServHost *sh=NULL; for(i=0; i<MAX_HOSTCACHE; i++) if (hostCache[i].type == type) if (hostCache[i].host.ip == h.ip) if (hostCache[i].host.port == h.port) if (hostCache[i].netid.isSame(netid)) { sh = &hostCache[i]; break; } char str[64]; h.toStr(str); h.value = 0; // make sure dead count is zero if (!sh) { LOG_NETWORK("New host: %s - %s",str,ServHost::getTypeStr(type)); // find empty slot for(i=0; i<MAX_HOSTCACHE; i++) if (hostCache[i].type == ServHost::T_NONE) { sh = &hostCache[i]; break; } // otherwise, find oldest host and replace if (!sh) for(i=0; i<MAX_HOSTCACHE; i++) if (hostCache[i].type != ServHost::T_NONE) { if (sh) { if (hostCache[i].time < sh->time) sh = &hostCache[i]; }else{ sh = &hostCache[i]; } } }else{ LOG_NETWORK("Duplicate host: %s - %s",str,ServHost::getTypeStr(type)); } if (sh) sh->init(h,type,time,netid);}// -----------------------------------void ServMgr::deadHost(Host &h,ServHost::TYPE t){ for(int i=0; i<MAX_HOSTCACHE; i++) if (hostCache[i].type == t) if (hostCache[i].host.ip == h.ip) if (hostCache[i].host.port == h.port) hostCache[i].init();}// -----------------------------------void ServMgr::clearHostCache(ServHost::TYPE type){ for(int i=0; i<MAX_HOSTCACHE; i++) if ((hostCache[i].type == type) || (type == ServHost::T_NONE)) hostCache[i].init();} // -----------------------------------unsigned int ServMgr::numHosts(ServHost::TYPE type){ unsigned int cnt = 0; for(int i=0; i<MAX_HOSTCACHE; i++) if ((hostCache[i].type == type) || (type == ServHost::T_NONE)) cnt++; return cnt;}// -----------------------------------int ServMgr::getNewestServents(Host *hl,int max,Host &rh){ int cnt=0; for(int i=0; i<max; i++) { // find newest host not in list ServHost *sh=NULL; for(int j=0; j<MAX_HOSTCACHE; j++) { // find newest servent if (hostCache[j].type == ServHost::T_SERVENT) if (!(rh.globalIP() && !hostCache[j].host.globalIP())) { // and not in list already bool found=false; for(int k=0; k<cnt; k++) if (hl[k].isSame(hostCache[j].host)) { found=true; break; } if (!found) { if (!sh) { sh = &hostCache[j]; }else{ if (hostCache[j].time > sh->time) sh = &hostCache[j]; } } } } // add to list if (sh) hl[cnt++]=sh->host; } return cnt;}// -----------------------------------ServHost ServMgr::getOutgoingServent(GnuID &netid){ ServHost host; Host lh(ClientSocket::getIP(NULL),0); // find newest host not in list ServHost *sh=NULL; for(int j=0; j<MAX_HOSTCACHE; j++) { ServHost *hc=&hostCache[j]; // find newest servent not already connected. if (hc->type == ServHost::T_SERVENT) if (hc->netid.isSame(netid)) { if (!((lh.globalIP() && !hc->host.globalIP()) || lh.isSame(hc->host))) { if (!findServent(Servent::T_OUTGOING,hc->host,hc->netid)) { if (!sh) { sh = hc; }else{ if (hc->time > sh->time) sh = hc; } } } } } if (sh) host = *sh; return host;}// -----------------------------------Servent *ServMgr::findOldestServent(Servent::TYPE type){ Servent *oldest=NULL; for(int i=0; i<ServMgr::MAX_SERVENTS; i++) { Servent *s = &servMgr->servents[i]; if (s->type == type) if (s->thread.active) if (s->isOlderThan(oldest)) oldest = s; } return oldest;}// -----------------------------------Servent *ServMgr::findServent(Servent::TYPE type, Host &host, GnuID &netid){ lock.on(); for(int i=0; i<MAX_SERVENTS; i++) { Servent *s = &servents[i]; if (s->type == type) { Host h = s->getHost(); if (h.isSame(host) && s->networkID.isSame(netid)) { lock.off(); return s; } } } lock.off(); return NULL;}// -----------------------------------Servent *ServMgr::findServent(unsigned int ip, unsigned short port, GnuID &netid){ 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) && (s->networkID.isSame(netid))) { 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->thread.active) 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) if (s->thread.active) 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->thread.active) 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -