Ejemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="config">Configuration.Configuration object</param>
 public Dispatcher(Configuration.ServiceConfig config)
 {
     logger.Info("starting init servers");
     foreach (var server in config.Servers)
     {
         if (server.WeithtRate > 0)
         {
             var s = new Server(server);
             if (s.State != ServerState.Disable)
             {
                 SocketPool sp = new SocketPool(s, config.SocketPool);
                 s.ScoketPool = sp;
                 ServerPool.Add(s);
             }
         }
     }
     logger.Info("init servers end");
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     Loads Propeller settings from the appropriate location. Using this method ensures that settings are loaded in
        ///     exactly the same way and from the same place as the Propeller engine would load them.</summary>
        /// <param name="settingsPath">
        ///     Path and filename of the settings to load.</param>
        /// <param name="log">
        ///     Information about how the settings were loaded is logged here. Must not be <c>null</c>.</param>
        /// <param name="firstRunEver">
        ///     Adjusts the log messages for improved human readability.</param>
        public static PropellerSettings LoadSettings(string settingsPath, LoggerBase log, bool firstRunEver)
        {
            settingsPath = settingsPath ?? SettingsUtil.GetAttribute <PropellerSettings>().GetFileName();
            log.Info((firstRunEver ? "Loading settings file: " : "Reloading settings file: ") + settingsPath);

            PropellerSettings settings;
            var success = false;

            try
            {
                success = SettingsUtil.LoadSettings(out settings, settingsPath);
                if (success)
                {
                    settings.SaveQuiet(settingsPath);
                    return(settings);
                }
            }
            catch (Exception e)
            {
                settings = new PropellerSettings();
                log.Error("Settings file could not be loaded: {0} ({1}). Using default settings.".Fmt(e.Message, e.GetType().FullName));
            }

            if (!success)
            {
                if (!File.Exists(settingsPath))
                {
                    try
                    {
                        settings.Save(settingsPath);
                        log.Info("Default settings saved to {0}.".Fmt(settingsPath));
                    }
                    catch (Exception ex)
                    {
                        log.Warn("Attempt to save default settings to {0} failed: {1} ({2})".Fmt(settingsPath, ex.Message, ex.GetType().FullName));
                    }
                }
            }
            return(settings);
        }
Ejemplo n.º 3
0
        public CSocket GetScoket()
        {
            CSocket rSocket = null;

            lock (pool)
            {
                if (pool.Count > 0)
                {
                    rSocket = pool.Dequeue();
                }
                else if (pool.Total < socketPoolConfig.MaxPoolSize)
                {
                    var socket = new CSocket(endPoint, this, socketPoolConfig);
                    pool.AllSocket.Add(socket);
                    rSocket = socket;
                    logger.Info("created new socket.server:" + _Server.Name);
                }
                else
                {
                    if (!releasedEvent.WaitOne((int)socketPoolConfig.WaitTimeout.TotalMilliseconds))
                    {
                        logger.Error("socket connection pool is full(" + pool.Total + ")!server cu:" + _Server.CurrUserCount);
                        throw new TimeoutException("socket connection pool is full!");
                    }
                    else
                    {
                        rSocket = pool.Dequeue();
                    }
                }
            }
            if (rSocket == null)
            {
                throw new Exception("can't get socket from pool!");
            }
            rSocket.Reset();
            rSocket.InPool = false;
            return(rSocket);
        }
Ejemplo n.º 4
0
 private HttpResponse errorHandler(HttpRequest req, Exception exception)
 {
     if (exception is HttpException httpExc)
     {
         _log.Info("Request {0} failure code {1}.".Fmt(req.Url.ToFull(), (int)httpExc.StatusCode));
     }
     else
     {
         lock (_log)
         {
             _log.Error("Error in handler for request {0}:".Fmt(req.Url.ToFull()));
             PropellerUtil.LogException(_log, exception);
         }
     }
     throw exception;
 }
Ejemplo n.º 5
0
        void IPropellerModule.Init(LoggerBase log, JsonValue settings, ISettingsSaver saver)
        {
            _log      = log;
            _saver    = saver;
            _settings = ClassifyJson.Deserialize <Settings>(settings) ?? new Settings();

            var validPaths = new List <string>();

            foreach (var path in _settings.Paths)
            {
                if (!Directory.Exists(path))
                {
                    _log.Warn(@"DocGen: Warning: The folder ""{0}"" specified in the settings does not exist. Ignoring path.".Fmt(path));
                }
                else
                {
                    validPaths.Add(path);
                }
            }
            _settings.Paths = validPaths.ToArray();

            saver.SaveSettings(ClassifyJson.Serialize(_settings));

            string copyToPath = null;

            if (_settings.DllTempPath != null)
            {
                // Try to clean up old folders we've created before
                var tempPath = _settings.DllTempPath;
                Directory.CreateDirectory(tempPath);
                foreach (var path in Directory.GetDirectories(tempPath, "docgen-tmp-*"))
                {
                    try { Directory.Delete(path, true); }
                    catch { }
                }

                // Find a new folder to put the DLL files into
                int j = 1;
                copyToPath = Path.Combine(tempPath, "docgen-tmp-" + j);
                while (Directory.Exists(copyToPath))
                {
                    j++;
                    copyToPath = Path.Combine(tempPath, "docgen-tmp-" + j);
                }
                Directory.CreateDirectory(copyToPath);
            }

            _docGen = new DocumentationGenerator(_settings.Paths, _settings.RequireAuthentication ? _settings.UsernamePasswordFile ?? "" : null, copyToPath);
            lock (_log)
            {
                _log.Info("DocGen initialised with {0} assemblies: {1}".Fmt(_docGen.AssembliesLoaded.Count, _docGen.AssembliesLoaded.JoinString(", ")));
                if (_docGen.AssemblyLoadErrors.Count > 0)
                {
                    _log.Warn("{0} assembly load errors:".Fmt(_docGen.AssemblyLoadErrors.Count));
                    foreach (var tuple in _docGen.AssemblyLoadErrors)
                    {
                        _log.Warn("{0} error: {1}".Fmt(tuple.Item1, tuple.Item2));
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private static int _appDomainCount = 0;     // only used to give each AppDomain a unique name

        public AppDomainInfo(LoggerBase log, PropellerSettings settings, PropellerModuleSettings moduleSettings, ISettingsSaver saver)
        {
            ModuleSettings     = moduleSettings;
            Saver              = saver;
            _log               = log;
            _activeConnections = 0;

            // Determine the temporary folder that DLLs will be copied to
            var tempFolder = settings.TempFolder ?? Path.GetTempPath();

            Directory.CreateDirectory(tempFolder);

            // Find a new folder to put the DLL/EXE files into
            int j = 1;

            do
            {
                TempPathUsed = Path.Combine(tempFolder, "propeller-tmp-" + (j++));
            }while (Directory.Exists(TempPathUsed));
            Directory.CreateDirectory(TempPathUsed);

            // Copy all the DLLs/EXEs to the temporary folder
            foreach (var sourceFile in
                     new[] { typeof(PropellerEngine), typeof(IPropellerModule), typeof(HttpServer), typeof(Ut) }.Select(type => type.Assembly.Location).Concat(
                         new[] { "*.exe", "*.dll", "*.pdb" }.SelectMany(ext => Directory.EnumerateFiles(Path.GetDirectoryName(moduleSettings.ModuleDll), ext))))
            {
                var destFile = Path.Combine(TempPathUsed, Path.GetFileName(sourceFile));
                if (File.Exists(destFile))
                {
                    _log.Warn(2, "Skipping file {0} because destination file {1} already exists.".Fmt(sourceFile, destFile));
                }
                else
                {
                    _log.Info(2, "Copying file {0} to {1}".Fmt(sourceFile, destFile));
                    File.Copy(sourceFile, destFile);
                }
            }

            // Create an AppDomain
            var setup = new AppDomainSetup {
                ApplicationBase = TempPathUsed, PrivateBinPath = TempPathUsed
            };

            AppDomain   = AppDomain.CreateDomain("Propeller AppDomain #{0}, module {1}".Fmt(_appDomainCount++, moduleSettings.ModuleName), null, setup);
            RunnerProxy = (AppDomainRunner)AppDomain.CreateInstanceAndUnwrap("Propeller", "RT.Propeller.AppDomainRunner");
            RunnerProxy.Init(
                Path.Combine(TempPathUsed, Path.GetFileName(moduleSettings.ModuleDll)),
                moduleSettings.ModuleType,
                moduleSettings.ModuleName,
                moduleSettings.Settings,
                _log,
                saver);

            IEnumerable <string> filters = moduleSettings.MonitorFilters ?? Enumerable.Empty <string>();

            if (RunnerProxy.FileFiltersToBeMonitoredForChanges != null)
            {
                filters = filters.Concat(RunnerProxy.FileFiltersToBeMonitoredForChanges);
            }
            foreach (var filter in filters.Concat(moduleSettings.ModuleDll))
            {
                addFileSystemWatcher(Path.GetDirectoryName(filter), Path.GetFileName(filter));
            }

            UrlMappings = moduleSettings.Hooks.Select(hook => new UrlMapping(hook, Handle, true)).ToArray();

            _log.Info("Module {0} URLs: {1}".Fmt(moduleSettings.ModuleName, moduleSettings.Hooks.JoinString("; ")));
        }