Пример #1
0
        /**
         * 1,userid是用户的id,是大于0的整数。
         * 如果不需要传入userid,则可以传入小于等于0的数值。
         * </br>
         * 2,如果不指定userid,则所有与此appkey关联的所有用户的消息都会发送到此连接上</br>
         * 3,connectId为连接标识(用于多连接)。可以为空。对于多连接的详细描述请参考文档。
         * @param appkey
         * @param secret
         * @param userid 传入用户的userid
         * @param connectId 连接标识
         */
        public Configuration(string appkey, string secret, string userid, string connectId)
        {
            JdCometStreamRequest cometReq = new JdCometStreamRequest(appkey, secret, userid, connectId);

            connectReqParam = new List <JdCometStreamRequest>(1);
            connectReqParam.Add(cometReq);
        }
Пример #2
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;
                }
            }
        }
Пример #3
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));
        }
Пример #4
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();
        }
Пример #5
0
        void ControlThread(StreamMsgConsumeFactory msgConsumeFactory, ref bool bsJd, Configuration conf, JdCometStreamRequest cometReq)
        {
            long lastSleepTime = 0;

            while (!bsJd)
            {
                if (allSJd)
                {
                    break;
                }

                try
                {
                    if (StreamConstants.SERVER_DEPLOY.Equals(serverRespCode))
                    {   // 服务端在发布
                        logger.Info("Server is upgrade sleep " + conf.GetSleepTimeOfServerInUpgrade() + " seconds");
                        Thread.Sleep(conf.GetSleepTimeOfServerInUpgrade() * 1000);
                        StartConsumeThread(cometReq);
                    }
                    else if (/*客户端第一次发起连接请求*/
                        StreamConstants.CLIENT_FIRST_CONNECT.Equals(serverRespCode) ||
                        /*服务端主动断开了所有的连接*/
                        StreamConstants.SERVER_REHASH.Equals(serverRespCode) ||
                        /*连接到达最大时间*/
                        StreamConstants.CONNECT_REACH_MAX_TIME.Equals(serverRespCode) ||
                        /*在一些异常情况下需要重连*/
                        StreamConstants.RECONNECT.Equals(serverRespCode))
                    {
                        StartConsumeThread(cometReq);
                    }
                    else if (/*客户端自己把自己踢开*/
                        StreamConstants.CLIENT_KICKOFF.Equals(serverRespCode) ||
                        /*服务端把客户端踢开*/
                        StreamConstants.SERVER_KICKOFF.Equals(serverRespCode))
                    {
                        if ((StreamConstants.CLIENT_KICKOFF.Equals(serverRespCode) && !isReconnect) ||
                            StreamConstants.SERVER_KICKOFF.Equals(serverRespCode))
                        {
                            break;// 终止掉当前线程
                        }
                    }
                    else
                    {   //错误码设置出错,停止线程
                        bsJd = true;
                        break;
                    }
                    //连接成功,开始休眠
                    try
                    {
                        Monitor.Enter(objLock);
                        {
                            lastSleepTime = DateTime.Now.Ticks;

                            Monitor.Wait(objLock, conf.GetHttpReconnectInterval() * 1000);
                            if (DateTime.Now.Ticks - lastSleepTime >= (conf.GetHttpReconnectInterval()) * 1000 * 10000)
                            {
                                /*
                                 * 快要到达连接的最大时间了,需要重新发起连接
                                 */
                                serverRespCode = StreamConstants.RECONNECT;
                                isReconnect    = true;
                            }//否则,是由于某种原因被唤醒的
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.Message);
                    }
                    finally
                    {
                        Monitor.Exit(objLock);
                    }
                }
                catch (Exception e)
                {
                    logger.Error("Occur some error,sJd the stream consume" + e.Message);
                    bsJd = true;
                    try
                    {
                        Monitor.Enter(objLock);
                        Monitor.PulseAll(objLock);
                    }
                    finally
                    {
                        Monitor.Exit(objLock);
                    }
                }
            }
            if (currentStreamImpl != null)
            {
                try
                {
                    currentStreamImpl.Close();
                }
                catch (Exception e)
                {
                    // ignore
                }
            }
            logger.Info("SJd stream consume");
        }