예제 #1
0
        private FTPResponse ReadResponse()
        {
            lock (m_cmdsocket)
            {
                FTPResponse response     = new FTPResponse();
                string      responsetext = "";
                int         bytesrecvd   = 0;

                // make sure any command sent has enough time to respond
                Thread.Sleep(750);

                for (; m_cmdsocket.Available > 0;)
                {
                    bytesrecvd    = m_cmdsocket.Receive(m_buffer, m_buffer.Length, 0);
                    responsetext += Encoding.ASCII.GetString(m_buffer, 0, bytesrecvd);
                }

                if (responsetext.Length == 0)
                {
                    response.ID   = 0;
                    response.Text = "";
                    return(response);
                }

                string[] message = responsetext.Replace("\r", "").Split('\n');

                // we may have multiple responses,
                // particularly if retriving small amounts of data like directory listings
                // such as the command sent and transfer complete together
                // a response may also have multiple lines
                for (int m = 0; m < message.Length; m++)
                {
                    try
                    {
                        // is the first line a response?  If so, the first 3 characters
                        // are the response ID number
                        FTPResponse resp = new FTPResponse();
                        if (message[m].Length > 0)
                        {
                            try
                            {
                                resp.ID = int.Parse(message[m].Substring(0, 3));
                            }
                            catch (Exception)
                            {
                                resp.ID = 0;
                            }

                            resp.Text = message[m].Substring(4);

                            if (ResponseReceived != null)
                            {
                                foreach (FTPResponseHandler rh in ResponseReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        rh(this, resp);
                                    }
                                    catch (Exception ex)
                                    {
                                        System.Diagnostics.Debug.WriteLine(string.Format("FTPResponseHandler threw {0}\r\n{1}", ex.GetType().Name, ex.Message));
                                        // if any event handler fails, we ignore it
                                    }
                                }
                            }

                            if (m == 0)
                            {
                                response = resp;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    return(response);
                }

                // return the first response received
                return(response);
            }
        }
예제 #2
0
        private void EndCommandResponse(IAsyncResult state)
        {
            FTPResponse response      = new FTPResponse();
            string      responsetext  = "";
            int         recievedBytes = 0;

            try
            {
                recievedBytes = m_cmdsocket.EndReceive(state);
            }
            catch
            {
                FTPParameters.EndResponseEvent.Set();
                return;
            }

            responsetext += Encoding.ASCII.GetString(m_buffer, 0, recievedBytes);

            if (String.IsNullOrEmpty(responsetext))
            {
                response.ID   = 0;
                response.Text = "";
                m_response    = response;
                FTPParameters.EndResponseEvent.Set();
                return;
            }

            string[] message = responsetext.Replace("\r", "").Split('\n');

            // we may have multiple responses,
            // particularly if retriving small amounts of data like directory listings
            // such as the command sent and transfer complete together
            // a response may also have multiple lines
            for (int m = 0; m < message.Length; m++)
            {
                try
                {
                    // is the first line a response?  If so, the first 3 characters
                    // are the response ID number
                    FTPResponse resp = new FTPResponse();

                    if (message[m].Length > 0)
                    {
                        try
                        {
                            resp.ID = (StatusCode)int.Parse(message[m].Substring(0, 3));
                        }
                        catch (Exception)
                        {
                            resp.ID = 0;
                        }

                        resp.Text = message[m].Substring(4);

                        if (ResponseReceived != null)
                        {
                            foreach (FTPResponseHandler rh in ResponseReceived.GetInvocationList())
                            {
                                try
                                {
                                    rh(this, resp);
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(string.Format("FTPResponseHandler threw {0}\r\n{1}", ex.GetType().Name, ex.Message));
                                    // if any event handler fails, we ignore it
                                }
                            }
                        }

                        if (m == 0)
                        {
                            response = resp;
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }

                m_response = response;
                FTPParameters.EndResponseEvent.Set();
                return;
            }

            // return the first response received
            m_response = response;
            FTPParameters.EndResponseEvent.Set();
            return;
        }