public void WhenIacDoSgaByteStreamShouldReturnEmptyAndReplyIacWill()
#endif
        {
            ISocket socket = A.Fake <ISocket>();

            using (INetworkStream networkStream = A.Fake <INetworkStream>())
            {
                A.CallTo(() => socket.GetStream()).Returns(networkStream);
                A.CallTo(() => socket.Connected).Returns(true);
                bool isFirst = true;
                A.CallTo(() => socket.Available).ReturnsLazily(() =>
                {
                    if (isFirst)
                    {
                        isFirst = false;
                        return(1);
                    }
                    return(0);
                });
                TcpByteStream tcpByteStream = new TcpByteStream(socket);
                A.CallTo(() => networkStream.ReadByte()).ReturnsNextFromSequence(new int[] { (int)Commands.InterpretAsCommand, (int)Commands.Do, (int)Options.SuppressGoAhead });
                tcpByteStream.Connected.Should().BeTrue();
                ByteStreamHandler sut = new ByteStreamHandler(tcpByteStream, new CancellationTokenSource());

#if ASYNC
                string response = await sut.ReadAsync(TimeSpan.FromMilliseconds(10));
#else
                string response = sut.Read(TimeSpan.FromMilliseconds(10));
#endif
                response.Should().BeEmpty();
                A.CallTo(() => networkStream.WriteByte((byte)Commands.InterpretAsCommand)).MustHaveHappened();
                A.CallTo(() => networkStream.WriteByte((byte)Commands.Will)).MustHaveHappened();
            }
        }
        //------------------------------------------------------------------------------
        //
        // Method: SetupAndReadMessage
        //
        //------------------------------------------------------------------------------
        /// <summary>
        /// Sets up variables and objects, and calls routines to read a message.
        /// </summary>
        /// <param name="parseState">The current state of parsing the message.</param>
        /// <param name="messageSequenceNumber">Populated with the sequence number of the received message.</param>
        /// <returns>The bytes of the message that were read.</returns>
        /// <remarks>In the usual case this routine will return the entire message.  It will only return a partial message if a cancel request occurs, or a pending connection is detected during reading.</remarks>
        private Queue <byte> SetupAndReadMessage(ref MessageParseState parseState, ref int messageSequenceNumber)
        {
            byte[] messageSequenceNumberBytes           = new byte[4];     // Holds the bytes of the message sequence number
            int    messageSequenceNumberCurrentPosition = 0;               // The current read position within the message sequence number

            byte[]         messageSizeHeaderBytes           = new byte[8]; // Holds the bytes of the message size header
            int            messageSizeHeaderCurrentPosition = 0;           // The current read position within the message size header
            long           messageSize   = -1;                             // The size of the message body
            Queue <byte>   messageBytes  = new Queue <byte>();             // Holds the bytes of the message body
            INetworkStream networkStream = client.GetStream();

            // Continue to read until a complete message has been received, unless a cancel request has been received or there is a pending connection (i.e. TcpRemoteSender has reconnected due to an error)
            while ((cancelRequest == false) && (listener.Pending() == false) && (parseState != MessageParseState.ReadCompleteMessage))
            {
                ReadAndParseMessageData(networkStream, ref parseState, messageSequenceNumberBytes, ref messageSequenceNumberCurrentPosition, ref messageSequenceNumber, messageSizeHeaderBytes, ref messageSizeHeaderCurrentPosition, ref messageSize, messageBytes);
            }

            // If a complete message has been received, send back the acknowledgement byte
            if ((cancelRequest == false) && (parseState == MessageParseState.ReadCompleteMessage))
            {
                networkStream.WriteByte(messageAcknowledgementByte);
            }

            return(messageBytes);
        }
示例#3
0
        public void GivenAFakedSocketACallToWriteByteShouldBeRelayed()
        {
            byte           writtenByte = new byte();
            ISocket        socket      = A.Fake <ISocket>();
            INetworkStream stream      = A.Fake <INetworkStream>();

            A.CallTo(() => socket.GetStream()).Returns(stream);
            TcpByteStream sut = new TcpByteStream(socket);

            sut.WriteByte(writtenByte);

            A.CallTo(() => stream.WriteByte(writtenByte)).MustHaveHappened();
        }