public Configuration(string appkey, string secret, string userid, string connectId) { this.connectUrl = "http://stream.api.taobao.com/stream"; this.httpConnectionTimeout = 5; this.httpReadTimeout = 90; this.httpConnectRetryCount = 3; this.httpConnectRetryInterval = 0x10; this.sleepTimeOfServerInUpgrade = 300; this.httpReconnectInterval = 0x15054; this.minThreads = 100; this.maxThreads = 200; this.queueSize = 0xc350; TopCometStreamRequest item = new TopCometStreamRequest(appkey, secret, userid, connectId); this.connectReqParam = new List<TopCometStreamRequest>(1); this.connectReqParam.Add(item); }
private void ControlThread(StreamMsgConsumeFactory msgConsumeFactory, ref bool bstop, Configuration conf, TopCometStreamRequest cometReq) { long ticks = 0L; while (!bstop) { if (this.allStop) { break; } try { if ("102".Equals(this.serverRespCode)) { this.logger.Info("Server is upgrade sleep " + conf.GetSleepTimeOfServerInUpgrade() + " seconds"); Thread.Sleep((int) (conf.GetSleepTimeOfServerInUpgrade() * 0x3e8)); this.StartConsumeThread(cometReq); } else if (("501".Equals(this.serverRespCode) || "103".Equals(this.serverRespCode)) || ("101".Equals(this.serverRespCode) || "500".Equals(this.serverRespCode))) { this.StartConsumeThread(cometReq); } else { if ("104".Equals(this.serverRespCode) || "105".Equals(this.serverRespCode)) { if ((!"104".Equals(this.serverRespCode) || this.isReconnect) && !"105".Equals(this.serverRespCode)) { goto Label_0117; } } else { bstop = true; } break; } Label_0117: try { Monitor.Enter(this.objLock); ticks = DateTime.Now.Ticks; Monitor.Wait(this.objLock, (int) (conf.GetHttpReconnectInterval() * 0x3e8)); if ((DateTime.Now.Ticks - ticks) >= ((conf.GetHttpReconnectInterval() * 0x3e8) * 0x2710)) { this.serverRespCode = "500"; this.isReconnect = true; } } catch (Exception exception) { this.logger.Error(exception.Message); } finally { Monitor.Exit(this.objLock); } continue; } catch (Exception exception2) { this.logger.Error("Occur some error,stop the stream consume" + exception2.Message); bstop = true; try { Monitor.Enter(this.objLock); Monitor.PulseAll(this.objLock); } finally { Monitor.Exit(this.objLock); } continue; } } if (this.currentStreamImpl != null) { try { this.currentStreamImpl.Close(); } catch (Exception) { } } this.logger.Info("Stop stream consume"); }
private void StartConsumeThread(TopCometStreamRequest cometReq) { IStreamImplementation stream = null; try { stream = this.GetMsgStreamImpl(cometReq); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnConnect(); } } catch (TopCometSysErrorException exception) { this.bstop = true; this.logger.Error(exception.Message); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnSysErrorException(exception); } } catch (Exception exception2) { this.bstop = true; this.logger.Error(exception2.Message); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnConnectError(exception2); } } this.lastStartConsumeThread = DateTime.Now.Ticks; new Thread(delegate { this.TopCometStreamConsume(this.lastStartConsumeThread, ref this.bstop, stream, cometReq.GetConnectListener()); }) { Name = "top-stream-consume-thread" + cometReq.GetConnectId() }.Start(); }
public IStreamImplementation GetMsgStreamImpl(TopCometStreamRequest cometReq) { if (cometReq != null) { cometReq.GetConnectListener().OnBeforeConnect(); } TopDictionary parameters = new TopDictionary(); parameters.Add("app_key", cometReq.GetAppkey()); if (!string.IsNullOrEmpty(cometReq.GetUserId())) { parameters.Add("user", cometReq.GetUserId()); } if (!string.IsNullOrEmpty(cometReq.GetConnectId())) { parameters.Add("id", cometReq.GetConnectId()); } parameters.Add("timestamp", DateTime.Now.Ticks); IDictionary<string, string> otherParam = cometReq.GetOtherParam(); if ((otherParam != null) && (otherParam.Count > 0)) { IEnumerator<KeyValuePair<string, string>> enumerator = otherParam.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair<string, string> current = enumerator.Current; KeyValuePair<string, string> pair2 = enumerator.Current; parameters.Add(current.Key, pair2.Value); } } string str = null; try { str = TopUtils.SignTopRequest(parameters, cometReq.GetSecret(), true); if (string.IsNullOrEmpty(str)) { throw new Exception("Get sign error"); } } catch (Exception exception) { throw exception; } parameters.Add("sign", str); HttpResponse response = new HttpClient(this.conf, parameters).Post(); return (this.currentStreamImpl = new MessageStreamImpl(this.msgConsumeFactory, response, this.cometMessageListener, this)); }