public MessageStreamImpl(StreamMsgConsumeFactory msgConsumeFactory,
     HttpResponse response, ITopCometMessageListener msgListener, TopCometStreamImpl cometStreamImpl)
     : base(msgConsumeFactory, response)
 {
     this.msgListener = msgListener;
     this.objLock = cometStreamImpl.GetControlLock();
     this.cometStreamImpl = cometStreamImpl;
 }
 public AbstractStreamImpl(StreamMsgConsumeFactory msgConsumeFactory, HttpResponse response)
 {
     this.msgConsumeFactory = msgConsumeFactory;
     this.response = response;
 }
Пример #3
0
        public HttpResponse Post()
        {
            int retriedCount;
            int retry = conf.GetHttpConnectRetryCount() + 1;
            HttpResponse resp = null;
            for (retriedCount = 1; retriedCount <= retry; retriedCount++)
            {
                try
                {
                    HttpWebRequest con = null;
                    System.IO.Stream outStream = null;
                    try
                    {
                        con = GetConnection(conf.GetConnectUrl(), conf.GetHttpConnectionTimeout(), conf.GetHttpReadTimeout());
                        con.KeepAlive = true;

                        SetHeaders(con, conf.GetRequestHeader());
                        con.Method = "POST";
                        con.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

                        string postParam = WebUtils.BuildQuery(parameters);
                        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParam);

                        outStream = con.GetRequestStream();
                        outStream.Write(bytes, 0, bytes.Length);
                        outStream.Close();

                        HttpWebResponse response = (HttpWebResponse)con.GetResponse();
                        HttpStatusCode responseCode = response.StatusCode;

                        if (HttpStatusCode.OK == responseCode)
                        {
                            log.Info("connect successful");

                            StringBuilder respHeader = new StringBuilder();
                            WebHeaderCollection responseHeaders = con.Headers;

                            foreach (string key in responseHeaders.AllKeys)
                            {
                                string[] values = responseHeaders.GetValues(key);

                                foreach (string value in values)
                                {
                                    if (key != null)
                                    {
                                        respHeader.Append(key).Append("=").Append(value);
                                    }
                                    else
                                    {
                                        respHeader.Append(value);
                                    }
                                    respHeader.Append(";");
                                }

                                log.Info("Response: " + respHeader.ToString());
                            }
                            resp = new HttpResponse(con);
                            return resp;
                        }
                        else if (HttpStatusCode.BadRequest == responseCode)
                        {   //参数校验出错
                            log.Info("Request param is invalid,errmsg is:" + con.Headers.Get(StreamConstants.ERR_MSG_HEADER));
                            throw new TopCometSysErrorException("Server response err msg:" + con.Headers.Get(StreamConstants.ERR_MSG_HEADER));
                        }
                        else if (HttpStatusCode.Forbidden == responseCode)
                        {//服务端在发布,需要休眠一段时间

                            log.Info("Server is deploying,sleep " + retriedCount * conf.GetHttpConnectRetryInterval() + " seconds");
                            if (retriedCount == conf.GetHttpConnectRetryCount())
                            {
                                log.Info("May be server occure some error,please contact top tech support");
                                throw new TopCometSysErrorException("May be server occure some error,please contact top tech support");
                            }
                            try
                            {
                                Thread.Sleep(retriedCount * conf.GetHttpConnectRetryInterval() * 1000);
                            }
                            catch (Exception e)
                            {
                                //ignore;
                            }
                            continue;
                        }
                    }
                    catch (Exception e)
                    {
                        log.Error(e.Message);
                    }
                    finally
                    {
                        try
                        {
                            if (outStream != null)
                            {
                                outStream.Close();
                            }
                        }
                        catch (Exception ignore)
                        {
                        }
                    }
                }
                catch (Exception ioe)
                {
                    // connection timeout or read timeout
                    if (retriedCount == conf.GetHttpConnectRetryCount())
                    {
                        throw new TopCometSysErrorException(ioe.Message);
                    }
                }
                try
                {
                    log.Info("Sleeping " + conf.GetHttpConnectRetryInterval() + " seconds until the next retry.");
                    Thread.Sleep(retriedCount * conf.GetHttpConnectRetryInterval() * 1000);
                }
                catch (Exception ignore)
                {
                    //nothing to do
                }
            }
            return resp;
        }