зеркало из https://github.com/microsoft/IPC.git
[examples] Minor refactoring (#38)
This commit is contained in:
Родитель
e2865d34b3
Коммит
86df124c5f
|
@ -6,7 +6,7 @@
|
|||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{E84069C7-B1E2-4BF1-9E2F-57827A42D162}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>CalcManaged</RootNamespace>
|
||||
<RootNamespace>Calc.Managed</RootNamespace>
|
||||
<AssemblyName>CalcManaged</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
|
|
|
@ -4,7 +4,7 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using IPC.Managed;
|
||||
|
||||
namespace CalcManaged
|
||||
namespace Calc.Managed
|
||||
{
|
||||
internal static class Client
|
||||
{
|
||||
|
@ -16,18 +16,18 @@ namespace CalcManaged
|
|||
public static void Run(TransportFactory factory, string address)
|
||||
{
|
||||
Console.WriteLine("Press Ctrl+C to exit.");
|
||||
var exit = new ManualResetEvent(false);
|
||||
Console.CancelKeyPress += (sender, args) => { args.Cancel = true; exit.Set(); };
|
||||
|
||||
Console.WriteLine($"Connecting to {address}");
|
||||
|
||||
using (var transport = factory.Make<Calc.Managed.Request, Calc.Managed.Response>())
|
||||
using (var exit = new ManualResetEvent(false))
|
||||
using (var transport = factory.Make<Request, Response>())
|
||||
using (var clientAccessor = transport.ConnectClient(address, true))
|
||||
{
|
||||
Console.CancelKeyPress += (sender, args) => { args.Cancel = true; exit.Set(); };
|
||||
|
||||
clientAccessor.Error += (sender, args) => Console.WriteLine($"IPC: {args.Exception.Message}");
|
||||
|
||||
var random = new Random();
|
||||
IClient<Calc.Managed.Request, Calc.Managed.Response> client = null;
|
||||
IClient<Request, Response> client = null;
|
||||
|
||||
while (!exit.WaitOne(TimeSpan.FromSeconds(1)))
|
||||
{
|
||||
|
@ -43,20 +43,21 @@ namespace CalcManaged
|
|||
continue;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Connected: {client.InputMemory.Name} -> {client.OutputMemory.Name}");
|
||||
var info = $"{client.InputMemory.Name} -> {client.OutputMemory.Name}";
|
||||
|
||||
client.Closed += (sender, args) =>
|
||||
Console.WriteLine($"Disconnected: {client.InputMemory.Name} -> {client.OutputMemory.Name}");
|
||||
Console.WriteLine($"Connected: {info}");
|
||||
|
||||
client.Closed += (sender, args) => Console.WriteLine($"Disconnected: {info}");
|
||||
}
|
||||
|
||||
var request = new Calc.Managed.Request
|
||||
var request = new Request
|
||||
{
|
||||
X = (float)random.NextDouble(1.0, 99.0),
|
||||
Y = (float)random.NextDouble(1.0, 99.0),
|
||||
Op = (Calc.Managed.Operation)random.Next(0, 4)
|
||||
Op = (Operation)random.Next(0, 4)
|
||||
};
|
||||
|
||||
Calc.Managed.Response response;
|
||||
Response response;
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using IPC.Managed;
|
||||
|
||||
namespace CalcManaged
|
||||
namespace Calc.Managed
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
|
@ -12,11 +12,6 @@ namespace CalcManaged
|
|||
_factory.Register(System.Reflection.Assembly.Load("CalcInterop"));
|
||||
}
|
||||
|
||||
static void RunServer(string address)
|
||||
{
|
||||
Server.Run(_factory, address);
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
var address = "ipc://calc";
|
||||
|
@ -37,7 +32,7 @@ namespace CalcManaged
|
|||
goto default;
|
||||
|
||||
default:
|
||||
Console.WriteLine("Pass --server or --client options.");
|
||||
Console.WriteLine("Pass --server or --client option.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Threading;
|
||||
using IPC.Managed;
|
||||
|
||||
namespace CalcManaged
|
||||
namespace Calc.Managed
|
||||
{
|
||||
internal static class Server
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ namespace CalcManaged
|
|||
{
|
||||
Console.WriteLine($"Hosting server at {address}");
|
||||
|
||||
using (var transport = factory.Make<Calc.Managed.Request, Calc.Managed.Response>())
|
||||
using (var transport = factory.Make<Request, Response>())
|
||||
using (var serversAccessor = transport.AcceptServers(address, (inMemory, outMemory) => new Service(outMemory).Invoke))
|
||||
{
|
||||
serversAccessor.Error += (sender, args) => Console.WriteLine($"IPC: {args.Exception.Message}");
|
||||
|
@ -23,9 +23,11 @@ namespace CalcManaged
|
|||
|
||||
Console.WriteLine("Press Ctrl+C to exit.");
|
||||
|
||||
var exit = new ManualResetEvent(false);
|
||||
Console.CancelKeyPress += (sender, args) => { args.Cancel = true; exit.Set(); };
|
||||
exit.WaitOne();
|
||||
using (var exit = new ManualResetEvent(false))
|
||||
{
|
||||
Console.CancelKeyPress += (sender, args) => { args.Cancel = true; exit.Set(); };
|
||||
exit.WaitOne();
|
||||
}
|
||||
|
||||
Console.WriteLine("Exiting...");
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Threading.Tasks;
|
||||
using IPC.Managed;
|
||||
|
||||
namespace CalcManaged
|
||||
namespace Calc.Managed
|
||||
{
|
||||
internal class Service
|
||||
{
|
||||
|
@ -13,31 +13,31 @@ namespace CalcManaged
|
|||
_memory = memory;
|
||||
}
|
||||
|
||||
public async Task<Calc.Managed.Response> Invoke(Calc.Managed.Request request)
|
||||
public Task<Response> Invoke(Request request)
|
||||
{
|
||||
var response = new Calc.Managed.Response(_memory);
|
||||
var response = new Response(_memory);
|
||||
var text = new StringBuilder();
|
||||
text.Append(request.X);
|
||||
text.Append(' ');
|
||||
|
||||
switch (request.Op)
|
||||
{
|
||||
case Calc.Managed.Operation.Add:
|
||||
case Operation.Add:
|
||||
response.Z = request.X + request.Y;
|
||||
text.Append('+');
|
||||
break;
|
||||
|
||||
case Calc.Managed.Operation.Subtract:
|
||||
case Operation.Subtract:
|
||||
response.Z = request.X - request.Y;
|
||||
text.Append('-');
|
||||
break;
|
||||
|
||||
case Calc.Managed.Operation.Multiply:
|
||||
case Operation.Multiply:
|
||||
response.Z = request.X * request.Y;
|
||||
text.Append('*');
|
||||
break;
|
||||
|
||||
case Calc.Managed.Operation.Divide:
|
||||
case Operation.Divide:
|
||||
response.Z = request.X / request.Y;
|
||||
text.Append('/');
|
||||
break;
|
||||
|
@ -47,8 +47,8 @@ namespace CalcManaged
|
|||
text.Append(request.Y);
|
||||
text.Append(" = ");
|
||||
response.Text = text.ToString();
|
||||
|
||||
return response;
|
||||
|
||||
return Task.FromResult(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,17 +18,12 @@ namespace Calc
|
|||
address,
|
||||
[](auto& connection)
|
||||
{
|
||||
auto info = connection.GetInputChannel().GetMemory()->GetName() + " -> "
|
||||
+ connection.GetOutputChannel().GetMemory()->GetName();
|
||||
const auto info = connection.GetInputChannel().GetMemory()->GetName() + " -> "
|
||||
+ connection.GetOutputChannel().GetMemory()->GetName();
|
||||
|
||||
std::cout << "Connected: " << info << std::endl;
|
||||
|
||||
connection.RegisterCloseHandler(
|
||||
[info]
|
||||
{
|
||||
std::cout << "Disconnected: " << info << std::endl;
|
||||
},
|
||||
true);
|
||||
connection.RegisterCloseHandler([info] { std::cout << "Disconnected: " << info << std::endl; }, true);
|
||||
|
||||
return Service{ connection.GetOutputChannel().GetMemory() };
|
||||
});
|
||||
|
|
|
@ -27,7 +27,7 @@ int main(int argc, char* argv[])
|
|||
}
|
||||
|
||||
default:
|
||||
std::cout << "Pass --server or --client options." << std::endl;
|
||||
std::cout << "Pass --server or --client option." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ The [Boost package](https://www.nuget.org/packages/boost/) will automatically st
|
|||
|
||||
# Getting Started
|
||||
|
||||
Start with [C++](https://github.com/Microsoft/IPC/blob/master/UnitTests/TransportTests.cpp) and [C#](https://github.com/Microsoft/IPC/blob/master/UnitTestsManaged/TransportTests.cs) tests.
|
||||
Start with [examples](https://github.com/Microsoft/IPC/tree/master/Examples), [C++](https://github.com/Microsoft/IPC/blob/master/UnitTests/TransportTests.cpp) and [C#](https://github.com/Microsoft/IPC/blob/master/UnitTestsManaged/TransportTests.cs) tests.
|
||||
|
||||
# Contributing
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче