コード例 #1
0
ファイル: FilterStream.cs プロジェクト: YSRE/SuperRocket
        /// <summary>
        /// Reads data from this stream and stores it in the specified array.
        /// </summary>
        /// <param name="buffer">A byte array that receives the bytes read from
        /// the stream.</param>
        /// <param name="offset">The zero-based index into the buffer at which to
        /// begin storing the data read from this stream.</param>
        /// <param name="count">The the maximum number of bytes to read from the
        /// stream.</param>
        /// <returns>The the number of bytes read from the underlying stream. When
        /// there is no more data to be read, returns 0.</returns>
        /// <exception cref="IOException">The read operation failed.</exception>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (state == FilterStreamState.WaitingForServerResponse)
            {
                // We need to receive the server response, before we can satisfy
                // any reads.
                ReadServerResponse();
                state = FilterStreamState.SatisfyRead;
            }

            if (state != FilterStreamState.SatisfyRead)
            {
                throw new IOException("Unexpected Read call.");
            }
            // This really shouldn't happen but who knows.
            if (count > (receivedData.Length - receivedConsumed))
            {
                throw new IOException("Read passed the end of received data.");
            }
            Array.Copy(receivedData, receivedConsumed, buffer, offset, count);
            receivedConsumed += count;
            // Read buffer is empty. We don't expect another call to read for now.
            if (receivedConsumed == receivedData.Length)
            {
                state = FilterStreamState.ReadingHandshake;
            }
            return(count);
        }
コード例 #2
0
ファイル: FilterStream.cs プロジェクト: YSRE/SuperRocket
        /// <summary>
        /// Write the specified number of bytes to the underlying stream using the
        /// specified buffer and offset.
        /// </summary>
        /// <param name="buffer">A byte array that supplies the bytes written to
        /// the stream.</param>
        /// <param name="offset">The zero-based index in the buffer at which to
        /// begin reading bytes to be written to the stream.</param>
        /// <param name="count">The number of bytes to read from buffer.</param>
        /// <exception cref="IOException">The write operation failed.</exception>
        public override void Write(byte[] buffer, int offset, int count)
        {
            switch (state)
            {
            case FilterStreamState.ReadingHandshake:
                if (ReadHandshake(buffer, offset, count))
                {
                    state = FilterStreamState.ReadingPayload;
                }
                break;

            case FilterStreamState.ReadingPayload:
                if (ReadPayload(buffer, offset, count))
                {
                    if (SendPayload())
                    {
                        state = FilterStreamState.SatisfyRead;
                        // Put a fake handshake into the client's receive buffer to
                        // play along with the NegotiateStream protocol.
                        receivedData     = new Handshake(HandshakeType.HandshakeDone, 0).Serialize();
                        receivedConsumed = 0;
                    }
                    else
                    {
                        state = FilterStreamState.WaitingForServerResponse;
                    }
                }
                break;

            default:
                throw new IOException("Unexpected Write call.");
            }
        }
コード例 #3
0
ファイル: FilterStream.cs プロジェクト: YSRE/SuperRocket
        /// <summary>
        /// Reads the client's handshake from the specified buffer.
        /// </summary>
        /// <param name="buffer">A byte array from which the handshake data
        /// will be read.</param>
        /// <param name="offset">The zero-based index in the buffer at which to
        /// begin reading bytes.</param>
        /// <param name="count">The number of bytes to read from buffer.</param>
        /// <returns>True if the handshake has been read completely, otherwise
        /// false.</returns>
        bool ReadHandshake(byte[] buffer, int offset, int count)
        {
            // Accumulate data into buffer until 5 bytes have been read.
            int read = Math.Min(count, 5 - handshakeData.Length);

            handshakeData.Append(buffer, offset, read);
            if (handshakeData.Length == 5)
            {
                // We're now expecting the payload data.
                state = FilterStreamState.ReadingPayload;

                handshake = Handshake.Deserialize(handshakeData.ToArray());
                handshakeData.Clear();
                // Append rest of buffer to payloadData.
                payloadData.Append(buffer, offset + read, count - read);
                return(true);
            }
            // We haven't read 5 bytes yet.
            return(false);
        }
コード例 #4
0
ファイル: FilterStream.cs プロジェクト: abhibecb/S22.Imap
		/// <summary>
		/// Reads data from this stream and stores it in the specified array.
		/// </summary>
		/// <param name="buffer">A byte array that receives the bytes read from
		/// the stream.</param>
		/// <param name="offset">The zero-based index into the buffer at which to
		/// begin storing the data read from this stream.</param>
		/// <param name="count">The the maximum number of bytes to read from the
		/// stream.</param>
		/// <returns>The the number of bytes read from the underlying stream. When
		/// there is no more data to be read, returns 0.</returns>
		/// <exception cref="IOException">The read operation failed.</exception>
		public override int Read(byte[] buffer, int offset, int count) {
			if (state == FilterStreamState.WaitingForServerResponse) {
				// We need to receive the server response, before we can satisfy
				// any reads.
				ReadServerResponse();
				state = FilterStreamState.SatisfyRead;
			}

			if (state != FilterStreamState.SatisfyRead)
				throw new IOException("Unexpected Read call.");
			// This really shouldn't happen but who knows.
			if (count > (receivedData.Length - receivedConsumed))
				throw new IOException("Read passed the end of received data.");
			Array.Copy(receivedData, receivedConsumed, buffer, offset, count);
			receivedConsumed += count;
			// Read buffer is empty. We don't expect another call to read for now.
			if (receivedConsumed == receivedData.Length)
					state = FilterStreamState.ReadingHandshake;
			return count;
		}
