-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- File scoped namepaces, collection expressions, target type new, top level statements, IHostApplicationBuilder
- Loading branch information
Showing
44 changed files
with
1,946 additions
and
2,281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,55 @@ | ||
using System; | ||
using System.Net; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Threading.Tasks; | ||
using Bedrock.Framework; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using ServerApplication; | ||
|
||
namespace ServerApplication | ||
{ | ||
public partial class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
// Manual wire up of the server | ||
var services = new ServiceCollection(); | ||
services.AddLogging(builder => | ||
{ | ||
builder.SetMinimumLevel(LogLevel.Debug); | ||
builder.AddConsole(); | ||
}); | ||
|
||
services.AddSignalR(); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var server = new ServerBuilder(serviceProvider) | ||
.UseSockets(sockets => | ||
{ | ||
// Echo server | ||
sockets.ListenLocalhost(5000, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<EchoServerApplication>()); | ||
var builder = Host.CreateApplicationBuilder(); | ||
|
||
// HTTP/1.1 server | ||
sockets.Listen(IPAddress.Loopback, 5001, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<HttpApplication>()); | ||
builder.Logging.SetMinimumLevel(LogLevel.Debug); | ||
|
||
// SignalR Hub | ||
sockets.Listen(IPAddress.Loopback, 5002, | ||
builder => builder.UseConnectionLogging().UseHub<Chat>()); | ||
builder.Services.AddSignalR(); | ||
|
||
// MQTT application | ||
sockets.Listen(IPAddress.Loopback, 5003, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<MqttApplication>()); | ||
|
||
// Echo Server with TLS | ||
sockets.Listen(IPAddress.Loopback, 5004, | ||
builder => builder.UseServerTls(options => | ||
{ | ||
options.LocalCertificate = X509CertificateLoader.LoadPkcs12FromFile("testcert.pfx", "testcert"); | ||
|
||
// NOTE: Do not do this in a production environment | ||
options.AllowAnyRemoteCertificate(); | ||
}) | ||
.UseConnectionLogging().UseConnectionHandler<EchoServerApplication>()); | ||
builder.ConfigureServer(server => | ||
{ | ||
server.UseSockets(sockets => | ||
{ | ||
// Echo server | ||
sockets.ListenLocalhost(5000, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<EchoServerApplication>()); | ||
|
||
sockets.Listen(IPAddress.Loopback, 5005, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<MyCustomProtocol>()); | ||
}) | ||
.Build(); | ||
// HTTP/1.1 server | ||
sockets.Listen(IPAddress.Loopback, 5001, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<HttpApplication>()); | ||
|
||
var logger = serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger<Program>(); | ||
// SignalR Hub | ||
sockets.Listen(IPAddress.Loopback, 5002, | ||
builder => builder.UseConnectionLogging().UseHub<Chat>()); | ||
|
||
await server.StartAsync(); | ||
// MQTT application | ||
sockets.Listen(IPAddress.Loopback, 5003, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<MqttApplication>()); | ||
|
||
foreach (var ep in server.EndPoints) | ||
// Echo Server with TLS | ||
sockets.Listen(IPAddress.Loopback, 5004, | ||
builder => builder.UseServerTls(options => | ||
{ | ||
logger.LogInformation("Listening on {EndPoint}", ep); | ||
} | ||
options.LocalCertificate = X509CertificateLoader.LoadPkcs12FromFile("testcert.pfx", "testcert"); | ||
|
||
var tcs = new TaskCompletionSource<object>(); | ||
Console.CancelKeyPress += (sender, e) => | ||
{ | ||
tcs.TrySetResult(null); | ||
e.Cancel = true; | ||
}; | ||
// NOTE: Do not do this in a production environment | ||
options.AllowAnyRemoteCertificate(); | ||
}) | ||
.UseConnectionLogging().UseConnectionHandler<EchoServerApplication>()); | ||
|
||
sockets.Listen(IPAddress.Loopback, 5005, | ||
builder => builder.UseConnectionLogging().UseConnectionHandler<MyCustomProtocol>()); | ||
}); | ||
}); | ||
|
||
await tcs.Task; | ||
var host = builder.Build(); | ||
|
||
await server.StopAsync(); | ||
} | ||
} | ||
} | ||
host.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11
src/Bedrock.Framework/Hosting/ServerHostedServiceOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
namespace Bedrock.Framework; | ||
|
||
namespace Bedrock.Framework | ||
public class ServerHostedServiceOptions | ||
{ | ||
public class ServerHostedServiceOptions | ||
{ | ||
public ServerBuilder ServerBuilder { get; set; } | ||
} | ||
public ServerBuilder ServerBuilder { get; set; } | ||
} |
Oops, something went wrong.