/// <summary> /// Constructor for proxy base class. /// </summary> /// <param name="name">Name of the proxy.</param> /// <param name="listenAddress">Address the proxy listens on.</param> /// <param name="listenPort">Port the proxy listens on.</param> /// <param name="httpsListenPort">Port the proxy listens on for HTTPS</param> /// <param name="proxyPath">Directory path the proxy is running in.</param> /// <param name="maxCacheSize">The max cache size in bytes.</param> /// <param name="cachePath">Path to the proxy's cache</param> /// <param name="packageCachePath">Path to the proxy's packages</param> protected RCProxy(string name, IPAddress listenAddress, int listenPort, int httpsListenPort, string proxyPath, long maxCacheSize, string cachePath, string packageCachePath) { _name = name; // setup proxy listener variables _listenAddress = listenAddress; _listenPort = listenPort; _httpsListenPort = httpsListenPort; _proxyPath = proxyPath; // no pending requests _requestEvent = new AutoResetEvent(false); //create and initialize the logger _logger = LogManager.GetLogger(this.GetType()); bool success = false; // initialize the cache directory success = InitializeCache(maxCacheSize, cachePath); if (!success) { _logger.Warn("Error initializing the " + name + " cache."); } // initialize the packages cache success = InitializePackagesCache(proxyPath + packageCachePath); if (!success) { _logger.Warn("Error initializing the " + name + " packages cache."); } // Load the blacklist LoadBlacklist(); // initialize the network usage detector _networkUsageDetector = new NetworkUsageDetector(this); // Start the timer that logs the network speed and so on. _changeNetworkStatusTimer = new Timer(LogSpeedAndApplyNetworkSpeedSettings, null, NETWORK_DETECTION_INTERVAL, NETWORK_DETECTION_INTERVAL); // Restore old state LoadState(); // Tell the programm to serialize state before shutdown Program.AddShutDownDelegate(SaveState); }
/// <summary> /// Includes a download into the network speed statistics. /// </summary> /// <param name="results">The speed results.</param> public void IncludeDownloadInCalculation(NetworkUsageDetector.NetworkUsageResults results) { Logger.Debug(String.Format("Speed: {0} for {1} bytes in {2:0.00} seconds.", results.SpeedBs, results.BytesDownloaded, results.ElapsedSeconds)); lock (_speedLockObj) { // the bytes and seconds used so far are multiplicated with NETWORK_SPEED_REDUCTION_FACTOR // (exponential decay) //_speedCalculationSecondsUsed = _speedCalculationSecondsUsed * NETWORK_SPEED_REDUCTION_FACTOR; //_speedCalculationBytesUsed = (int)(_speedCalculationBytesUsed * NETWORK_SPEED_REDUCTION_FACTOR); //// New values //double newSpeedCalcSecondsUsed = _speedCalculationSecondsUsed + results.ElapsedSeconds; //long newSpeedCalcBytesUsed = _speedCalculationBytesUsed + results.BytesDownloaded; //// In percent of how much the already existing values are weighted //double weightOfOldResults = 1; //double weightOfNewResults; //if (_speedCalculationBytesUsed == 0 || _speedCalculationSecondsUsed == 0) //{ // weightOfOldResults = 0; // weightOfNewResults = 1; //} //else //{ // weightOfNewResults = NETWORK_SPEED_TIME_WEIGHT * (results.ElapsedSeconds / newSpeedCalcSecondsUsed) // + (1 - NETWORK_SPEED_TIME_WEIGHT) * (results.BytesDownloaded / newSpeedCalcBytesUsed); //} //// Save new speed value //_networkSpeedBS = (long)((_networkSpeedBS + results.SpeedBs * weightOfNewResults) // / (weightOfOldResults + weightOfNewResults)); //// Save new values //_speedCalculationSecondsUsed = newSpeedCalcSecondsUsed; //_speedCalculationBytesUsed = newSpeedCalcBytesUsed; // Simple exponential decay if (_networkSpeedBS == 0) { _networkSpeedBS = results.SpeedBs; } else { _networkSpeedBS = (long)((_networkSpeedBS * NETWORK_SPEED_REDUCTION_FACTOR + results.SpeedBs * (1 - NETWORK_SPEED_REDUCTION_FACTOR))); } Logger.Debug("Detected current overall speed: " + _networkSpeedBS + " bytes/s."); } }
/// <summary> /// Create a new worker. /// </summary> /// <param name="networkUsageDetector">The outer class instance.</param> public NetworkUsageDetectorWorker(NetworkUsageDetector networkUsageDetector) { this._networkUsageDetector = networkUsageDetector; }