Пример #1
0
        public static NetPacket GetPacket(byte[] buffer)
        {
            if (buffer.Length < NetPacketHead.HEAD_SIZE)
            {
                throw (new Exception("数据包长度过短,无法转换"));
            }
            if (buffer.Length < (NetPacketHead.HEAD_SIZE + BitConverter.ToInt32(buffer, 8)))
            {
                throw (new Exception("数据包长度过短,无法转换"));
            }
            Int32 version = BitConverter.ToInt32(buffer, 0);//协议号

            if (version != NetPacketHead.Version)
            {
                throw (new Exception("数据协议版本号不符!"));
            }
            NetPacket     packet     = new NetPacket();
            NetPacketHead packetHead = new NetPacketHead();

            packetHead.PType   = (NetPacketType)BitConverter.ToInt32(buffer, sizeof(Int32));//封包类型
            packetHead.Len     = BitConverter.ToInt32(buffer, sizeof(Int32) + sizeof(Int32));
            packet._packetHead = packetHead;

            Byte[] packetBuffer = new Byte[packetHead.Len];
            Array.Copy(buffer, NetPacketHead.HEAD_SIZE, packetBuffer, 0, packetHead.Len);
            switch (packetHead.PType)
            {
            case NetPacketType.STRING:
                packet.Data = encoding.GetString(packetBuffer);
                break;

            case NetPacketType.BINARY:
                NetFile f           = new NetFile();
                int     filenamelen = BitConverter.ToInt32(packetBuffer, 0);//文件名长度
                f.FileName = encoding.GetString(packetBuffer, sizeof(Int32), filenamelen);
                f.Content  = new Byte[packetBuffer.Length - sizeof(Int32) - filenamelen];
                Array.Copy(packetBuffer, sizeof(Int32) + filenamelen, f.Content, 0, f.Content.Length);
                packet.Data = f;
                break;

            case NetPacketType.COMPLEX:
                NetObject no          = new NetObject();
                int       typenamelen = BitConverter.ToInt32(packetBuffer, 0);//文件名长度
                no.TypeName = NetPacket.encoding.GetString(packetBuffer, sizeof(Int32), typenamelen);

                byte[] Content = new Byte[packetBuffer.Length - sizeof(Int32) - typenamelen];
                Array.Copy(packetBuffer, sizeof(Int32) + typenamelen, Content, 0, Content.Length);

                MemoryStream mStream = new MemoryStream();
                mStream.Write(Content, 0, Content.Length);
                mStream.Position = 0;
                no.gragh         = BinarySerializeHelper.DeSerialize(mStream);
                mStream.Close();
                packet.Data = no;
                break;
            }
            return(packet);
        }
Пример #2
0
        private static void LoadLikeShortcut()
        {
            if (STATIC_LikeShortcuts.Count == 0)
            {
                string path = Path.Combine(Application.StartupPath, STATIC_Shortcuts_Folder, STATIC_LikeShortcuts_FileName);
                if (File.Exists(path))
                {
                    using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        ISerializeHelper se = new BinarySerializeHelper();
                        STATIC_LikeShortcuts = se.DeSerialize <List <BoxFile> >(stream);
                    }
                }

                if (STATIC_LikeShortcuts == null || STATIC_LikeShortcuts.Count == 0)
                {
                    STATIC_LikeShortcuts = new List <BoxFile>();
                }
            }
        }
Пример #3
0
        private static void LoadShortcut()
        {
            if (STATIC_Shortcuts.Count == 0)
            {
                string path = Path.Combine(Application.StartupPath, STATIC_Shortcuts_Folder, STATIC_Shortcuts_FileName);
                if (File.Exists(path))
                {
                    using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        ISerializeHelper se = new BinarySerializeHelper();
                        STATIC_Shortcuts = se.DeSerialize <Dictionary <BoxGroup, List <BoxFile> > >(stream);
                    }
                }

                if (STATIC_Shortcuts == null || STATIC_Shortcuts.Count == 0)
                {
                    STATIC_Shortcuts = new Dictionary <BoxGroup, List <BoxFile> >();
                    STATIC_Shortcuts.Add(new BoxGroup()
                    {
                        Key = getUniqueKey(STATIC_GroupName), Name = STATIC_GroupName
                    }, null);
                }
            }
        }