示例#1
0
        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));
        }
示例#2
0
        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();
        }
示例#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
        void JdCometStreamConsume(long lastStartConsumeThread, ref bool bsJd, IStreamImplementation stream, IConnectionLifeCycleListener connectListener)
        {
            startConsumeThreadTimes = 0;
            while (!allSJd&&!closed && stream.IsAlive())
            {
                try
                {
                    stream.NextMsg();
                }
                catch (Exception e)
                {//出现了read time out异常
                    // 资源清理
                    if (stream != null)
                    {
                        try
                        {
                            stream.Close();
                        }
                        catch (Exception e1)
                        {
                            logger.Error(e1.Message);
                        }
                    }
                    stream = null;
                    closed = true;
                    //通知
                    if (connectionListener != null)
                    {
                        try
                        {
                            connectionListener.OnReadTimeout();
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.Message);
                        }
                    }
                    /**
                     * 30分钟内发送了10次IOException
                     */
                    if (DateTime.Now.Ticks - lastStartConsumeThread < 18000000000)
                    {// 短时间内由于读取IOException连接了10次,则退出
                        startConsumeThreadTimes++;
                        if (startConsumeThreadTimes >= 10)
                        {
                            bsJd = true;
                            if (connectionListener != null)
                            {
                                try
                                {
                                    connectionListener.OnMaxReadTimeoutException();
                                }
                                catch (Exception maxE) { logger.Error(maxE.Message); }
                            }
                            logger.Error("Occure too many exception,sJd the system,please check");
                            //通知唤醒控制线程,但是不在发起重连接
                            try
                            {
                                Monitor.Enter(objLock);
                                Monitor.PulseAll(objLock);
                            }
                            catch (Exception e2)
                            {
                            }
                            finally
                            {
                                Monitor.Exit(objLock);
                            }
                        }
                        else
                        {   //没有到达10次,通知重连
                            startConsumeThreadTimes = 0;
                            serverRespCode = StreamConstants.RECONNECT;
                            try
                            {
                                Monitor.Enter(objLock);
                                Monitor.PulseAll(objLock);
                            }
                            catch (Exception e2)
                            {
                            }
                            finally
                            {
                                Monitor.Exit(objLock);
                            }
                            closed = false;
                            break;
                        }
                    }
                    else
                    {
                        // 通知重连
                        Console.WriteLine(" 通知重连" + DateTime.Now.ToString());
                        startConsumeThreadTimes = 0;
                        serverRespCode = StreamConstants.RECONNECT;

                        try
                        {
                            Monitor.Enter(objLock);
                            Console.WriteLine(" PulseAll" + DateTime.Now.ToString());
                            Monitor.PulseAll(objLock);
                        }
                        catch (Exception e2)
                        {
                        }
                        finally
                        {
                            Monitor.Exit(objLock);
                        }
                        closed = false;
                        break;
                    }
                }
            }
            //出现异常情况下做资源清理
            if (stream != null)
            {
                try
                {
                    stream.Close();
                }
                catch (Exception e)
                {
                    logger.Warn(e.Message);
                }
            }
        }
