예제 #1
0
        public static unsafe void SimpleReadingFromStreamTestSerializer()
        {
            var serializer = new DataContractSerializer(typeof(TestMessageBody));
            var memStream  = new MemoryStream();
            var input      = new byte[1024];

            for (var i = 0; i < input.Length; i++)
            {
                input[i] = (byte)i;
            }
            var writer = XmlDictionaryWriter.CreateBinaryWriter(memStream);

            serializer.WriteObject(writer, new TestMessageBody(input));


            var bytearrayList = new List <byte[]>()
            {
                memStream.ToArray()
            };
            var pin = new PinCollection();

            Console.WriteLine("Buffer length Input" + bytearrayList[0].Length);
            var msgStream = new NativeMessageStream(CreateNativeBufferPtr(bytearrayList, pin));

            byte[] arr = new byte[bytearrayList[0].Length];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = (byte)msgStream.ReadByte();
            }
            Assert.IsTrue(arr.SequenceEqual(memStream.ToArray()));
        }
예제 #2
0
        public static unsafe void OutOfArgumentExceptionTest()
        {
            var bytearr1      = Encoding.Unicode.GetBytes("This is first byte array");
            var bytearr2      = Encoding.Unicode.GetBytes("This is second byte array");
            var pin           = new PinCollection();
            var bytearrayList = new List <byte[]>()
            {
                bytearr1, bytearr2
            };
            var nativebuffer = CreateNativeBufferPtr(bytearrayList, pin);

            var msgStream = new NativeMessageStream(nativebuffer);

            Assert.IsTrue(msgStream.Length == (bytearr1.Length + bytearr2.Length));
            var streambytes = new byte[msgStream.Length];

            try
            {
                var bytesread1 = msgStream.Read(streambytes, 0, (int)streambytes.Length + 1);
                Assert.Fail("Exception Not Thrown");
            }
            catch (ArgumentOutOfRangeException)
            {
            }
        }
예제 #3
0
        public static unsafe void ReadingInChunksFromMultipleByteArrayTest()
        {
            var bytearr1      = Encoding.Unicode.GetBytes("This is first byte array");
            var bytearr2      = Encoding.Unicode.GetBytes("This is second byte array");
            var pin           = new PinCollection();
            var bytearrayList = new List <byte[]>()
            {
                bytearr1, bytearr2
            };
            var nativebuffer = CreateNativeBufferPtr(bytearrayList, pin);

            var msgStream = new NativeMessageStream(nativebuffer);

            Assert.IsTrue(msgStream.Length == (bytearr1.Length + bytearr2.Length));
            var streambytes = new byte[msgStream.Length];
            var bytesread1  = msgStream.Read(streambytes, 0, 25);

            Assert.IsTrue(bytesread1 == 25);
            Assert.IsTrue(msgStream.Position == bytesread1);

            var bytesread2 = msgStream.Read(streambytes, bytesread1, (int)msgStream.Length - bytesread1);

            Assert.IsTrue(msgStream.Position == bytesread1 + bytesread2);
            Assert.IsTrue((bytesread1 + bytesread2) == msgStream.Length);
        }
예제 #4
0
        public static unsafe void SimpleReadingFromStreamTest()
        {
            var bytearr       = Encoding.Unicode.GetBytes("Here is some data.");
            var bytearrayList = new List <byte[]>()
            {
                bytearr
            };
            var pin       = new PinCollection();
            var msgStream = new NativeMessageStream(CreateNativeBufferPtr(bytearrayList, pin));

            Assert.IsTrue(msgStream.Length == bytearr.Length);
            var streambytes = new byte[msgStream.Length];
            var bytesread   = msgStream.Read(streambytes, 0, streambytes.Length);

            Assert.IsTrue(bytearr.SequenceEqual(streambytes));
            Assert.IsTrue(bytesread == streambytes.Length);
        }
예제 #5
0
        public static unsafe void ReadFromFullyReadStreamTest()
        {
            var bytearr       = Encoding.Unicode.GetBytes("Here is some data.");
            var bytearrayList = new List <byte[]>()
            {
                bytearr
            };
            var pin       = new PinCollection();
            var msgStream = new NativeMessageStream(CreateNativeBufferPtr(bytearrayList, pin));

            Assert.IsTrue(msgStream.Length == bytearr.Length);
            var streambytes = new byte[msgStream.Length];
            //First Read
            var bytesread = msgStream.Read(streambytes, 0, streambytes.Length);

            Assert.IsTrue(bytesread == streambytes.Length);
            //Second read
            bytesread = msgStream.Read(streambytes, 0, streambytes.Length);
            Assert.IsTrue(bytesread == 0);
        }
예제 #6
0
        public static unsafe void ReadingMoreBytesThanInStreamTest()
        {
            var bytearr1      = Encoding.Unicode.GetBytes("This is first byte array");
            var bytearr2      = Encoding.Unicode.GetBytes("This is second byte array");
            var pin           = new PinCollection();
            var bytearrayList = new List <byte[]>()
            {
                bytearr1, bytearr2
            };
            var nativebuffer = CreateNativeBufferPtr(bytearrayList, pin);
            var msgStream    = new NativeMessageStream(nativebuffer);

            Assert.IsTrue(msgStream.Length == (bytearr1.Length + bytearr2.Length));
            var streambytes = new byte[1024];

            var bytesread1 = msgStream.Read(streambytes, 0, (int)msgStream.Length + 1);

            Assert.IsTrue(bytesread1 > 0);
            Assert.IsTrue(bytesread1 == msgStream.Length);
        }
예제 #7
0
        public static unsafe void SimpleReadingFromStreamTestWithMultipleByteArray()
        {
            var bytearr1 = Encoding.Unicode.GetBytes("This is first byte array");
            var bytearr2 = Encoding.Unicode.GetBytes("This is second byte array");
            var pin      = new PinCollection();

            byte[] headerBytes = { 4, 5 };

            var bytearrayList = new List <byte[]>()
            {
                bytearr1, bytearr2
            };

            var nativebuffer = CreateNativeBufferPtr(bytearrayList, pin);

            var msgStream = new NativeMessageStream(nativebuffer);

            Assert.IsTrue(msgStream.Length == (bytearr1.Length + bytearr2.Length));
            var streambytes = new byte[msgStream.Length];
            var bytesread   = msgStream.Read(streambytes, 0, streambytes.Length);

            Assert.IsTrue(bytesread == streambytes.Length);
        }