コード例 #5
0
ファイル: FilterStream.cs プロジェクト: abhibecb/S22.Imap
		/// <summary>
		/// Reads the client's handshake from the specified buffer.
		/// </summary>
		/// <param name="buffer">A byte array from which the handshake data
		/// will be read.</param>
		/// <param name="offset">The zero-based index in the buffer at which to
		/// begin reading bytes.</param>
		/// <param name="count">The number of bytes to read from buffer.</param>
		/// <returns>True if the handshake has been read completely, otherwise
		/// false.</returns>
		bool ReadHandshake(byte[] buffer, int offset, int count) {
			// Accumulate data into buffer until 5 bytes have been read.
			int read = Math.Min(count, 5 - handshakeData.Length);
			handshakeData.Append(buffer, offset, read);
			if (handshakeData.Length == 5) {
				// We're now expecting the payload data.
				state = FilterStreamState.ReadingPayload;

				handshake = Handshake.Deserialize(handshakeData.ToArray());
				handshakeData.Clear();
				// Append rest of buffer to payloadData.
				payloadData.Append(buffer, offset + read, count - read);
				return true;
			}
			// We haven't read 5 bytes yet.
			return false;
		}
コード例 #6
0
ファイル: FilterStream.cs プロジェクト: abhibecb/S22.Imap
		/// <summary>
		/// Write the specified number of bytes to the underlying stream using the
		/// specified buffer and offset.
		/// </summary>
		/// <param name="buffer">A byte array that supplies the bytes written to
		/// the stream.</param>
		/// <param name="offset">The zero-based index in the buffer at which to
		/// begin reading bytes to be written to the stream.</param>
		/// <param name="count">The number of bytes to read from buffer.</param>
		/// <exception cref="IOException">The write operation failed.</exception>
		public override void Write(byte[] buffer, int offset, int count) {
			switch (state) {
				case FilterStreamState.ReadingHandshake:
					if (ReadHandshake(buffer, offset, count))
						state = FilterStreamState.ReadingPayload;
					break;
				case FilterStreamState.ReadingPayload:
					if (ReadPayload(buffer, offset, count)) {
						if (SendPayload()) {
							state = FilterStreamState.SatisfyRead;
							// Put a fake handshake into the client's receive buffer to
							// play along with the NegotiateStream protocol.
							receivedData = new Handshake(HandshakeType.HandshakeDone, 0).Serialize();
							receivedConsumed = 0;
						} else {
							state = FilterStreamState.WaitingForServerResponse;
						}
					}
					break;
				default:
					throw new IOException("Unexpected Write call.");
			}
		}
コード例 #7
0
ファイル: FilterStream.cs プロジェクト: BoyarinO/EmailClient
        private bool ReadHandshake(byte[] buffer, int offset, int count)
        {
            int read = Math.Min(count, 5 - handshakeData.Length);
            this.handshakeData.Append(buffer, offset, read);
            if (this.handshakeData.Length == 5)
            {
                this.state = FilterStreamState.ReadingPayload;

                this.handshake = Handshake.Deserialize(this.handshakeData.ToArray());
                this.handshakeData.Clear();

                this.payloadData.Append(buffer, offset + read, count - read);

                return true;
            }

            return false;
        }
コード例 #8
0
ファイル: FilterStream.cs プロジェクト: BoyarinO/EmailClient
        public override void Write(byte[] buffer, int offset, int count)
        {
            switch (this.state)
            {
                case FilterStreamState.ReadingHandshake:
                    if (this.ReadHandshake(buffer, offset, count))
                    {
                        this.state = FilterStreamState.ReadingPayload;
                    }

                    break;
                case FilterStreamState.ReadingPayload:
                    if (this.ReadPayload(buffer, offset, count))
                    {
                        if (this.SendPayload())
                        {
                            this.state = FilterStreamState.SatisfyRead;
                            this.receivedData = new Handshake(HandshakeType.HandshakeDone, 0).Serialize();
                            this.receivedConsumed = 0;
                        }
                        else
                        {
                            this.state = FilterStreamState.WaitingForServerResponse;
                        }
                    }

                    break;
                default:
                    throw new IOException("Unexpected Write call.");
            }
        }
コード例 #9
0
ファイル: FilterStream.cs プロジェクト: BoyarinO/EmailClient
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this.state == FilterStreamState.WaitingForServerResponse)
            {
                this.ReadServerResponse();
                this.state = FilterStreamState.SatisfyRead;
            }

            if (this.state != FilterStreamState.SatisfyRead)
            {
                throw new IOException("Unexpected Read call.");
            }

            if (count > (this.receivedData.Length - this.receivedConsumed))
            {
                throw new IOException("Read passed the end of received data.");
            }

            Array.Copy(this.receivedData, this.receivedConsumed, buffer, offset, count);
            this.receivedConsumed += count;

            if (this.receivedConsumed == this.receivedData.Length)
            {
                this.state = FilterStreamState.ReadingHandshake;
            }

            return count;
        }