第一个函数
d:\sourcecode\github\supersocket\quickstart\basic\telnetserver_startbyconfig\program.cs
static void Main(string[] args) { Console.WriteLine("Press any key to start the server!"); Console.ReadKey(); Console.WriteLine(); var bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) { Console.WriteLine("Failed to initialize!"); Console.ReadKey(); return; } var result = bootstrap.Start(); Console.WriteLine("Start result: {0}!", result); if (result == StartResult.Failed) { Console.WriteLine("Failed to start!"); Console.ReadKey(); return; } Console.WriteLine("Press key 'q' to stop it!"); while (Console.ReadKey().KeyChar != 'q') { Console.WriteLine(); continue; } Console.WriteLine(); //Stop the appServer bootstrap.Stop(); Console.WriteLine("The server was stopped!"); }
第二个函数
d:\sourcecode\github\supersocket\socketengine\defaultbootstrap.cs
/// <summary> /// Starts this bootstrap. /// </summary> /// <returns></returns> public StartResult Start() { if (!m_Initialized) { if (m_GlobalLog.IsErrorEnabled) m_GlobalLog.Error("You cannot invoke method Start() before initializing!"); return StartResult.Failed; } var result = StartResult.None; var succeeded = 0; foreach (var server in m_AppServers) { if (!server.Start()) { if (m_GlobalLog.IsErrorEnabled) m_GlobalLog.InfoFormat("The server instance {0} has failed to be started!", server.Name); } else { succeeded++; if (Config.Isolation != IsolationMode.None) { if (m_GlobalLog.IsInfoEnabled) m_GlobalLog.InfoFormat("The server instance {0} has been started!", server.Name); } } } if (m_AppServers.Any()) { if (m_AppServers.Count == succeeded) result = StartResult.Success; else if (succeeded == 0) result = StartResult.Failed; else result = StartResult.PartialSuccess; } if (m_PerfMonitor != null) { m_PerfMonitor.Start(); if (m_GlobalLog.IsDebugEnabled) m_GlobalLog.Debug("The PerformanceMonitor has been started!"); } return result; }
第三个函数
d:\sourcecode\github\supersocket\socketbase\appserver.cs
/// <summary> /// Starts this AppServer instance. /// </summary> /// <returns></returns> public override bool Start() { if (!base.Start()) return false; if (!Config.DisableSessionSnapshot) StartSessionSnapshotTimer(); if (Config.ClearIdleSession) StartClearSessionTimer(); return true; }
第四个函数
d:\sourcecode\github\supersocket\socketbase\appserverbase.cs
/// <summary> /// Starts this server instance. /// </summary> /// <returns> /// return true if start successfull, else false /// </returns> public virtual bool Start() { var origStateCode = Interlocked.CompareExchange(ref m_StateCode, ServerStateConst.Starting, ServerStateConst.NotStarted); if (origStateCode != ServerStateConst.NotStarted) { if (origStateCode < ServerStateConst.NotStarted) throw new Exception("You cannot start a server instance which has not been setup yet."); if (Logger.IsErrorEnabled) Logger.ErrorFormat("This server instance is in the state {0}, you cannot start it now.", (ServerState)origStateCode); return false; } if (!m_SocketServer.Start()) { m_StateCode = ServerStateConst.NotStarted; return false; } StartedTime = DateTime.Now; m_StateCode = ServerStateConst.Running; m_ServerStatus[StatusInfoKeys.IsRunning] = true; m_ServerStatus[StatusInfoKeys.StartedTime] = StartedTime; try { //Will be removed in the next version #pragma warning disable 0612, 618 OnStartup(); #pragma warning restore 0612, 618 OnStarted(); } catch (Exception e) { if (Logger.IsErrorEnabled) { Logger.Error("One exception wa thrown in the method 'OnStartup()'.", e); } } finally { if (Logger.IsInfoEnabled) Logger.Info(string.Format("The server instance {0} has been started!", Name)); } return true; }
第五个函数
D:\SourceCode\GitHub\SuperSocket\SocketEngine\AsyncSocketServer.cs
public override bool Start() { try { int bufferSize = AppServer.Config.ReceiveBufferSize; if (bufferSize <= 0) bufferSize = 1024 * 4; m_BufferManager = new BufferManager(bufferSize * AppServer.Config.MaxConnectionNumber, bufferSize); try { m_BufferManager.InitBuffer(); } catch (Exception e) { AppServer.Logger.Error("Failed to allocate buffer for async socket communication, may because there is no enough memory, please decrease maxConnectionNumber in configuration!", e); return false; } // preallocate pool of SocketAsyncEventArgs objects SocketAsyncEventArgs socketEventArg; var socketArgsProxyList = new List<SocketAsyncEventArgsProxy>(AppServer.Config.MaxConnectionNumber); for (int i = 0; i < AppServer.Config.MaxConnectionNumber; i++) { //Pre-allocate a set of reusable SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); m_BufferManager.SetBuffer(socketEventArg); socketArgsProxyList.Add(new SocketAsyncEventArgsProxy(socketEventArg)); } m_ReadWritePool = new ConcurrentStack<SocketAsyncEventArgsProxy>(socketArgsProxyList); if (!base.Start()) return false; IsRunning = true; return true; } catch (Exception e) { AppServer.Logger.Error(e); return false; } }
第六个函数
d:\sourcecode\github\supersocket\socketengine\socketserverbase.cs
public virtual bool Start() { IsStopped = false; ILog log = AppServer.Logger; var config = AppServer.Config; var sendingQueuePool = new SmartPool<SendingQueue>(); sendingQueuePool.Initialize(Math.Max(config.MaxConnectionNumber / 6, 256), Math.Max(config.MaxConnectionNumber * 2, 256), new SendingQueueSourceCreator(config.SendingQueueSize)); SendingQueuePool = sendingQueuePool; for (var i = 0; i < ListenerInfos.Length; i++) { var listener = CreateListener(ListenerInfos[i]); listener.Error += new ErrorHandler(OnListenerError); listener.Stopped += new EventHandler(OnListenerStopped); listener.NewClientAccepted += new NewClientAcceptHandler(OnNewClientAccepted); if (listener.Start(AppServer.Config)) { Listeners.Add(listener); if (log.IsDebugEnabled) { log.DebugFormat("Listener ({0}) was started", listener.EndPoint); } } else //If one listener failed to start, stop started listeners { if (log.IsDebugEnabled) { log.DebugFormat("Listener ({0}) failed to start", listener.EndPoint); } for (var j = 0; j < Listeners.Count; j++) { Listeners[j].Stop(); } Listeners.Clear(); return false; } } IsRunning = true; return true; }
第七个函数
D:\SourceCode\GitHub\SuperSocket\SocketEngine\TcpAsyncSocketListener.cs
/// <summary> /// Starts to listen /// </summary> /// <param name="config">The server config.</param> /// <returns></returns> public override bool Start(IServerConfig config) { m_ListenSocket = new Socket(this.Info.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { m_ListenSocket.Bind(this.Info.EndPoint); m_ListenSocket.Listen(m_ListenBackLog); m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); SocketAsyncEventArgs acceptEventArg = new SocketAsyncEventArgs(); m_AcceptSAE = acceptEventArg; acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(acceptEventArg_Completed); if (!m_ListenSocket.AcceptAsync(acceptEventArg)) ProcessAccept(acceptEventArg); return true; } catch (Exception e) { OnError(e); return false; } }