Beispiel #1
0
public void DirectConvert()
{
    var a   = 1.1;
    var b   = "some";
    var tax = PacketConvert.GetBytes(a);
    var tay = PacketConvert.GetBytes(a, typeof(double));
    var tbx = PacketConvert.GetBytes(b);
    var tby = PacketConvert.GetBytes(b, typeof(string));

    var rax = PacketConvert.GetValue <double>(tax);
    var ray = PacketConvert.GetValue(tay, typeof(double));
    var rbx = PacketConvert.GetValue <string>(tbx);
    var rby = PacketConvert.GetValue(tby, typeof(string));

    Assert.AreEqual(a, rax);
    Assert.AreEqual(a, ray);
    Assert.AreEqual(b, rbx);
    Assert.AreEqual(b, rby);

    var off = new Random().Next(8, 16);
    var ba  = new byte[128];

    Buffer.BlockCopy(tax, 0, ba, off, tax.Length);
    var bb = new byte[128];

    Buffer.BlockCopy(tbx, 0, bb, off, tbx.Length);

    var sax = PacketConvert.GetValue <double>(ba, off, tax.Length);
    var say = PacketConvert.GetValue(ba, off, tax.Length, typeof(double));
    var sbx = PacketConvert.GetValue <string>(bb, off, tbx.Length);
    var sby = PacketConvert.GetValue(bb, off, tbx.Length, typeof(string));

    Assert.AreEqual(a, sax);
    Assert.AreEqual(a, say);
    Assert.AreEqual(b, sbx);
    Assert.AreEqual(b, sby);
}
Beispiel #2
0
        public void DeserializeOffset()
        {
            var obj = new { id = 1.1, text = "what the" };
            var ta  = PacketConvert.Serialize(obj);
            var buf = new byte[ta.Length + 128];
            var off = new Random().Next(8, 32);

            Buffer.BlockCopy(ta, 0, buf, off, ta.Length);

            var ra = PacketConvert.Deserialize(ta, obj.GetType());
            var rb = PacketConvert.Deserialize(buf, off, ta.Length, obj.GetType());

            void AreEqual(object a, object b)
            {
                var x = a.Cast(obj);
                var y = b.Cast(obj);

                if (x.id == y.id && x.text == y.text)
                {
                    return;
                }
                throw new ApplicationException();
            }

            AreEqual(obj, ra);
            AreEqual(obj, rb);

            var str = "world";
            var tb  = PacketConvert.GetBytes(str);
            var tc  = new byte[tb.Length + 128];

            Buffer.BlockCopy(tb, 0, tc, off, tb.Length);
            var rc = PacketConvert.Deserialize <string>(tc, off, tb.Length);

            Assert.AreEqual(str, rc);
        }