Example #1
0
 public void Remove(AsyncSocketUserToken userToken)
 {
     lock (m_list)
     {
         m_list.Remove(userToken);
     }
 }
Example #2
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            Debug.WriteLog.Log(eLogLevel.ell_Debug,
                               "Client connection accepted. Local Address: " + acceptEventArgs.AcceptSocket.LocalEndPoint + ", Remote Address: " + acceptEventArgs.AcceptSocket.RemoteEndPoint);
            AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop();

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

            try
            {
                bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求
                if (!willRaiseEvent)
                {
                    lock (userToken)
                    {
                        ProcessReceive(userToken.ReceiveEventArgs);
                    }
                }
            }
            catch (Exception E)
            {
                Debug.WriteLog.Log(eLogLevel.ell_Error, E.StackTrace);
            }

            StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接
        }
Example #3
0
        void ProcessAccept(SocketAsyncEventArgs asyncEventArgs)
        {
            Program.Logger.InfoFormat("Client connection accepted, LocalEndPoint:{0}, RemoteEndPoint:{1}",
                                      asyncEventArgs.AcceptSocket.LocalEndPoint.ToString(), asyncEventArgs.AcceptSocket.RemoteEndPoint.ToString());

            AsyncSocketUserToken userToken = m_AsyncSocketUserTokenPool.Pop();

            m_AsyncSocketUserTokenList.Add(userToken);
            userToken.ConnectSocket   = asyncEventArgs.AcceptSocket;
            userToken.ConnectDateTime = DateTime.Now;

            try
            {
                bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs);
                if (!willRaiseEvent)
                {
                    lock (userToken)
                    {
                        ProcessReceive(userToken.ReceiveEventArgs);
                    }
                }
            }
            catch (Exception ex)
            {
                Program.Logger.ErrorFormat("Accept client {0}, error message {1}", userToken.ConnectSocket, ex.Message);
                Program.Logger.Error(ex.StackTrace);
            }
            //把当前异步释放,等待下次连接
            StartAccept(asyncEventArgs);
        }
Example #4
0
 public BaseSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
     : base(asyncSocketServer, asyncSocketUserToken)
 {
     m_userName   = "";
     m_logined    = false;
     m_socketFlag = "";
 }
        /// <summary>
        /// 关闭客户端连接
        /// </summary>
        /// <param name="userToken"></param>
        public void CloseClientSocket(AsyncSocketUserToken userToken)
        {
            if (userToken.ConnectSocket == null)
                return;
            string socketInfo = string.Format("Local Address: {0} Remote Address: {1}", userToken.ConnectSocket.LocalEndPoint,
                userToken.ConnectSocket.RemoteEndPoint);
            Program.Logger.InfoFormat("Client connection disconnected. {0}", socketInfo);
            try
            {
                userToken.ConnectSocket.Shutdown(SocketShutdown.Both);//禁用某 Socket 上的发送和接收
            }
            catch (Exception E)
            {
                Program.Logger.ErrorFormat("CloseClientSocket Disconnect client {0} error, message: {1}", socketInfo, E.Message);
            }

            lock (UploadSocketProtocol.bsTree)
            {
                UploadSocketProtocol.bsTree.DropCar(userToken.id); //从道路树中删除该道路
            }
            userToken.ConnectSocket.Close();//关闭 Socket 连接并释放所有关联的资源
            userToken.ConnectSocket = null; //释放引用,并清理缓存,包括释放协议对象等资源

            m_maxNumberAcceptedClients.Release();//信号量,增加一个可用连接
            m_asyncSocketUserTokenPool.Push(userToken);//将这个不用的userToken放入asyncSocketUserTokenPool池中
            m_asyncSocketUserTokenList.Remove(userToken);//从连接到服务器的客户端列表中删除该客户端连接
        }
