Beispiel #1
0
        /// <summary>
        /// 直接格式化一个带FormatClassAttibutes 标签的类,返回BYTE[]数组,此数组可以直接发送不需要组合所数据包。所以也称为类抽象数据包
        /// </summary>
        /// <param name="o"></param>
        /// <param name="dataExtra">数据加密回调</param>
        /// <returns></returns>
        public static byte[] FormatFCA(object o, FDataExtraHandle dataExtra)
        {
            Type otype = o.GetType();

            FormatClassAttibutes fca = null;

            if (FormatClassAttibutesDiy.ContainsKey(otype))
            {
                fca = FormatClassAttibutesDiy[otype];
            }
            else
            {
                Attribute[] Attributes = Attribute.GetCustomAttributes(otype);

                foreach (Attribute p in Attributes)
                {
                    fca = p as FormatClassAttibutes;

                    if (fca != null)
                    {
                        FormatClassAttibutesDiy[otype] = fca;
                        break;
                    }
                }
            }

            if (fca != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    BinaryWriter bufflist = new BinaryWriter(stream);


                    if (dataExtra != null)
                    {
                        bufflist.Write(fca.BufferCmdType);
                        byte[] classdata = SerializeObject(o);
                        bufflist.Write(classdata.Length);
                        bufflist.Write(classdata);

                        byte[] fdata = dataExtra(stream.ToArray());

                        stream.Position = 0;
                        stream.SetLength(0);
                        bufflist.Write(0);
                        bufflist.Write(fdata);
                    }
                    else
                    {
                        bufflist.Write(0);
                        bufflist.Write(fca.BufferCmdType);
                        byte[] classdata = SerializeObject(o);
                        bufflist.Write(classdata.Length);
                        bufflist.Write(classdata);
                    }


                    int l = (int)(stream.Length);

                    byte[] data = GetSocketBytes(l);

                    stream.Position = 0;

                    bufflist.Write(data);


                    byte[] pdata = stream.ToArray();
                    stream.Close();
                    stream.Dispose();

                    return(pdata);
                }
            }

            throw new EntryPointNotFoundException("无法找到 FormatClassAttibutes 标签");
        }
Beispiel #2
0
        /// <summary>
        /// 直接格式化一个带FormatClassAttibutes 标签的类,返回BYTE[]数组,此数组可以直接发送不需要组合所数据包。所以也称为类抽象数据包
        /// </summary>
        /// <param name="o"></param>
        /// <param name="dataExtra">数据加密回调</param>
        /// <returns></returns>
        public static new  byte[] FormatFCA(object o, FDataExtraHandle dataExtra)
        {
            Type otype = o.GetType();
            FormatClassAttibutes fca = null;

            if (FormatClassAttibutesDiy.ContainsKey(otype))
            {
                fca = FormatClassAttibutesDiy[otype];
            }
            else
            {
                Attribute[] Attributes = Attribute.GetCustomAttributes(otype);

                foreach (Attribute p in Attributes)
                {
                    fca = p as FormatClassAttibutes;

                    if (fca != null)
                    {
                        FormatClassAttibutesDiy.Add(otype, fca);
                        break;
                    }
                }
            }
            if (fca != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    BinaryWriter bufflist = new BinaryWriter(stream);

                    bufflist.Write(GetBytes(fca.BufferCmdType));

                    byte[] classdata = SerializeObject(o);
                    bufflist.Write(GetBytes(classdata.Length));
                    bufflist.Write(classdata);


                    byte[] fdata = null;

                    if (dataExtra != null)
                    {
                        fdata = dataExtra(stream.ToArray());
                    }
                    else
                    {
                        fdata = stream.ToArray();
                    }

                    stream.Position = 0;
                    stream.SetLength(0);



                    int x = fdata.Length;

                    if ((fdata.Length + 1) < 128)
                    {
                        x += 1;
                    }
                    else if ((fdata.Length + 2) < 16384)
                    {
                        x += 2;
                    }
                    else if ((fdata.Length + 3) < 2097152)
                    {
                        x += 3;
                    }
                    else
                    {
                        x += 4;
                    }

                    byte[] tmp = GetBytes(x);

                    int l = fdata.Length + tmp.Length;

                    byte[] data = GetBytes(l);

                    bufflist.Write((byte)0xFF);
                    bufflist.Write(data);
                    bufflist.Write(fdata);

                    byte[] pdata = stream.ToArray();
                    stream.Close();
                    stream.Dispose();
                    return(pdata);
                }
            }

            throw new EntryPointNotFoundException("无法找到 FormatClassAttibutes 标签");
        }