12
12
using Grpc . Core ;
13
13
using System ;
14
14
using System . Collections . Generic ;
15
+ using System . Threading ;
15
16
using System . Threading . Tasks ;
16
- using Grpc . Core . Utils ;
17
17
using Mpf . Vpe ;
18
18
using NLog ;
19
19
@@ -35,7 +35,9 @@ public class MpfClient
35
35
private MpfHardwareService . MpfHardwareServiceClient _client ;
36
36
37
37
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
38
-
38
+ private Thread _commandsThread ;
39
+ private AsyncServerStreamingCall < Commands > _commandStream ;
40
+ private AsyncClientStreamingCall < SwitchChanges , EmptyResponse > _switchStream ;
39
41
40
42
public void Connect ( string serverIpPort = "127.0.0.1:50051" )
41
43
{
@@ -51,42 +53,54 @@ public void StartGame(Dictionary<string, bool> initialSwitches)
51
53
ms . InitialSwitchStates . Add ( sw , initialSwitches [ sw ] ) ;
52
54
}
53
55
54
- Logger . Info ( "Starting client..." ) ;
55
- using ( var call = _client . Start ( ms ) ) {
56
+ _commandsThread = new Thread ( ( ) => ReceiveCommands ( ms ) ) { IsBackground = true } ;
57
+ _commandsThread . Start ( ) ;
56
58
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 ) ;
60
72
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 ( ) ) {
89
76
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
+ }
90
104
Logger . Info ( $ "{ count } commands dispatched.") ;
91
105
}
92
106
}
@@ -98,6 +112,7 @@ public MachineDescription GetMachineDescription()
98
112
}
99
113
100
114
public void Shutdown ( ) {
115
+ _commandStream . Dispose ( ) ;
101
116
_channel . ShutdownAsync ( ) . Wait ( ) ;
102
117
}
103
118
}
0 commit comments