www.pudn.com > sharewindows.rar > Nblh_Link_Control_Client.cs, change:2016-01-07,size:8138b


using NBLH_LINK.Analysis; 
using NBLH_LINK.Args; 
using NBLH_LINK.Common; 
using NBLH_LINK.Task; 
using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
 
namespace NBLH_LINK 
{ 
    public delegate void ControlMessageHandle(object sender, Nblh_Link_Args_Message message); 
    //public delegate void ReceiveClientMessageHandle(object sender, Nblh_Link_Args_Socket socket); 
    public delegate void ReceiveBitmapSizeHandle(object sender, Nblh_Args_BitmapSize args); 
    public delegate void ReceiveBitmapBytesHandle(object sender, Nblh_Args_BitmapBytes args); 
    public delegate void ConnectedHandle(object sender, Nblh_Args_Connected args); 
    public class Nblh_Link_Control_Client 
    { 
        public event ControlMessageHandle MessageEvent; 
        public event ReceiveBitmapSizeHandle BitmapSizeEvent; 
        public event ReceiveBitmapBytesHandle BitmapBytesEvent; 
        public event ConnectedHandle ConnectedEvent; 
        IList<Nblh_Link_Task> m_links = new List<Nblh_Link_Task>(); 
        Nblh_Link_Client m_client = new Nblh_Link_Client(); 
        // 任务状态 
        bool m_status = false; 
 
        Queue<Packet> m_messageQueue = new Queue<Packet>(); 
 
        private int m_circle = 100; 
        /// <summary> 
        /// 执行周期 
        /// </summary> 
        public int Circle 
        { 
            get { return m_circle; } 
            set { m_circle = value; } 
        } 
        public Nblh_Link_Control_Client() 
        { 
            // 添加任务 
            AddTask(400, DestTop_Type.DesktopBitmapSize, 2); 
            AddTask(501, DestTop_Type.DesktopBitmapBytes, -1); 
            // 关联事件 
            m_client.MessageEvent += m_client_MessageEvent; 
            m_client.ReveiveEvent += m_client_ReveiveEvent; 
            m_client.ConnectedEvent += m_client_ConnectedEvent; 
            m_client.DisConnectEvent += m_client_DisConnectEvent; 
        } 
 
        private void m_client_ReveiveEvent(object sender, Nblh_Args_Buffer args) 
        { 
            try 
            { 
                IList<Packet> packets = Nblh_Link_Common_Convert.GetPackets(args.Bytes, args.Index, args.Length); 
                lock (this) 
                { 
                    foreach (var item in packets) 
                    { 
                        m_messageQueue.Enqueue(item); 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
 
            } 
        } 
        ~Nblh_Link_Control_Client() 
        { 
            // 删除事件 
            m_client.MessageEvent -= m_client_MessageEvent; 
            m_client.ReveiveEvent -= m_client_ReveiveEvent; 
            m_client.ConnectedEvent -= m_client_ConnectedEvent; 
            m_client.DisConnectEvent -= m_client_DisConnectEvent; 
        } 
        public void Func_Start(string ip, int port) 
        { 
            m_status = true; 
            m_client.Func_Link_Init(ip, port); 
        } 
 
        void m_client_DisConnectEvent(object sender, Nblh_Args_DisConnect args) 
        { 
            //throw new NotImplementedException(); 
            m_status = false; 
        } 
 
        void m_client_ConnectedEvent(object sender, Nblh_Args_Connected args) 
        { 
            // 添加多线程 
            ThreadPool.QueueUserWorkItem(n => { Run(); }); 
            ThreadPool.QueueUserWorkItem(n => { Analysis(); }); 
        } 
 
 
        void m_client_MessageEvent(object sender, Nblh_Link_Args_Message message) 
        { 
            //throw new NotImplementedException(); 
            if(MessageEvent!=null) 
            { 
                MessageEvent(this, message); 
            } 
        } 
        /// <summary> 
        /// 解析接收到的数据 
        /// </summary> 
        public void Analysis() 
        { 
            while (m_status) 
            { 
                if (m_messageQueue.Count <= 0) 
                { 
                    Thread.Sleep(100); 
                    continue; 
                } 
                lock (this) 
                { 
                    try 
                    { 
                        int count = m_messageQueue.First().Count; 
                        if(count>m_messageQueue.Count) 
                        { 
                            Thread.Sleep(100); 
                            continue; 
                        } 
                        List<Packet> packets = new List<Packet>(); 
                        for(int pos=0;pos<count;pos++) 
                        { 
                            packets.Add(m_messageQueue.Dequeue()); 
                        } 
                        switch(packets.First().Type) 
                        { 
                            case DestTop_Type.DesktopBitmapSize: 
                                ExecuteBitmapSize(packets); 
                                break; 
                            case DestTop_Type.DesktopBitmapBytes: 
                                ExecuteBitmapBytes(packets); 
                                break; 
                        } 
                    } 
                    catch (Exception ex) 
                    { 
                         
                    } 
                } 
                Thread.Sleep(Circle / m_links.Count()); 
            } 
        } 
        public void ExecuteBitmapSize(IList<Packet> packets) 
        { 
            byte[] bytes = Nblh_Link_Common_Convert.GetValid(packets); 
            Size obj = Nblh_Link_Common_Convert.GetObject<Size>(bytes, 0, bytes.Length); 
            if (BitmapSizeEvent!=null) 
            { 
                BitmapSizeEvent(this, new Nblh_Args_BitmapSize(obj)); 
            } 
        } 
        public void ExecuteBitmapBytes(IList<Packet> packets) 
        { 
            try 
            { 
                byte[] bytes = Nblh_Link_Common_Convert.GetValid(packets); 
                if (BitmapBytesEvent != null) 
                { 
                    BitmapBytesEvent(this, new Nblh_Args_BitmapBytes(bytes, 0, bytes.Length)); 
                } 
            } 
            catch(Exception ex) 
            { 
 
            } 
        } 
        /// <summary> 
        /// 用于执行任务 
        /// </summary> 
        public void Run() 
        { 
            byte[] buffer=null; 
            while (m_status) 
            { 
 
                foreach (var item in m_links) 
                { 
                    if (item.LiefCount != -1 && item.LiefCount < item.LiefCycle) 
                    { 
                        continue; 
                    } 
                    if (item.LeftOver <= 0) 
                    { 
                        buffer = Nblh_Link_Common_Convert.GetBytes<Nblh_Link_Order>(new Nblh_Link_Order() { Type = item.OrderType }); 
                        IList<Packet> packets = Nblh_Link_Common_Convert.GetPackets(item.OrderType, buffer, 0, buffer.Length); 
                        foreach (var packet in packets) 
                        { 
                            byte[] bytes = Nblh_Link_Common_Convert.GetBytes(packet); 
                            m_client.Func_Link_SendData(bytes, 0, bytes.Length - 1); 
                        } 
 
                        item.LeftOver = item.Circle; 
                        break; 
                    } 
                    else 
                    { 
                        item.LeftOver -= Circle; 
                    } 
                    if (item.LiefCount != -1) 
                    { 
                        item.LiefCycle++; 
                    } 
 
                } 
                Thread.Sleep(Circle); 
            } 
        } 
        public void Func_Stop() 
        { 
            m_client.Func_Link_Free(); 
            m_status = false; 
        } 
        public void AddTask(int circle, DestTop_Type type, int LiefCount) 
        { 
            m_links.Add(new Nblh_Link_Task() { Circle = circle, LeftOver = 0, OrderType = type, LiefCount = LiefCount ,LiefCycle =0}); 
        } 
    } 
}