Skip to content

Commit

Permalink
Merge pull request #109 from Cysharp/feature/ServiceBuilderApi
Browse files Browse the repository at this point in the history
Switch to builder style API
  • Loading branch information
hadashiA authored Mar 29, 2024
2 parents 9e5aa69 + db866b5 commit bad9720
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 363 deletions.
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,8 @@ use `AddMessagePipeRedis` to enable redis provider.
Host.CreateDefaultBuilder()
.ConfigureServices((ctx, services) =>
{
services.AddMessagePipe();
services.AddMessagePipeRedis(IConnectionMultiplexer | IConnectionMultiplexerFactory, configure);
services.AddMessagePipe()
.AddRedis(IConnectionMultiplexer | IConnectionMultiplexerFactory, configure);
})
```

Expand Down Expand Up @@ -854,16 +854,16 @@ Host.CreateDefaultBuilder()
{
var config = ctx.Configuration.Get<MyConfig>();

services.AddMessagePipe();
var builder = services.AddMessagePipe();
if (config.IsLocal)
{
// use in-memory IDistributedPublisher/Subscriber in local.
services.AddInMemoryDistributedMessageBroker();
builder.AddInMemoryDistributedMessageBroker();
}
else
{
// use Redis IDistributedPublisher/Subscriber
services.AddMessagePipeRedis();
builder.AddRedis();
}
});
```
Expand All @@ -876,18 +876,18 @@ For the interprocess(NamedPipe/UDP/TCP) Pub/Sub(IPC), you can use `IDistributedP
MessagePipe.Interprocess is also exsits on Unity(except NamedPipe).

use `AddMessagePipeUdpInterprocess`, `AddMessagePipeTcpInterprocess`, `AddMessagePipeNamedPipeInterprocess`, `AddMessagePipeUdpInterprocessUds`, `AddMessagePipeTcpInterprocessUds` to enable interprocess provider(Uds is Unix domain socket, most performant option).
use `AddUdpInterprocess`, `AddTcpInterprocess`, `AddNamedPipeInterprocess`, `AddUdpInterprocessUds`, `AddTcpInterprocessUds` to enable interprocess provider(Uds is Unix domain socket, most performant option).

```csharp
Host.CreateDefaultBuilder()
.ConfigureServices((ctx, services) =>
{
services.AddMessagePipe();
services.AddMessagePipeUdpInterprocess("127.0.0.1", 3215, configure); // setup host and port.
// services.AddMessagePipeTcpInterprocess("127.0.0.1", 3215, configure);
// services.AddMessagePipeNamedPipeInterprocess("messagepipe-namedpipe", configure);
// services.AddMessagePipeUdpInterprocessUds("domainSocketPath")
// services.AddMessagePipeTcpInterprocessUds("domainSocketPath")
services.AddMessagePipe()
.AddUdpInterprocess("127.0.0.1", 3215, configure); // setup host and port.
// .AddTcpInterprocess("127.0.0.1", 3215, configure);
// .AddNamedPipeInterprocess("messagepipe-namedpipe", configure);
// .AddUdpInterprocessUds("domainSocketPath")
// .AddTcpInterprocessUds("domainSocketPath")
})
```

Expand Down Expand Up @@ -919,7 +919,7 @@ Tcp has no such restrictions and is the most flexible of all the options.
In default uses [MessagePack for C#](https://github.com/neuecc/MessagePack-CSharp)'s `ContractlessStandardResolver` for message serialization. You can change to use other `MessagePackSerializerOptions` by MessagePipeInterprocessOptions.MessagePackSerializerOptions.

```csharp
services.AddMessagePipeUdpInterprocess("127.0.0.1", 3215, options =>
builder.AddUdpInterprocess("127.0.0.1", 3215, options =>
{
// You can configure other options, `InstanceLifetime` and `UnhandledErrorHandler`.
options.MessagePackSerializerOptions = StandardResolver.Options;
Expand All @@ -932,11 +932,11 @@ For IPC-RPC, you can use `IRemoteRequestHandler<in TRequest, TResponse>` that in
Host.CreateDefaultBuilder()
.ConfigureServices((ctx, services) =>
{
services.AddMessagePipe();
services.AddMessagePipeTcpInterprocess("127.0.0.1", 3215, x =>
{
x.HostAsServer = true; // if remote process as server, set true(otherwise false(default)).
});
services.AddMessagePipe()
.AddTcpInterprocess("127.0.0.1", 3215, x =>
{
x.HostAsServer = true; // if remote process as server, set true(otherwise false(default)).
});
});
```

Expand Down Expand Up @@ -975,16 +975,16 @@ For Unity, requires to import MessagePack-CSharp package and needs slightly diff
var builder = new ContainerBuilder();
var options = builder.RegisterMessagePipe(configure);

var sc = builder.AsServiceCollection(); // require to convert ServiceCollection to enable Intereprocess
var messagePipeBuilder = builder.ToMessagePipeBuilder(); // require to convert ServiceCollection to enable Intereprocess
var interprocessOptions = sc.AddMessagePipeTcpInterprocess();
var interprocessOptions = messagePipeBuilder.AddTcpInterprocess();

// register manually.
// IDistributedPublisher/Subscriber
sc.RegisterTcpInterprocessMessageBroker<int, int>(interprocessOptions);
messagePipeBuilder.RegisterTcpInterprocessMessageBroker<int, int>(interprocessOptions);
// RemoteHandler
builder.RegisterAsyncRequestHandler<int, string, MyAsyncHandler>(options); // for server
sc.RegisterTcpRemoteRequestHandler<int, string>(interprocessOptions); // for client
messagePipeBuilder.RegisterTcpRemoteRequestHandler<int, string>(interprocessOptions); // for client
```

MessagePipeOptions
Expand Down
4 changes: 2 additions & 2 deletions sandbox/InterprocessServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static void Main(string[] args)
Host.CreateDefaultBuilder()
.ConfigureServices(x =>
{
x.AddMessagePipe();
x.AddMessagePipeNamedPipeInterprocess("messagepipe-pipe");
x.AddMessagePipe()
.AddNamedPipeInterprocess("messagepipe-pipe");
})
.RunConsoleAppFrameworkAsync<Program>(args);
}
Expand Down
Loading

0 comments on commit bad9720

Please sign in to comment.