Example #1
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArg)
        {
            try
            {
                WaitHandle.WaitAll(_resetEvent);
                _resetEvent[0].Set();
                Socket clientSocket = acceptEventArg.AcceptSocket;
                if (clientSocket == null)
                {
                    acceptEventArg.AcceptSocket = null;
                    StartAccept(acceptEventArg);
                    return;
                }
                SocketAsyncEventArgs receiveEventArgs = _readWritePool.Pop();
                receiveEventArgs.AcceptSocket = acceptEventArg.AcceptSocket;
                DataToken    token        = (DataToken)receiveEventArgs.UserToken;
                SocketObject socketObject = new SocketObject(Guid.NewGuid(), acceptEventArg.AcceptSocket);
                socketObject.Init();
                socketObject.sessionPool    = _sessionPool;
                socketObject.LastAccessTime = DateTime.Now;
                token.Socket = socketObject;

#if DEBUG
                Console.WriteLine("{0}>>{1} connect success", DateTime.Now.ToLongTimeString(), token.RemoteAddress);
#endif
                _sessionPool.Put(socketObject);
                _saeaProxy.StartReceive(receiveEventArgs);
                _acceptPool.Push(acceptEventArg);

                if (ConnectCompleted != null)
                {
                    ConnectCompleted.BeginInvoke(new SocketProcessEventArgs()
                    {
                        Socket = socketObject
                    }, null, null);
                }
            }
            catch (SocketException ex)
            {
                DataToken token = acceptEventArg.UserToken as DataToken;
                TraceLog.WriteError("Listener Error when processing data received from {0}:\r\n{1}", acceptEventArg.RemoteEndPoint, ex);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Listener error:{0}", ex.ToString());
            }

            // Accept the next connection request.
            acceptEventArg.AcceptSocket = null;
            StartAccept(acceptEventArg);
        }
Example #2
0
        private void StartReceive()
        {
            socketObject.Init();
            socketObject.LastAccessTime = DateTime.Now;
            if (ConnectCompleted != null)
            {
                ConnectCompleted.BeginInvoke(new SocketProcessEventArgs()
                {
                    Socket = socketObject
                }, null, null);
            }
            _saeaReceive = _readWritePool.Pop();
            _saeaReceive.AcceptSocket = socketObject.Connection;
            ((DataToken)_saeaReceive.UserToken).Socket = socketObject;

            _saeaSend = _readWritePool.Pop();
            _saeaSend.AcceptSocket = socketObject.Connection;
            ((DataToken)_saeaSend.UserToken).Socket = socketObject;

            _saeaProxy.StartReceive(_saeaReceive);
        }