2
2
#define MLA_SUPPORTED_TRAINING_PLATFORM
3
3
#endif
4
4
5
- # if MLA_SUPPORTED_TRAINING_PLATFORM
5
+ #if MLA_SUPPORTED_TRAINING_PLATFORM
6
6
using Grpc . Core ;
7
7
#if UNITY_EDITOR
8
8
using UnityEditor ;
@@ -50,6 +50,7 @@ internal class RpcCommunicator : ICommunicator
50
50
51
51
/// The Unity to External client.
52
52
UnityToExternalProto . UnityToExternalProtoClient m_Client ;
53
+ Channel m_Channel ;
53
54
54
55
/// <summary>
55
56
/// Initializes a new instance of the RPCCommunicator class.
@@ -140,6 +141,7 @@ out input
140
141
Debug . Log ( $ "Unexpected exception when trying to initialize communication: { ex } ") ;
141
142
}
142
143
initParametersOut = new UnityRLInitParameters ( ) ;
144
+ NotifyQuitAndShutDownChannel ( ) ;
143
145
return false ;
144
146
}
145
147
@@ -181,6 +183,8 @@ out input
181
183
182
184
UpdateEnvironmentWithInput ( input . RlInput ) ;
183
185
initParametersOut = initializationInput . RlInitializationInput . ToUnityRLInitParameters ( ) ;
186
+ // Be sure to shut down the grpc channel when the application is quitting.
187
+ Application . quitting += NotifyQuitAndShutDownChannel ;
184
188
return true ;
185
189
#else
186
190
initParametersOut = new UnityRLInitParameters ( ) ;
@@ -217,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
217
221
UnityInputProto Initialize ( int port , UnityOutputProto unityOutput , out UnityInputProto unityInput )
218
222
{
219
223
m_IsOpen = true ;
220
- var channel = new Channel ( $ "localhost:{ port } ", ChannelCredentials . Insecure ) ;
224
+ m_Channel = new Channel ( $ "localhost:{ port } ", ChannelCredentials . Insecure ) ;
221
225
222
- m_Client = new UnityToExternalProto . UnityToExternalProtoClient ( channel ) ;
226
+ m_Client = new UnityToExternalProto . UnityToExternalProtoClient ( m_Channel ) ;
223
227
var result = m_Client . Exchange ( WrapMessage ( unityOutput , 200 ) ) ;
224
228
var inputMessage = m_Client . Exchange ( WrapMessage ( null , 200 ) ) ;
225
229
unityInput = inputMessage . UnityInput ;
@@ -229,11 +233,24 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
229
233
if ( result . Header . Status != 200 || inputMessage . Header . Status != 200 )
230
234
{
231
235
m_IsOpen = false ;
232
- QuitCommandReceived ? . Invoke ( ) ;
236
+ NotifyQuitAndShutDownChannel ( ) ;
233
237
}
234
238
return result . UnityInput ;
235
239
}
236
240
241
+ void NotifyQuitAndShutDownChannel ( )
242
+ {
243
+ QuitCommandReceived ? . Invoke ( ) ;
244
+ try
245
+ {
246
+ m_Channel . ShutdownAsync ( ) . Wait ( ) ;
247
+ }
248
+ catch ( Exception )
249
+ {
250
+ // do nothing
251
+ }
252
+ }
253
+
237
254
#endregion
238
255
239
256
#region Destruction
@@ -269,7 +286,7 @@ void SendCommandEvent(CommandProto command)
269
286
{
270
287
case CommandProto . Quit :
271
288
{
272
- QuitCommandReceived ? . Invoke ( ) ;
289
+ NotifyQuitAndShutDownChannel ( ) ;
273
290
return ;
274
291
}
275
292
case CommandProto . Reset :
@@ -456,7 +473,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
456
473
// Not sure if the quit command is actually sent when a
457
474
// non 200 message is received. Notify that we are indeed
458
475
// quitting.
459
- QuitCommandReceived ? . Invoke ( ) ;
476
+ NotifyQuitAndShutDownChannel ( ) ;
460
477
return message . UnityInput ;
461
478
}
462
479
catch ( Exception ex )
@@ -488,7 +505,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
488
505
}
489
506
490
507
m_IsOpen = false ;
491
- QuitCommandReceived ? . Invoke ( ) ;
508
+ NotifyQuitAndShutDownChannel ( ) ;
492
509
return null ;
493
510
}
494
511
}
0 commit comments