Example #6
0
 public void Remove(AsyncSocketUserToken token)
 {
     if (m_list.Contains(token))
     {
         m_list.Remove(token);
     }
 }
        private void BuildingSocketInvokeElement(AsyncSocketUserToken userToken)
        {
            byte flag = userToken.ReceiveEventArgs.Buffer[userToken.ReceiveEventArgs.Offset];

            if (flag == (byte)ProtocolFlag.Upload)
            {
                userToken.AsyncSocketInvokeElement = new UploadSocketProtocol(this, userToken);
            }
            else if (flag == (byte)ProtocolFlag.Download)
            {
                userToken.AsyncSocketInvokeElement = new DownloadSocketProtocol(this, userToken);
            }
            else if (flag == (byte)ProtocolFlag.RemoteStream)
            {
                userToken.AsyncSocketInvokeElement = new RemoteStreamSocketProtocol(this, userToken);
            }
            else if (flag == (byte)ProtocolFlag.Throughput)
            {
                userToken.AsyncSocketInvokeElement = new ThroughputSocketProtocol(this, userToken);
            }
            else if (flag == (byte)ProtocolFlag.Control)
            {
                userToken.AsyncSocketInvokeElement = new ControlSocketProtocol(this, userToken);
            }
            else if (flag == (byte)ProtocolFlag.LogOutput)
            {
                userToken.AsyncSocketInvokeElement = new LogOutputSocketProtocol(this, userToken);
            }
            if (userToken.AsyncSocketInvokeElement != null)
            {
                Program.Logger.InfoFormat("Building socket invoke element {0}.Local Address: {1}, Remote Address: {2}",
                                          userToken.AsyncSocketInvokeElement, userToken.ClientSocket.LocalEndPoint, userToken.ClientSocket.RemoteEndPoint);
            }
        }
        void IO_Completed(object sender, SocketAsyncEventArgs asyncEventArgs)
        {
            AsyncSocketUserToken userToken = asyncEventArgs.UserToken as AsyncSocketUserToken;

            userToken.ActiveDateTime = DateTime.Now;
            try
            {
                lock (userToken)
                {
                    if (asyncEventArgs.LastOperation == SocketAsyncOperation.Receive)
                    {
                        ProcessReceive(asyncEventArgs);
                    }
                    else if (asyncEventArgs.LastOperation == SocketAsyncOperation.Send)
                    {
                        ProcessSend(asyncEventArgs);
                    }
                    else
                    {
                        throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                    }
                }
            }
            catch (Exception E)
            {
                Program.Logger.ErrorFormat("IO_Completed {0} error, message: {1}", userToken.ClientSocket, E.Message);
                Program.Logger.Error(E.StackTrace);
            }
        }
        /// <summary>
        /// 接收链接后的处理过程
        /// </summary>
        /// <param name="e"></param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}", acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint);

            //从连接池中分配一个连接赋给当前连接
            AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop();

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

            try
            {
                // As soon as the client is connected, post a receive to the connection
                bool 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}, trace:{2}", userToken.ConnectSocket, E.Message, E.StackTrace);
                //Program.Logger.Error(E.StackTrace);
            }

            // Accept the next connection request
            StartAccept(e);
        }
Example #10
0
 public void Add(AsyncSocketUserToken userToken)
 {
     lock (m_list)
     {
         m_list.Add(userToken);
     }
 }
Example #11
0
        /// <summary>
        /// 异步发送完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void IO_SendCompleted(object sender, SocketAsyncEventArgs e)
        {
            AsyncSocketUserToken userToken = e.UserToken as AsyncSocketUserToken;

            userToken.ActiveDateTime = DateTime.Now;
            try
            {
                lock (userToken)
                {
                    if (e.LastOperation == SocketAsyncOperation.Send)
                    {
                        ProcessSend(e);
                    }
                    else
                    {
                        throw new ArgumentException("The last operation completed on the socket was not send");
                    }
                }
            }
            catch (Exception E)
            {
                Program.Logger.ErrorFormat("IO_Completed {0} error, message: {1}, trace:{2}", userToken.ConnectSocket, E.Message, E.StackTrace);
                //Program.Logger.Error(E.StackTrace);
            }
        }
