Example #1
0
        /// <summary>
        /// 接受客户端连接
        /// </summary>
        /// <param name="ar"></param>
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = ar.AsyncState as TcpListener;
                Socket      socket   = listener.EndAcceptSocket(ar);
                listener.BeginAcceptSocket(new AsyncCallback(AcceptCallback), listener);

                MonitorClient monitorClient = new MonitorClient(socket);
                monitorClient.ReceiveDateTime   = DateTime.Now;
                monitorClient.MonitorClientType = CometClientType.RegistryCenter;
                monitorClient.ClearBuffer();
                monitorClient.Socket.BeginReceive(monitorClient.ReceiveBuffer, 0, monitorClient.ReceiveBuffer.Length
                                                  , SocketFlags.None, new AsyncCallback(ReceiveCallback), monitorClient);

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

                lock (m_MonitorClients)
                {
                    m_MonitorClients.Add(monitorClient);
                }
            }
            catch (Exception ex)
            {
                XTrace.WriteLine(ex.ToString());
            }
        }
Example #2
0
        /// <summary>
        /// 接受客户端连接
        /// </summary>
        /// <param name="ar"></param>
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = ar.AsyncState as TcpListener;
                Socket socket = listener.EndAcceptSocket(ar);
                listener.BeginAcceptSocket(new AsyncCallback(AcceptCallback), listener);

                MonitorClient monitorClient = new MonitorClient(socket);
                monitorClient.ReceiveDateTime = DateTime.Now;
                monitorClient.MonitorClientType = CometClientType.RegistryCenter;
                monitorClient.ClearBuffer();
                monitorClient.Socket.BeginReceive(monitorClient.ReceiveBuffer, 0, monitorClient.ReceiveBuffer.Length
                    , SocketFlags.None, new AsyncCallback(ReceiveCallback), monitorClient);

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

                lock (m_MonitorClients)
                {
                    m_MonitorClients.Add(monitorClient);
                }
            }
            catch (Exception ex)
            {
                XTrace.WriteLine(ex.ToString());
            }
        }
Example #3
0
        /// <summary>
        /// 处理客户端的请求
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            MonitorClient monitorClient = (MonitorClient)ar.AsyncState;

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

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

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

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

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

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