Ejemplo n.º 1
0
        /// <summary>
        /// 向单个客户端发送数据
        /// </summary>
        /// <param name="rsClient"></param>
        /// <param name="data"></param>
        /// <param name="isAsync">默认为异步调用m</param>
        public void SendData(MonitorClient monitorClient, CometMessageAction action, String data, Boolean isAsync = true)
        {
            try
            {
                CometMessage rm = new CometMessage()
                {
                    Action      = action,
                    MessageBody = data,
                    IsAsync     = isAsync
                };

                String messageData = XmlUtil.SaveXmlFromObj <CometMessage>(rm);
                Console.WriteLine("发送数据:{0}", messageData);

                Byte[] msg = Encoding.UTF8.GetBytes(messageData);
                monitorClient.Socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendCallback), monitorClient);
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("发送数据时发生异常:" + ex.ToString());
                lock (m_MonitorClients)
                {
                    m_MonitorClients.Remove(monitorClient);
                    monitorClient.Dispose();
                }
            }
        }
Ejemplo n.º 2
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();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 发送数据回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void SendCallback(IAsyncResult ar)
        {
            MonitorClient monitorClient = ar.AsyncState as MonitorClient;

            try
            {
                monitorClient.Socket.EndSend(ar);
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("发送数据时发生异常:" + ex.ToString());
                lock (m_MonitorClients)
                {
                    m_MonitorClients.Remove(monitorClient);
                    monitorClient.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 向单个客户端发送数据
        /// </summary>
        /// <param name="rsClient"></param>
        /// <param name="data"></param>
        /// <param name="isAsync">默认为异步调用m</param>
        public void SendData(MonitorClient monitorClient, CometMessageAction action, String data, Boolean isAsync = true)
        {
            try
            {
                CometMessage rm = new CometMessage()
                {
                    Action = action,
                    MessageBody = data,
                    IsAsync = isAsync
                };

                String messageData = XmlUtil.SaveXmlFromObj<CometMessage>(rm);
                Console.WriteLine("发送数据:{0}", messageData);

                Byte[] msg = Encoding.UTF8.GetBytes(messageData);
                monitorClient.Socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendCallback), monitorClient);
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("发送数据时发生异常:" + ex.ToString());
                lock (m_MonitorClients)
                {
                    m_MonitorClients.Remove(monitorClient);
                    monitorClient.Dispose();
                }
            }
        }