public void Start() { List <TopCometStreamRequest> cometRequests = conf.GetConnectReqParam(); msgConsumeFactory = new StreamMsgConsumeFactory(conf.GetMinThreads(), conf.GetMaxThreads(), conf.GetQueueSize()); for (int i = 0; i < cometRequests.Count; i++) { try { TopCometStreamRequest cometRequest = cometRequests[i]; if (cometRequest.GetConnectListener() == null) { cometRequest.SetConnectListener(connectionListener); } if (cometRequest.GetMsgListener() == null) { cometRequest.SetMsgListener(cometMessageListener); } Thread controlThread = new Thread(delegate() { ControlThread(msgConsumeFactory, ref bstop, conf, cometRequest); }); controlThread.Name = "stream-control-thread-connectid-" + cometRequest.GetConnectId(); controlThread.Start(); controlThreads.Add(controlThread); } catch (Exception e) { continue; } } }
public IStreamImplementation GetMsgStreamImpl(TopCometStreamRequest cometReq) { if (cometReq != null) { cometReq.GetConnectListener().OnBeforeConnect(); } TopDictionary param = new TopDictionary(); param.Add(StreamConstants.PARAM_APPKEY, cometReq.GetAppkey()); if (!String.IsNullOrEmpty(cometReq.GetUserId())) { param.Add(StreamConstants.PARAM_USERID, cometReq.GetUserId()); } if (!String.IsNullOrEmpty(cometReq.GetConnectId())) { param.Add(StreamConstants.PARAM_CONNECT_ID, cometReq.GetConnectId()); } param.Add(StreamConstants.PARAM_TIMESTAMP, DateTime.Now.Ticks); IDictionary <string, string> otherParam = cometReq.GetOtherParam(); if (otherParam != null && otherParam.Count > 0) { IEnumerator <KeyValuePair <string, string> > kvps = otherParam.GetEnumerator(); while (kvps.MoveNext()) { param.Add(kvps.Current.Key, kvps.Current.Value); } } string sign = null; try { sign = TopUtils.SignTopRequest(param, cometReq.GetSecret(), true); if (String.IsNullOrEmpty(sign)) { throw new Exception("Get sign error"); } } catch (Exception e) { throw e; } param.Add(StreamConstants.PARAM_SIGN, sign); HttpClient httpClient = new HttpClient(conf, param); HttpResponse response; response = httpClient.Post(); return(currentStreamImpl = new MessageStreamImpl(msgConsumeFactory, response, cometMessageListener, this)); }
public IStreamImplementation GetMsgStreamImpl(TopCometStreamRequest cometReq) { if (cometReq != null) { cometReq.GetConnectListener().OnBeforeConnect(); } TopDictionary param = new TopDictionary(); param.Add(StreamConstants.PARAM_APPKEY, cometReq.GetAppkey()); if (!String.IsNullOrEmpty(cometReq.GetUserId())) { param.Add(StreamConstants.PARAM_USERID, cometReq.GetUserId()); } if (!String.IsNullOrEmpty(cometReq.GetConnectId())) { param.Add(StreamConstants.PARAM_CONNECT_ID, cometReq.GetConnectId()); } param.Add(StreamConstants.PARAM_TIMESTAMP, DateTime.Now.Ticks); IDictionary<string, string> otherParam = cometReq.GetOtherParam(); if (otherParam != null && otherParam.Count > 0) { IEnumerator<KeyValuePair<string, string>> kvps = otherParam.GetEnumerator(); while (kvps.MoveNext()) { param.Add(kvps.Current.Key, kvps.Current.Value); } } string sign = null; try { sign = TopUtils.SignTopRequest(param, cometReq.GetSecret(), true); if (String.IsNullOrEmpty(sign)) { throw new Exception("Get sign error"); } } catch (Exception e) { throw e; } param.Add(StreamConstants.PARAM_SIGN, sign); HttpClient httpClient = new HttpClient(conf, param); HttpResponse response; response = httpClient.Post(); return currentStreamImpl = new MessageStreamImpl(msgConsumeFactory, response, cometMessageListener, this); }
private void StartConsumeThread(TopCometStreamRequest cometReq) { IStreamImplementation stream = null; try { stream = GetMsgStreamImpl(cometReq); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnConnect(); } } catch (TopCometSysErrorException e) { bstop = true; logger.Error(e.Message); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnSysErrorException(e); } } catch (Exception ex) { bstop = true; logger.Error(ex.Message); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnConnectError(ex); } } lastStartConsumeThread = DateTime.Now.Ticks; Thread TopCometStreamConsumeThread = new Thread(delegate() { TopCometStreamConsume(lastStartConsumeThread, ref bstop, stream, cometReq.GetConnectListener()); }); TopCometStreamConsumeThread.Name = "top-stream-consume-thread" + cometReq.GetConnectId(); TopCometStreamConsumeThread.Start(); }