Beispiel #1
0
        private static void Main(string[] args)
        {
            _server = new TcpSocketServer("127.0.0.1", 6666);

            _server.OnAcceptCompletedEvent += Server_OnAcceptCompletedEvent;

            _server.OnCloseCompletedEvent += Server_OnCloseCompletedEvent;

            _server.OnDisconnectClientCompletedEvent += Server_OnDisconnectClientCompletedEvent;

            _server.OnReceiveCompletedEvent += Server_OnReceiveCompletedEvent;

            _server.OnSendCompletedEvent += Server_OnSendCompletedEvent;

            _server.Start();

            Console.WriteLine("Start server ...");

            while (true)
            {
                string val = Console.ReadLine();

                if (val == "exit")
                {
                    _server.Close();

                    break;
                }

                _server.SendAll(Encoding.UTF8.GetBytes(val));
            }
        }
Beispiel #2
0
        private bool socketListen()
        {
            // 启动状态确认,避免重复启动
            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.");
                }
                Logger.Error(string.Format("This server instance is in the state {0}, you cannot start it now.", (ServerState)origStateCode));
                return(false);
            }

            // 调用SocketServer.Start()
            if (!m_SocketServer.Start())
            {
                m_StateCode = ServerStateConst.NotStarted;
                return(false);
            }
            StartedTime = DateTime.Now;
            m_StateCode = ServerStateConst.Running;
            return(true);
        }
        /// <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);
        }
Beispiel #4
0
 public async Task StartAsync(CancellationToken cancellationToken)
 {
     await Task.Factory.StartNew(async() =>
     {
         _socketTcpServer.Init();
         //等待应用程序启动完成后在开启Tcp服务
         await Task.Delay(3000, cancellationToken);
         ConstDefine.CheckUserId = (byte)CommonHelper.MakeCheckUserId(_kj1012CollectionSetting.UserId);
         var result = _socketTcpServer.Start(new IPEndPoint(IPAddress.Any, _kj1012CollectionSetting.SocketServerPort));
         if (!result)
         {
             _logger.LogError("socket server start fail");
         }
     }, cancellationToken);
 }
        public virtual bool Start()
        {
            if (this.IsRunning)
            {
                Logger.LogError("This socket server is running already, you needn't start it.");
                return(false);
            }

            if (!m_SocketServer.Start())
            {
                return(false);
            }

            OnStartup();

            return(true);
        }
Beispiel #6
0
        public virtual bool Start()
        {
            if (!m_SocketServer.Start())
            {
                return(false);
            }

            if (Config.ClearIdleSession)
            {
                StartClearSessionTimer();
            }

            if (!StartConsoleHost())
            {
                LogUtil.LogError(this, "Failed to start console service host for " + Name);
                Stop();
                return(false);
            }

            return(true);
        }
        public void Start()
        {
            if (loggerFactroy == null)
            {
                throw new InvalidOperationException("LoggerFactroy not set : Please call the \"SetLoggerFactroy\" Method and set it up.");
            }

            logger = loggerFactroy.GetLogger(GetType());
            if (logger == null)
            {
                throw new ArgumentNullException("Unable to create logger.");
            }

            if (serverGenerator == null)
            {
                throw new InvalidOperationException("SocketServer not set : Please call the \"SetSocketServer\" Method and set it up.");
            }

            if (serverConfigrator == null)
            {
                throw new InvalidOperationException("SocketServer Configrator not set : Please call the \"SetSocketServerConfigrator\" Method and set it up");
            }

            if (sessionConfigrator == null)
            {
                throw new InvalidOperationException("SocketSession Configrator not set : Please call the \"SetSocketSessionConfigrator\" Method and set it up");
            }

            server = serverGenerator.Invoke();
            if (server == null)
            {
                throw new InvalidOperationException("Server generator returned null");
            }

            serverConfigrator.Invoke(server);

            server.Start(this);
        }
Beispiel #8
0
        /// <summary>
        /// Starts this server instance.
        /// </summary>
        /// <returns>
        /// return true if start successfull, else false
        /// </returns>
        public virtual bool Start()
        {
            if (this.IsRunning)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error("This socket server is running already, you needn't start it.");
                }

                return(false);
            }

            if (!m_SocketServer.Start())
            {
                return(false);
            }

            StartedTime = DateTime.Now;

            OnStartup();

            return(true);
        }
Beispiel #9
0
 public void Start(int port)
 {
     m_server.Start(port);
 }
Beispiel #10
0
 public void Start()
 {
     _server.Start();
 }