Esempio n. 1
0
        /// <summary>
        /// Attempts to read a whole message from fromstream
        /// </summary>
        /// <param name="fromstream"></param>
        /// <returns></returns>
        public static void ReadStreamToMessageStream(MemoryBuffer fromstream, out ChatMessage message)
        {
            message = null;

            //check that we have at least 4 bytes in the buffer, it contains the length of the message
            if (fromstream.Length > 4)
            {
                byte[] lenbuf  = new byte[4];
                var    peeklen = fromstream.Peek(lenbuf, 0, lenbuf.Length);
                if (peeklen == 4)
                {
                    //check that we have the whole message in the buffer
                    var messagelength = BitConverter.ToInt32(lenbuf, 0);
                    if (fromstream.Length >= messagelength + 4)
                    {
                        var messagebuffer = new byte[messagelength];

                        //clear the length from the buffer
                        fromstream.Read(lenbuf, 0, lenbuf.Length);

                        //read the message from the buffer
                        fromstream.Read(messagebuffer, 0, messagelength);

                        var messagestream = new MemoryStream(messagebuffer, 0, messagelength);
                        message = DecodeMessage(messagestream);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Attempts to read a whole message from fromstream
        /// </summary>
        /// <param name="fromstream"></param>
        /// <returns></returns>
        public static void ReadStreamToMessageStream(MemoryBuffer fromstream, out ChatMessage message)
        {
            message = null;

            //check that we have at least 4 bytes in the buffer, it contains the length of the message
            if (fromstream.Length > 4)
            {
                byte[] lenbuf = new byte[4];
                var peeklen = fromstream.Peek(lenbuf, 0, lenbuf.Length);
                if (peeklen == 4)
                {
                    //check that we have the whole message in the buffer
                    var messagelength = BitConverter.ToInt32(lenbuf, 0);
                    if (fromstream.Length >= messagelength + 4)
                    {
                        var messagebuffer = new byte[messagelength];

                        //clear the length from the buffer
                        fromstream.Read(lenbuf, 0, lenbuf.Length);

                        //read the message from the buffer
                        fromstream.Read(messagebuffer, 0, messagelength);

                        var messagestream = new MemoryStream(messagebuffer, 0, messagelength);
                        message = DecodeMessage(messagestream);


                    }
                }
            }
        }
Esempio n. 3
0
        public void ReadWritePeekMultiplTest()
        {
            MemoryBuffer target = new MemoryBuffer(251);
            byte[] buffer = new byte[10];
            for (byte i = 0; i < buffer.Length; i++)
                buffer[i] = i;

            for (byte i = 0; i < 50; i++)
            {
                target.Write(buffer, 0, buffer.Length);
                var tempbuf = new byte[10];
                var peekbuf = new byte[10];
                var peekcount = target.Peek(peekbuf, 0, peekbuf.Length);
                CollectionAssert.AreEqual(buffer, peekbuf, "Peek not equal output");


                var readcount = target.Read(tempbuf, 0, tempbuf.Length / 2);
                readcount += target.Read(tempbuf, tempbuf.Length / 2, tempbuf.Length / 2);

                CollectionAssert.AreEqual(peekbuf, tempbuf, "Peek not equal Read");
                CollectionAssert.AreEqual(buffer, tempbuf, "Input not equal output");
                Assert.AreEqual(tempbuf.Length, readcount, "Read count is not write count");
                Assert.AreEqual(peekcount, readcount, "Read count is not Peek count");
            }
        }