Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gRPC Native library to universal for arm64 and x86_64 #5283

Merged
merged 1 commit into from Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file not shown.
24 changes: 17 additions & 7 deletions com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
Expand Up @@ -2,7 +2,7 @@
#define MLA_SUPPORTED_TRAINING_PLATFORM
#endif

# if MLA_SUPPORTED_TRAINING_PLATFORM
#if MLA_SUPPORTED_TRAINING_PLATFORM
using Grpc.Core;
#if UNITY_EDITOR
using UnityEditor;
Expand Down Expand Up @@ -50,6 +50,7 @@ internal class RpcCommunicator : ICommunicator

/// The Unity to External client.
UnityToExternalProto.UnityToExternalProtoClient m_Client;
Channel m_Channel;

/// <summary>
/// Initializes a new instance of the RPCCommunicator class.
Expand Down Expand Up @@ -140,6 +141,7 @@ out input
Debug.Log($"Unexpected exception when trying to initialize communication: {ex}");
}
initParametersOut = new UnityRLInitParameters();
m_Channel.ShutdownAsync().Wait();
return false;
}

Expand Down Expand Up @@ -181,6 +183,8 @@ out input

UpdateEnvironmentWithInput(input.RlInput);
initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters();
// Be sure to shut down the grpc channel when the application is quitting.
Application.quitting += NotifyQuitAndShutDownChannel;
return true;
#else
initParametersOut = new UnityRLInitParameters();
Expand Down Expand Up @@ -217,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInputProto unityInput)
{
m_IsOpen = true;
var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
m_Channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);

m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel);
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(m_Channel);
var result = m_Client.Exchange(WrapMessage(unityOutput, 200));
var inputMessage = m_Client.Exchange(WrapMessage(null, 200));
unityInput = inputMessage.UnityInput;
Expand All @@ -229,11 +233,17 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
if (result.Header.Status != 200 || inputMessage.Header.Status != 200)
{
m_IsOpen = false;
QuitCommandReceived?.Invoke();
NotifyQuitAndShutDownChannel();
surfnerd marked this conversation as resolved.
Show resolved Hide resolved
}
return result.UnityInput;
}

void NotifyQuitAndShutDownChannel()
{
QuitCommandReceived?.Invoke();
m_Channel.ShutdownAsync().Wait();
}

#endregion

#region Destruction
Expand Down Expand Up @@ -269,7 +279,7 @@ void SendCommandEvent(CommandProto command)
{
case CommandProto.Quit:
{
QuitCommandReceived?.Invoke();
NotifyQuitAndShutDownChannel();
return;
}
case CommandProto.Reset:
Expand Down Expand Up @@ -456,7 +466,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
// Not sure if the quit command is actually sent when a
// non 200 message is received. Notify that we are indeed
// quitting.
QuitCommandReceived?.Invoke();
NotifyQuitAndShutDownChannel();
return message.UnityInput;
}
catch (Exception ex)
Expand Down Expand Up @@ -488,7 +498,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
}

m_IsOpen = false;
QuitCommandReceived?.Invoke();
NotifyQuitAndShutDownChannel();
return null;
}
}
Expand Down