// // ServiceCollectionExtensions.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Net; using Microsoft.Extensions.DependencyInjection; using NosSmooth.Comms.Data; namespace NosSmooth.Comms.Tcp.Extensions; /// /// Extension methods for . /// public static class ServiceCollectionExtensions { /// /// Adds tcp server as . /// /// The service collection. /// A resovler used for resolving the ip and port to bind to. /// The same service collection. public static IServiceCollection AddTcpServer ( this IServiceCollection serviceCollection, Func bindResolver ) { return serviceCollection .AddSingleton(p => p.GetRequiredService()) .AddSingleton ( p => { var resolved = bindResolver(p); return new TcpServer(resolved.BindAddress, resolved.Port); } ); } /// /// Adds tcp client as . /// /// The service collection. /// A resovler used for resolving the hostname and port to connect to. /// The same service collection. public static IServiceCollection AddTcpClient ( this IServiceCollection serviceCollection, Func connectionResolver ) { return serviceCollection .AddSingleton(p => p.GetRequiredService()) .AddSingleton ( p => { var resolved = connectionResolver(p); return new TcpClient(resolved.Hostname, resolved.Port); } ); } }