Esempio n. 1
0
            public new byte[] Serialize()
            {
                List <byte> ary  = new List <byte>();
                HEADER      head = new HEADER();

                head.msgSOF  = msgSOF;
                head.msgID   = msgID;
                head.msgSize = msgSize;
                head.msgType = msgType;
                head.msgErr  = msgErr;
                head.msgUser = msgUser;
                head.msgTime = msgTime;
                head.ext1    = ext1;
                head.ext2    = ext2;

                ary.AddRange(head.Serialize());

                ary.AddRange(BitConverter.GetBytes(msgCount));

                for (int i = 0; i < msgCount; ++i)
                {
                    int    msgSize = Marshal.SizeOf(typeof(ChatMsg));
                    byte[] src     = new byte[msgSize];
                    var    gch     = GCHandle.Alloc(src, GCHandleType.Pinned);
                    var    pBuffer = gch.AddrOfPinnedObject();
                    Marshal.StructureToPtr(msgs[i], pBuffer, false);
                    gch.Free();
                    ary.AddRange(src);
                }

                return(ary.ToArray());
            }
Esempio n. 2
0
            public override byte[] Serialize()
            {
                List <byte> ary = new List <byte>();

                byte[]          bodyBuf = null;
                BinaryFormatter bf      = new BinaryFormatter();

                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, workHistory);
                    bodyBuf = ms.ToArray();
                }

                HEADER head = Clone();

                head.msgSize = HeaderSize() + bodyBuf.Length;
                ary.AddRange(head.Serialize());
                ary.AddRange(bodyBuf);

                return(ary.ToArray());
            }