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


using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net.Sockets; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace NBLH_LINK.Common 
{ 
    public class SocketPacket 
    { 
        private Packet m_packet; 
 
        public Packet Packet 
        { 
            get { return m_packet; } 
            set { m_packet = value; } 
        } 
        private Socket m_socket; 
 
        public Socket Socket 
        { 
            get { return m_socket; } 
            set { m_socket = value; } 
        } 
    } 
    [StructLayout(LayoutKind.Sequential)] 
    public class Packet 
    { 
        byte m_head; 
 
        public byte Head 
        { 
            get { return m_head; } 
            set { m_head = value; } 
        } 
        int m_count; 
        DestTop_Type m_type; 
 
        public DestTop_Type Type 
        { 
            get { return m_type; } 
            set { m_type = value; } 
        } 
 
        public int Count 
        { 
            get { return m_count; } 
            set { m_count = value; } 
        } 
        int m_order; 
 
        public int Order 
        { 
            get { return m_order; } 
            set { m_order = value; } 
        } 
        byte[] m_content; 
 
        public byte[] Content 
        { 
            get { return m_content; } 
            set { m_content = value; } 
        } 
        int m_length; 
 
        public int Length 
        { 
            get { return m_length; } 
            set { m_length = value; } 
        } 
        byte m_tail; 
 
        public byte Tail 
        { 
            get { return m_tail; } 
            set { m_tail = value; } 
        } 
        private byte m_crc; 
 
        public byte Crc 
        { 
            get { return m_crc; } 
            set { m_crc = value; } 
        } 
    } 
    public class Nblh_Link_Common_Convert 
    { 
        public static byte[] GetBytes<T>(T obj) 
        { 
            string msg = JsonConvert.SerializeObject(obj); 
            return Encoding.UTF8.GetBytes(msg); 
        } 
        public static T GetObject<T>(IList<Packet> packets) 
        { 
            T obj = default(T); 
            MemoryStream stream = new MemoryStream(); 
 
            try 
            { 
                foreach (var item in packets) 
                { 
                    stream.Write(item.Content, 0, item.Length); 
                } 
                 
            } 
            catch(Exception ex) 
            { 
 
            } 
            return obj; 
 
        } 
        public static T GetObject<T>(byte[] bytes, int index, int length) 
        { 
            T obj = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes, index, length)); 
            return obj; 
        } 
        public static byte[] GetBytes(Packet packet) 
        { 
            MemoryStream stream = new MemoryStream(); 
            if (packet.Count <= 0) 
            { 
                return null; 
            } 
            byte[] bHead = BitConverter.GetBytes(packet.Head); 
            byte[] bType = BitConverter.GetBytes((int)packet.Type); 
            byte[] bCount = BitConverter.GetBytes(packet.Count); 
            byte[] bOrder = BitConverter.GetBytes(packet.Order); 
            byte[] bLength = BitConverter.GetBytes(packet.Length); 
            byte[] bTail = BitConverter.GetBytes(packet.Tail); 
            byte[] bCrc = BitConverter.GetBytes(packet.Crc); 
 
            stream.Write(bHead, 0, bHead.Length); 
            stream.Write(bType, 0, bType.Length); 
            stream.Write(bCount, 0, bCount.Length); 
            stream.Write(bOrder, 0, bOrder.Length); 
            stream.Write(bLength, 0, bLength.Length); 
            stream.Write(packet.Content, 0, packet.Length); 
            stream.Write(bTail, 0, bTail.Length); 
            stream.Write(bCrc, 0, bCrc.Length); 
 
            return stream.ToArray(); 
        } 
        /// <summary> 
        /// 得到有效数据 
        /// </summary> 
        /// <param name="packets"></param> 
        /// <returns></returns> 
        public static byte[] GetValid(IList<Packet> packets) 
        { 
            MemoryStream stream = new MemoryStream(); 
            if (packets.Count <= 0) 
            { 
                return null; 
            } 
            int count = packets.First().Count; 
            if (count < packets.Count) 
            { 
                return null; 
            } 
            for (int pos = 0; pos < count; pos++) 
            { 
                stream.Write(packets[pos].Content, 0, packets[pos].Length); 
            } 
            return stream.ToArray(); 
        } 
        public static byte[] GetBytes(IList<Packet> packets) 
        { 
            MemoryStream stream = new MemoryStream(); 
            if(packets.Count<=0) 
            { 
                return null; 
            } 
            int count = packets.First().Count; 
            if(count<packets.Count) 
            { 
                return null; 
            } 
            int offset = 0; 
            for (int pos = 0; pos < count; pos++) 
            { 
                byte[] bHead = BitConverter.GetBytes(packets[pos].Head); 
                byte[] bType = BitConverter.GetBytes((int)packets[pos].Type); 
                byte[] bCount = BitConverter.GetBytes(packets[pos].Count); 
                byte[] bOrder = BitConverter.GetBytes(packets[pos].Order); 
                byte[] bLength = BitConverter.GetBytes(packets[pos].Length); 
                byte[] bTail = BitConverter.GetBytes(packets[pos].Tail); 
                byte[] bCrc = BitConverter.GetBytes(packets[pos].Crc); 
 
                stream.Write(bHead, 0, bHead.Length); 
                stream.Write(bType, 0, bType.Length); 
                stream.Write(bCount, 0, bCount.Length); 
                stream.Write(bOrder, 0, bOrder.Length); 
                stream.Write(bLength, 0, bLength.Length); 
                stream.Write(packets[pos].Content, 0, packets[pos].Length); 
                stream.Write(bTail, 0, bTail.Length); 
                stream.Write(bCrc, 0, bCrc.Length); 
            } 
            return stream.ToArray(); 
        } 
        public static IList<Packet> GetPackets(byte[] bytes, int index, int length) 
        { 
            // 得到长度 
            Packet packet=new Packet(); 
            int lHead = BitConverter.GetBytes(packet.Head).Length; 
            int lType = BitConverter.GetBytes((int)packet.Type).Length; 
            int lCount = BitConverter.GetBytes(packet.Count).Length; 
            int lOrder = BitConverter.GetBytes(packet.Order).Length; 
            int lLength = BitConverter.GetBytes(packet.Length).Length; 
            int lTail = BitConverter.GetBytes(packet.Tail).Length; 
            int lCrc = BitConverter.GetBytes(packet.Crc).Length; 
 
            byte[] bHead = new byte[lHead]; 
            byte[] bType = new byte[lType]; 
            byte[] bCount = new byte[lCount]; 
            byte[] bOrder = new byte[lOrder]; 
            byte[] bLength = new byte[lLength]; 
            byte[]bContent=null; 
            byte[] bTail = new byte[lTail]; 
            byte[] bCrc = new byte[lCrc]; 
             
            IList<Packet> packets = new List<Packet>(); 
            MemoryStream stream = new MemoryStream(bytes, index, length); 
            stream.Seek(0, SeekOrigin.Begin); 
 
            int offset =index; 
            try 
            { 
                while (offset <= length) 
                { 
                    packet = new Packet(); 
                    stream.Read(bHead, 0, lHead); offset += lHead; 
                    stream.Read(bType, 0, lType); offset += lType; 
                    stream.Read(bCount, 0, lCount); offset += lCount; 
                    stream.Read(bOrder, 0, lOrder); offset += lOrder; 
                    stream.Read(bLength, 0, lLength); offset += lLength; 
 
                    packet.Head = bHead[0]; 
                    packet.Type = (DestTop_Type)BitConverter.ToInt32(bType, 0); 
                    packet.Count = BitConverter.ToInt32(bCount, 0); 
                    packet.Order = BitConverter.ToInt32(bOrder, 0); 
                    packet.Length = BitConverter.ToInt32(bLength, 0); 
                    bContent = new byte[packet.Length]; 
 
                    stream.Read(bContent, 0, packet.Length); offset += packet.Length; 
                    stream.Read(bTail, 0, lTail); offset += lTail; 
                    stream.Read(bCrc, 0, lCrc); offset += lCrc; 
 
                    packet.Content = bContent; 
                    packet.Tail = bTail[0]; 
                    packet.Crc = bCrc[0]; 
                    packets.Add(packet); 
                } 
                return packets; 
            } 
            catch (Exception ex) 
            { 
                return new List<Packet>();  
            } 
             
        } 
        public static IList<Packet> GetPackets(DestTop_Type type,byte[] bytes, int index, int length) 
        { 
            IList<Packet> packets = new List<Packet>(); 
            int offset = index; 
            Packet temp = new Packet();//用于计算大小 
            int buffersize = Nblh_Link_Common_Config.BufferSize - Marshal.SizeOf(temp); 
            int count = (length - index + Nblh_Link_Common_Config.BufferSize) / Nblh_Link_Common_Config.BufferSize; 
            int pos = 0; 
            MemoryStream stream = new MemoryStream(bytes, index, length); 
            while (offset < length) 
            { 
                if (length > offset + Nblh_Link_Common_Config.BufferSize - 1) 
                { 
                    Packet packet = new Packet() 
                    { 
                        Count = count, 
                        Head = 0x01, 
                        Tail = 0x02, 
                        Content = new MemoryStream(bytes, offset, Nblh_Link_Common_Config.BufferSize - 1).ToArray(), 
                        Crc = 0x02, 
                        Length = Nblh_Link_Common_Config.BufferSize-1, 
                        Order = pos++, 
                        Type = type 
                    }; 
                    packets.Add(packet); 
                    offset += Nblh_Link_Common_Config.BufferSize - 1; 
                } 
                else 
                { 
                    Packet packet = new Packet() 
                    { 
                        Count = count, 
                        Head = 0x01, 
                        Tail = 0x02, 
                        Content = new MemoryStream(bytes, offset, length - offset).ToArray(), 
                        Crc = 0x02, 
                        Length = length - offset, 
                        Order = pos++, 
                        Type = type 
                    }; 
                    packets.Add(packet); 
                    offset += length - offset; 
                } 
            } 
            return packets; 
        } 
         
    } 
}