/// <summary> /// Sends the accumulated payload data to the server. /// </summary> /// <returns>true if the client is done sending data, otherwise /// false.</returns> bool SendPayload() { // IMAP (as well as Pop3 and Smtp) can't deal with binary data and // expect the data as Base64-encoded ASCII string terminated with a // CRLF. string base64 = Convert.ToBase64String(payloadData.ToArray()); payloadData.Clear(); // Send it off to the IMAP server. byte[] data = Encoding.ASCII.GetBytes(base64 + "\r\n"); innerStream.Write(data, 0, data.Length); // If the latest client handshake is of type HandshakeDone, then the // client wont be sending any further handshake messages. return(handshake.MessageId == HandshakeType.HandshakeDone); }
/// <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); }