-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!(core): redesign all api. (#12)
- Loading branch information
1 parent
148fcc7
commit bc06b2a
Showing
43 changed files
with
694 additions
and
864 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using HandyIpc.Core; | ||
|
||
namespace HandyIpc | ||
{ | ||
public class ContainerClientBuilder : IClientConfiguration | ||
{ | ||
private Func<IClient> _clientFactory = () => throw new InvalidOperationException( | ||
$"Must invoke the {nameof(IServerConfiguration)}.Use(Func<{nameof(IClient)}> factory) method " + | ||
"to register a factory before invoking the Build method."); | ||
private Func<ISerializer> _serializerFactory = () => throw new InvalidOperationException( | ||
$"Must invoke the {nameof(IServerConfiguration)}.Use(Func<{nameof(ISerializer)}> factory) method " + | ||
"to register a factory before invoking the Build method."); | ||
private Func<ILogger> _loggerFactory = () => new DebugLogger(); | ||
|
||
public IClientConfiguration Use(Func<ISerializer> factory) | ||
{ | ||
_serializerFactory = factory; | ||
return this; | ||
} | ||
|
||
public IClientConfiguration Use(Func<ILogger> factory) | ||
{ | ||
_loggerFactory = factory; | ||
return this; | ||
} | ||
|
||
public IClientConfiguration Use(Func<IClient> factory) | ||
{ | ||
_clientFactory = factory; | ||
return this; | ||
} | ||
|
||
public IContainerClient Build() | ||
{ | ||
return new ContainerClient(new Sender(_clientFactory()), _serializerFactory()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HandyIpc | ||
{ | ||
public static class ContainerClientExtensions | ||
{ | ||
public static T Resolve<T>(this IContainerClient client) => client.Resolve<T>(typeof(T).GetDefaultKey()); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
|
||
namespace HandyIpc | ||
{ | ||
public static class ContainerRegistryExtensions | ||
{ | ||
public static IContainerRegistry Register<TInterface, TImpl>(this IContainerRegistry registry, string? key = null) | ||
where TInterface : class | ||
where TImpl : TInterface, new() | ||
{ | ||
return registry.Register<TInterface>(() => new TImpl(), key); | ||
} | ||
|
||
public static IContainerRegistry Register<TInterface>(this IContainerRegistry registry, Func<TInterface> factory, string? key = null) | ||
where TInterface : class | ||
{ | ||
key ??= typeof(TInterface).GetDefaultKey(); | ||
return registry.Register(typeof(TInterface), factory, key); | ||
} | ||
|
||
public static IContainerRegistry Register(this IContainerRegistry registry, Type interfaceType, Type classType, string? key = null) | ||
{ | ||
key ??= interfaceType.GetDefaultKey(); | ||
return classType.ContainsGenericParameters | ||
? registry.Register(interfaceType, GenericFactory, key) | ||
: registry.Register(interfaceType, () => Activator.CreateInstance(classType), key); | ||
|
||
// Local Method | ||
object GenericFactory(Type[] genericTypes) | ||
{ | ||
var constructedClassType = classType.MakeGenericType(genericTypes); | ||
return Activator.CreateInstance(constructedClassType); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using HandyIpc.Core; | ||
|
||
namespace HandyIpc | ||
{ | ||
internal sealed class ContainerServer : IContainerServer | ||
{ | ||
private readonly IServer _server; | ||
private readonly Middleware _middleware; | ||
private readonly ISerializer _serializer; | ||
private readonly ILogger _logger; | ||
|
||
private CancellationTokenSource? _cancellationTokenSource; | ||
|
||
public bool IsRunning { get; private set; } | ||
|
||
public ContainerServer(IServer server, Middleware middleware, ISerializer serializer, ILogger logger) | ||
{ | ||
_server = server; | ||
_middleware = middleware; | ||
_serializer = serializer; | ||
_logger = logger; | ||
} | ||
|
||
public void Start() | ||
{ | ||
if (_cancellationTokenSource is null or { IsCancellationRequested: true }) | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
} | ||
|
||
#pragma warning disable 4014 | ||
// Async run the server without waiting. | ||
StartAsync(_cancellationTokenSource.Token); | ||
#pragma warning restore 4014 | ||
|
||
IsRunning = true; | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_cancellationTokenSource?.Cancel(); | ||
IsRunning = false; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Stop(); | ||
_server.Dispose(); | ||
} | ||
|
||
private async Task StartAsync(CancellationToken token) | ||
{ | ||
while (!token.IsCancellationRequested) | ||
{ | ||
IConnection connection = await _server.WaitForConnectionAsync(); | ||
RequestHandler handler = _middleware.ToHandler(_serializer, _logger); | ||
|
||
if (token.IsCancellationRequested) | ||
{ | ||
break; | ||
} | ||
|
||
// Do not await the request handler, and go to await next stream connection directly. | ||
#pragma warning disable 4014 | ||
HandleRequestAsync(connection, handler, token); | ||
#pragma warning restore 4014 | ||
} | ||
} | ||
|
||
private async Task HandleRequestAsync(IConnection connection, RequestHandler handler, CancellationToken token) | ||
{ | ||
try | ||
{ | ||
while (true) | ||
{ | ||
if (token.IsCancellationRequested) | ||
{ | ||
break; | ||
} | ||
|
||
byte[] buffer = await connection.ReadAsync(token); | ||
if (buffer.Length == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
byte[] output = await handler(buffer); | ||
await connection.WriteAsync(output, token); | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// Ignore | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.Error("Unexpected exception occurred when starting the server instance.", e); | ||
} | ||
finally | ||
{ | ||
connection.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.