private void WriteMemoryToDb() { using (DataConnection db = MySqlTools.CreateDataConnection( ConfigLoadingManager.GetInstance() .GetConfig().Database.GetConnectionString())) { try { db.BeginTransaction(IsolationLevel.Serializable); foreach (var p in ArticleCache.Memory.Values) { db.InsertOrReplace(new ArticleTable { Id = p.Id, Time = (ulong)p.Time, Info = JsonConvert.SerializeObject(p) }); } } finally { db.CommitTransaction(); } } }
static private void UpdateToSource(uint articleId, ArticleInfo articleInfo) { var newDbElement = new ArticleTable { Id = articleInfo.Id, Time = (ulong)articleInfo.Time, Info = JsonConvert.SerializeObject(articleInfo) }; using (DataConnection db = MySqlTools.CreateDataConnection( ConfigLoadingManager.GetInstance() .GetConfig().Database.GetConnectionString())) { try { db.BeginTransaction(IsolationLevel.Serializable); var query = (from p in db.GetTable <ArticleTable>() where p.Id == newDbElement.Id select p); var isExists = query.Count() != 0; if (!isExists) { return; } db.InsertOrReplace(newDbElement); } finally { db.CommitTransaction(); } } }
static private void RemoveFromSource(uint articleId) { using (DataConnection db = MySqlTools.CreateDataConnection( ConfigLoadingManager.GetInstance() .GetConfig().Database.GetConnectionString())) { db.Delete(new ArticleTable { Id = (uint)articleId }); } }
//身份验证↓ private Reasons VerifyRequest(HttpRequest r, X509Certificate2 c) { string signature = null; long timeStamp; if (!VerificationHelper.GetHeaders(r, out signature, out timeStamp)) { return(Reasons.HeaderMissing); } return(VerificationHelper.VerifyRequest(ReadyForVerify(r.QueryString, timeStamp), signature, timeStamp, ConfigLoadingManager.GetInstance().GetConfig().MaxAllowedStampDifference, c)); }
private bool connectToBackend() { try { this.closeConn(); this.toCompilerApi = new Socket(SocketType.Stream, ProtocolType.Tcp); this.toCompilerApi.Connect(IPAddress.Parse("127.0.0.1"), ConfigLoadingManager.GetInstance().GetConfig().CompilerPort); } catch (SocketException) { return(false); } return(true); }
//Cache callbacks↓ private void LoadAllToCache() { using (DataConnection db = MySqlTools.CreateDataConnection( ConfigLoadingManager.GetInstance() .GetConfig().Database.GetConnectionString())) { var query = from p in db.GetTable <ArticleTable>() select p; foreach (var p in query) { var article = JsonConvert.DeserializeObject <ArticleInfo>(p.Info); article.Id = p.Id; article.Time = (long)p.Time; this.ArticleCache.Memory.TryAdd(article.Id, article); } } }
static private ArticleInfo GetFromSource(uint articleId) { using (DataConnection db = MySqlTools.CreateDataConnection( ConfigLoadingManager.GetInstance() .GetConfig().Database.GetConnectionString())) { var query = from p in db.GetTable <ArticleTable>() where p.Id == articleId select p; if (query.Count() == 0) { return(null); } var columns = query.First(); var article = JsonConvert.DeserializeObject <ArticleInfo>(columns.Info); article.Id = columns.Id; article.Time = (long)columns.Time; return(article); } }
public HttpResponse OnRequest(HttpRequest r) { if (r.Path == "/article/latest" && r.Method == HttpMethod.Get) { return(GetLatest(r)); } if (r.Path.StartsWith("/article")) { if (r.Method == HttpMethod.Get) { return(GetSingle(r)); } if (r.Method == HttpMethod.Post && r.Path == "/article") { var verificationCertificate = new X509Certificate2(ConfigLoadingManager.GetInstance().GetConfig().VerificationCertificate, ConfigLoadingManager.GetInstance().GetConfig().VerificationCertificatePassword); string action; if (!r.QueryString.TryGetValue("action", out action)) { return(HttpResponse.BadRequest); } switch (action) { case "new": return(NewOrReplaceArticle(r, verificationCertificate, false)); case "replace": return(NewOrReplaceArticle(r, verificationCertificate, true)); case "delete": return(DeleteArticle(r, verificationCertificate)); } } } return(HttpResponse.BadRequest); }
static void RunServer() { var config = ConfigLoadingManager.GetInstance().GetConfig(); SortedDictionary <string, RouteHandler> routeHandlers = new SortedDictionary <string, RouteHandler>(); routeHandlers.Add("/article/latest", ArticleHandler.GetInstance()); routeHandlers.Add("/article", ArticleHandler.GetInstance()); routeHandlers.Add("/interpreter", InterpreterHandler.GetInstance()); SortedList <string, RouteHandler> regexRouteHandlers = new SortedList <string, RouteHandler>(); regexRouteHandlers.Add(@"\/article\/\d+$", ArticleHandler.GetInstance()); LogManager exceptionLogger = new LogManager(ConfigLoadingManager.GetInstance().GetConfig().ExceptionLogFile); LogManager accessLogger = new LogManager(ConfigLoadingManager.GetInstance().GetConfig().AccessLogFile); ExecuteRouteHandler executeRouteHandler = new ExecuteRouteHandler { RouteHandlers = routeHandlers, RegexRouteHandlers = regexRouteHandlers, ExceptionLogger = exceptionLogger, AccessLogger = accessLogger }; HttpRequestDispatcher httpDispatcher = null; HttpRequestDispatcher httpsDispatcher = null; if (config.HttpListenAddress.IsAvailable()) { httpDispatcher = new HttpRequestDispatcher(); httpDispatcher.Start(config.HttpListenAddress.IP, config.HttpListenAddress.Port, config.SessionReadBufferSize, config.SessionNoActionTimeout, executeRouteHandler.HttpRequestHandler, executeRouteHandler.InternalServerError, exceptionLogger); Console.WriteLine("Http Server - " + Environment.NewLine + config.HttpListenAddress.IP + ":" + config.HttpListenAddress.Port); } if (config.HttpsListenAddress.IsAvailable()) { httpsDispatcher = new HttpRequestDispatcher(); httpsDispatcher.Start(config.HttpsListenAddress.IP, config.HttpsListenAddress.Port, config.SessionReadBufferSize, config.SessionNoActionTimeout, new X509Certificate2(config.HttpsPfxCertificate, config.HttpsPfxCertificatePassword), executeRouteHandler.HttpRequestHandler, executeRouteHandler.InternalServerError, exceptionLogger); Console.WriteLine("Https Server - " + Environment.NewLine + config.HttpsListenAddress.IP + ":" + config.HttpsListenAddress.Port); } if (httpDispatcher == null && httpsDispatcher == null) { return; } exceptionLogger.LogAsync("startup successfully"); bool stopFlag = false; object mut = new object(); new Thread(() => { const int second = 5; Thread.Sleep(second * 1000); while (true) { Console.Clear(); Console.Write("Active Session: " + (httpDispatcher.SessionCount + httpsDispatcher.SessionCount).ToString()); lock (mut) { for (int i = 0; i < second; i++) { if (stopFlag) { return; } Console.Write('.'); Monitor.Wait(mut, 1000); if (stopFlag) { return; } } } } }).Start(); Console.WriteLine("press any key to shut down..."); Console.ReadKey(); lock (mut) { stopFlag = true; Monitor.Pulse(mut); } //stop dispatcher if (httpDispatcher != null) { httpDispatcher.Stop(); } if (httpsDispatcher != null) { httpsDispatcher.Stop(); } //stop logic ArticleHandler.GetInstance().Stop(); exceptionLogger.LogAsync("stopped"); //stop logger exceptionLogger.Stop(); accessLogger.Stop(); }