Пример #1
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = ar.AsyncState as TcpListener;
                Socket      socket   = listener.EndAcceptSocket(ar);
                listener.BeginAcceptSocket(new AsyncCallback(AcceptCallback), listener);

                RegistryClient registryClient = new RegistryClient(socket);
                registryClient.ReceiveDateTime    = DateTime.Now;
                registryClient.RegistryClientType = RegistryClientType.AnKnown;
                registryClient.ClearBuffer();
                registryClient.Socket.BeginReceive(registryClient.ReceiveBuffer, 0, registryClient.ReceiveBuffer.Length
                                                   , SocketFlags.None, new AsyncCallback(ReceiveCallback), registryClient);

                Console.WriteLine("接收客户端:{0}", registryClient.Socket.RemoteEndPoint.ToString());

                lock (m_RegistryClients)
                {
                    m_RegistryClients.Add(registryClient);
                }
            }
            catch (Exception ex)
            {
                XTrace.WriteLine(ex.ToString());
            }
        }
Пример #2
0
        /// <summary>
        /// 接收请求回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            RegistryClient registryClient = (RegistryClient)ar.AsyncState;

            try
            {
                int dataLength = registryClient.Socket.EndReceive(ar);

                if (dataLength == 0)
                {
                    Console.WriteLine("接收客户端:{0}已经断开连接。", registryClient.ClientIP);
                    lock (m_RegistryClients){
                        m_RegistryClients.Remove(registryClient);
                        registryClient.Dispose();
                    }
                }
                else
                {
                    String data = Encoding.UTF8.GetString(registryClient.ReceiveBuffer, 0, dataLength);

                    Console.WriteLine("接收客户端:{0}发送的数据:{1}。", registryClient.ClientIP, data);

                    //--解析来自客户端的类型
                    CometMessage regMessage = XmlUtil.LoadObjFromXML <CometMessage>(data);
                    registryClient.RegistryClientType = regMessage.ClientType;
                    registryClient.ClientVersion      = regMessage.ClientVersion;

                    registryClient.ClearBuffer();
                    registryClient.Socket.BeginReceive(registryClient.ReceiveBuffer, 0, registryClient.ReceiveBuffer.Length
                                                       , SocketFlags.None, new AsyncCallback(ReceiveCallback), registryClient);

                    //--由消息处理器进行处理
                    m_MessageProcessor.Process(registryClient, regMessage);
                };
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("接收数据时发生异常:" + ex.ToString());
                lock (m_RegistryClients)
                {
                    m_RegistryClients.Remove(registryClient);
                    registryClient.Dispose();
                }
            }
        }