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


using NBLH_LINK.Analysis; 
using NBLH_LINK.Args; 
using NBLH_LINK.Common; 
using NBLH_LINK.Task; 
using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net.Sockets; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
 
namespace NBLH_LINK 
{ 
    public delegate void ServiceControlMessageHandle(object sender, Nblh_Link_Args_Message args); 
    public class Nblh_Link_Control_Service 
    { 
        public event ServiceControlMessageHandle ServiceControlMessageEvent; 
        Nblh_Link_DeskTop m_control = new Nblh_Link_DeskTop(); 
        Nblh_Link_Service m_service = new Nblh_Link_Service(); 
        // 任务队列 
        Queue<SocketPacket> m_task = new Queue<SocketPacket>(); 
        //IList<Packet> m_packets = new List<Packet>();  
        byte[] m_bytes; 
 
        public byte[] Bytes 
        { 
            get { 
                byte[] bytes; 
                lock(this) 
                { 
                    bytes = m_bytes; 
                } 
                return bytes;  
            } 
            set 
            { 
                lock(this) 
                { 
                    if (value.Length > 0) 
                    { 
 
                        m_bytes = value; 
                    } 
                } 
            } 
        } 
        private int m_circle = 50; 
        bool status = false; 
        public Nblh_Link_Control_Service() 
        {             
            m_service.ReceiveEvent += service_ReceiveEvent; 
            m_service.MessageEvent += m_service_MessageEvent; 
        } 
 
        private void service_ReceiveEvent(object sender, Args.Nblh_Args_Buffer buffer) 
        { 
            IList<Packet> obj = Nblh_Link_Common_Convert.GetPackets(buffer.Bytes, buffer.Index, buffer.Length); 
             
            // 队列处理时处于单件模式下 
            foreach (var item in obj) 
            { 
                this.m_task.Enqueue(new SocketPacket() { Packet = item, Socket = (Socket)sender }); 
            } 
        } 
        List<string> msg = new List<string>(); 
        void m_service_MessageEvent(object sender, Args.Nblh_Link_Args_Message message) 
        { 
            if(ServiceControlMessageEvent!=null) 
            { 
                ServiceControlMessageEvent(this, message); 
            } 
        } 
        ~Nblh_Link_Control_Service() 
        { 
            m_service.ReceiveEvent -= service_ReceiveEvent; 
        } 
        public void Func_Control_Start(int port) 
        { 
            if(status==true) 
            { 
                return; 
            } 
            m_service.Func_Link_Init(port); 
            status = true; 
            ThreadPool.QueueUserWorkItem(q => { Run(); }); 
        } 
 
 
        public void Func_Control_Stop() 
        { 
            status = false; 
            m_service.Func_Link_Free(); 
            // 清空任务队列 
            this.m_task.Clear(); 
        } 
        public void Run() 
        { 
            while (status) 
            { 
                Bytes = m_control.GetDesktopBitmapBytes(); 
                if (m_task.Count <= 0) 
                { 
                    Thread.Sleep(m_circle*2); 
                    continue; 
                } 
                lock (this) 
                { 
                    try 
                    { 
                        SocketPacket socketPacket=m_task.Dequeue(); 
                        switch (socketPacket.Packet.Type) 
                        { 
                            case DestTop_Type.DesktopBitmapSize: 
                                Func_Control_SendMessage(socketPacket.Socket,DestTop_Type.DesktopBitmapSize); 
                                break; 
                            case DestTop_Type.DesktopBitmapBytes: 
                                Func_Control_SendMessage(socketPacket.Socket, DestTop_Type.DesktopBitmapBytes); 
                                break; 
                        } 
                    } 
                    catch (Exception ex) 
                    { 
 
                    } 
                } 
 
                Thread.Sleep(m_circle); 
            } 
        } 
        public void Func_Control_SendMessage(Socket client,DestTop_Type type) 
        { 
            Nblh_Link_Analysis_DeskTop desttop = null; 
            byte[] bytes = null; 
            IList<Packet> packets = null; 
            switch (type) 
            { 
                case DestTop_Type.DesktopBitmapSize: 
                    { 
                        bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(m_control.GetDesktopBitmapSize())); 
                        packets = Nblh_Link_Common_Convert.GetPackets(DestTop_Type.DesktopBitmapSize, bytes, 0, bytes.Length); 
                    } 
                    break; 
                case DestTop_Type.DesktopBitmapBytes: 
                    { 
                        bytes = Bytes;//m_control.GetDesktopBitmapBytes(); 
                        packets = Nblh_Link_Common_Convert.GetPackets(DestTop_Type.DesktopBitmapBytes, bytes, 0, bytes.Length); 
                    } 
                    break; 
            } 
            if (packets != null && packets.Count > 0) 
            { 
                try 
                { 
                    foreach (var item in packets) 
                    { 
                        byte[] buffer = Nblh_Link_Common_Convert.GetBytes(item); 
 
                        m_service.Func_Link_Send(client, buffer, 0, buffer.Length - 1); 
                        //.Func_Link_SendData(buffer, 0, buffer.Length - 1); 
                    } 
                } 
                catch (Exception ex) 
                { 
 
                } 
            } 
        } 
    } 
}