コード例 #1
0
        public bool SendAndReceive(string command, ref IList <string> data, CommandProcessor processor = null,
                                   Encoding encoding = null, bool pushResultToDatadespiteProcessor = false)
        {
#if !NETFX_CORE
            if (_idleState == IdleState.On)
            {
                PauseIdling();
            }
#endif
            lock (_lock)
            {
                if (_client == null || !_client.Connected)
                {
                    throw new SocketException((int)SocketError.NotConnected);
                }

                const string tmpl = "IMAPX{0} {1}";
                _counter++;

                StreamReader reader = encoding == null || Equals(encoding, Encoding.UTF8)
                    ? _streamReader
                    : new StreamReader(_ioStream, encoding);

                var parts = new Queue <string>(NewLineRex.Split(command));

                string text  = string.Format(tmpl, _counter, parts.Dequeue().Trim()) + "\r\n";
                byte[] bytes = Encoding.UTF8.GetBytes(text.ToCharArray());

                if (IsDebug)
                {
                    Debug.WriteLine(text);
                }

                _ioStream.Write(bytes, 0, bytes.Length);

                _lastActivity = DateTime.Now;

                while (true)
                {
                    string tmp = reader.ReadLine();

                    if (tmp == null)
                    {
#if !NETFX_CORE
                        if (_idleState == IdleState.Paused)
                        {
                            StartIdling();
                        }
#endif
                        return(false);
                    }

                    if (IsDebug)
                    {
                        Debug.WriteLine(tmp);
                    }

                    if (processor == null || pushResultToDatadespiteProcessor)
                    {
                        data.Add(tmp);
                    }

                    if (processor != null)
                    {
                        processor.ProcessCommandResult(tmp);
                    }

                    if (tmp.StartsWith("+ ") && (parts.Count > 0 || (processor != null && processor.TwoWayProcessing)))
                    {
                        if (parts.Count > 0)
                        {
                            text = parts.Dequeue().Trim() + "\r\n";

                            if (IsDebug)
                            {
                                Debug.WriteLine(text);
                            }

                            bytes = Encoding.UTF8.GetBytes(text.ToCharArray());
                        }
                        else if (processor != null)
                        {
                            bytes = processor.AppendCommandData(tmp);
                        }

                        _ioStream.Write(bytes, 0, bytes.Length);
                        continue;
                    }

                    if (tmp.StartsWith(string.Format(tmpl, _counter, ResponseType.Ok)))
                    {
#if !NETFX_CORE
                        if (_idleState == IdleState.Paused)
                        {
                            StartIdling();
                        }
#endif
                        return(true);
                    }

                    if (tmp.StartsWith(string.Format(tmpl, _counter, ResponseType.PreAuth)))
                    {
#if !NETFX_CORE
                        if (_idleState == IdleState.Paused)
                        {
                            StartIdling();
                        }
#endif
                        return(true);
                    }

                    if (tmp.StartsWith(string.Format(tmpl, _counter, ResponseType.No)) ||
                        tmp.StartsWith(string.Format(tmpl, _counter, ResponseType.Bad)))
                    {
#if !NETFX_CORE
                        if (_idleState == IdleState.Paused)
                        {
                            StartIdling();
                        }
#endif
                        Match serverAlertMatch = Expressions.ServerAlertRex.Match(tmp);
                        if (serverAlertMatch.Success && tmp.Contains("IMAP") && tmp.Contains("abled"))
                        {
                            throw new ServerAlertException(serverAlertMatch.Groups[1].Value);
                        }
                        return(false);
                    }
                }
            }
        }