Compare commits
5
Commits
8fbb2e2e46
...
3fe7fa42e5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3fe7fa42e5 | ||
|
|
ae723b9bea | ||
|
|
f00e3298b1 | ||
|
|
d618bac679 | ||
|
|
23c2a9ea16 |
@@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Shared\Shared.csproj" />
|
||||
<ProjectReference Include="..\Server\Server.csproj" />
|
||||
<ProjectReference Include="..\PipeShared\PipeShared.csproj" />
|
||||
<ProjectReference Include="..\PipeServer\PipeServer.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
@@ -1,11 +1,12 @@
|
||||
using System.Diagnostics;
|
||||
using PipeMethodCalls;
|
||||
using PipeShared;
|
||||
using System.Diagnostics;
|
||||
using System.Xml.Linq;
|
||||
using PipeMethodCalls;
|
||||
using ClientServerShared;
|
||||
|
||||
// All output is written to an XML report next to the executable
|
||||
string reportPath = Path.Combine(AppContext.BaseDirectory, "ClientReport.xml");
|
||||
var report = new XElement("Report", new XAttribute("Started", DateTime.Now.ToString("O")));
|
||||
bool colorSwitch = false;
|
||||
|
||||
Log("Client started.");
|
||||
|
||||
@@ -13,7 +14,7 @@ Log("Client started.");
|
||||
string pipeName = $"PipeDemo-{Environment.ProcessId}-{Guid.NewGuid():N}";
|
||||
|
||||
Process? serverProcess = null;
|
||||
PipeClient<ICalculator> client = null!;
|
||||
PipeClient<IServerMethods> client = null!;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -52,10 +53,20 @@ lock (report)
|
||||
new XDocument(report).Save(reportPath);
|
||||
|
||||
// Thread-safe: also called from the Process.Exited event
|
||||
|
||||
void Log(string message)
|
||||
{
|
||||
lock (report)
|
||||
report.Add(new XElement("Entry", new XAttribute("Time", DateTime.Now.ToString("O")), message));
|
||||
if (message.Contains("Server exception"))
|
||||
{
|
||||
Console.ForegroundColor = colorSwitch ? ConsoleColor.DarkRed : ConsoleColor.Red;
|
||||
colorSwitch = !colorSwitch;
|
||||
}
|
||||
else
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(message);
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
async Task BasicCallsExampleAsync()
|
||||
@@ -150,7 +161,7 @@ async Task StartServerAndConnectAsync(string pipeName)
|
||||
};
|
||||
serverProcess = process;
|
||||
|
||||
client = new PipeClient<ICalculator>(new JsonFieldsPipeSerializer(), pipeName);
|
||||
client = new PipeClient<IServerMethods>(new JsonFieldsPipeSerializer(), pipeName);
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||
await client.ConnectAsync(cts.Token);
|
||||
}
|
||||
@@ -159,7 +170,7 @@ async Task StartServerAndConnectAsync(string pipeName)
|
||||
static Process StartServer(string pipeName)
|
||||
{
|
||||
string baseDir = AppContext.BaseDirectory;
|
||||
string serverExe = Path.Combine(baseDir, OperatingSystem.IsWindows() ? "Server.exe" : "Server");
|
||||
string serverExe = Path.Combine(baseDir, OperatingSystem.IsWindows() ? "PipeServer.exe" : "PipeServer");
|
||||
ProcessStartInfo startInfo;
|
||||
if (File.Exists(serverExe))
|
||||
{
|
||||
@@ -168,7 +179,7 @@ static Process StartServer(string pipeName)
|
||||
else
|
||||
{
|
||||
startInfo = new ProcessStartInfo("dotnet");
|
||||
startInfo.ArgumentList.Add(Path.Combine(baseDir, "Server.dll"));
|
||||
startInfo.ArgumentList.Add(Path.Combine(baseDir, "PipeServer.dll"));
|
||||
}
|
||||
startInfo.ArgumentList.Add($"{PipeConstants.PipeNameArgPrefix}{pipeName}");
|
||||
startInfo.UseShellExecute = false;
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
<Solution>
|
||||
<Project Path="Client/Client.csproj" />
|
||||
<Project Path="Server/Server.csproj" />
|
||||
<Project Path="Shared/Shared.csproj" />
|
||||
<Project Path="PipeClient/PipeClient.csproj" />
|
||||
<Project Path="PipeServer/PipeServer.csproj" />
|
||||
<Project Path="PipeShared/PipeShared.csproj" />
|
||||
</Solution>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Shared\Shared.csproj" />
|
||||
<ProjectReference Include="..\PipeShared\PipeShared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
@@ -1,4 +1,4 @@
|
||||
using ClientServerShared;
|
||||
using PipeShared;
|
||||
using PipeMethodCalls;
|
||||
using Server;
|
||||
|
||||
@@ -9,10 +9,10 @@ Console.WriteLine($"Server started. Pipe: {pipeName}");
|
||||
|
||||
// Exceptions in handler methods are caught by PipeMethodCalls and sent to the client
|
||||
// (client receives PipeInvokeFailedException with the server exception details).
|
||||
var server = new PipeServer<ICalculator>(
|
||||
var server = new PipeServer<IServerMethods>(
|
||||
new JsonFieldsPipeSerializer(),
|
||||
pipeName,
|
||||
() => new Calculator());
|
||||
() => new ServerMethods());
|
||||
|
||||
await server.WaitForConnectionAsync();
|
||||
Console.WriteLine("Client connected.");
|
||||
@@ -1,10 +1,10 @@
|
||||
using System.Collections.Concurrent;
|
||||
using PipeShared;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Numerics;
|
||||
using ClientServerShared;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public class Calculator : ICalculator
|
||||
public class ServerMethods : IServerMethods
|
||||
{
|
||||
public int Add(int a, int b) => a + b;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace ClientServerShared;
|
||||
namespace PipeShared;
|
||||
|
||||
public interface ICalculator
|
||||
public interface IServerMethods
|
||||
{
|
||||
int Add(int a, int b);
|
||||
Vector3 AddVector(Vector3 a, Vector3 b);
|
||||
@@ -1,7 +1,7 @@
|
||||
using PipeMethodCalls;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ClientServerShared;
|
||||
namespace PipeShared;
|
||||
|
||||
/// <summary>
|
||||
/// JSON serializer that also includes public fields (required for System.Numerics.Vector3).
|
||||
Reference in New Issue
Block a user