示例#1
0
        public void TestFloat64()
        {
            //using (var stream = new MemoryStream())
            {
                double d;
                Buffer stream = new Buffer();
                Serializer serializer = new Serializer(stream);
                Deserializer deserializer = new Deserializer(stream);

                // Boundary value tests

                serializer.Write(0.0);
                serializer.Write(Double.Epsilon);
                serializer.Write(Double.MinValue);
                serializer.Write(Double.MaxValue);
                serializer.Write(Double.NegativeInfinity);
                serializer.Write(Double.PositiveInfinity);
                serializer.Write(Double.NaN);

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                deserializer.Read(out d);
                Assert.AreEqual(0.0, d);
                deserializer.Read(out d);
                Assert.AreEqual(Double.Epsilon, d);
                deserializer.Read(out d);
                Assert.AreEqual(Double.MinValue, d);
                deserializer.Read(out d);
                Assert.AreEqual(Double.MaxValue, d);
                deserializer.Read(out d);
                Assert.AreEqual(Double.NegativeInfinity, d);
                deserializer.Read(out d);
                Assert.AreEqual(Double.PositiveInfinity, d);
                deserializer.Read(out d);
                Assert.AreEqual(Double.NaN, d);

                stream.Trim();
                //stream.SetLength(0);

                // Intermediate value tests

                serializer.Write(0.001234);
                serializer.Write(8765.4321);

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                deserializer.Read(out d);
                Assert.AreEqual(0.001234, d);
                deserializer.Read(out d);
                Assert.AreEqual(8765.4321, d);
            }
        }
示例#2
0
        public void TestFloat32()
        {
            //using (var stream = new MemoryStream())
            {
                float f;
                var stream = new x2.Buffer();
                Serializer serializer = new Serializer(stream);
                Deserializer deserializer = new Deserializer(stream);

                // Boundary value tests

                serializer.Write(0.0F);
                serializer.Write(Single.Epsilon);
                serializer.Write(Single.MinValue);
                serializer.Write(Single.MaxValue);
                serializer.Write(Single.NegativeInfinity);
                serializer.Write(Single.PositiveInfinity);
                serializer.Write(Single.NaN);

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                deserializer.Read(out f);
                Assert.AreEqual(0.0F, f);
                deserializer.Read(out f);
                Assert.AreEqual(Single.Epsilon, f);
                deserializer.Read(out f);
                Assert.AreEqual(Single.MinValue, f);
                deserializer.Read(out f);
                Assert.AreEqual(Single.MaxValue, f);
                deserializer.Read(out f);
                Assert.AreEqual(Single.NegativeInfinity, f);
                deserializer.Read(out f);
                Assert.AreEqual(Single.PositiveInfinity, f);
                deserializer.Read(out f);
                Assert.AreEqual(Single.NaN, f);

                stream.Trim();
                //stream.SetLength(0);

                // Intermediate value tests

                serializer.Write(0.001234F);
                serializer.Write(8765.4321F);

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                deserializer.Read(out f);
                Assert.AreEqual(0.001234F, f);
                deserializer.Read(out f);
                Assert.AreEqual(8765.4321F, f);
            }
        }
示例#3
0
 public override void Serialize(Serializer serializer)
 {
     base.Serialize(serializer);
     var touched = new Capo<bool>(fingerprint, tag.Offset);
     if (touched[0])
     {
         serializer.Write(foo_);
     }
     if (touched[1])
     {
         serializer.Write(bar_);
     }
 }
示例#4
0
 public override void Serialize(Serializer serializer,
     Type targetType, ref bool flag)
 {
     base.Serialize(serializer, targetType, ref flag);
     if (!flag) { return; }
     var touched = new Capo<bool>(fingerprint, tag.Offset);
     if (touched[0])
     {
         serializer.Write(result_);
     }
     if (targetType != null && targetType == typeof(HelloResp))
     {
         flag = false;
     }
 }
