Ejemplo n.º 1
0
        /// <summary>
        /// This method is be called when a message receive operation has been completed.
        /// </summary>
        /// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            if (socketError != SocketError.Success || bytesTransferred == 0)
            {
                m_joined = false;
                Error();
            }
            else
            {
                // Read the offset of the fist message in the current packet.
                Debug.Assert(bytesTransferred >= sizeof(ushort));

                ushort offset = m_data.GetUnsignedShort(m_options.Endian, 0);
                m_data.AdvanceOffset(sizeof(ushort));
                bytesTransferred -= sizeof(ushort);

                // Join the stream if needed.
                if (!m_joined)
                {
                    // There is no beginning of the message in current packet.
                    // Ignore the data.
                    if (offset == 0xffff)
                    {
                        BeginReceive();
                        return;
                    }

                    Debug.Assert(offset <= bytesTransferred);
                    Debug.Assert(m_decoder == null);

                    // We have to move data to the beginning of the first message.
                    m_data.AdvanceOffset(offset);
                    bytesTransferred -= offset;

                    // Mark the stream as joined.
                    m_joined = true;

                    // Create and connect decoder for the peer.
                    m_decoder = new V1Decoder(0, m_options.MaxMessageSize, m_options.Endian);
                    m_decoder.SetMsgSink(m_session);
                }

                // Push all the data to the decoder.
                int processed = m_decoder.ProcessBuffer(m_data, bytesTransferred);
                if (processed < bytesTransferred)
                {
                    // Save some state so we can resume the decoding process later.
                    m_pendingBytes = bytesTransferred - processed;
                    m_pendingData  = new ByteArraySegment(m_data, processed);

                    m_state = State.Stuck;
                }
                else
                {
                    m_session.Flush();

                    BeginReceive();
                }
            }
        }
Ejemplo n.º 2
0
        public void UnsignedShortBigEndian()
        {
            ByteArraySegment byteArraySegment = new ByteArraySegment(new byte[2]);

            byteArraySegment.PutUnsignedShort(Endianness.Big, 1, 0);

            Assert.Equal(1, byteArraySegment[1]);
            Assert.Equal(0, byteArraySegment[0]);

            long num = byteArraySegment.GetUnsignedShort(Endianness.Big, 0);

            Assert.Equal(1, num);

            byteArraySegment.PutUnsignedShort(Endianness.Big, 256, 0);

            Assert.Equal(1, byteArraySegment[0]);
            Assert.Equal(0, byteArraySegment[1]);

            num = byteArraySegment.GetUnsignedShort(Endianness.Big, 0);

            Assert.Equal(256, num);
        }
Ejemplo n.º 3
0
        public void UnsignedShortLittleEndian()
        {
            ByteArraySegment byteArraySegment = new ByteArraySegment(new byte[2]);

            byteArraySegment.PutUnsingedShort(Endianness.Little, 1, 0);

            Assert.AreEqual(1, byteArraySegment[0]);
            Assert.AreEqual(0, byteArraySegment[1]);

            long num = byteArraySegment.GetUnsignedShort(Endianness.Little, 0);

            Assert.AreEqual(1, num);

            byteArraySegment.PutUnsingedShort(Endianness.Little, 256, 0);

            Assert.AreEqual(1, byteArraySegment[1]);
            Assert.AreEqual(0, byteArraySegment[0]);

            num = byteArraySegment.GetUnsignedShort(Endianness.Little, 0);

            Assert.AreEqual(256, num);
        }