📄 network.cxx
字号:
/* * libjingle * Copyright 2004--2005, Google Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#if defined(_MSC_VER) && _MSC_VER < 1300
#pragma warning(disable:4786 4530)
#endif
#include "host.h"#include "network.h"#include "socket.h" // this includes something that makes windows happy#include "time.h"#include "basicdefs.h"#include <algorithm>#include <cassert>#include <cfloat>#include <cmath>#include <Iphlpapi.h>namespace {const double kAlpha = 0.5; // weight for data infinitely far in the pastconst double kHalfLife = 2000; // half life of exponential decay (in ms)const double kLog2 = 0.693147180559945309417;const double kLambda = kLog2 / kHalfLife;// assume so-so quality unless data says otherwiseconst double kDefaultQuality = cricket::QUALITY_FAIR;typedef std::map<std::string,std::string> StrMap;}namespace cricket {void NetworkManager::CreateNetworks(std::vector<Network*>& networks) { IP_ADAPTER_INFO info_temp; ULONG len = 0; if (GetAdaptersInfo(&info_temp, &len) != ERROR_BUFFER_OVERFLOW) return; IP_ADAPTER_INFO *infos = new IP_ADAPTER_INFO[len]; if (GetAdaptersInfo(infos, &len) != NO_ERROR) return; int count = 0; for (IP_ADAPTER_INFO *info = infos; info != NULL; info = info->Next) { if (info->Type == MIB_IF_TYPE_LOOPBACK) continue; if (strcmp(info->IpAddressList.IpAddress.String, "0.0.0.0") == 0) continue; // In production, don't transmit the network name because of // privacy concerns. Transmit a number instead. std::string name;#if defined(PRODUCTION) std::ostringstream ost; ost << count; name = ost.str(); count++;#else name = info->Description;#endif networks.push_back(new Network(name, SocketAddress::StringToIP(info->IpAddressList.IpAddress.String))); } delete infos;}void NetworkManager::GetNetworks(std::vector<Network*>& result) { std::vector<Network*> list; CreateNetworks(list); for (uint32 i = 0; i < list.size(); ++i) { NetworkMap::iterator iter = networks_.find(list[i]->name()); Network* network; if (iter == networks_.end()) { network = list[i]; } else { network = iter->second; network->set_ip(list[i]->ip()); delete list[i]; } networks_[network->name()] = network; result.push_back(network); }}Network::Network(const std::string& name, uint32 ip) : name_(name), ip_(ip), uniform_numerator_(0), uniform_denominator_(0), exponential_numerator_(0), exponential_denominator_(0), quality_(kDefaultQuality) { last_data_time_ = Time(); // TODO: seed the historical data with one data point based on the link speed // metric from XP (4.0 if < 50, 3.0 otherwise).}void Network::StartSession(NetworkSession* session) { assert(std::find(sessions_.begin(), sessions_.end(), session) == sessions_.end()); sessions_.push_back(session);}void Network::StopSession(NetworkSession* session) { SessionList::iterator iter = std::find(sessions_.begin(), sessions_.end(), session); if (iter != sessions_.end()) sessions_.erase(iter);}void Network::EstimateQuality() { uint32 now = Time(); // Add new data points for the current time. for (uint32 i = 0; i < sessions_.size(); ++i) { if (sessions_[i]->HasQuality()) AddDataPoint(now, sessions_[i]->GetCurrentQuality()); } // Construct the weighted average using both uniform and exponential weights. double exp_shift = exp(-kLambda * (now - last_data_time_)); double numerator = uniform_numerator_ + exp_shift * exponential_numerator_; double denominator = uniform_denominator_ + exp_shift * exponential_denominator_; if (denominator < DBL_EPSILON) quality_ = kDefaultQuality; else quality_ = numerator / denominator;}void Network::AddDataPoint(uint32 time, double quality) { uniform_numerator_ += kAlpha * quality; uniform_denominator_ += kAlpha; double exp_shift = exp(-kLambda * (time - last_data_time_)); exponential_numerator_ = (1 - kAlpha) * quality + exp_shift * exponential_numerator_; exponential_denominator_ = (1 - kAlpha) + exp_shift * exponential_denominator_; last_data_time_ = time;}} // namespace cricket
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -