示例#1
0
        /// <summary>
        /// 
        /// </summary>
        public SocketAsyncClient(IPEndPoint remoteEndPoint, int bufferSize, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            _remoteEndPoint = remoteEndPoint;
            _client = new Socket(addressFamily, socketType, protocolType);
            socketObject = new SocketObject(Guid.NewGuid(), _client);
            int maxConnection = 1;
            int numOfSaeaForRecSend = maxConnection * OpsToPreAlloc;
            _readWritePool = new SocketAsyncPool(numOfSaeaForRecSend);
            int numSize = numOfSaeaForRecSend * bufferSize;
            _bufferManager = new BufferManager(numSize, bufferSize);
            _bufferManager.InitBuffer();

            _saeaProxy = new SocketAsyncEventArgsProxy(bufferSize);
            _saeaProxy.ReceiveCompleted += OnReceiveCompleted;
            _saeaProxy.SendCompleted += OnSendCompleted;
            _saeaProxy.ClosedHandle += OnSocketClosing;

            SocketAsyncEventArgs saea;
            for (int i = 0; i < numOfSaeaForRecSend; i++)
            {
                saea = _saeaProxy.CreateNewSaea();
                _bufferManager.SetBuffer(saea);
                saea.UserToken = new DataToken(saea.Offset);
                _readWritePool.Push(saea);
            }
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        public SocketAsyncClient(IPEndPoint remoteEndPoint, int bufferSize, AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            _remoteEndPoint = remoteEndPoint;
            _client         = new Socket(addressFamily, socketType, protocolType);
            socketObject    = new SocketObject(Guid.NewGuid(), _client);
            int maxConnection       = 1;
            int numOfSaeaForRecSend = maxConnection * OpsToPreAlloc;

            _readWritePool = new SocketAsyncPool(numOfSaeaForRecSend);
            int numSize = numOfSaeaForRecSend * bufferSize;

            _bufferManager = new BufferManager(numSize, bufferSize);
            _bufferManager.InitBuffer();

            _saeaProxy = new SocketAsyncEventArgsProxy(bufferSize);
            _saeaProxy.ReceiveCompleted += OnReceiveCompleted;
            _saeaProxy.SendCompleted    += OnSendCompleted;
            _saeaProxy.ClosedHandle     += OnSocketClosing;

            SocketAsyncEventArgs saea;

            for (int i = 0; i < numOfSaeaForRecSend; i++)
            {
                saea = _saeaProxy.CreateNewSaea();
                _bufferManager.SetBuffer(saea);
                saea.UserToken = new DataToken(saea.Offset);
                _readWritePool.Push(saea);
            }
        }
示例#3
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="socketObject"></param>
 public bool Put(SocketObject socketObject)
 {
     SocketObject oldval;
     if (_pools.TryGetValue(socketObject.SessionId, out oldval))
     {
         _pools.TryUpdate(socketObject.SessionId, socketObject, oldval);
     }
     return _pools.TryAdd(socketObject.SessionId, socketObject);
 }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="socketObject"></param>
        public bool Put(SocketObject socketObject)
        {
            SocketObject oldval;

            if (_pools.TryGetValue(socketObject.SessionId, out oldval))
            {
                _pools.TryUpdate(socketObject.SessionId, socketObject, oldval);
            }
            return(_pools.TryAdd(socketObject.SessionId, socketObject));
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="socketObject"></param>
        /// <param name="data"></param>
        public void PushSend(SocketObject socketObject, byte[] data)
        {
            var e = _readWritePool.Pop();

            e.AcceptSocket = socketObject.Connection;
            DataToken token = (DataToken)e.UserToken;

            token.Socket = socketObject;
            _saeaProxy.Send(e, data);
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="socketObject"></param>
 /// <param name="data"></param>
 public void PushSend(SocketObject socketObject, byte[] data)
 {
     var e = _readWritePool.Pop();
     e.AcceptSocket = socketObject.Connection;
     DataToken token = (DataToken)e.UserToken;
     token.Socket = socketObject;
     _saeaProxy.Send(e, data);
 }
示例#9
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="socketObject"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public void PushSend(SocketObject socketObject, PacketData data)
 {
     PushSend(socketObject, data.ToByte());
 }
示例#10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="socketObject"></param>
        public bool Remove(SocketObject socketObject)
        {
            SocketObject oldval;

            return(_pools.TryRemove(socketObject.SessionId, out oldval));
        }
示例#11
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="socketObject"></param>
 public bool Remove(SocketObject socketObject)
 {
     SocketObject oldval;
     return _pools.TryRemove(socketObject.SessionId, out oldval);
 }
示例#12
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="socketObject"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public void PushSend(SocketObject socketObject, PacketData data)
 {
     PushSend(socketObject, data.ToByte());
 }
示例#13
0
        /// <summary>
        ///
        /// </summary>
        public bool CheckConnected(Guid sessionId)
        {
            SocketObject socketObject = _sessionPool.Find(sessionId);

            return(socketObject != null && socketObject.Connection.Connected);
        }