Example #1
0
		void ThreadMain(object state)
		{
			var auth = (OAuthAuthorization)state;

			this.Account = auth.Token;
			this.Terminated = false;

			try
			{
				var ub = new UriBuilder(TwitterUriBuilder.Stream.User(this.Track, this.Follows));
				var query = string.IsNullOrEmpty(ub.Query) ? null : ub.Query.TrimStart('?');

				ub.Query = null;

				using (var wc = new CustomWebClient
				{
					Headers =
					{
						{ HttpRequestHeader.UserAgent, "Solar/" + Assembly.GetEntryAssembly().GetName().Version },
					},
				})
				using (var ns = wc.OpenPost(ub.Uri, (string.IsNullOrEmpty(query) ? null : query + "&") + auth.CreateParameters("POST", ub.Uri, query)))
				using (var sr = new StreamReader(ns))
				{
					Connected.RaiseEvent(this, EventArgs.Empty);

					try
					{
						foreach (var i in sr.EnumerateLines()
											.Where(_ => !string.IsNullOrEmpty(_))
											.Select(DynamicJson.Parse))
						{
							if (IsDelete(i))
							{
								// delete
								if (i.delete.status())
									DeleteStatus.RaiseEvent(this, new EventArgs<StatusID>(i.delete.status.id));
							}
							else if (IsFriends(i))
							{
								// friends
								Friends.RaiseEvent(this, new EventArgs<IList<UserID>>(((dynamic[])i.friends).Select(_ => (UserID)_).Freeze()));
							}
							else if (IsEvent(i))
							{
								// event
								using (var client = new TwitterClient(auth.Token, StatusCache))
								{
									var e = new TwitterStreamEventArgs(client, i);

									switch (e.Type)
									{
										case "follow":
											if (e.Source.UserID == this.Account.UserID)
												Follow.RaiseEvent(this, e);
											else
												Followed.RaiseEvent(this, e);

											break;
										case "unfollow":
											Unfollow.RaiseEvent(this, e);

											break;
										case "block":
											Block.RaiseEvent(this, e);

											break;
										case "unblock":
											Unblock.RaiseEvent(this, e);

											break;
										case "favorite":
											if (e.Source.UserID == this.Account.UserID)
												Favorite.RaiseEvent(this, e);
											else
												Favorited.RaiseEvent(this, e);

											break;
										case "unfavorite":
											if (e.Source.UserID == this.Account.UserID)
												Unfavorite.RaiseEvent(this, e);
											else
												Unfavorited.RaiseEvent(this, e);

											break;
										case "retweet":
											if (e.Source.UserID == this.Account.UserID)
												Retweet.RaiseEvent(this, e);
											else
												Retweeted.RaiseEvent(this, e);

											break;
									}
								}
							}
							else if (IsDirectMessage(i))
							{
								// direct message
								using (var client = new TwitterClient(auth.Token, StatusCache))
									DirectMessage.RaiseEvent(this, new EventArgs<DirectMessage>(new DirectMessage(client, i.direct_message)));
							}
							else if (IsStatus(i))
							{
								// status
								using (var client = new TwitterClient(auth.Token, StatusCache))
									Status.RaiseEvent(this, new EventArgs<Status>(new Status(client, i)));
							}
						}
					}
					finally
					{
						wc.LastRequest.Abort();
					}
				}
			}
			catch (ThreadAbortException)
			{
			}
			catch (WebException ex)
			{
				WebError.RaiseEvent(this, new EventArgs<WebException>(ContentedWebException.Create(ex)));
			}
			catch (IOException ex)
			{
				ConnectionError.RaiseEvent(this, new EventArgs<IOException>(ex));
			}
			catch (Exception ex)
			{
				Exception.RaiseEvent(this, new EventArgs<Exception>(ex));
			}
			finally
			{
				streamingThread = null;
				Disconnected.RaiseEvent(this, EventArgs.Empty);
				this.Terminated = true;
			}
		}