Skip to content

Commit 9ff7ad7

Browse files
committed
client: Add support for streams.
1 parent 27cd995 commit 9ff7ad7

File tree

3 files changed

+72
-36
lines changed

3 files changed

+72
-36
lines changed

VisualPinball.Engine.Mpf.Test/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.Diagnostics;
15+
using System.Linq.Expressions;
1516
using System.Threading.Tasks;
1617
using VisualPinball.Engine.Mpf;
1718

1819
namespace MpfTest
1920
{
2021
public static class Program
2122
{
22-
public static void Main(string[] args)
23+
public static async Task Main(string[] args)
2324
{
2425
// Console.WriteLine("Starting...");
2526
// var client = new MpfClient();
@@ -42,6 +43,21 @@ public static void Main(string[] args)
4243

4344
var descr = mpfApi.GetMachineDescription();
4445
Console.WriteLine($"Description: {descr} in {s.ElapsedMilliseconds}ms");
46+
47+
ConsoleKeyInfo key;
48+
do {
49+
key = Console.ReadKey();
50+
switch (key.Key) {
51+
case ConsoleKey.A:
52+
await mpfApi.Switch("s_sling", true);
53+
break;
54+
case ConsoleKey.S:
55+
await mpfApi.Switch("s_sling", false);
56+
break;
57+
}
58+
} while (key.Key != ConsoleKey.Escape);
59+
60+
mpfApi.Dispose();
4561
}
4662
}
4763
}

VisualPinball.Engine.Mpf/MpfApi.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public MachineDescription GetMachineDescription()
5555
return _client.GetMachineDescription();
5656
}
5757

58+
public async Task Switch(string swName, bool swValue)
59+
{
60+
await _client.Switch(swName, swValue);
61+
}
62+
5863
public void Dispose()
5964
{
6065
_client?.Shutdown();

VisualPinball.Engine.Mpf/MpfClient.cs

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
using Grpc.Core;
1313
using System;
1414
using System.Collections.Generic;
15+
using System.Threading;
1516
using System.Threading.Tasks;
16-
using Grpc.Core.Utils;
1717
using Mpf.Vpe;
1818
using NLog;
1919

@@ -35,7 +35,9 @@ public class MpfClient
3535
private MpfHardwareService.MpfHardwareServiceClient _client;
3636

3737
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
38-
38+
private Thread _commandsThread;
39+
private AsyncServerStreamingCall<Commands> _commandStream;
40+
private AsyncClientStreamingCall<SwitchChanges, EmptyResponse> _switchStream;
3941

4042
public void Connect(string serverIpPort = "127.0.0.1:50051")
4143
{
@@ -51,42 +53,54 @@ public void StartGame(Dictionary<string, bool> initialSwitches)
5153
ms.InitialSwitchStates.Add(sw, initialSwitches[sw]);
5254
}
5355

54-
Logger.Info("Starting client...");
55-
using (var call = _client.Start(ms)) {
56+
_commandsThread = new Thread(() => ReceiveCommands(ms)) { IsBackground = true };
57+
_commandsThread.Start();
5658

57-
Logger.Info("Client started, retrieving commands...");
58-
var count = 0;
59-
call.ResponseStream.ForEachAsync(commands => {
59+
_switchStream = _client.SendSwitchChanges();
60+
}
61+
62+
public async Task Switch(string swName, bool swValue)
63+
{
64+
await _switchStream.RequestStream.WriteAsync(new SwitchChanges
65+
{SwitchNumber = swName, SwitchState = swValue});
66+
}
67+
68+
private async void ReceiveCommands(MachineState ms)
69+
{
70+
Logger.Info("Starting client...");
71+
_commandStream = _client.Start(ms);
6072

61-
Logger.Info($"New command: {commands.CommandCase}");
62-
count++;
63-
switch (commands.CommandCase) {
64-
case Commands.CommandOneofCase.None:
65-
break;
66-
case Commands.CommandOneofCase.FadeLight:
67-
OnFadeLight?.Invoke(this, commands.FadeLight);
68-
break;
69-
case Commands.CommandOneofCase.PulseCoil:
70-
OnPulseCoil?.Invoke(this, commands.PulseCoil);
71-
break;
72-
case Commands.CommandOneofCase.EnableCoil:
73-
OnEnableCoil?.Invoke(this, commands.EnableCoil);
74-
break;
75-
case Commands.CommandOneofCase.DisableCoil:
76-
OnDisableCoil?.Invoke(this, commands.DisableCoil);
77-
break;
78-
case Commands.CommandOneofCase.ConfigureHardwareRule:
79-
OnConfigureHardwareRule?.Invoke(this, commands.ConfigureHardwareRule);
80-
break;
81-
case Commands.CommandOneofCase.RemoveHardwareRule:
82-
OnRemoveHardwareRule?.Invoke(this, commands.RemoveHardwareRule);
83-
break;
84-
default:
85-
throw new ArgumentOutOfRangeException();
86-
}
87-
return Task.CompletedTask;
88-
}).Wait();
73+
Logger.Info("Client started, retrieving commands...");
74+
var count = 0;
75+
while (await _commandStream.ResponseStream.MoveNext()) {
8976

77+
var commands = _commandStream.ResponseStream.Current;
78+
Logger.Info($"New command: {commands.CommandCase}");
79+
count++;
80+
switch (commands.CommandCase) {
81+
case Commands.CommandOneofCase.None:
82+
break;
83+
case Commands.CommandOneofCase.FadeLight:
84+
OnFadeLight?.Invoke(this, commands.FadeLight);
85+
break;
86+
case Commands.CommandOneofCase.PulseCoil:
87+
OnPulseCoil?.Invoke(this, commands.PulseCoil);
88+
break;
89+
case Commands.CommandOneofCase.EnableCoil:
90+
OnEnableCoil?.Invoke(this, commands.EnableCoil);
91+
break;
92+
case Commands.CommandOneofCase.DisableCoil:
93+
OnDisableCoil?.Invoke(this, commands.DisableCoil);
94+
break;
95+
case Commands.CommandOneofCase.ConfigureHardwareRule:
96+
OnConfigureHardwareRule?.Invoke(this, commands.ConfigureHardwareRule);
97+
break;
98+
case Commands.CommandOneofCase.RemoveHardwareRule:
99+
OnRemoveHardwareRule?.Invoke(this, commands.RemoveHardwareRule);
100+
break;
101+
default:
102+
throw new ArgumentOutOfRangeException();
103+
}
90104
Logger.Info($"{count} commands dispatched.");
91105
}
92106
}
@@ -98,6 +112,7 @@ public MachineDescription GetMachineDescription()
98112
}
99113

100114
public void Shutdown() {
115+
_commandStream.Dispose();
101116
_channel.ShutdownAsync().Wait();
102117
}
103118
}

0 commit comments

Comments
 (0)