Example #12
0
        /// <summary>
        /// 发送消息(得先从队列里把要发送的数据拿出来)
        /// This method is invoked when an asynchronous send operation completes.
        /// The method issues another receive on the socket to read any additional data sent from the client
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private bool ProcessSend(SocketAsyncEventArgs e)
        {
            AsyncSocketUserToken userToken = e.UserToken as AsyncSocketUserToken;

            userToken.ActiveDateTime = DateTime.Now;
            if (e.SocketError == SocketError.Success)
            {
                AsyncSendBufferManager bufferManager = userToken.SendBufferManager;
                bufferManager.ClearFirstPacket(); //清除已发送的包
                int offset = 0;
                int count  = 0;
                if (bufferManager.GetFirstPacket(ref offset, ref count))
                {
                    return(SendAsyncEvent(userToken.ConnectSocket, userToken.SendEventArgs, bufferManager.DynamicBufferManager.Buffer, offset, count));
                }
                else
                {
                    return(true);
                }
                //return SendCallback();
            }
            else
            {
                CloseClientSocket(userToken);
                return(false);
            }
        }
        public void CloseClientSocket(AsyncSocketUserToken userToken)
        {
            if (userToken.ClientSocket == null)
            {
                return;
            }
            string socketInfo = string.Format("Local Address: {0} Remote Address: {1}", userToken.ClientSocket.LocalEndPoint,
                                              userToken.ClientSocket.RemoteEndPoint);

            Program.Logger.InfoFormat("Client connection disconnected. {0}", socketInfo);
            try
            {
                userToken.ClientSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception E)
            {
                Program.Logger.ErrorFormat("CloseClientSocket Disconnect client {0} error, message: {1}", socketInfo, E.Message);
            }
            userToken.ClientSocket.Close();
            userToken.ClientSocket = null; //释放引用,并清理缓存,包括释放协议对象等资源

            m_maxNumberAcceptedClients.Release();
            m_asyncSocketUserTokenPool.Push(userToken);
            m_asyncSocketUserTokenList.Remove(userToken);
        }
 public void Add(AsyncSocketUserToken userToken)
 {
     lock(m_list)
     {
         m_list.Add(userToken);
     }
 }
 public void Remove(AsyncSocketUserToken userToken)
 {
     lock (m_list)
     {
         m_list.Remove(userToken);
     }
 }
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}",
                                      acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint);

            AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop();

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

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

            StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接
        }
        /// <summary>
        /// 关闭socket连接
        /// </summary>
        /// <param name="s"></param>
        /// <param name="e"></param>
        public void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncSocketUserToken userToken = e.UserToken as AsyncSocketUserToken;

            if (userToken.ConnectSocket == null)
            {
                return;
            }
            Interlocked.Decrement(ref _clientCount);
            Log4Debug(String.Format("客户端 {0} 断开连接, 当前 {1} 个连接。", userToken.RemoteEndPoint.ToString(), _clientCount));

            try
            {
                userToken.ConnectSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception)
            {
                // Throw if client has closed, so it is not necessary to catch.
            }
            finally
            {
                userToken.ConnectSocket.Close();
                userToken.ConnectSocket = null;
            }

            //Interlocked.Decrement(ref _clientCount);
            _maxAcceptClientSem.Release();
            _userTokenPool.Push(userToken);
            _clientList.Remove(userToken);
        }
Example #18
0
        void IO_Completed(object sender, SocketAsyncEventArgs asyncEventArgs)
        {
            AsyncSocketUserToken userToken = asyncEventArgs.UserToken as AsyncSocketUserToken;

            userToken.ActiveDateTime = DateTime.Now;
            try
            {
                lock (userToken)
                {
                    if (asyncEventArgs.LastOperation == SocketAsyncOperation.Receive)
                    {
                        ProcessReceive(asyncEventArgs);
                    }
                    else if (asyncEventArgs.LastOperation == SocketAsyncOperation.Send)
                    {
                        ProcessSend(asyncEventArgs);
                    }
                    else
                    {
                        throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                    }
                }
            }
            catch (Exception E)
            {
                Debug.WriteLog.Log(eLogLevel.ell_Error, E.StackTrace);
            }
        }
        protected string m_userName; //用户用户名

        #endregion Fields

        #region Constructors

        public BaseSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
            : base(asyncSocketServer, asyncSocketUserToken)
        {
            m_userName = "";
            m_logined = false;
            m_socketFlag = "";
        }
