コード例 #1
0
ファイル: JsonDeserializer.cs プロジェクト: kazuki/ktwt
        public static T Deserialize <T> (string text)
        {
            JsonValue v = JsonValueReader.Read(text);

            if (v is JsonObject)
            {
                return(Deserialize <T> ((JsonObject)v));
            }
            else if (v is JsonArray)
            {
                return((T)(object)Deserialize(v as JsonArray, typeof(T)));
            }
            throw new FormatException();
        }
コード例 #2
0
ファイル: StreamingClient.cs プロジェクト: kazuki/ktwt
        void StreamingThread(object o)
        {
            InternalState info = (InternalState)o;
            string line = null;
            TimeSpan maxWait = TimeSpan.FromMinutes (1);
            TimeSpan startWait = TimeSpan.FromSeconds (2.5);
            TimeSpan wait = startWait;
            TimeSpan timeout = TimeSpan.FromSeconds (32);
            byte[] buffer = null;
            int filled = 0;
            while (_active) {
                try {
                    info.ConnectionState = StreamingState.Connecting;
                    using (IStreamingState state = info.Account.TwitterClient.StartStreaming (StreamingUri, "POST", info.StreamingPostData)) {
                        info.Handle = state;
                        info.RetryCount = 0;
                        info.ConnectionState = StreamingState.Connected;
                        while (_active) {
                            try {
                                line = ReadLineWithTimeout (state.Stream, ref buffer, ref filled, timeout);
                                if (line == null) break;
                                if (line.Length == 0) continue;
                                JsonValueReader jsonReader = new JsonValueReader (line);
                                JsonObject jsonRootObj = (JsonObject)jsonReader.Read ();
                                if (jsonRootObj.Value.ContainsKey ("delete") || jsonRootObj.Value.ContainsKey ("limit"))
                                    continue;
                                try {
                                    Target.Streaming_StatusArrived (this, new StatusArrivedEventArgs (JsonDeserializer.Deserialize<Status> (jsonRootObj)));
                                } catch {}
                            } catch {
                                break;
                            }
                        }
                    }
                } catch {}
                if (!_active) break;

                info.Handle = null;
                info.ConnectionState = StreamingState.Disconnected;

                if (info.RetryCount > 0) {
                    wait = wait + wait;
                    if (wait > maxWait)
                        wait = maxWait;
                    info.NextRetryTime = DateTime.Now + wait;
                    info.ConnectionState = StreamingState.Waiting;
                    Thread.Sleep (wait);
                }
                info.RetryCount++;
            }
        }