|
1 | 1 | # if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
|
2 | 2 | using Grpc.Core;
|
3 | 3 | #endif
|
| 4 | + |
| 5 | +#if MLA_SUPPORTED_TRAINING_PLATFORM |
| 6 | +using Grpc.Core; |
4 | 7 | #if UNITY_EDITOR
|
5 | 8 | using UnityEditor;
|
6 | 9 | #endif
|
@@ -49,9 +52,7 @@ internal class RpcCommunicator : ICommunicator
|
49 | 52 | # if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
|
50 | 53 | /// The Unity to External client.
|
51 | 54 | UnityToExternalProto.UnityToExternalProtoClient m_Client;
|
52 |
| -#endif |
53 |
| - /// The communicator parameters sent at construction |
54 |
| - CommunicatorInitParameters m_CommunicatorInitParameters; |
| 55 | + Channel m_Channel; |
55 | 56 |
|
56 | 57 | /// <summary>
|
57 | 58 | /// Initializes a new instance of the RPCCommunicator class.
|
@@ -231,6 +232,10 @@ public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameter
|
231 | 232 | );
|
232 | 233 | throw new UnityAgentsException("Incompatible trainer version.");
|
233 | 234 | }
|
| 235 | + initParametersOut = new UnityRLInitParameters(); |
| 236 | + m_Channel.ShutdownAsync().Wait(); |
| 237 | + return false; |
| 238 | + } |
234 | 239 |
|
235 | 240 | var packageVersionSupported = CheckPythonPackageVersionIsSupported(pythonPackageVersion);
|
236 | 241 | if (!packageVersionSupported)
|
@@ -262,7 +267,14 @@ public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameter
|
262 | 267 | }
|
263 | 268 |
|
264 | 269 | UpdateEnvironmentWithInput(input.RlInput);
|
265 |
| - return initializationInput.RlInitializationInput.ToUnityRLInitParameters(); |
| 270 | + initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters(); |
| 271 | + // Be sure to shut down the grpc channel when the application is quitting. |
| 272 | + Application.quitting += NotifyQuitAndShutDownChannel; |
| 273 | + return true; |
| 274 | +#else |
| 275 | + initParametersOut = new UnityRLInitParameters(); |
| 276 | + return false; |
| 277 | +#endif |
266 | 278 | }
|
267 | 279 |
|
268 | 280 | /// <summary>
|
@@ -296,24 +308,33 @@ UnityInputProto Initialize(UnityOutputProto unityOutput,
|
296 | 308 | {
|
297 | 309 | # if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
|
298 | 310 | m_IsOpen = true;
|
299 |
| - var channel = new Channel( |
300 |
| - "localhost:" + m_CommunicatorInitParameters.port, |
301 |
| - ChannelCredentials.Insecure); |
| 311 | + m_Channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure); |
302 | 312 |
|
303 |
| - m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel); |
| 313 | + m_Client = new UnityToExternalProto.UnityToExternalProtoClient(m_Channel); |
304 | 314 | var result = m_Client.Exchange(WrapMessage(unityOutput, 200));
|
305 | 315 | unityInput = m_Client.Exchange(WrapMessage(null, 200)).UnityInput;
|
306 | 316 | #if UNITY_EDITOR
|
307 | 317 | EditorApplication.playModeStateChanged += HandleOnPlayModeChanged;
|
308 | 318 | #endif
|
| 319 | + if (result.Header.Status != 200 || inputMessage.Header.Status != 200) |
| 320 | + { |
| 321 | + m_IsOpen = false; |
| 322 | + NotifyQuitAndShutDownChannel(); |
| 323 | + } |
309 | 324 | return result.UnityInput;
|
310 | 325 | #else
|
311 | 326 | throw new UnityAgentsException(
|
312 | 327 | "You cannot perform training on this platform.");
|
313 | 328 | #endif
|
314 | 329 | }
|
315 | 330 |
|
316 |
| - #endregion |
| 331 | + void NotifyQuitAndShutDownChannel() |
| 332 | + { |
| 333 | + QuitCommandReceived?.Invoke(); |
| 334 | + m_Channel.ShutdownAsync().Wait(); |
| 335 | + } |
| 336 | + |
| 337 | +#endregion |
317 | 338 |
|
318 | 339 | #region Destruction
|
319 | 340 |
|
@@ -352,10 +373,10 @@ void SendCommandEvent(CommandProto command)
|
352 | 373 | switch (command)
|
353 | 374 | {
|
354 | 375 | case CommandProto.Quit:
|
355 |
| - { |
356 |
| - QuitCommandReceived?.Invoke(); |
357 |
| - return; |
358 |
| - } |
| 376 | + { |
| 377 | + NotifyQuitAndShutDownChannel(); |
| 378 | + return; |
| 379 | + } |
359 | 380 | case CommandProto.Reset:
|
360 | 381 | {
|
361 | 382 | foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys)
|
@@ -540,13 +561,13 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
|
540 | 561 | // Not sure if the quit command is actually sent when a
|
541 | 562 | // non 200 message is received. Notify that we are indeed
|
542 | 563 | // quitting.
|
543 |
| - QuitCommandReceived?.Invoke(); |
| 564 | + NotifyQuitAndShutDownChannel(); |
544 | 565 | return message.UnityInput;
|
545 | 566 | }
|
546 | 567 | catch
|
547 | 568 | {
|
548 | 569 | m_IsOpen = false;
|
549 |
| - QuitCommandReceived?.Invoke(); |
| 570 | + NotifyQuitAndShutDownChannel(); |
550 | 571 | return null;
|
551 | 572 | }
|
552 | 573 | #else
|
|
0 commit comments