Example #1
0
        /// <summary>
        /// Trys to read the next available message
        /// </summary>
        /// <param name="stream">The stream where the messages is recieving</param>
        /// <returns>Returns the list of FTPResponseMessages</returns>
        private List<FTPResponseMessage> readLines(NetworkStream stream)
        {
            // Initialization
            List<FTPResponseMessage> responseMessages = new List<FTPResponseMessage>();

            char[] seperator = { '\n' };
            char[] toRemove = { '\r' };

            Byte[] buffer = new Byte[this.BlockSize];
            Int32 bytes = new Int32();
            String tempMessage = "";
            String[] messages;

            // while new messages are available on the stream
            while (stream.DataAvailable)
            {
                // read the message
                bytes = stream.Read(buffer, 0, buffer.Length);

                // encodes the recieved message
                tempMessage += this.Encoding.GetString(buffer, 0, bytes);
            }

            // split the message by the speerator
            messages = tempMessage.Split(seperator);

            // for each message in the splitted array of messages
            foreach (String message in messages)
            {
                // if the message is not empty
                if (messages.Length > 0)
                {
                    // Initialize a new ftpResponseMessage
                    FTPResponseMessage responseMessage = new FTPResponseMessage();

                    // remove the heading and trailing whitespaces
                    String ftpMessage = message.Trim(toRemove);

                    // initialize a default responseCode
                    Int32 responseCode = (Int32)FTPServerResponseCode.None;

                    // if the recieved ftpMessage is null or empty
                    if (String.IsNullOrEmpty(ftpMessage))
                    {
                        // parse the next message
                        continue;
                    }

                    // if the message includes a responseCode
                    if (ftpMessage.Length > 2 && Int32.TryParse(ftpMessage.Substring(0, FTP.RESPONSE_CODE_LENGTH), out responseCode))
                    {
                        // delete this response from the full message
                        ftpMessage = message.Substring(FTP.RESPONSE_CODE_LENGTH);
                    }

                    responseMessage.Message = ftpMessage;                              // set the ftpMessage
                    responseMessage.ResponseCode = (FTPServerResponseCode)responseCode;     // set the responseCode

                    // add this ftpmessage to the list of the ftpMessages
                    responseMessages.Add(responseMessage);
                }
            }

            // return the list of ftpMessages
            return responseMessages;
        }
Example #2
0
        /// <summary>
        /// Trys to read the next available message
        /// </summary>
        /// <param name="stream">The stream where the messages is recieving</param>
        /// <returns>Returns the list of FTPResponseMessages</returns>
        private List <FTPResponseMessage> readLines(NetworkStream stream)
        {
            // Initialization
            List <FTPResponseMessage> responseMessages = new List <FTPResponseMessage>();

            char[] seperator = { '\n' };
            char[] toRemove  = { '\r' };

            Byte[] buffer      = new Byte[this.BlockSize];
            Int32  bytes       = new Int32();
            String tempMessage = "";

            String[] messages;


            // while new messages are available on the stream
            while (stream.DataAvailable)
            {
                // read the message
                bytes = stream.Read(buffer, 0, buffer.Length);

                // encodes the recieved message
                tempMessage += this.Encoding.GetString(buffer, 0, bytes);
            }

            // split the message by the speerator
            messages = tempMessage.Split(seperator);

            // for each message in the splitted array of messages
            foreach (String message in messages)
            {
                // if the message is not empty
                if (messages.Length > 0)
                {
                    // Initialize a new ftpResponseMessage
                    FTPResponseMessage responseMessage = new FTPResponseMessage();

                    // remove the heading and trailing whitespaces
                    String ftpMessage = message.Trim(toRemove);

                    // initialize a default responseCode
                    Int32 responseCode = (Int32)FTPServerResponseCode.None;


                    // if the recieved ftpMessage is null or empty
                    if (String.IsNullOrEmpty(ftpMessage))
                    {
                        // parse the next message
                        continue;
                    }

                    // if the message includes a responseCode
                    if (ftpMessage.Length > 2 && Int32.TryParse(ftpMessage.Substring(0, FTP.RESPONSE_CODE_LENGTH), out responseCode))
                    {
                        // delete this response from the full message
                        ftpMessage = message.Substring(FTP.RESPONSE_CODE_LENGTH);
                    }


                    responseMessage.Message      = ftpMessage;                          // set the ftpMessage
                    responseMessage.ResponseCode = (FTPServerResponseCode)responseCode; // set the responseCode

                    // add this ftpmessage to the list of the ftpMessages
                    responseMessages.Add(responseMessage);
                }
            }

            // return the list of ftpMessages
            return(responseMessages);
        }