Merge branch 'SignalR' of s421801/Squirrowse into master

This commit is contained in:
Daniel Grabowski 2019-11-04 08:04:00 +00:00 committed by Gogs
commit 8c92364785
22 changed files with 395 additions and 33 deletions

View File

@ -1,12 +1,26 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Squirrowse.Client.Service;
namespace Squirrowse.Client
{
class Program
public class Program
{
static void Main(string[] args)
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddTransient<IConnectionManager, ConnectionManager>();//keep as transient for now
});
}
}

View File

@ -0,0 +1,10 @@
{
"profiles": {
"Squirrowse.Client": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
namespace Squirrowse.Client.Service
{
public class ConnectionManager : IConnectionManager
{
private readonly HubConnection _connection;
public ConnectionManager(string url, int port)
{
_connection = new HubConnectionBuilder()
.WithUrl($"{url}:{port}/StreamHub")
.AddMessagePackProtocol()
.WithAutomaticReconnect()
.Build();
}
public HubConnection EstablishHubConnection() => _connection;
}
}

View File

@ -0,0 +1,10 @@
using Microsoft.AspNetCore.SignalR.Client;
namespace Squirrowse.Client.Service
{
public interface IConnectionManager
{
HubConnection EstablishHubConnection();
}
}

View File

@ -1,8 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>dotnet-Squirrowse.Client-D6805387-040A-46DF-9DAE-926B46C981A6</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.0.0" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Squirrowse.Client
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -0,0 +1,34 @@
using FluentAssertions;
using OpenCvSharp;
using Squirrowse.Core.Services;
using Xunit;
namespace Squirrowse.Core.Tests
{
public class ImgExtensionTests
{
private readonly Mat sampleMat = new Mat(500, 600, MatType.CV_8UC3);
[SkippableFact]
public void ByteShouldBeConvertedToMat()
{
//
Skip.If(true, "Cannot use fluent assertion in this kind of test (compare pointer to obj)");
//
var bytes = sampleMat.ConvertToJpgByte();
var reMet = bytes.ConvertByteToMat();
reMet.Should().BeEquivalentTo(sampleMat);
}
[Fact]
public void MatShouldBeConvertedToByteArr()
{
var newByteArr = sampleMat.ConvertToJpgByte();
newByteArr.Should().BeOfType(typeof(byte[]));
newByteArr.Should().NotBeNullOrEmpty();
}
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
<PackageReference Include="Xunit.SkippableFact" Version="1.3.12" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Squirrowse.Core\Squirrowse.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,16 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Squirrowse.Core
{
public static class CoreModule
{
public static IServiceCollection AddCoreModule(this IServiceCollection services)
{
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
return services;
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace Squirrowse.Client.Models
{
public class VideoFrame
{
public byte[] FrameBytes { get; set; }
public DateTime TimeStamp => DateTime.Now;
public Guid id => Guid.NewGuid();
public string Issuer { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System.Linq;
using System.Runtime.CompilerServices;
using OpenCvSharp;
namespace Squirrowse.Core.Services
{
public static class ImgExtensions
{
public static byte[] ConvertToJpgByte(this Mat mat)
{
Cv2.ImEncode(".jpg", mat, out var imgbuffer);//no need to dispose
return imgbuffer.Any() ? imgbuffer : new byte[] { };
}
public static Mat ConvertByteToMat(this byte[] bytearr)
{
using var tempMat = Cv2.ImDecode(bytearr, ImreadModes.Unchanged); //keep as disposable
return tempMat ?? new Mat();
}
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Squirrowse.Service.Hubs
{
public interface IStreamHub
{
Task UploadByteStream(IAsyncEnumerable<byte[]> stream);
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using NLog;
namespace Squirrowse.Service.Hubs
{
public class StreamHub : Hub, IStreamHub
{
private readonly ILogger<StreamHub> logger;
public StreamHub(ILogger<StreamHub> logger)
{
this.logger = logger;
}
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
{
await foreach (var frame in stream)
{
logger.LogInformation($"Got frame size: {frame.Length} ");
await Task.Delay(100); //leave some delay for debug purpose
}
}
}
}

View File

@ -1,26 +1,68 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using NLog.Web;
namespace Squirrowse.Service
{
public class Program
{
public static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
Configuration = CreateConfigurationBuilder().Build();
var logger = GetLogger();
try
{
logger.Debug("Application started");
CreateHostBuilder(args)
.Build()
.Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception when building WebHost");
throw;
}
finally
{
LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseConfiguration(Configuration);
webBuilder.UseNLog();
});
}
private static IConfigurationBuilder CreateConfigurationBuilder()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environment}.json", false, true);
}
private static ILogger GetLogger()
{
var nlogConfigSection = Configuration.GetSection("NLog");
LogManager.Configuration = new NLogLoggingConfiguration(nlogConfigSection);
ILogger logger = LogManager.GetCurrentClassLogger();
return logger;
}
}
}

View File

@ -11,8 +11,8 @@
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchBrowser": false,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
@ -8,4 +8,14 @@
<Folder Include="Controllers\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="7.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="3.0.0-*" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.0.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
<PackageReference Include="System.Reactive.Linq" Version="4.0.0" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup>
</Project>

View File

@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Squirrowse.Service.Hubs;
namespace Squirrowse.Service
{
@ -26,15 +22,16 @@ namespace Squirrowse.Service
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
services.AddSignalR()
.AddHubOptions<StreamHub>(opt => opt.MaximumReceiveMessageSize = 102400000)//~100mb per frame instead of 32kb default
.AddMessagePackProtocol();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
@ -45,7 +42,8 @@ namespace Squirrowse.Service
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<StreamHub>($"{nameof(StreamHub)}");
});
}
}
}
}

View File

@ -6,5 +6,36 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"NLog": {
"autoreload": true,
"throwConfigExceptions": true,
"variables": {
"logDirectory": "${basedir}/logs"
},
"targets": {
"FileLogger": {
"type": "AsyncWrapper",
"target": {
"wrappedFile": {
"type": "File",
"fileName": "${logDirectory}/${machinename}.Log.txt",
"layout": "${longdate} ${level}: ${message} ${exception:format=tostring}",
"archiveFileName": "${logDirectory}/archives/${machinename}.Log_{#}.txt",
"archiveDateFormat": "yyyy-MM-dd",
"archiveAboveSize": "5242880",
"archiveEvery": "Day",
"archiveNumbering": "DateAndSequence",
"maxArchiveFiles": "90"
}
}
}
},
"rules": [
{
"logger": "*",
"writeTo": "FileLogger"
}
]
},
"AllowedHosts": "*"
}
}

View File

@ -3,9 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Squirrowse.Service", "Squirrowse.Service\Squirrowse.Service.csproj", "{8C085621-BAAA-4E96-B027-561BC18751EE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Squirrowse.Service", "Squirrowse.Service\Squirrowse.Service.csproj", "{8C085621-BAAA-4E96-B027-561BC18751EE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Squirrowse.Client", "Squirrowse.Client\Squirrowse.Client.csproj", "{A523B8A9-958B-45EC-B5E4-54FDF08C1C5D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Squirrowse.Client", "Squirrowse.Client\Squirrowse.Client.csproj", "{558A5917-6AD3-4C40-ACA1-EE3B8B8927C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Squirrowse.Core", "Squirrowse.Core\Squirrowse.Core.csproj", "{D0989FCC-484E-4ADB-BA5E-1020894F9C09}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{BB569A06-F4D1-4927-87AB-86C4BD2AFBC5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Squirrowse.Core.Tests", "Squirrowse.Core.Tests\Squirrowse.Core.Tests.csproj", "{CFA96677-EAA7-4871-AAD8-E6336973366F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -17,14 +23,25 @@ Global
{8C085621-BAAA-4E96-B027-561BC18751EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C085621-BAAA-4E96-B027-561BC18751EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C085621-BAAA-4E96-B027-561BC18751EE}.Release|Any CPU.Build.0 = Release|Any CPU
{A523B8A9-958B-45EC-B5E4-54FDF08C1C5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A523B8A9-958B-45EC-B5E4-54FDF08C1C5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A523B8A9-958B-45EC-B5E4-54FDF08C1C5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A523B8A9-958B-45EC-B5E4-54FDF08C1C5D}.Release|Any CPU.Build.0 = Release|Any CPU
{558A5917-6AD3-4C40-ACA1-EE3B8B8927C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{558A5917-6AD3-4C40-ACA1-EE3B8B8927C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{558A5917-6AD3-4C40-ACA1-EE3B8B8927C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{558A5917-6AD3-4C40-ACA1-EE3B8B8927C8}.Release|Any CPU.Build.0 = Release|Any CPU
{D0989FCC-484E-4ADB-BA5E-1020894F9C09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0989FCC-484E-4ADB-BA5E-1020894F9C09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0989FCC-484E-4ADB-BA5E-1020894F9C09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0989FCC-484E-4ADB-BA5E-1020894F9C09}.Release|Any CPU.Build.0 = Release|Any CPU
{CFA96677-EAA7-4871-AAD8-E6336973366F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFA96677-EAA7-4871-AAD8-E6336973366F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFA96677-EAA7-4871-AAD8-E6336973366F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFA96677-EAA7-4871-AAD8-E6336973366F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CFA96677-EAA7-4871-AAD8-E6336973366F} = {BB569A06-F4D1-4927-87AB-86C4BD2AFBC5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1CC5C3B8-3825-4EB5-ACFF-73B4CAC8945D}
EndGlobalSection