/// <summary> /// 异步更新秘钥 /// </summary> /// <param name="session"></param> /// <param name="secretVersion"></param> private void AsynUpdateSecret(string session, Nullable <Int64> secretVersion) { string cacheKey = GenerateSecretKey(session, secretVersion); lock (AsynQueueKeyLock) { // 不需要重复提交秘钥请求 if (asynQueueKey.ContainsKey(cacheKey)) { return; } SecretContext secretContext = GetSecret(session, GenerateSecretKey(session, secretVersion)); if (secretContext != null && secretContext.IsValid()) { return; } asynQueueKey.Add(cacheKey, EmptyObject); } WaitCallback secretApiCallback = (state) => { try { CallSecretApi(session, secretVersion); } catch (Exception e) { Log.Error(string.Format("asyn update secret error: {0}", e.Message)); } finally { lock (AsynQueueKeyLock) { asynQueueKey.Remove(cacheKey); } } }; try { ThreadPool.QueueUserWorkItem(secretApiCallback); } catch (Exception e) { lock (AsynQueueKeyLock) { asynQueueKey.Remove(cacheKey); } Log.Error(string.Format("add QueueUserWorkItem error: {0}", e.Message)); } }
private void DoUploadBiz() { long uploadTime = TopUtils.GetCurrentTimeMillis() + FlushInterval; while (true) { try { Thread.Sleep(SleepTime); IDictionary <string, Counter> appUserCounterMap = SecurityCounter.GetUserCounter(topClient.appKey); if (TopUtils.GetCurrentTimeMillis() >= uploadTime || (appUserCounterMap.Count * 4 > 65536)) { StringBuilder counterBuilder = new StringBuilder(); IDictionary <string, Counter> cloneAppUserCounter = SecurityCounter.CloneUserCounter(topClient.appKey); SecurityCounter.CleanUserCounter(topClient.appKey); int count = 0; foreach (KeyValuePair <string, Counter> currentPair in cloneAppUserCounter) { Counter counter = currentPair.Value; if (counterBuilder.Length > 0) { counterBuilder.Append(";"); } counterBuilder.Append(currentPair.Key); GenerateCounter(counterBuilder, counter); if ((++count) % 100 == 0) { DoUpload(counterBuilder.ToString(), AppUserSecretType); counterBuilder = new StringBuilder(); } } if (counterBuilder.Length > 0) { DoUpload(counterBuilder.ToString(), AppUserSecretType); counterBuilder = new StringBuilder(); } Counter appCounter = SecurityCounter.GetAppCounter(topClient.appKey); counterBuilder.Append(SecurityCore.GetAppUserSecretCache().Count); GenerateCounter(counterBuilder, appCounter); appCounter.Reset(); long uploadInterval = DoUpload(counterBuilder.ToString(), AppSecretType); uploadTime = TopUtils.GetCurrentTimeMillis() + uploadInterval; } } catch (Exception e) { Log.Error(string.Format("flushSecretApiReporter error: {0}", e.Message)); } } }
private static void On <T>(ITopLogger log, EventHandler <T> eventHandler, T args) where T : EventArgs { try { if (eventHandler != null) { eventHandler(null, args); } } catch (Exception e) { //here is global on error log.Error(e.StackTrace); //TODO:close channel here? } }
/// <summary> /// 异步更新秘钥 /// </summary> /// <param name="session"></param> /// <param name="secretVersion"></param> private void AsynUpdateSecret(string session, Nullable <Int64> secretVersion) { string cacheKey = GenerateSecretKey(session, secretVersion); RwLock.AcquireWriterLock(50); try { if (RwLock.IsWriterLockHeld) { // 不需要重复提交秘钥请求 if (asynQueueKey.ContainsKey(cacheKey)) { return; } asynQueueKey.Add(cacheKey, EmptyObject); WaitCallback secretApiCallback = (state) => { try { CallSecretApi(session, secretVersion); } catch (Exception e) { Log.Error(string.Format("asyn update secret error: {0}", e.Message)); } finally { asynQueueKey.Remove(cacheKey); } }; ThreadPool.QueueUserWorkItem(secretApiCallback); } } finally { if (RwLock.IsWriterLockHeld) { RwLock.ReleaseWriterLock(); } } }
public T Execute <T>(ITopRequest <T> request, string session, DateTime timestamp) where T : TopResponse { if (disableTrace) { return(DoExecute <T>(request, session, timestamp)); } else { try { return(DoExecute <T>(request, session, timestamp)); } catch (Exception e) { topLogger.Error(this.serverUrl + "\r\n" + e.StackTrace); throw e; } } }
private void InitSecretThread() { Thread uploadThread = new Thread(o => { int uploadInterval = FlushInterval; while (true) { try { Thread.Sleep(uploadInterval); IDictionary <string, object> jsonMap = new Dictionary <string, object>(); jsonMap.Add("sessionNum", SecurityCore.GetAppUserSecretCache().Count); jsonMap.Add("encryptPhoneNum", SecurityCounter.GetEncryptPhoneNum()); jsonMap.Add("encryptNickNum", SecurityCounter.GetEncryptNickNum()); jsonMap.Add("encryptReceiverNameNum", SecurityCounter.GetEncryptReceiverNameNum()); jsonMap.Add("decryptPhoneNum", SecurityCounter.GetDecryptPhoneNum()); jsonMap.Add("decryptNickNum", SecurityCounter.GetDecryptNickNum()); jsonMap.Add("decryptReceiverNameNum", SecurityCounter.GetDecryptReceiverNameNum()); jsonMap.Add("searchPhoneNum", SecurityCounter.GetSearchPhoneNum()); jsonMap.Add("searchNickNum", SecurityCounter.GetSearchNickNum()); jsonMap.Add("searchReceiverNameNum", SecurityCounter.GetSearchReceiverNameNum()); SecurityCounter.Reset(); String contentJson = TopUtils.ObjectToJson(jsonMap); uploadInterval = DoUpload(contentJson, SecretType); } catch (Exception e) { Log.Error(string.Format("flushSecretApiReporter error: {0}", e.Message)); } } }); uploadThread.IsBackground = true; uploadThread.Name = "flushSecretApiReporter-thread"; uploadThread.Start(); }
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); }
private T DoExecute <T>(ITopRequest <T> request, string session, DateTime timestamp) where T : TopResponse { // 提前检查业务参数 try { request.Validate(); } catch (TopException e) { return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg)); } // 添加协议级请求参数 TopDictionary txtParams = new TopDictionary(request.GetParameters()); txtParams.Add(METHOD, request.GetApiName()); txtParams.Add(VERSION, "2.0"); txtParams.Add(SIGN_METHOD, Constants.SIGN_METHOD_MD5); txtParams.Add(APP_KEY, appKey); txtParams.Add(FORMAT, format); txtParams.Add(PARTNER_ID, GetSdkVersion()); txtParams.Add(TIMESTAMP, timestamp); txtParams.Add(TARGET_APP_KEY, request.GetTargetAppKey()); txtParams.Add(SESSION, session); txtParams.AddAll(this.systemParameters); if (this.useSimplifyJson) { txtParams.Add(SIMPLIFY, "true"); } // 添加签名参数 txtParams.Add(SIGN, TopUtils.SignTopRequest(txtParams, appSecret, true)); // 添加头部参数 if (this.useGzipEncoding) { request.GetHeaderParameters()[Constants.ACCEPT_ENCODING] = Constants.CONTENT_ENCODING_GZIP; } string realServerUrl = GetServerUrl(this.serverUrl, request.GetApiName(), session); string reqUrl = webUtils.BuildGetUrl(realServerUrl, txtParams); try { string body; if (request is ITopUploadRequest <T> ) // 是否需要上传文件 { ITopUploadRequest <T> uRequest = (ITopUploadRequest <T>)request; IDictionary <string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParameters()); body = webUtils.DoPost(realServerUrl, txtParams, fileParams, request.GetHeaderParameters()); } else { body = webUtils.DoPost(realServerUrl, txtParams, request.GetHeaderParameters()); } // 解释响应结果 T rsp; if (disableParser) { rsp = Activator.CreateInstance <T>(); rsp.Body = body; } else { if (FORMAT_XML.Equals(format)) { ITopParser tp = new TopXmlParser(); rsp = tp.Parse <T>(body); } else { ITopParser tp; if (useSimplifyJson) { tp = new TopJsonSimplifyParser(); } else { tp = new TopJsonParser(); } rsp = tp.Parse <T>(body); } } // 追踪错误的请求 if (!disableTrace && rsp.IsError) { StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body); topLogger.Warn(sb.ToString()); } return(rsp); } catch (Exception e) { if (!disableTrace) { StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace); topLogger.Error(sb.ToString()); } throw e; } }
private T DoExecute <T>(IAliyunRequest <T> request, string session, DateTime timestamp) where T : AliyunResponse { // 提前检查业务参数 try { request.Validate(); } catch (TopException e) { return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg)); } // 添加协议级请求参数 TopDictionary txtParams = new TopDictionary(request.GetParameters()); txtParams.AddAll(this.systemParameters); AddCommonParams(request, txtParams); string reqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams); try { string body; if (request is IAliyunUploadRequest <T> ) // 是否需要上传文件 { IAliyunUploadRequest <T> uRequest = (IAliyunUploadRequest <T>)request; IDictionary <string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParameters()); body = webUtils.DoPost(this.serverUrl.TrimEnd('/'), txtParams, fileParams); } else { body = webUtils.DoPost(this.serverUrl.TrimEnd('/'), txtParams); } // 解释响应结果 T rsp; if (disableParser) { rsp = Activator.CreateInstance <T>(); rsp.Body = body; } else { if (FORMAT_XML.Equals(format)) { IAliyunParser tp = new AliyunXmlParser(); rsp = tp.Parse <T>(body); } else { IAliyunParser tp = new AliyunJsonParser(); rsp = tp.Parse <T>(body); } } // 追踪错误的请求 if (!disableTrace && rsp.IsError) { StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body); topLogger.Warn(sb.ToString()); } return(rsp); } catch (Exception e) { if (!disableTrace) { StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace); topLogger.Error(sb.ToString()); } throw e; } }
void ControlThread(StreamMsgConsumeFactory msgConsumeFactory, ref bool bstop, Configuration conf, TopCometStreamRequest cometReq) { long lastSleepTime = 0; while (!bstop) { if (allStop) { 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 { //错误码设置出错,停止线程 bstop = 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,stop the stream consume" + e.Message); bstop = true; try { Monitor.Enter(objLock); Monitor.PulseAll(objLock); } finally { Monitor.Exit(objLock); } } } if (currentStreamImpl != null) { try { currentStreamImpl.Close(); } catch (Exception e) { // ignore } } logger.Info("Stop stream consume"); }
static void Main(string[] args) { //DataTable dt = new DataTable(); //dt.Columns.Add("Type", typeof(int)); //dt.Columns.Add("MainNo", typeof(string)); //dt.Columns.Add("SubNo", typeof(string)); //List<TestEntity> entities = new List<TestEntity> { new TestEntity{ Type = 1, MainNo = "1", SubNo = "11" }, // new TestEntity { Type = 1, MainNo = "1", SubNo = "12"}, // new TestEntity { Type = 1, MainNo = "2", SubNo = "21"}, // new TestEntity { Type = 1, MainNo = "2", SubNo = "22"}, // new TestEntity { Type = 1, MainNo = "3", SubNo = "31"}, // new TestEntity { Type = 1, MainNo = "3", SubNo = "32"}, // new TestEntity { Type = 2, MainNo = "1", SubNo = "11"}, // new TestEntity { Type = 2, MainNo = "1", SubNo = "12"}, // new TestEntity { Type = 2, MainNo = "2", SubNo = "21"}, // new TestEntity { Type = 2, MainNo = "2", SubNo = "22"}, // new TestEntity { Type = 2, MainNo = "3", SubNo = "31"}, // new TestEntity { Type = 2, MainNo = "3", SubNo = "32"} //}; //foreach(var ent in entities) //{ // DataRow dr = dt.NewRow(); // dr["Type"] = ent.Type; // dr["MainNo"] = ent.MainNo; // dr["SubNo"] = ent.SubNo; // dt.Rows.Add(dr); //} //DataRow[] drs = dt.Select("Type = 2"); //List<string> deleteNos = new List<string>() { "1", "2" }; //foreach (var no in deleteNos) //{ // var drRemoves = from dr in drs // where dr["MainNo"].ToString() == no // select dr; // foreach (DataRow drRemove in drRemoves) // drRemove.d //} //Console.WriteLine(drs.Length); //Console.ReadKey(); //WebClient wc = new WebClient(); //wc.Encoding = Encoding.UTF8; //var html = wc.DownloadString("https://www.meet99.com/map-beijing.html"); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.meet99.com/map-beijing.html"); //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //using (Stream stream = response.GetResponseStream()) //{ // using (StreamReader sr = new StreamReader(stream)) // { // var html = sr.ReadToEnd(); // } //} // HtmlConvent<Area> convertor = ConvertHtmlToAreas; //ExtractData ed = new ExtractData(); //ed.Extract<Area>(basUrl + "/map", convertor); //Regex reg = new Regex(@"result=(-?\d+)"); //if(reg.Matches("result=0&msgid=12343545465675").Count > 0) //{ // Console.WriteLine(reg.Matches("result=0&msgid=12343545465675")[1].Value); //} //DataTable dt = new DataTable(); //dt.Columns.Add("num", typeof(int)); //for(int i = 0; i < 10; i++) //{ // DataRow dr = dt.NewRow(); // dr["num"] = i + 1; // dt.Rows.Add(dr); //} //foreach(DataRow dr in dt.Rows) //{ // dt.Rows.Remove(dr); //} //SerialPort sp = new SerialPort("COM2", 9600); ////SerialPort sp2 = new SerialPort("COM3", 9600); ////Console.WriteLine(sp2.IsOpen); ////try ////{ //// sp2.Open(); ////}catch(Exception ex) ////{ //// Console.WriteLine(ex.Message); ////} //if (!sp.IsOpen) //{ // sp.Open(); // //sp2.Open(); //} //for (int i = 0; i < 10; i++) //{ // sp.WriteLine("hello world" + i); // Thread.Sleep(3000); //} ////if (sp2.BytesToRead > 0) ////{ //// var line = sp2.ReadLine(); //// Console.WriteLine(line + "aa"); ////} //sp.Close(); ////sp2.Close(); //Console.WriteLine("结束"); ITopLogger logger = DefaultTopLogger.GetDefault(); logger.Error("123"); Console.ReadKey(); }
public override string ParseLine(string msg) { if (!string.IsNullOrEmpty(msg)) { try { Regex rg = new Regex(pattern, RegexOptions.Compiled); MatchCollection matches = rg.Matches(msg); if (matches.Count > 0) { string code = matches[0].Groups["code"].Value; if (StreamConstants.NEW_MESSAGE.Equals(code)) { return(matches[0].Groups["msg"].Value); } else if (StreamConstants.HEAT_BEAT.Equals(code)) { msgListener.OnHeartBeat(); } else if (StreamConstants.CONNECT_REACH_MAX_TIME.Equals(code)) { msgListener.OnConnectReachMaxTime(); WakeUp(code); } else if (StreamConstants.DISCARD_MESSAGE.Equals(code)) { msgListener.OnDiscardMsg(matches[0].Groups["msg"].Value.ToString()); } else if (StreamConstants.SERVER_DEPLOY.Equals(code)) { msgListener.OnServerUpgrade(matches[0].Groups["msg"].Value.ToString()); WakeUp(code); } else if (StreamConstants.SERVER_REHASH.Equals(code)) { msgListener.OnServerRehash(); WakeUp(code); } else if (StreamConstants.CLIENT_KICKOFF.Equals(code)) { msgListener.OnClientKickOff(); WakeUp(code); } else if (StreamConstants.SERVER_KICKOFF.Equals(code)) { msgListener.OnServerKickOff(); WakeUp(code); } else if (StreamConstants.CONNECT_SUCCESS.Equals(code)) { msgListener.OnConnectMsg(matches[0].Groups["msg"].Value); } else { msgListener.OnOtherMsg(matches[0].Groups["msg"].Value); } } } catch (Exception e) { logger.Error("Message is invalid:" + msg + e.Message); msgListener.OnException(e); return(null); } } return(null); }
private T DoExecute <T>(ITopRequest <T> request, string session, DateTime timestamp) where T : TopResponse { // 提前检查业务参数 try { request.Validate(); } catch (TopException e) { return(createErrorResponse <T>(e.ErrorCode, e.ErrorMsg)); } // 添加协议级请求参数 TopDictionary txtParams = new TopDictionary(request.GetParameters()); txtParams.Add(METHOD, request.GetApiName()); txtParams.Add(VERSION, "2.0"); txtParams.Add(APP_KEY, appKey); txtParams.Add(FORMAT, format); txtParams.Add(PARTNER_ID, SDK_VERSION); txtParams.Add(TIMESTAMP, timestamp); txtParams.Add(SESSION, session); txtParams.AddAll(this.systemParameters); // 添加签名参数 txtParams.Add(SIGN, TopUtils.SignTopRequest(txtParams, appSecret)); string reqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams); try { string body; if (request is ITopUploadRequest <T> ) // 是否需要上传文件 { ITopUploadRequest <T> uRequest = (ITopUploadRequest <T>)request; IDictionary <string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParameters()); body = webUtils.DoPost(this.serverUrl, txtParams, fileParams); } else { body = webUtils.DoPost(this.serverUrl, txtParams); } // 解释响应结果 T rsp; if (disableParser) { rsp = Activator.CreateInstance <T>(); rsp.Body = body; } else { if (FORMAT_XML.Equals(format)) { ITopParser tp = new TopXmlParser(); rsp = tp.Parse <T>(body); } else { ITopParser tp = new TopJsonParser(); rsp = tp.Parse <T>(body); } } // 追踪错误的请求 if (!disableTrace && rsp.IsError) { StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body); topLogger.Warn(sb.ToString()); } return(rsp); } catch (Exception e) { if (!disableTrace) { StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace); topLogger.Error(sb.ToString()); } throw e; } }