Example #20
0
        void ProcessReceive(SocketAsyncEventArgs asyncEventArgs)
        {
            AsyncSocketUserToken userToken = asyncEventArgs.UserToken as AsyncSocketUserToken;

            if (userToken.ConnectSocket == null)
            {
                return;
            }
            userToken.ActiveDateTime = DateTime.Now;

            if (userToken.ReceiveEventArgs.BytesTransferred > 0 && userToken.ReceiveEventArgs.SocketError == SocketError.Success)
            {
                int offset = userToken.ReceiveEventArgs.Offset;
                int count  = userToken.ReceiveEventArgs.BytesTransferred;
                //存在socket对象,并且没有绑定协议对象,则进行协议对象绑定
                if (userToken.AsyncSocketInvokeElement == null & userToken.ConnectSocket != null)
                {
                    BuildingSocketInvokeElement(userToken);
                    offset = offset + 1;
                    count  = count - 1;
                }
                if (userToken.AsyncSocketInvokeElement == null)
                {
                    Program.Logger.WarnFormat("Illegal Client connection, LocalEndPoint:{0}, RemoteEndPoint:{1}",
                                              asyncEventArgs.AcceptSocket.LocalEndPoint.ToString(), asyncEventArgs.AcceptSocket.RemoteEndPoint.ToString());
                    CloseClientSocket(userToken);
                }
                else
                {
                    if (count > 0)
                    {
                        if (!userToken.AsyncSocketInvokeElement.ProcessReceive(userToken.ReceiveEventArgs.Buffer, offset, count))
                        {
                            CloseClientSocket(userToken);
                        }
                        else
                        {
                            bool willRaiseEvenet = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs);
                            if (!willRaiseEvenet)
                            {
                                ProcessReceive(userToken.ReceiveEventArgs);
                            }
                        }
                    }
                    else
                    {
                        bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs);
                        if (!willRaiseEvent)
                        {
                            ProcessReceive(userToken.ReceiveEventArgs);
                        }
                    }
                }
            }
            else
            {
                CloseClientSocket(userToken);
            }
        }
Example #21
0
        /// <summary>
        /// 处理接收数据
        /// </summary>
        /// <param name="receiveEventArgs"></param>
        private void ProcessReceive(SocketAsyncEventArgs receiveEventArgs)
        {
            AsyncSocketUserToken userToken = receiveEventArgs.UserToken as AsyncSocketUserToken;

            if (userToken.ConnectSocket == null)
            {
                return;
            }
            userToken.ActiveDateTime = DateTime.Now;
            if (userToken.ReceiveEventArgs.BytesTransferred > 0 && userToken.ReceiveEventArgs.SocketError == SocketError.Success)
            {
                int offset = userToken.ReceiveEventArgs.Offset;
                int count  = userToken.ReceiveEventArgs.BytesTransferred;
                //(确定是什么协议,用于绑定上线客户端,以后来的就都走这个socket)存在Socket对象,并且没有绑定协议对象,则进行协议对象绑定
                if ((userToken.AsyncSocketInvokeElement == null) & (userToken.ConnectSocket != null))
                {
                    //查找是哪一个协议
                    BuildingSocketInvokeElement(userToken);
                    offset = offset + 1;
                    count  = count - 1;
                }
                if (userToken.AsyncSocketInvokeElement == null) //如果没有解析对象,提示非法连接并关闭连接
                {
                    Program.Logger.WarnFormat("Illegal client connection. Local Address: {0}, Remote Address: {1}", userToken.ConnectSocket.LocalEndPoint, userToken.ConnectSocket.RemoteEndPoint);
                    CloseClientSocket(userToken);
                }
                else//现在是收到消息内容了
                {
                    if (count > 0) //处理接收数据
                    {
                        bool blnProcessReceive = userToken.AsyncSocketInvokeElement.ProcessReceive(userToken.ReceiveEventArgs.Buffer, offset, count);
                        if (!blnProcessReceive) //如果处理数据返回失败,则断开连接
                        {
                            CloseClientSocket(userToken);
                        }
                        else //否则投递下次介绍数据请求
                        {
                            bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求
                            if (!willRaiseEvent)//如果 I/O 操作同步完成,将返回 false。
                            {
                                ProcessReceive(userToken.ReceiveEventArgs);
                            }
                        }
                    }
                    else
                    {
                        bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求
                        if (!willRaiseEvent)
                        {
                            ProcessReceive(userToken.ReceiveEventArgs);
                        }
                    }
                }
            }
            else
            {
                CloseClientSocket(userToken);
            }
        }
 public void CopyList(ref AsyncSocketUserToken[] array)
 {
     lock (m_list)
     {
         array = new AsyncSocketUserToken[m_list.Count];
         m_list.CopyTo(array);
     }
 }
