コード例 #1
0
ファイル: SocketEngine.cs プロジェクト: lionzhou1981/Lion
        /// <summary>
        /// 开始连接端口
        /// </summary>
        /// <param name="_host">连接的主机名</param>
        /// <param name="_port">连接的主机端口</param>
        /// <returns>是否连接成功</returns>
        public bool Connect(string _host, int _port)
        {
            try
            {
                this.type   = SocketEngineType.Client;
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this.socket.Connect(_host, _port);
            }
            catch (SocketException)
            {
            }
            catch (Exception _ex)
            {
                this.OnException(_ex);
            }

            if (this.socket.Connected)
            {
                Thread _thread = new Thread(new ThreadStart(this.Receive));
                _thread.Start();

                if (this.keepAlive > 0)
                {
                    this.timeroutThreadRunning  = true;
                    this.timeoutThread          = new Thread(new ParameterizedThreadStart(this.Keep));
                    this.timeoutThread.Priority = ThreadPriority.Lowest;
                    this.timeoutThread.Start(this.keepAlive);
                }
            }
            return(this.socket.Connected);
        }
コード例 #2
0
        public void EngineCanBeParsed(SocketEngineType expectedEngineType, params string[] args)
        {
            (bool isSuccess, CommandLineOptions commandLineOptions) = ConsoleLineArgumentsParser.ParseArguments(args);

            Assert.True(isSuccess);
            Assert.Equal(expectedEngineType, commandLineOptions.SocketEngine);
        }
コード例 #3
0
ファイル: SocketEngine.cs プロジェクト: lionzhou1981/Lion
        /// <summary>
        /// 开始监听端口
        /// </summary>
        /// <param name="_endPoint">监听端口</param>
        /// <returns>是否成功监听端口</returns>
        private bool Listen(EndPoint _endPoint)
        {
            try
            {
                this.type = SocketEngineType.Server;

                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                this.socket.LingerState.LingerTime = 0;

                this.socket.Bind(_endPoint);
                this.socket.Listen(this.limitedPending);

                this.bufferManager = new SocketEngineBuffer(this.LimitedSession, 4096);
                this.bufferManager.Init();

                this.Sessions = new SocketSessionCollection(this.limitedSession);
                for (int i = 0; i < this.LimitedSession; i++)
                {
                    SocketSession _socketSession = new SocketSession(this, i);
                    _socketSession.SocketAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(Async_Completed);
                    this.Sessions[i] = _socketSession;
                }

                this.timeroutThreadRunning = true;
                this.timeoutThread         = new Thread(new ThreadStart(this.Timeout));
                this.timeoutThread.Start();

                this.BeginAccept();

                return(true);
            }
            catch (Exception _ex)
            {
                this.OnException(_ex);
                return(false);
            }
        }