Пример #1
0
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] Serialize(ExpressCommand data)
        {
            int n = 5 + 4 + data.byteData.Length + 4 + data.intData.Length * 4 + 4 + data.timeData.Length * 6 + 4;

            foreach (string s in data.strData)
            {
                n += System.Text.Encoding.Default.GetByteCount(s) + 1;
            }
            foreach (string s in data.strs)
            {
                n += System.Text.Encoding.Default.GetByteCount(s) + 1;
            }
            byte[] b = new byte[n];
            b[0] = data.Cmd;
            BitConverter.GetBytes(data.Workstation).CopyTo(b, 1);
            // b[1] = data.Workstation;
            b[3] = data.Datatype;
            BitConverter.GetBytes(data.postid).CopyTo(b, 4);
            n = 6;
            BitConverter.GetBytes((int)data.byteData.Length).CopyTo(b, n);
            n += 4;
            data.byteData.CopyTo(b, n);
            n += data.byteData.Length;
            BitConverter.GetBytes((int)data.intData.Length).CopyTo(b, n);
            n += 4;
            foreach (uint u in data.intData)
            {
                BitConverter.GetBytes(u).CopyTo(b, n);
                n += 4;
            }
            BitConverter.GetBytes((int)data.timeData.Length).CopyTo(b, n);
            n += 4;
            foreach (DateTime t in data.timeData)
            {
                Tools.Time2Byte(t).CopyTo(b, n);
                n += 6;
            }
            BitConverter.GetBytes((int)data.strData.Length).CopyTo(b, n);
            n += 4;
            foreach (string s in data.strData)
            {
                Tools.String2Byte(s, b, ref n);
            }
            foreach (string s in data.strs)
            {
                Tools.String2Byte(s, b, ref n);
            }
            return(b);
        }
Пример #2
0
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static ExpressCommand Deserialize(byte[] b)
        {
            if (b.Length < 12)
            {
                return(null);
            }
            int i, n;

            try
            {
                ExpressCommand cmd = new ExpressCommand();

                cmd.Cmd         = b[0];
                cmd.Workstation = BitConverter.ToInt16(b, 1);
                // cmd.Workstation = b[1];
                cmd.Datatype = b[3];
                cmd.postid   = BitConverter.ToInt16(b, 4);
                i            = 6;
                n            = BitConverter.ToInt32(b, i);
                i           += 4;
                if (i + n + 3 > b.Length)
                {
                    return(null);
                }
                cmd.byteData = new byte[n];
                Buffer.BlockCopy(b, i, cmd.byteData, 0, n);
                i += n;
                n  = BitConverter.ToInt32(b, i);
                i += 4;
                if (i + n * 4 + 2 > b.Length)
                {
                    return(null);
                }
                cmd.intData = new uint[n];
                int j;
                for (j = 0; j < n; j++)
                {
                    cmd.intData[j] = BitConverter.ToUInt32(b, i);
                    i += 4;
                }
                n            = BitConverter.ToInt32(b, i);
                i           += 4;
                cmd.timeData = new DateTime[n];
                if (i + n * 6 + 1 > b.Length)
                {
                    return(null);
                }
                for (j = 0; j < n; j++)
                {
                    cmd.timeData[j] = Tools.Byte2Time(b, i);
                    i += 6;
                }
                n           = BitConverter.ToInt32(b, i);
                i          += 4;
                cmd.strData = new string[n];
                if (i + n > b.Length)
                {
                    return(null);
                }
                try
                {
                    for (j = 0; j < n; j++)
                    {
                        cmd.strData[j] = Tools.Byte2String(b, ref i);
                    }
                }
                catch
                {
                    cmd = null;
                }
                try
                {
                    for (j = 0; j < cmd.strs.Length; j++)
                    {
                        try
                        {
                            cmd.strs[j] = Tools.Byte2String(b, ref i);
                        }
                        catch
                        {
                            cmd.strs[j] = "";
                        }
                    }
                }
                catch
                {
                    cmd = null;
                }

                return(cmd);
            }
            catch
            {
                return(null);
            }
        }