Example #23
0
 public void CopyList(ref AsyncSocketUserToken[] array)
 {
     lock (m_list)
     {
         array = new AsyncSocketUserToken[m_list.Count];
         m_list.CopyTo(array);
     }
 }
 /// <summary>
 /// 初始化连接池
 /// </summary>
 public void Init()
 {
     for (int i = 0; i < m_numConnections; i++) //按照连接数建立读写对象
     {
         AsyncSocketUserToken userToken = new AsyncSocketUserToken(m_receiveBufferSize);
         userToken.ReceiveEventArgs.Completed += IO_Completed;
         userToken.SendEventArgs.Completed    += IO_Completed;
         m_asyncSocketUserTokenPool.Push(userToken);
     }
 }
 public void Push(AsyncSocketUserToken item)
 {
     if (item == null)
     {
         throw new ArgumentException("Items added to a AsyncSocketUserToken cannot be null");
     }
     lock (m_pool)
     {
         m_pool.Push(item);
     }
 }
Example #26
0
 public UploadSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
     : base(asyncSocketServer, asyncSocketUserToken)
 {
     m_socketFlag = "Upload";
     m_fileName   = "";
     m_fileStream = null;
     lock (m_asyncSocketServer.UploadSocketProtocolMgr)
     {
         m_asyncSocketServer.UploadSocketProtocolMgr.Add(this);
     }
 }
Example #27
0
 public void Push(AsyncSocketUserToken token)
 {
     if (token == null)
     {
         throw new Exception("add to AsyncSocketUserToken Pool cannot be null!");
     }
     else
     {
         m_pool.Push(token);
     }
 }
