コード例 #1
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}",
                                      acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint);

            Program.OutputLog.LogFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}",
                                        acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint);

            var userToken = _mAsyncSocketUserTokenPool.Pop();

            AsyncSocketUserTokenList.Add(userToken); //添加到正在连接列表
            userToken.ConnectSocket   = acceptEventArgs.AcceptSocket;
            userToken.ConnectDateTime = DateTime.Now;

            try
            {
                var willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求
                if (!willRaiseEvent)
                {
                    lock (userToken)
                    {
                        ProcessReceive(userToken.ReceiveEventArgs);
                    }
                }
            }
            catch (Exception e)
            {
                Program.Logger.ErrorFormat("Accept client {0} error, message: {1}", userToken.ConnectSocket, e.Message);
                Program.Logger.Error(e.StackTrace);

                Program.OutputLog.LogFormat("Accept client {0} error, message: {1}", userToken.ConnectSocket, e.Message);
            }

            StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接
        }
コード例 #2
0
        public AsyncSocketServer(int numConnections)
        {
            _mIsStarted         = false;
            _mNumConnections    = numConnections;
            _mReceiveBufferSize = ProtocolConst.ReceiveBufferSize;

            _mAsyncSocketUserTokenPool = new AsyncSocketUserTokenPool(numConnections);
            AsyncSocketUserTokenList   = new AsyncSocketUserTokenList();
            _mMaxNumberAcceptedClients = new Semaphore(numConnections, numConnections);
        }
コード例 #3
0
        public void CloseClientSocket(AsyncSocketUserToken userToken)
        {
            if (userToken.ConnectSocket == null)
            {
                return;
            }

            var socketInfo =
                $"Local Address: {userToken.ConnectSocket.LocalEndPoint} Remote Address: {userToken.ConnectSocket.RemoteEndPoint}";

            Program.Logger.InfoFormat("Client connection disconnected. {0}", socketInfo);

            Program.OutputLog.LogFormat("Client connection disconnected. {0}", socketInfo);


            if (userToken.ConnectSocket.Connected)
            {
                try
                {
                    userToken.ConnectSocket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception e)
                {
                    Program.Logger.ErrorFormat("CloseClientSocket Disconnect client {0} error, message: {1}", socketInfo, e.Message);

                    Program.OutputLog.LogFormat("CloseClientSocket Disconnect client {0} error, message: {1}", socketInfo, e.Message);
                }

                userToken.ConnectSocket.Close();
            }

            userToken.ConnectSocket = null; //释放引用,并清理缓存,包括释放协议对象等资源
            userToken.ReceiveBuffer.Clear(userToken.ReceiveBuffer.DataCount);
            userToken.SendBuffer.ClearPacket();

            _mMaxNumberAcceptedClients.Release();
            _mAsyncSocketUserTokenPool.Push(userToken);
            AsyncSocketUserTokenList.Remove(userToken);
        }
コード例 #4
0
        public void Init()
        {
            for (var i = 0; i < _mNumConnections; i++) //按照连接数建立读写对象
            {
                var userToken = new AsyncSocketUserToken(_mReceiveBufferSize);
                userToken.ReceiveEventArgs.Completed += IO_Completed;
                userToken.SendEventArgs.Completed    += IO_Completed;
                _mAsyncSocketUserTokenPool.Push(userToken);
            }

            var adjustTimeSchedule = new ScheduleTask(0, true, ScheduleType.StartOnCondition);

            adjustTimeSchedule.OnScheduleExecuting += delegate(object sender, ScheduleTaskExecuteEventArgs args)
            {
                var task = (ScheduleTask)sender;
                if (task == null || task.LastExecuteTime.Date == DateTime.Today.Date)
                {
                    return;
                }
                if (DateTime.Now.Hour == 3 && DateTime.Now.Minute == 0 && DateTime.Now.Second < 30)
                {
                    AsyncSocketUserTokenList.AdjestTime();
                }
            };
            adjustTimeSchedule.StartCondition = running =>
            {
                if (running)
                {
                    return(true);
                }
                return(DateTime.Now.Hour == 3);
            };
            ScheduleManager.Register(adjustTimeSchedule);

            EsProtocolMgr = new EsProtocolMgr();
        }