private void SetupBasic(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory) { if (rootConfig == null) { throw new ArgumentNullException("rootConfig"); } RootConfig = rootConfig; if (config == null) { throw new ArgumentNullException("config"); } if (!string.IsNullOrEmpty(config.Name)) { m_Name = config.Name; } else { m_Name = string.Format("{0}-{1}", this.GetType().Name, Math.Abs(this.GetHashCode())); } Config = config; SetDefaultCulture(rootConfig, config); if (!m_ThreadPoolConfigured) { if (!TheadPoolEx.ResetThreadPool(rootConfig.MaxWorkingThreads >= 0 ? rootConfig.MaxWorkingThreads : new Nullable <int>(), rootConfig.MaxCompletionPortThreads >= 0 ? rootConfig.MaxCompletionPortThreads : new Nullable <int>(), rootConfig.MinWorkingThreads >= 0 ? rootConfig.MinWorkingThreads : new Nullable <int>(), rootConfig.MinCompletionPortThreads >= 0 ? rootConfig.MinCompletionPortThreads : new Nullable <int>())) { throw new Exception("Failed to configure thread pool!"); } m_ThreadPoolConfigured = true; } if (socketServerFactory == null) { var socketServerFactoryType = Type.GetType("SuperSocket.SocketEngine.SocketServerFactory, SuperSocket.SocketEngine", true); socketServerFactory = (ISocketServerFactory)Activator.CreateInstance(socketServerFactoryType); } m_SocketServerFactory = socketServerFactory; //Read text encoding from the configuration if (!string.IsNullOrEmpty(config.TextEncoding)) { TextEncoding = Encoding.GetEncoding(config.TextEncoding); } else { TextEncoding = new ASCIIEncoding(); } }
public virtual bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory, ICustomProtocol <TCommandInfo> protocol) { if (rootConfig == null) { throw new ArgumentNullException("rootConfig"); } RootConfig = rootConfig; if (!m_ThreadPoolConfigured) { if (!TheadPoolEx.ResetThreadPool(rootConfig.MaxWorkingThreads >= 0 ? rootConfig.MaxWorkingThreads : new Nullable <int>(), rootConfig.MaxCompletionPortThreads >= 0 ? rootConfig.MaxCompletionPortThreads : new Nullable <int>(), rootConfig.MinWorkingThreads >= 0 ? rootConfig.MinWorkingThreads : new Nullable <int>(), rootConfig.MinCompletionPortThreads >= 0 ? rootConfig.MinCompletionPortThreads : new Nullable <int>())) { return(false); } m_ThreadPoolConfigured = true; } if (config == null) { throw new ArgumentNullException("config"); } Config = config; Name = config.Name; m_LogCommand = config.LogCommand; m_SocketServerFactory = socketServerFactory; SetupLogger(); if (!SetupLocalEndpoint(config)) { Logger.LogError("Invalid config ip/port"); return(false); } if (!SetupProtocol(config, protocol)) { return(false); } if (!SetupCommands(m_CommandDict)) { return(false); } if (!SetupSecurity(config)) { return(false); } return(SetupSocketServer()); }
private void SetupBasic(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory) { if (rootConfig == null) { throw new ArgumentNullException("rootConfig"); } RootConfig = rootConfig; if (config == null) { throw new ArgumentNullException("config"); } Config = config; if (!m_ThreadPoolConfigured) { if (!TheadPoolEx.ResetThreadPool(rootConfig.MaxWorkingThreads >= 0 ? rootConfig.MaxWorkingThreads : new Nullable <int>(), rootConfig.MaxCompletionPortThreads >= 0 ? rootConfig.MaxCompletionPortThreads : new Nullable <int>(), rootConfig.MinWorkingThreads >= 0 ? rootConfig.MinWorkingThreads : new Nullable <int>(), rootConfig.MinCompletionPortThreads >= 0 ? rootConfig.MinCompletionPortThreads : new Nullable <int>())) { throw new Exception("Failed to configure thread pool!"); } m_ThreadPoolConfigured = true; } if (socketServerFactory == null) { throw new ArgumentNullException("socketServerFactory"); } m_SocketServerFactory = socketServerFactory; }
/// <summary> /// Setups the appServer instance /// </summary> /// <param name="rootConfig">The root config.</param> /// <param name="config">The socket server instance config.</param> /// <param name="socketServerFactory">The socket server factory.</param> /// <param name="requestFilterFactory">The request filter factory.</param> /// <returns></returns> protected virtual bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory, IRequestFilterFactory <TRequestInfo> requestFilterFactory) { if (rootConfig == null) { throw new ArgumentNullException("rootConfig"); } RootConfig = rootConfig; if (!m_ThreadPoolConfigured) { if (!TheadPoolEx.ResetThreadPool(rootConfig.MaxWorkingThreads >= 0 ? rootConfig.MaxWorkingThreads : new Nullable <int>(), rootConfig.MaxCompletionPortThreads >= 0 ? rootConfig.MaxCompletionPortThreads : new Nullable <int>(), rootConfig.MinWorkingThreads >= 0 ? rootConfig.MinWorkingThreads : new Nullable <int>(), rootConfig.MinCompletionPortThreads >= 0 ? rootConfig.MinCompletionPortThreads : new Nullable <int>())) { return(false); } m_ThreadPoolConfigured = true; } if (config == null) { throw new ArgumentNullException("config"); } if (!(config is ServerConfig)) { //Use config plain model directly to avoid extra object casting in runtime var newConfig = new ServerConfig(); config.CopyPropertiesTo(newConfig); config = newConfig; } Config = config; m_SocketServerFactory = socketServerFactory; SetupLogger(); if (!SetupSecurity(config)) { return(false); } if (!SetupListeners(config)) { if (Logger.IsErrorEnabled) { Logger.Error("Invalid config ip/port"); } return(false); } if (!SetupRequestFilterFactory(config, requestFilterFactory)) { return(false); } m_CommandLoaders = new List <ICommandLoader> { new ReflectCommandLoader() }; if (Config.EnableDynamicCommand) { ICommandLoader dynamicCommandLoader; try { dynamicCommandLoader = AssemblyUtil.CreateInstance <ICommandLoader>("SuperSocket.Dlr.DynamicCommandLoader, SuperSocket.Dlr"); } catch (Exception e) { if (Logger.IsErrorEnabled) { Logger.Error("The file SuperSocket.Dlr is required for dynamic command support!", e); } return(false); } m_CommandLoaders.Add(dynamicCommandLoader); } if (!SetupCommands(m_CommandDict)) { return(false); } return(SetupSocketServer()); }
public virtual bool Init(ServerExecuteType executeType, string version, string name = null) { Running = new AtomicBool(); Stopped = new AtomicBool(); ExecuteType = executeType; Name = string.IsNullOrEmpty(name) == true?Process.GetCurrentProcess().ProcessName : name; Version = version; ModuleName = Name; CreateLogger(); Logger.Info("Init"); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); if (LoadServerConfig() == false) { if (ExecuteType == ServerExecuteType.Console) { Console.ReadKey(); } return(false); } TheadPoolEx.SetMinMaxThreads( m_ServerConfig.MinWorkerThreads == 0 ? Environment.ProcessorCount : m_ServerConfig.MinWorkerThreads, m_ServerConfig.MaxWorkerThreads == 0 ? Environment.ProcessorCount * 2: m_ServerConfig.MaxWorkerThreads, m_ServerConfig.MinCompletionPortThreads == 0 ? Environment.ProcessorCount : m_ServerConfig.MinCompletionPortThreads, m_ServerConfig.MaxCompletionPortThreads == 0 ? Environment.ProcessorCount * 2 : m_ServerConfig.MaxCompletionPortThreads); m_Sessions = new ConcurrentDictionary <long, Session>(); m_Listeners = new List <Listener>(); try { int bufferSize = m_ServerConfig.ReceiveBufferSize; if (bufferSize <= 0) { bufferSize = 1024 * 4; } // Send, Recv m_BufferManager = new BufferManager(bufferSize * m_ServerConfig.MaxConnectionNumber * 2, bufferSize); try { m_BufferManager.InitBuffer(); int[] poolSizes = new int[] { 4096, 16, 128, 256, 1024 }; m_PooledBufferManager = new PooledBufferManager(poolSizes); { SocketAsyncEventArgs socketEventArg; var socketArgsList = new List <SocketAsyncEventArgs>(m_ServerConfig.MaxConnectionNumber); for (int i = 0; i < m_ServerConfig.MaxConnectionNumber; i++) { socketEventArg = new SocketAsyncEventArgs(); socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(CompletedReceive); m_BufferManager.SetBuffer(socketEventArg); socketArgsList.Add(socketEventArg); } m_RecvSAEAPool = new ConcurrentStack <SocketAsyncEventArgs>(socketArgsList); } { SocketAsyncEventArgs socketEventArg; var socketArgsList = new List <SocketAsyncEventArgs>(m_ServerConfig.MaxConnectionNumber); for (int i = 0; i < m_ServerConfig.MaxConnectionNumber; i++) { socketEventArg = new SocketAsyncEventArgs(); socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(CompletedSend); //m_BufferManager.SetBuffer(socketEventArg); // Send할때 별도의 풀을 사용할거라서 socketEventArg.SetBuffer(null, 0, 0); socketArgsList.Add(socketEventArg); } m_SendSAEAPool = new ConcurrentStack <SocketAsyncEventArgs>(socketArgsList); } } catch (Exception e) { Logger.Error("메모리 부족! 최대 접속 허용 인원 설정을 낮추세요", e); if (ExecuteType == ServerExecuteType.Console) { Console.ReadKey(); } return(false); } m_Updater = new TimerThread(Update, null, 1); return(true); } catch (Exception e) { Logger.Error("Server.Init", e); if (ExecuteType == ServerExecuteType.Console) { Console.ReadKey(); } return(false); } }