示例#5
0
        void TopCometStreamConsume(long lastStartConsumeThread, ref bool bstop, IStreamImplementation stream, IConnectionLifeCycleListener connectListener)
        {
            startConsumeThreadTimes = 0;
            while (!allStop && !closed && stream.IsAlive())
            {
                try
                {
                    stream.NextMsg();
                }
                catch (Exception e)
                {//出现了read time out异常
                    // 资源清理
                    if (stream != null)
                    {
                        try
                        {
                            stream.Close();
                        }
                        catch (Exception e1)
                        {
                            logger.Error(e1.Message);
                        }
                    }
                    stream = null;
                    closed = true;
                    //通知
                    if (connectionListener != null)
                    {
                        try
                        {
                            connectionListener.OnReadTimeout();
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.Message);
                        }
                    }

                    /**
                     * 30分钟内发送了10次IOException
                     */
                    if (DateTime.Now.Ticks - lastStartConsumeThread < 18000000000)
                    {// 短时间内由于读取IOException连接了10次,则退出
                        startConsumeThreadTimes++;
                        if (startConsumeThreadTimes >= 10)
                        {
                            bstop = true;
                            if (connectionListener != null)
                            {
                                try
                                {
                                    connectionListener.OnMaxReadTimeoutException();
                                }
                                catch (Exception maxE) { logger.Error(maxE.Message); }
                            }
                            logger.Error("Occure too many exception,stop the system,please check");
                            //通知唤醒控制线程,但是不在发起重连接
                            try
                            {
                                Monitor.Enter(objLock);
                                Monitor.PulseAll(objLock);
                            }
                            catch (Exception e2)
                            {
                            }
                            finally
                            {
                                Monitor.Exit(objLock);
                            }
                        }
                        else
                        {   //没有到达10次,通知重连
                            startConsumeThreadTimes = 0;
                            serverRespCode          = StreamConstants.RECONNECT;
                            try
                            {
                                Monitor.Enter(objLock);
                                Monitor.PulseAll(objLock);
                            }
                            catch (Exception e2)
                            {
                            }
                            finally
                            {
                                Monitor.Exit(objLock);
                            }
                            closed = false;
                            break;
                        }
                    }
                    else
                    {
                        // 通知重连
                        Console.WriteLine(" 通知重连" + DateTime.Now.ToString());
                        startConsumeThreadTimes = 0;
                        serverRespCode          = StreamConstants.RECONNECT;

                        try
                        {
                            Monitor.Enter(objLock);
                            Console.WriteLine(" PulseAll" + DateTime.Now.ToString());
                            Monitor.PulseAll(objLock);
                        }
                        catch (Exception e2)
                        {
                        }
                        finally
                        {
                            Monitor.Exit(objLock);
                        }
                        closed = false;
                        break;
                    }
                }
            }
            //出现异常情况下做资源清理
            if (stream != null)
            {
                try
                {
                    stream.Close();
                }
                catch (Exception e)
                {
                    logger.Warn(e.Message);
                }
            }
        }
 private void TopCometStreamConsume(long lastStartConsumeThread, ref bool bstop, IStreamImplementation stream, IConnectionLifeCycleListener connectListener)
 {
     this.startConsumeThreadTimes = 0;
     while ((!this.allStop && !this.closed) && stream.IsAlive())
     {
         try
         {
             stream.NextMsg();
             continue;
         }
         catch (Exception)
         {
             if (stream != null)
             {
                 try
                 {
                     stream.Close();
                 }
                 catch (Exception exception)
                 {
                     this.logger.Error(exception.Message);
                 }
             }
             stream = null;
             this.closed = true;
             if (this.connectionListener != null)
             {
                 try
                 {
                     this.connectionListener.OnReadTimeout();
                 }
                 catch (Exception exception2)
                 {
                     this.logger.Error(exception2.Message);
                 }
             }
             if ((DateTime.Now.Ticks - lastStartConsumeThread) < 0x430e23400L)
             {
                 this.startConsumeThreadTimes++;
                 if (this.startConsumeThreadTimes >= 10)
                 {
                     bstop = true;
                     if (this.connectionListener != null)
                     {
                         try
                         {
                             this.connectionListener.OnMaxReadTimeoutException();
                         }
                         catch (Exception exception3)
                         {
                             this.logger.Error(exception3.Message);
                         }
                     }
                     this.logger.Error("Occure too many exception,stop the system,please check");
                     try
                     {
                         try
                         {
                             Monitor.Enter(this.objLock);
                             Monitor.PulseAll(this.objLock);
                         }
                         catch (Exception)
                         {
                         }
                         continue;
                     }
                     finally
                     {
                         Monitor.Exit(this.objLock);
                     }
                 }
                 this.startConsumeThreadTimes = 0;
                 this.serverRespCode = "500";
                 try
                 {
                     Monitor.Enter(this.objLock);
                     Monitor.PulseAll(this.objLock);
                 }
                 catch (Exception)
                 {
                 }
                 finally
                 {
                     Monitor.Exit(this.objLock);
                 }
                 this.closed = false;
             }
             else
             {
                 Console.WriteLine(" 通知重连" + DateTime.Now.ToString());
                 this.startConsumeThreadTimes = 0;
                 this.serverRespCode = "500";
                 try
                 {
                     Monitor.Enter(this.objLock);
                     Console.WriteLine(" PulseAll" + DateTime.Now.ToString());
                     Monitor.PulseAll(this.objLock);
                 }
                 catch (Exception)
                 {
                 }
                 finally
                 {
                     Monitor.Exit(this.objLock);
                 }
                 this.closed = false;
             }
             break;
         }
     }
     if (stream != null)
     {
         try
         {
             stream.Close();
         }
         catch (Exception exception4)
         {
             this.logger.Warn(exception4.Message);
         }
     }
 }
 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));
 }