Exemplo n.º 1
0
        public void Start()
        {
            List <JdCometStreamRequest> cometRequests = conf.GetConnectReqParam();

            msgConsumeFactory = new StreamMsgConsumeFactory(conf.GetMinThreads(), conf.GetMaxThreads(), conf.GetQueueSize());

            for (int i = 0; i < cometRequests.Count; i++)
            {
                try
                {
                    JdCometStreamRequest 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 bsJd, conf, cometRequest);
                    });
                    controlThread.Name = "stream-control-thread-connectid-" + cometRequest.GetConnectId();
                    controlThread.Start();
                    controlThreads.Add(controlThread);
                }
                catch (Exception e)
                {
                    continue;
                }
            }
        }
Exemplo n.º 2
0
        public IStreamImplementation GetMsgStreamImpl(JdCometStreamRequest cometReq)
        {
            if (cometReq != null)
            {
                cometReq.GetConnectListener().OnBeforeConnect();
            }

            JdDictionary param = new JdDictionary();

            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 = JdUtils.SignJdRequest(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));
        }
Exemplo n.º 3
0
        private void StartConsumeThread(JdCometStreamRequest cometReq)
        {
            IStreamImplementation stream = null;

            try
            {
                stream = GetMsgStreamImpl(cometReq);
                if (cometReq.GetConnectListener() != null)
                {
                    cometReq.GetConnectListener().OnConnect();
                }
            }
            catch (JdCometSysErrorException e)
            {
                bsJd = true;
                logger.Error(e.Message);
                if (cometReq.GetConnectListener() != null)
                {
                    cometReq.GetConnectListener().OnSysErrorException(e);
                }
            }
            catch (Exception ex)
            {
                bsJd = true;
                logger.Error(ex.Message);
                if (cometReq.GetConnectListener() != null)
                {
                    cometReq.GetConnectListener().OnConnectError(ex);
                }
            }

            lastStartConsumeThread = DateTime.Now.Ticks;

            Thread JdCometStreamConsumeThread = new Thread(delegate()
            {
                JdCometStreamConsume(lastStartConsumeThread, ref bsJd, stream, cometReq.GetConnectListener());
            });

            JdCometStreamConsumeThread.Name = "Jd-stream-consume-thread" + cometReq.GetConnectId();
            JdCometStreamConsumeThread.Start();
        }