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.
@@ -182,6 +183,8 @@ out input
182
183
183
184
UpdateEnvironmentWithInput ( input . RlInput ) ;
184
185
initParametersOut = initializationInput . RlInitializationInput . ToUnityRLInitParameters ( ) ;
186
+ // Be sure to shut down the grpc channel when the application is quitting.
187
+ Application . quitting += NotifyQuitAndShutDownChannel ;
185
188
return true ;
186
189
#else
187
190
initParametersOut = new UnityRLInitParameters ( ) ;
@@ -218,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
218
221
UnityInputProto Initialize ( int port , UnityOutputProto unityOutput , out UnityInputProto unityInput )
219
222
{
220
223
m_IsOpen = true ;
221
- var channel = new Channel ( $ "localhost:{ port } ", ChannelCredentials . Insecure ) ;
224
+ m_Channel = new Channel ( $ "localhost:{ port } ", ChannelCredentials . Insecure ) ;
222
225
223
- m_Client = new UnityToExternalProto . UnityToExternalProtoClient ( channel ) ;
226
+ m_Client = new UnityToExternalProto . UnityToExternalProtoClient ( m_Channel ) ;
224
227
var result = m_Client . Exchange ( WrapMessage ( unityOutput , 200 ) ) ;
225
228
var inputMessage = m_Client . Exchange ( WrapMessage ( null , 200 ) ) ;
226
229
unityInput = inputMessage . UnityInput ;
@@ -230,7 +233,7 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
230
233
if ( result . Header . Status != 200 || inputMessage . Header . Status != 200 )
231
234
{
232
235
m_IsOpen = false ;
233
- QuitCommandReceived ? . Invoke ( ) ;
236
+ NotifyQuitAndShutDownChannel ( ) ;
234
237
}
235
238
return result . UnityInput ;
236
239
}
@@ -283,7 +286,7 @@ void SendCommandEvent(CommandProto command)
283
286
{
284
287
case CommandProto . Quit :
285
288
{
286
- QuitCommandReceived ? . Invoke ( ) ;
289
+ NotifyQuitAndShutDownChannel ( ) ;
287
290
return ;
288
291
}
289
292
case CommandProto . Reset :
@@ -470,7 +473,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
470
473
// Not sure if the quit command is actually sent when a
471
474
// non 200 message is received. Notify that we are indeed
472
475
// quitting.
473
- QuitCommandReceived ? . Invoke ( ) ;
476
+ NotifyQuitAndShutDownChannel ( ) ;
474
477
return message . UnityInput ;
475
478
}
476
479
catch ( Exception ex )
@@ -502,7 +505,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
502
505
}
503
506
504
507
m_IsOpen = false ;
505
- QuitCommandReceived ? . Invoke ( ) ;
508
+ NotifyQuitAndShutDownChannel ( ) ;
506
509
return null ;
507
510
}
508
511
}
0 commit comments