Example #28
0
 public void Push(AsyncSocketUserToken item)
 {
     if (item == null)
     {
         throw new ArgumentException("Items added to a AsyncSocketUserToken cannot be null");
     }
     lock (m_pool)
     {
         m_pool.Push(item);
     }
 }
        public AsyncSocketInvokeElement(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
        {
            m_asyncSocketServer    = asyncSocketServer;
            m_asyncSocketUserToken = asyncSocketUserToken;

            m_incomingDataParser    = new IncomingDataParser();
            m_outgoingDataAssembler = new OutgoingDataAssembler();

            m_connectDateTime = DateTime.UtcNow;
            m_activeDateTime  = DateTime.UtcNow;
            m_sendAsync       = false;
        }
        public LogOutputSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
            : base(asyncSocketServer, asyncSocketUserToken)
        {
            m_socketFlag = "LogOutput";
            m_logFixedBuffer = new LogFixedBuffer();
            lock (Program.AsyncSocketSvr.LogOutputSocketProtocolMgr)
            {
                Program.AsyncSocketSvr.LogOutputSocketProtocolMgr.Add(this);
            }

            SendResponse();
        }
Example #31
0
        public void Init()
        {
            AsyncSocketUserToken userToken = null;

            for (int i = 0; i < m_numConnections; i++)
            {
                userToken = new AsyncSocketUserToken(m_receiveBufferSize);
                userToken.ReceiveEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                userToken.SendEventArgs.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                m_AsyncSocketUserTokenPool.Push(userToken);
            }
        }
        public LogOutputSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
            : base(asyncSocketServer, asyncSocketUserToken)
        {
            m_socketFlag     = "LogOutput";
            m_logFixedBuffer = new LogFixedBuffer();
            lock (Program.AsyncSocketSvr.LogOutputSocketProtocolMgr)
            {
                Program.AsyncSocketSvr.LogOutputSocketProtocolMgr.Add(this);
            }

            SendResponse();
        }
Example #33
0
 public void GCUserToken(AsyncSocketUserToken userToken)
 {
     for (int i = 0; i < m_asyncSocketUserTokenList.Count; i++)
     {
         if (m_asyncSocketUserTokenList[i] != null && m_asyncSocketUserTokenList[i].TokenID == userToken.TokenID)
         {
             m_asyncSocketUserTokenList.Remove(userToken);
             m_asyncSocketUserTokenPool.Push(userToken);
             break;
         }
     }
 }
Example #34
0
        public StateObject(AsyncSocketServer svr, AsyncSocketUserToken asyncSocketUserToken)
            : base(svr, asyncSocketUserToken)
        {
            server = svr;

            this.asyncSocketTocken = asyncSocketUserToken;
            this.clientSocket      = asyncSocketUserToken.ConnectSocket;

            IPEndPoint clientipe = (IPEndPoint)clientSocket.RemoteEndPoint;

            ClientIPAddress = clientipe.Address.ToString();
        }
Example #35
0
 public UploadSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
     : base(asyncSocketServer, asyncSocketUserToken)
 {
     m_socketFlag = "Upload";
     m_fileName   = "";
     m_fileStream = null;
     lock (m_asyncSocketServer.UploadSocketProtocolMgr)
     {
         m_asyncSocketServer.UploadSocketProtocolMgr.Add(this);
     }
     fileDirection = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["FileDirectory"].Value;
 }
Example #36
0
        protected OutgoingDataAssembler m_outgoingDataAssembler; //协议组装器,用来组织服务端返回的命令

        public AsyncSocketInvokeElement(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
        {
            m_asyncSocketServer  = asyncSocketServer;
            AsyncSocketUserToken = asyncSocketUserToken;
            NetByteOrder         = false;

            m_incomingDataParser    = new IncomingDataParser();
            m_outgoingDataAssembler = new OutgoingDataAssembler();
            m_sendAsync             = false;

            ConnectDT = DateTime.UtcNow;
            ActiveDT  = DateTime.UtcNow;
        }
 public DownloadSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
     : base(asyncSocketServer, asyncSocketUserToken)
 {
     m_fileName   = "";
     m_socketFlag = "Download";
     m_fileStream = null;
     m_sendFile   = false;
     m_packetSize = 64 * 124;
     lock (m_asyncSocketServer.DownloadSocketProtocolMgr)
     {
         m_asyncSocketServer.DownloadSocketProtocolMgr.Add(this);
     }
 }
 /// <summary>
 /// 初始化好多好多的userToken(最基本的读写对象)
 /// </summary>
 public void Init()
 {
     AsyncSocketUserToken userToken;
     for (int i = 0; i < m_numConnections; i++) //按照连接数建立读写对象
     {
         userToken = new AsyncSocketUserToken(m_receiveBufferSize);//初始化userToken
         userToken.id = i;//为每一个连接标号
         userToken.ReceiveEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
         userToken.SendEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
         m_asyncSocketUserTokenPool.Push(userToken);//将userToken添加到UserTokenPool中
     }
 }
 /// <summary>
 /// 绑数据协议
 /// </summary>
 /// <param name="userToken"></param>
 private void BuildingSocketInvokeElement(AsyncSocketUserToken userToken)
 {
     byte flag = userToken.ReceiveEventArgs.Buffer[userToken.ReceiveEventArgs.Offset];
     if (flag == (byte)ProtocolFlag.Upload)
         userToken.AsyncSocketInvokeElement = new UploadSocketProtocol(this, userToken);
     else if (flag == (byte)ProtocolFlag.Control)
         userToken.AsyncSocketInvokeElement = new ControlSocketProtocol(this, userToken);
     else if (flag == (byte)ProtocolFlag.LogOutput)
         userToken.AsyncSocketInvokeElement = new LogOutputSocketProtocol(this, userToken);
     if (userToken.AsyncSocketInvokeElement != null)
     {
         Program.Logger.InfoFormat("Building socket invoke element {0}.Local Address: {1}, Remote Address: {2}",
             userToken.AsyncSocketInvokeElement, userToken.ConnectSocket.LocalEndPoint, userToken.ConnectSocket.RemoteEndPoint);
     }
 }
 public ControlSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
     : base(asyncSocketServer, asyncSocketUserToken)
 {
     m_socketFlag = "Control";
 }