예제 #1
0
        private async Task <string[]> _receiveMsgAsync(bool force = false)
        {
            List <string> responses = new List <string>(3);

            if (IsCanceled && !force)
            {
                return(responses.ToArray());
            }

            byte[] buffer = new byte[512];
            string response = string.Empty;
            string line = string.Empty;
            int    received, index;
            bool   done = false;

            await Task.Run(() =>
            {
                do
                {
                    received = 0;
                    if (!IsCanceled || force)
                    {
                        try { received = ControlStream.Read(buffer, 0, buffer.Length); }
                        catch { }
                    }
                    if (received == 0)
                    {
                        break;
                    }

                    response += _encoding.GetString(buffer, 0, received);

                    if (response.Contains("\n"))
                    {
                        while (response.Length > 0)
                        {
                            index = response.Index("\r");
                            if (index == -1)
                            {
                                break;
                            }

                            line = response.Substring(0, index);

                            if (response.Length > 1)
                            {
                                if (line.Length > 0)
                                {
                                    responses.Add(line);
                                }
                                response = response.Remove(0, line.Length + 2);
                            }
                        }

                        if ((responses.Count > 0) &&
                            (responses[responses.Count - 1].Length > 3) &&
                            (responses[responses.Count - 1][3] == ' '))
                        {
                            done = true;
                        }
                    }
                }while (!done);
            });

            return(responses.ToArray());
        }