private void StreamingThread()
        {
            var cno = Interlocked.Increment(ref STGC);

            try
            {
                using (var sr = new StreamReader(this.receiveStream))
                {
                    while (!sr.EndOfStream && !parentCore.IsDisposed && !disposed && !isTimeout)
                    {
                        timeoutCount = 0;
                        String cline = sr.ReadLine();
                        System.Diagnostics.Debug.WriteLine("thread: " + cno + " ( " + Provider.ToString() + " ) / " + cline);
                        this.parentCore.EnqueueReceivedObject(this.Provider, cline);
                        // sr.ReadLine());
                    }
                }
            }
            catch (ObjectDisposedException) { }
            catch (ThreadAbortException) { }
            catch (Exception e)
            {
                // ignore all errors when disposing
                if (!disposed)
                {
                    parentCore.RaiseOnExceptionThrown(e);
                }
            }
            finally
            {
                System.Diagnostics.Debug.WriteLine("thread: ***DISCONNECTED*** " + cno + " ( " + Provider.ToString() + " ) ");
                FinalizeStream();
            }
        }
示例#2
0
 /// <summary>
 /// Finalize streaming connection
 /// </summary>
 /// <param name="disposing">Called from Dispose() method</param>
 protected virtual void Dispose(bool disposing)
 {
     if (this.disposed)
     {
         return;
     }
     System.Diagnostics.Debug.WriteLine("Disposing:" + Provider.ToString());
     this.disposed = true;
     FinalizeStream();
 }
示例#3
0
文件: Basic.cs 项目: karno/MinTw
        public override sealed System.Xml.Linq.XDocument RequestAPI(string uriParticle, CredentialProvider.RequestMethod method, IEnumerable<KeyValuePair<string, string>> param)
        {
            if (String.IsNullOrEmpty(uriParticle))
                throw new ArgumentNullException(uriParticle);
            else if (uriParticle.Length < 5)
                throw new ArgumentException("uri is too short.");
            string target = TwitterUri + (uriParticle.EndsWith("/") ? uriParticle.Substring(1) : uriParticle);

            if (target.EndsWith("format"))
                target = target.Substring(0, target.Length - 6) + "xml";
            else if (target.EndsWith("json"))
                target = target.Substring(0, target.Length - 4) + "xml";

            try
            {
                var req = Http.CreateRequest(new Uri(target), true);
                req.Credentials = new System.Net.NetworkCredential(UserName, Password);
                var ret = Http.WebConnect<XDocument>(
                    req,
                    method.ToString(), null,
                    new Http.DStreamCallbackFull<XDocument>((res) =>
                    {
                        int rateLimit;
                        if (int.TryParse(res.Headers["X-RateLimit-Limit"], out rateLimit))
                        {
                            this.RateLimitMax = rateLimit;
                        }
                        int rateLimitRemaining;
                        if (int.TryParse(res.Headers["X-RateLimit-Remaining"], out rateLimitRemaining))
                        {
                            this.RateLimitRemaining = rateLimitRemaining;
                        }
                        long rateLimitReset;
                        if (long.TryParse(res.Headers["X-RateLimit-Reset"], out rateLimitReset))
                        {
                            this.RateLimitReset = UnixEpoch.GetDateTimeByUnixEpoch(rateLimitReset);
                        }

                        XDocument xd = null;
                        try
                        {
                            using (var s = res.GetResponseStream())
                            {
                                using (var sr = new StreamReader(s))
                                {
                                    xd = XDocument.Load(sr);
                                }
                            }
                        }
                        catch (XmlException)
                        {
                            throw;
                        }
                        return xd;
                    }));
                if (ret.Succeeded && ret.Data != null)
                {
                    return ret.Data;
                }
                else
                {
                    if (ret.Exception != null)
                        throw ret.Exception;
                    else
                        throw new WebException(ret.Message);
                }
            }
            catch (WebException we)
            {
                System.Diagnostics.Debug.WriteLine(we.ToString());
            }
            catch (XmlException xe)
            {
                throw new Exceptions.TwitterXmlParseException(xe);
            }
            catch (IOException)
            {
                throw;
            }

            return null;
        }
示例#4
0
文件: OAuth.cs 项目: rekkusu/MinTw
        //Implementation
        public override sealed XDocument RequestAPI(string uriParticle, CredentialProvider.RequestMethod method, IEnumerable<KeyValuePair<string,string>> param)
        {
            if (String.IsNullOrEmpty(uriParticle))
                throw new ArgumentNullException(uriParticle);
            else if (uriParticle.Length < 5)
                throw new ArgumentException("uri is too short.");
            string target = TwitterUri + (uriParticle.EndsWith("/") ? uriParticle.Substring(1) : uriParticle);

            if (target.EndsWith("format"))
                target = target.Substring(0, target.Length - 6) + "json";
            else if (target.EndsWith("xml"))
                target = target.Substring(0, target.Length - 3) + "json";

            if(String.IsNullOrEmpty(Token) || String.IsNullOrEmpty(Secret))
            {
                throw new Exceptions.TwitterOAuthRequestException("OAuth is not validated.");
            }
            var authuri = CreateUrl(target, method, param);
            try
            {
                var ret = Http.WebConnect<XDocument>(
                    Http.CreateRequest(new Uri(authuri), true),
                    method.ToString(), null,
                    new Http.DStreamCallbackFull<XDocument>((res) =>
                    {
                        int rateLimit;
                        if (int.TryParse(res.Headers["X-RateLimit-Limit"], out rateLimit))
                        {
                            this.RateLimitMax = rateLimit;
                        }
                        int rateLimitRemaining;
                        if (int.TryParse(res.Headers["X-RateLimit-Remaining"], out rateLimitRemaining))
                        {
                            this.RateLimitRemaining = rateLimitRemaining;
                        }
                        long rateLimitReset;
                        if (long.TryParse(res.Headers["X-RateLimit-Reset"], out rateLimitReset))
                        {
                            this.RateLimitReset = UnixEpoch.GetDateTimeByUnixEpoch(rateLimitReset);
                        }

                        XDocument xd = null;
                        try
                        {
                            using (var s = res.GetResponseStream())
                            {
                                using (var r = JsonReaderWriterFactory.CreateJsonReader(s, XmlDictionaryReaderQuotas.Max))
                                {
                                    xd = XDocument.Load(r);
                                }
                            }
                        }
                        catch (XmlException)
                        {
                            throw;
                        }
                        return xd;
                    }));
                if (ret.Succeeded && ret.Data != null)
                {
                    return ret.Data;
                }
                else
                {
                    if (ret.Exception != null)
                        throw ret.Exception;
                    else
                        throw new WebException(ret.Message);
                }
            }
            catch (WebException we)
            {
                System.Diagnostics.Debug.WriteLine(we.ToString());
            }
            catch (XmlException xe)
            {
                throw new Exceptions.TwitterXmlParseException(xe);
            }
            catch (IOException)
            {
                throw;
            }

            return null;
        }