示例#5
0
        public void TestVariableLengthInt64()
        {
            //using (var stream = new MemoryStream())
            {
                long l, bytes;
                Buffer stream = new Buffer();
                Serializer serializer = new Serializer(stream);
                Deserializer deserializer = new Deserializer(stream);

                // Boundary value tests

                serializer.Write(0L);
                serializer.Write(-1L);
                serializer.Write(Int64.MaxValue);
                serializer.Write(Int64.MinValue);

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(1, bytes);
                Assert.AreEqual(0L, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(1, bytes);
                Assert.AreEqual(-1L, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(10, bytes);
                Assert.AreEqual(Int64.MaxValue, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(10, bytes);
                Assert.AreEqual(Int64.MinValue, l);

                stream.Trim();
                //stream.SetLength(0);

                // Intermediate value tests

                serializer.Write(0x00003f80L >> 1);  // 2
                serializer.Write(0x001fc000L >> 1);  // 3
                serializer.Write(0x0fe00000L >> 1);  // 4
                serializer.Write(0x00000007f0000000L >> 1);  // 5
                serializer.Write(0x000003f800000000L >> 1);  // 6
                serializer.Write(0x0001fc0000000000L >> 1);  // 7
                serializer.Write(0x00fe000000000000L >> 1);  // 8
                serializer.Write(0x7f00000000000000L >> 1);  // 9

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(2, bytes);
                Assert.AreEqual(0x00003f80L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(3, bytes);
                Assert.AreEqual(0x001fc000L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(4, bytes);
                Assert.AreEqual(0x0fe00000L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(5, bytes);
                Assert.AreEqual(0x00000007f0000000L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(6, bytes);
                Assert.AreEqual(0x000003f800000000L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(7, bytes);
                Assert.AreEqual(0x0001fc0000000000L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(8, bytes);
                Assert.AreEqual(0x00fe000000000000L >> 1, l);

                bytes = deserializer.Read(out l);
                Assert.AreEqual(9, bytes);
                Assert.AreEqual(0x7f00000000000000L >> 1, l);
            }
        }
示例#6
0
        public void TestVariableLengthInt32()
        {
            //using (var stream = new MemoryStream())
            {
                int i, bytes;
                Buffer stream = new Buffer();
                Serializer serializer = new Serializer(stream);
                Deserializer deserializer = new Deserializer(stream);

                // Boundary value tests

                serializer.Write(0);
                serializer.Write(-1);
                serializer.Write(Int32.MaxValue);
                serializer.Write(Int32.MinValue);

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(1, bytes);
                Assert.AreEqual(0, i);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(1, bytes);
                Assert.AreEqual(-1, i);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(5, bytes);
                Assert.AreEqual(Int32.MaxValue, i);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(5, bytes);
                Assert.AreEqual(Int32.MinValue, i);

                stream.Trim();
                //stream.SetLength(0);

                // Intermediate value tests

                serializer.Write(0x00003f80 >> 1);  // 2
                serializer.Write(0x001fc000 >> 1);  // 3
                serializer.Write(0x0fe00000 >> 1);  // 4

                stream.Rewind();
                //stream.Seek(0, SeekOrigin.Begin);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(2, bytes);
                Assert.AreEqual(0x00003f80 >> 1, i);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(3, bytes);
                Assert.AreEqual(0x001fc000 >> 1, i);

                bytes = deserializer.Read(out i);
                Assert.AreEqual(4, bytes);
                Assert.AreEqual(0x0fe00000 >> 1, i);
            }
        }
示例#7
0
文件: Cell.cs 项目: jaykang920/x2clr
 /// <summary>
 /// Overridden by subclasses to build a serialization chain.
 /// </summary>
 public virtual void Serialize(Serializer serializer,
     Type targetType, ref bool flag)
 {
     fingerprint.Serialize(serializer);
 }
示例#8
0
文件: Cell.cs 项目: jaykang920/x2clr
 public void Serialize(Serializer serializer)
 {
     bool flag = true;
     Serialize(serializer, null, ref flag);
 }
示例#9
0
 public override void Serialize(Serializer serializer,
     Type targetType, ref bool flag)
 {
     base.Serialize(serializer, targetType, ref flag);
     if (!flag) { return; }
     if (targetType != null && targetType == typeof(HeartbeatEvent))
     {
         flag = false;
     }
 }
示例#10
0
 public override void Serialize(Serializer serializer)
 {
     base.Serialize(serializer);
 }
示例#11
0
        internal void BeginSend()
        {
            lock (syncRoot)
            {
                if (eventsToSend.Count == 0)
                {
                    return;
                }
                // Swap the event buffers.
                if (eventsSending.Count != 0)
                {
                    eventsSending.Clear();
                }
                List<Event> temp = eventsSending;
                eventsSending = eventsToSend;
                eventsToSend = temp;
                temp = null;
            }

            // Capture send buffers.
            txBufferList.Clear();
            lengthToSend = 0;
            int count = eventsSending.Count;
            int bufferCount = buffersSending.Count;
            if (bufferCount < count)
            {
                for (int i = 0, n = count - bufferCount; i < n; ++i)
                {
                    buffersSending.Add(new SendBuffer());
                }
            }
            else
            {
                for (int i = 0, n = bufferCount - count; i < n; ++i)
                {
                    int j = bufferCount - (i + 1);
                    buffersSending[j].Dispose();
                    buffersSending.RemoveAt(j);
                }
            }
            for (int i = 0; i < count; ++i)
            {
                Event e = eventsSending[i];

                var sendBuffer = buffersSending[i];
                sendBuffer.Reset();
                Serializer serializer = new Serializer(sendBuffer.Buffer);
                serializer.Write(e.GetTypeId());
                e.Serialize(serializer);

                bool transformed = false;
                if (BufferTransform != null && txTransformReady && e._Transform)
                {
                    BufferTransform.Transform(sendBuffer.Buffer, (int)sendBuffer.Buffer.Length);
                    transformed = true;
                }

                BuildHeader(sendBuffer, transformed);

                sendBuffer.ListOccupiedSegments(txBufferList);
                lengthToSend += sendBuffer.Length;

                OnEventSent(e);
            }

            Interlocked.Add(ref txCounter, count);

            SendInternal();
        }
示例#12
0
 public override void Serialize(Serializer serializer)
 {
     serializer.Write(GetTypeId());
     base.Serialize(serializer);
 }
示例#13
0
        private void BeginSendTo(Event e)
        {
            int handle = e._Handle;

            EndPoint endPoint;
            using (new ReadLock(rwlock))
            {
                int count = map.Count;

                if (count == 0)
                {
                    Log.Error("{0} no known peers - dropped event {1}", Name, e);
                    goto next;
                }

                if (count == 1 && handle == 0)
                {
                    endPoint = map.Values[0];
                }
                else
                {
                    if (!map.TryGetValue(handle, out endPoint))
                    {
                        Log.Error("{0} unknown handle {1} - dropped event {2}",
                            Name, handle, e);
                        goto next;
                    }
                }
            }

            // Apply the datagram length limit.
            int length = e.GetLength();
            if (length > txBuffer.BlockSize)
            {
                Log.Error("{0} dropped big event {1}", Name, e);
                goto next;
            }

            txBuffer.Reset();
            Serializer serializer = new Serializer(txBuffer);
            serializer.Write(e.GetTypeId());
            e.Serialize(serializer);

            if (BufferTransform != null)
            {
                BufferTransform.Transform(txBuffer, (int)txBuffer.Length);
            }

            try
            {
                SendToInternal(endPoint);

                Diag.IncrementEventsSent();

                Log.Debug("{0} {1} sent event {2}", Name, handle, e);

                return;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception ex)
            {
                Log.Info("{0} send error {1}", Name, ex);
            }

            next:
            OnSendToInternal(0);
        }
示例#14
0
        public void TestPartialSerialization()
        {
            EventFactory.Register<SampleEvent5>();

            var buffer = new x2.Buffer();

            var cell1 = new SampleCell1 {  // base
                Foo = 9,
                Bar = "hello"
            };
            var cell2 = new SampleCell2 {  // derived
                Foo = 9,
                Bar = "hello",
                Baz = true
            };

            var event1 = new SampleEvent5();  // has base

            // base > base > base
            event1.SampleCell = cell1;
            Serializer serializer = new Serializer(buffer);
            serializer.Write(event1.GetTypeId());
            event1.Serialize(serializer);

            long bufferLength = buffer.Length;

            buffer.Rewind();
            Deserializer deserializer = new Deserializer(buffer);

            var retrieved = deserializer.Create();
            retrieved.Deserialize(deserializer);

            var event11 = retrieved as SampleEvent5;
            Assert.NotNull(event11);

            Assert.AreEqual(event1.SampleCell.Foo, event11.SampleCell.Foo);
            Assert.AreEqual(event1.SampleCell.Bar, event11.SampleCell.Bar);

            buffer.Reset();

            // derived > base > base
            event1.SampleCell = cell2;  // base <= derived
            serializer = new Serializer(buffer);
            serializer.Write(event1.GetTypeId());
            event1.Serialize(serializer);

            Assert.AreEqual(bufferLength, buffer.Length);

            {
                var event2 = new SampleEvent6();  // has derived
                event2.SampleCell = cell2;  // derived <= derived
                var buffer2 = new x2.Buffer();
                serializer = new Serializer(buffer2);
                serializer.Write(event2.GetTypeId());
                event2.Serialize(serializer);
                Assert.Greater(buffer2.Length, buffer.Length);
            }

            buffer.Rewind();
            deserializer = new Deserializer(buffer);

            retrieved = deserializer.Create();
            retrieved.Deserialize(deserializer);

            var event12 = retrieved as SampleEvent5;
            Assert.NotNull(event12);

            Assert.AreEqual(event1.SampleCell.Foo, event12.SampleCell.Foo);
            Assert.AreEqual(event1.SampleCell.Bar, event12.SampleCell.Bar);
        }
示例#15
0
 public override void Serialize(Serializer serializer,
     Type targetType, ref bool flag)
 {
     base.Serialize(serializer, targetType, ref flag);
     if (!flag) { return; }
     var touched = new Capo<bool>(fingerprint, tag.Offset);
     if (touched[0])
     {
         serializer.Write(token_);
     }
     if (touched[1])
     {
         serializer.Write(rxCounter_);
     }
     if (touched[2])
     {
         serializer.Write(txCounter_);
     }
     if (touched[3])
     {
         serializer.Write(txBuffered_);
     }
     if (targetType != null && targetType == typeof(SessionReq))
     {
         flag = false;
     }
 }
示例#16
0
文件: Event.cs 项目: jaykang920/x2clr
 /// <summary>
 /// Overridden by subclasses to build a serialization chain.
 /// </summary>
 public override void Serialize(Serializer serializer,
     Type targetType, ref bool flag)
 {
     base.Serialize(serializer, targetType, ref flag);
     var touched = new Capo<bool>(fingerprint, tag.Offset);
     if (touched[1])
     {
         serializer.Write(_waitHandle);
     }
     if (targetType != null && targetType == typeof(Event))
     {
         flag = false;
     }
 }