Beispiel #1
0
        public MemoryStream SerializeSinaglePackage(UpdateLatestPacakge msg)
        {
            MemoryStream stream = ObjectAllocatorHolder <MemoryStream> .Allocate();

            stream.Seek(0, SeekOrigin.Begin);
            var  binaryWriter = MyBinaryWriter.Allocate(stream);
            long bodyLength;
            var  old = MessagePool.GetPackageBySeq(msg.Head.BaseUserCmdSeq);

            if (old != null)
            {
                msg.Head.Serialize(binaryWriter);
                bodyLength = SerializeComponents(binaryWriter, old.UpdateComponents, msg.UpdateComponents);
            }
            else
            {
                msg.Head.BaseUserCmdSeq = -1;
                msg.Head.Serialize(binaryWriter);
                bodyLength = SerializeComponents(binaryWriter, _emptyUpdateComponents, msg.UpdateComponents);
            }

            AssertUtility.Assert(bodyLength < 65535);
            msg.Head.ReWriteBodyLength(binaryWriter, (short)bodyLength);
            binaryWriter.ReleaseReference();
            return(stream);
        }
Beispiel #2
0
        public VNetPacketMemSteam SerializeSinaglePackage(UpdateLatestPacakge msg)
        {
            VNetPacketMemSteam stream = VNetPacketMemSteam.Allocate();

            stream.Stream.Seek(0, SeekOrigin.Begin);
            var  binaryWriter = MyBinaryWriter.Allocate(stream.Stream);
            long bodyLength;
            var  old   = MessagePool.GetPackageBySeq(msg.Head.LastAckUserCmdSeq);
            int  count = 0;

            if (old != null)
            {
                msg.Head.Serialize(binaryWriter);
                bodyLength = SerializeComponents(binaryWriter, old.UpdateComponents, msg.UpdateComponents, out count);
            }
            else
            {
                msg.Head.LastAckUserCmdSeq = -1;
                msg.Head.Serialize(binaryWriter);
                bodyLength = SerializeComponents(binaryWriter, _emptyUpdateComponents, msg.UpdateComponents, out count);
            }

            AssertUtility.Assert(bodyLength < 65535);

            msg.Head.ReWriteComponentCountAndBodyLength(binaryWriter, (short)bodyLength, (byte)count);
            binaryWriter.ReleaseReference();
            return(stream);
        }
Beispiel #3
0
        public int Serialize(Stream outStream, object message)
        {
            var msg          = message as UpdateLatestPacakge;
            var binaryWriter = MyBinaryWriter.Allocate(outStream);

            binaryWriter.Write(_version);
            if (MessagePool.GetPackageBySeq(msg.Head.LastUserCmdSeq) != null)
            {
                _logger.ErrorFormat("repetition  msg.Head.UserCmdSeq{0}", msg.Head.LastUserCmdSeq);
                binaryWriter.Write((byte)0);
                return(binaryWriter.WriterLenght);
            }

            MessagePool.AddMessage(msg);
            MessagePool.ClearOldMessage(msg.Head.LastAckUserCmdSeq);
            if (_sendHistoryStreams.Count > _sendCount)
            {
                RemoveHistoryFirst();
            }

            for (int i = 0; i < _sendCount; i++)
            {
                if (_sendHistorySeqs.Count > 0 && msg.Head.LastAckUserCmdSeq > 0 &&
                    _sendHistorySeqs.First() <= msg.Head.LastAckUserCmdSeq)
                {
                    RemoveHistoryFirst();
                }
                else
                {
                    break;
                }
            }

            var stream = SerializeSinaglePackage(msg);

            _sendHistoryStreams.AddLast(stream);
            _sendHistorySeqs.AddLast(msg.Head.LastUserCmdSeq);


            binaryWriter.Write((byte)_sendHistoryStreams.Count);

            foreach (var sendHistroyStream in _sendHistoryStreams)
            {
                binaryWriter.Write(sendHistroyStream.Stream.GetBuffer(),
                                   (int)(sendHistroyStream.Stream.Position - sendHistroyStream.Length), (int)sendHistroyStream.Length);
            }

            _logger.DebugFormat("send package{0}", binaryWriter.Position);

            var ret = binaryWriter.WriterLenght;

            binaryWriter.ReleaseReference();
            return(ret);
        }
Beispiel #4
0
        public void Serialize(ISnapshot baseSnap, ISnapshot snap, Stream stream)
        {
//            _binaryWriter = new MyBinaryWriter(stream);
            _binaryWriter = MyBinaryWriter.Allocate(stream);
            Reset();
            snap.Header.Serialize(_binaryWriter, _version);
            var baseMap    = baseSnap.EntityMap;
            var currentMap = snap.EntityMap;
            SnapshotPatchGenerator handler = new SnapshotPatchGenerator(_serializerManager);

            EntityMapComparator.Diff(baseMap, currentMap, handler, "serialize");
            SnapshotPatch patch = handler.Detach();

            patch.BaseSnapshotSeq = baseSnap.SnapshotSeq;
            patch.Serialize(_binaryWriter, _serializerManager);
            _binaryWriter.ReleaseReference();
            patch.ReleaseReference();
        }