public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { //loggerFactory.AddConsole(Microsoft.Extensions.Logging.LogLevel.Trace); app.UseCors("AllowLocalhost8080"); var webSocketOptions = new WebSocketOptions() { KeepAliveInterval = TimeSpan.FromSeconds(60), ReceiveBufferSize = 4 * 1024 }; app.UseWebSockets(webSocketOptions); var options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("App/index.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); app.Run((context) => { var promise = new TaskCompletionSource <bool>(); theSyncContext.Post(_ => { Task task = theModule.HandleClientRequest(context); task.ContinueWith(completedTask => promise.CompleteFromTask(completedTask)); }, null); return(promise.Task); }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { //loggerFactory.AddConsole(Microsoft.Extensions.Logging.LogLevel.Trace); app.Use(async(context, nextMiddleware) => { string path = context.Request.Path; if (path == "/") { context.Response.OnStarting(() => { context.Response.Headers.Add("Expires", (DateTime.UtcNow + TimeSpan.FromMinutes(1)).ToString("r")); return(Task.CompletedTask); }); } await nextMiddleware(); }); app.UseCors("AllowLocalhost8080"); var webSocketOptions = new WebSocketOptions() { KeepAliveInterval = TimeSpan.FromSeconds(60), ReceiveBufferSize = 4 * 1024 }; app.UseWebSockets(webSocketOptions); string regex = $"^{theModule!.BundlesPrefix}/(.*)"; var rewriteOptions = new RewriteOptions().AddRewrite(regex, "/$1", skipRemainingRules: true); app.UseRewriter(rewriteOptions); var options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("App/index.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); app.Run((context) => { var promise = new TaskCompletionSource <bool>(); theSyncContext !.Post(_ => { Task task = theModule.HandleClientRequest(context); task.ContinueWith(completedTask => promise.CompleteFromTask(completedTask)); }, null); return(promise.Task); }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { //loggerFactory.AddConsole(Microsoft.Extensions.Logging.LogLevel.Trace); var webSocketOptions = new WebSocketOptions() { KeepAliveInterval = TimeSpan.FromSeconds(60), ReceiveBufferSize = 8 * 1024 }; app.UseWebSockets(webSocketOptions); app.Run((context) => { var promise = new TaskCompletionSource <bool>(); theSyncContext !.Post(_ => { Task task = theCore !.HandleClientRequest(context); task.ContinueWith(completedTask => promise.CompleteFromTask(completedTask)); }, null); return(promise.Task); }); }
public static async Task MakeConfigRecTask(MqttConfig config, ModuleInitInfo info, string certDir, Func <bool> shutdown) { SynchronizationContext theSyncContext = SynchronizationContext.Current !; var mqttOptions = MakeMqttOptions(certDir, config, "ConfigRec"); var configRec = config.ConfigReceive !; string topic = (string.IsNullOrEmpty(config.TopicRoot) ? "" : config.TopicRoot + "/") + configRec.Topic; Connection clientFAST = await EnsureConnectOrThrow(info, null); while (!shutdown()) { IMqttClient?clientMQTT = await EnsureConnect(mqttOptions, null);; if (clientMQTT == null) { Console.WriteLine("Can not connect to MQTT broker"); await Task.Delay(5000); continue; } clientFAST = await EnsureConnectOrThrow(info, clientFAST); var reader = new LargePayloadReader(configRec.MaxBuckets); clientMQTT.UseApplicationMessageReceivedHandler((arg) => { var promise = new TaskCompletionSource <bool>(); theSyncContext !.Post(_ => { Task task = OnReceivedConfigWriteRequest(clientMQTT, reader, configRec.ModuleID, topic, clientFAST, arg); task.ContinueWith(completedTask => promise.CompleteFromTask(completedTask)); }, null); return(promise.Task); }); var topics = GetTopicsToSubscribe(topic, bucktesCount: configRec.MaxBuckets); foreach (var top in topics) { Console.WriteLine($"Subscribing to topic {top.Topic}"); await clientMQTT.SubscribeAsync(top); } while (!shutdown()) { try { await clientMQTT.PingAsync(CancellationToken.None); } catch (Exception exp) { Exception e = exp.GetBaseException() ?? exp; Console.Error.WriteLine($"MakeConfigRecTask: Connection broken during Ping. Trying to reconnect. Err: {e.Message}"); break; } try { clientFAST = await EnsureConnectOrThrow(info, clientFAST); } catch (Exception) { Console.Error.WriteLine("Connection to FAST core broken. Trying to reconnect..."); break; } await Time.WaitUntil(Timestamp.Now + Duration.FromSeconds(6), abort : shutdown); } await CloseIntern(clientMQTT); } }
public static async Task MakeVarRecTask(MqttConfig config, ModuleInitInfo info, string certDir, Func <bool> shutdown) { SynchronizationContext theSyncContext = SynchronizationContext.Current !; var mqttOptions = MakeMqttOptions(certDir, config, "VarRec"); var varRec = config.VarReceive !; string topic = (string.IsNullOrEmpty(config.TopicRoot) ? "" : config.TopicRoot + "/") + varRec.Topic; bool configChanged = false; Action onConfigChanged = () => { Console.WriteLine("MakeVarRecTask: onConfigChanged called"); configChanged = true; }; Connection clientFAST = await EnsureConnectOrThrow(info, null, onConfigChanged, varRec.ModuleID); while (!shutdown()) { IMqttClient?clientMQTT = await EnsureConnect(mqttOptions, null);; if (clientMQTT == null) { Console.WriteLine("Can not connect to MQTT broker"); await Task.Delay(5000); continue; } clientFAST = await EnsureConnectOrThrow(info, clientFAST, onConfigChanged, varRec.ModuleID); List <ObjectInfo> objs = await clientFAST.GetAllObjects(varRec.ModuleID); ObjectInfo[] writableObjs = objs.Where(obj => obj.Variables.Any(v => v.Writable)).ToArray(); MqttTopicFilter[] topics = writableObjs.Select(obj => new MqttTopicFilter() { QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce, Topic = $"{topic}/{obj.ID.LocalObjectID}" }).ToArray(); clientMQTT.UseApplicationMessageReceivedHandler((arg) => { var promise = new TaskCompletionSource <bool>(); theSyncContext !.Post(_ => { Task task = OnReceivedVarWriteRequest(varRec, clientFAST, arg); task.ContinueWith(completedTask => promise.CompleteFromTask(completedTask)); }, null); return(promise.Task); }); foreach (var top in topics) { Console.WriteLine($"Subscribing to topic {top.Topic}"); await clientMQTT.SubscribeAsync(top); } while (!configChanged && !shutdown()) { try { await clientMQTT.PingAsync(CancellationToken.None); } catch (Exception exp) { Exception e = exp.GetBaseException() ?? exp; Console.Error.WriteLine($"MakeVarRecTask: Connection broken during Ping. Trying to reconnect. Err: {e.Message}"); break; } try { clientFAST = await EnsureConnectOrThrow(info, clientFAST, onConfigChanged, varRec.ModuleID); } catch (Exception) { Console.Error.WriteLine("Connection to FAST core broken. Trying to reconnect..."); break; } await Time.WaitUntil(Timestamp.Now + Duration.FromSeconds(6), abort : () => configChanged || shutdown()); } configChanged = false; await CloseIntern(clientMQTT); } }