PeerHandler Class ReferenceCustom sockethandler for bittorrent client. More...
Collaboration diagram for PeerHandler:
Detailed DescriptionCustom sockethandler for bittorrent client.
Definition at line 40 of file PeerHandler.h. Member Typedef Documentation
Definition at line 42 of file PeerHandler.h.
Constructor & Destructor Documentation
Definition at line 47 of file PeerHandler.cpp. 00048 :SocketHandler() 00049 ,m_listen_port(0) 00050 ,m_choke_timer(300) 00051 ,m_min_peers(10) 00052 ,m_max_peers(20) 00053 ,m_local_choke_time(30) 00054 ,m_ignore_choke(false) 00055 ,m_max_request_age(30) 00056 ,m_quit(false) 00057 ,m_debug(0) 00058 ,m_downloaders(4) 00059 ,m_optimistic(1) 00060 { 00061 srand(time(NULL) * (time_t)this); 00062 }
Definition at line 65 of file PeerHandler.cpp. 00066 :SocketHandler(p) 00067 ,m_listen_port(0) 00068 ,m_choke_timer(300) 00069 ,m_min_peers(10) 00070 ,m_max_peers(20) 00071 ,m_local_choke_time(30) 00072 ,m_ignore_choke(false) 00073 ,m_max_request_age(30) 00074 ,m_quit(false) 00075 ,m_debug(0) 00076 ,m_downloaders(4) 00077 ,m_optimistic(1) 00078 { 00079 }
Definition at line 82 of file PeerHandler.cpp. References m_sessions. 00083 { 00084 while (m_sessions.size()) 00085 { 00086 session_m::iterator it = m_sessions.begin(); 00087 Session *p = (*it).second; 00088 delete p; 00089 m_sessions.erase(it); 00090 } 00091 }
Member Function Documentation
Definition at line 94 of file PeerHandler.cpp. References m_sessions. Referenced by MetainfoSocket::InitSession(). 00095 { 00096 m_sessions[p -> GetInfoHash()] = p; 00097 }
Definition at line 100 of file PeerHandler.cpp. References m_sessions. Referenced by MetainfoSocket::InitSession(). 00101 { 00102 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) 00103 { 00104 if ((*it).first == hash && dynamic_cast<Session *>((*it).second)) 00105 return true; 00106 } 00107 return false; 00108 }
Definition at line 127 of file PeerHandler.cpp. References m_sessions. Referenced by FileManager::FileManager(), FileManager::GetFiles(), FileManager::ReadPiece(), FileManager::Verify(), and FileManager::Write(). 00128 { 00129 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) 00130 { 00131 if ((*it).first == hash && dynamic_cast<Session *>((*it).second)) 00132 return (*it).second; 00133 } 00134 return NULL; 00135 }
Definition at line 52 of file PeerHandler.h. References m_torrent_dir. Referenced by FileManager::FileManager(), FileManager::GetFiles(), and MetainfoSocket::InitSession(). 00052 { return m_torrent_dir; }
Definition at line 53 of file PeerHandler.h. References m_torrent_dir. Referenced by main(). 00053 { m_torrent_dir = x; }
Definition at line 111 of file PeerHandler.cpp. Referenced by MetainfoSocket::InitSession(), and FileManager::OpenFiles(). 00112 { 00113 for (size_t i = 0; i < path.size(); i++) 00114 { 00115 if (path[i] == '/') 00116 { 00117 #ifdef _WIN32 00118 _mkdir(path.substr(0, i).c_str()); 00119 #else 00120 mkdir(path.substr(0, i).c_str(), 0750); 00121 #endif 00122 } 00123 } 00124 }
Definition at line 138 of file PeerHandler.cpp. References m_sessions. Referenced by main(). 00139 { 00140 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) 00141 { 00142 Session *sess = (*it).second; 00143 if (sess) 00144 { 00145 if (now > sess -> GetTimeTracker() + sess -> GetInterval()) 00146 { 00147 tSocket *p = new tSocket(*this, sess -> GetInfoHash()); 00148 p -> SetDeleteByHandler(); 00149 Add(p); 00150 sess -> SetTimeTracker(); 00151 } 00152 sess -> Update(); 00153 } 00154 } 00155 }
Definition at line 58 of file PeerHandler.h. References m_listen_port. 00058 { return m_listen_port; }
Definition at line 59 of file PeerHandler.h. References m_listen_port. Referenced by main(). 00059 { m_listen_port = x; }
Definition at line 61 of file PeerHandler.h. References m_extern_ip. Referenced by main(). 00061 { m_extern_ip = x; }
Definition at line 158 of file PeerHandler.cpp. References cstring::c_str(). Referenced by MyLog::error(). 00159 { 00160 // char *colors = "rgybmcwlRGYBMCWL"; 00161 char *colors = "rgymcRGYBMCWL"; 00162 va_list ap; 00163 va_start(ap, format); 00164 char tmp[5000]; 00165 #ifdef _WIN32 00166 vsprintf(tmp, format, ap); 00167 va_end(ap); 00168 printf("%s\n", tmp); 00169 #else 00170 vsnprintf(tmp, 5000, format, ap); 00171 va_end(ap); 00172 cstring str; 00173 char slask[10]; 00174 sprintf(slask, "&%c", colors[id % strlen(colors)]); 00175 str = slask; 00176 str += tmp; 00177 str += "&n"; 00178 printf("%s\n", str.c_str()); 00179 if (0) 00180 { 00181 FILE *fil = fopen("btlog","at"); 00182 if (!fil) 00183 fil = fopen("btlog","wt"); 00184 fprintf(fil,"%s\n", str.c_str()); 00185 fclose(fil); 00186 } 00187 #endif 00188 }
Definition at line 65 of file PeerHandler.h. References m_choke_timer. Referenced by Session::AddConnect(). 00065 { return m_choke_timer; }
Definition at line 66 of file PeerHandler.h. References m_choke_timer. Referenced by main(). 00066 { m_choke_timer = x; }
outgoing connections stop at this limit Definition at line 68 of file PeerHandler.h. References m_min_peers. Referenced by Session::Update(). 00068 { return m_min_peers; }
Definition at line 69 of file PeerHandler.h. References m_min_peers. Referenced by main(). 00069 { m_min_peers = x; }
no more incoming connections will be accepted above this limit Definition at line 71 of file PeerHandler.h. References m_max_peers. 00071 { return m_max_peers; }
Definition at line 72 of file PeerHandler.h. References m_max_peers. Referenced by main(). 00072 { m_max_peers = x; }
Definition at line 74 of file PeerHandler.h. References m_local_choke_time. 00074 { return m_local_choke_time; }
Definition at line 75 of file PeerHandler.h. References m_local_choke_time. 00075 { m_local_choke_time = x; }
Definition at line 191 of file PeerHandler.cpp. Referenced by Session::Update(). 00192 { 00193 size_t q = 0; 00194 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00195 { 00196 pSocket *p = dynamic_cast<pSocket *>((*it).second); 00197 if (p && p -> GetHash() == hash) 00198 { 00199 q++; 00200 } 00201 } 00202 return q; 00203 }
Definition at line 206 of file PeerHandler.cpp. 00207 { 00208 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00209 { 00210 pSocket *p = dynamic_cast<pSocket *>((*it).second); 00211 if (p && p -> GetHash() == peer -> GetHash() && p -> GetRemoteAddress() == peer -> GetIP()) 00212 { 00213 return true; 00214 } 00215 } 00216 return false; 00217 }
Definition at line 220 of file PeerHandler.cpp. 00221 { 00222 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00223 { 00224 pSocket *p = dynamic_cast<pSocket *>((*it).second); 00225 if (p && p -> GetHash() == peer -> GetHash() && p -> GetRemoteAddress() == peer -> GetIP()) 00226 { 00227 return p; 00228 } 00229 } 00230 return NULL; 00231 }
Definition at line 234 of file PeerHandler.cpp. Referenced by Session::Update(). 00235 { 00236 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00237 { 00238 pSocket *p = dynamic_cast<pSocket *>((*it).second); 00239 if (p && p -> GetHash() == hash && p -> CTS() ) 00240 { 00241 p -> SendHave(piece); 00242 } 00243 } 00244 }
Definition at line 82 of file PeerHandler.h. References m_ignore_choke. Referenced by Session::Update(). 00082 { return m_ignore_choke; }
Definition at line 83 of file PeerHandler.h. References m_ignore_choke. Referenced by main(). 00083 { m_ignore_choke = x; }
Definition at line 85 of file PeerHandler.h. References m_max_request_age. 00085 { return m_max_request_age; }
Definition at line 86 of file PeerHandler.h. References m_max_request_age. Referenced by main(). 00086 { m_max_request_age = x; }
Definition at line 247 of file PeerHandler.cpp. References m_sessions. Referenced by main(). 00248 { 00249 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) 00250 { 00251 Session *sess = (*it).second; 00252 sess -> Save(); 00253 } 00254 }
Definition at line 90 of file PeerHandler.h. References m_sessions. Referenced by main(). 00090 { 00091 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) 00092 { 00093 Session *sess = (*it).second; 00094 if (sess) 00095 { 00096 sess -> PeerStatus(); 00097 } 00098 } 00099 }
Definition at line 257 of file PeerHandler.cpp. References m_sessions. 00258 { 00259 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) 00260 { 00261 Session *sess = (*it).second; 00262 sess -> Status(sock); 00263 } 00264 }
Definition at line 103 of file PeerHandler.h. References m_quit. Referenced by main(). 00103 { return m_quit; }
Definition at line 107 of file PeerHandler.h. References m_debug. Referenced by MyLog::error(), and Session::Update(). 00107 { return m_debug; }
Definition at line 108 of file PeerHandler.h. References m_downloaders. Referenced by CheckDownloadRate(), and Session::Update(). 00108 { return m_downloaders; }
Definition at line 109 of file PeerHandler.h. References m_downloaders. Referenced by main(). 00109 { m_downloaders = x; }
Definition at line 110 of file PeerHandler.h. References m_optimistic. Referenced by CheckDownloadRate(), and Session::Update(). 00110 { return m_optimistic; }
Definition at line 111 of file PeerHandler.h. References m_optimistic. Referenced by main(). 00111 { m_optimistic = x; }
Definition at line 267 of file PeerHandler.cpp. References DEB, GetDownloaders(), and GetOptimistic(). Referenced by Session::Update(). 00268 { 00269 // TODO: disabled 00270 return; 00271 00272 std::map<pSocket *,size_t> mmap; 00273 size_t max = 0; 00274 DEB(printf("CheckDownloadRate()\n");) 00275 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++) 00276 { 00277 pSocket *p = dynamic_cast<pSocket *>((*it).second); 00278 if (p) 00279 { 00280 Peer *peer = p -> GetPeer(); 00281 if (p && peer && p -> CTS() && !peer -> IsChoked()) 00282 { 00283 size_t sz; 00284 if (p -> GetDownloadRate(sz)) 00285 { 00286 mmap[p] = sz; 00287 max = MAX(max,sz); 00288 } 00289 } 00290 } 00291 } 00292 if (mmap.size() == GetDownloaders() + GetOptimistic() ) 00293 { 00294 pSocket *least = NULL; 00295 for (std::map<pSocket *,size_t>::iterator it = mmap.begin(); it != mmap.end(); it++) 00296 { 00297 pSocket *sock = (*it).first; 00298 size_t sz = (*it).second; 00299 if (sz < max) 00300 { 00301 least = sock; 00302 max = sz; 00303 } 00304 } 00305 if (least) 00306 { 00307 //DEB(printf("Kicking pSocket with download rate %d bytes/sec\n", max);) 00308 least -> SendChoke(true); 00309 } 00310 } 00311 }
Definition at line 314 of file PeerHandler.cpp. 00315 { 00316 char slask[100]; 00317 #ifdef _WIN32 00318 SYSTEMTIME time; 00319 ::GetLocalTime(&time); 00320 sprintf(slask, "%ld.%03ld", time.wHour * 3600 + 00321 time.wMinute + 60 + time.wSecond, time.wMilliseconds); 00322 #else 00323 struct timeval tv; 00324 struct timezone tz; 00325 gettimeofday(&tv, &tz); 00326 sprintf(slask, "%ld.%06ld", tv.tv_sec, tv.tv_usec); 00327 #endif 00328 return slask; 00329 }
Member Data Documentation
Definition at line 120 of file PeerHandler.h. Referenced by GetSession(), RegSession(), Save(), SessionExists(), Show(), Tick(), and ~PeerHandler().
Definition at line 121 of file PeerHandler.h. Referenced by GetTorrentDirectory(), and SetTorrentDirectory().
Definition at line 127 of file PeerHandler.h. Referenced by LocalChokeTime(), and SetLocalChokeTime().
Definition at line 129 of file PeerHandler.h. Referenced by MaxRequestAge(), and SetMaxRequestAge().
The documentation for this class was generated from the following files: |