Exemplo n.º 1
0
        /// <summary>
        /// 接收数据事件
        /// </summary>
        /// <param name="ar"></param>
        private void OnRecvData(IAsyncResult ar)
        {
            int recv = 0;

            try
            {
                recv = m_tcpclient.GetStream().EndRead(ar);
            }
            catch (Exception e)
            {
                Logger.Trace(e);
                recv = 0;
            }

            if (recv <= 0)
            {
                // connection has been closed
                Logger.Trace("OnClose");
                OnClose(ar);
                return;
            }

            if (m_rdpclient == null)
            {
                Logger.Trace("OnClose");
                OnClose(null);
                return;
            }

            try
            {
                NetworkStream stream = m_rdpclient.GetStream();
                if (stream == null)
                {
                    Logger.Trace("OnClose");
                    OnClose(null);
                    return;
                }
                stream.Write(m_buffer, 0, recv);

                m_qos.DataIn(recv);
            }
            catch (Exception e)
            {
                Logger.Trace(e);
            }

            // continue listening for tcp datagram packets
            try
            {
                m_tcpclient.GetStream().BeginRead(m_buffer, 0, m_buffer.Length, OnRecvData, this);
            }
            catch (Exception e)
            {
                Logger.Trace(e);
                OnClose(ar);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 接收数据事件
        /// </summary>
        /// <param name="ar"></param>
        private void OnRecvData(IAsyncResult ar)
        {
            int recv = 0;

            try
            {
                recv = m_tcpclient.GetStream().EndRead(ar);
            }
            catch (Exception e)
            {
                Logger.Trace(e);
                recv = 0;
            }

            if (recv <= 0)
            {
                // connection has been closed
                Logger.Trace("OnClose");
                OnClose(ar);
                return;
            }

            if (m_start == 0)
            {
                int    cmd  = 0;
                byte[] data = m_protocol.ParseCmd(m_buffer, recv, ref cmd);
                if (cmd != (int)Protocol.CMD.C2C_RDPSTART || data == null || data.Length == 0)
                {
                    Logger.Trace("OnClose");
                    OnClose(ar);
                    return;
                }
                string sdata = System.Text.UTF8Encoding.UTF8.GetString(data);
                if (string.IsNullOrEmpty(sdata))
                {
                    Logger.Trace("OnClose");
                    OnClose(ar);
                    return;
                }
                string[] rdata = sdata.Split(new char[] { '|' }, 3);
                if (rdata == null || rdata.Length != 3)
                {
                    Logger.Trace("OnClose");
                    OnClose(ar);
                    return;
                }
                int.TryParse(rdata[0], out m_rdpviewtype);
                if (m_pwdcode != rdata[2])
                {
                    Logger.Trace("OnClose");
                    data = m_protocol.C2C_RdpStartRes("PWDERROR");
                    WriteData(data);
                    OnClose(ar);
                    return;
                }

                if (!ConnectRdp(m_rdpport))
                {
                    Logger.Trace("OnClose");
                    data = m_protocol.C2C_RdpStartRes("RDPERROR");
                    WriteData(data);
                    OnClose(ar);
                    return;
                }
                m_start = 1;
                data    = m_protocol.C2C_RdpStartRes("OK");
                WriteData(data);
            }
            else
            {
                try
                {
                    NetworkStream stream = m_tcprdp.GetStream();
                    if (stream == null)
                    {
                        Logger.Trace("OnClose");
                        OnClose(null);
                        return;
                    }
                    stream.Write(m_buffer, 0, recv);

                    m_qos.DataIn(recv);
                }
                catch (Exception e)
                {
                    Logger.Trace(e);
                }
            }

            // continue listening for tcp datagram packets
            try
            {
                m_tcpclient.GetStream().BeginRead(m_buffer, 0, m_buffer.Length, OnRecvData, this);
            }
            catch (Exception e)
            {
                Logger.Trace(e);
                OnClose(ar);
            }
        }