UnityInputProto Exchange(UnityOutputProto unityOutput) { #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX if (!m_IsOpen) { return(null); } try { var message = m_Client.Exchange(WrapMessage(unityOutput, 200)); if (message.Header.Status == 200) { return(message.UnityInput); } m_IsOpen = false; // Not sure if the quit command is actually sent when a // non 200 message is received. Notify that we are indeed // quitting. QuitCommandReceived?.Invoke(); return(message.UnityInput); } catch { m_IsOpen = false; QuitCommandReceived?.Invoke(); return(null); } #else throw new UnityAgentsException( "You cannot perform training on this platform."); #endif }
void SendCommandEvent(CommandProto command) { switch (command) { case CommandProto.Quit: { QuitCommandReceived?.Invoke(); return; } case CommandProto.Reset: { foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys) { m_OrderedAgentsRequestingDecisions[brainName].Clear(); } ResetCommandReceived?.Invoke(); return; } default: { return; } } }
UnityInputProto Initialize(UnityOutputProto unityOutput, out UnityInputProto unityInput) { #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX m_IsOpen = true; var channel = new Channel( "localhost:" + m_CommunicatorInitParameters.port, ChannelCredentials.Insecure); m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel); var result = m_Client.Exchange(WrapMessage(unityOutput, 200)); var inputMessage = m_Client.Exchange(WrapMessage(null, 200)); unityInput = inputMessage.UnityInput; #if UNITY_EDITOR EditorApplication.playModeStateChanged += HandleOnPlayModeChanged; #endif if (result.Header.Status != 200 || inputMessage.Header.Status != 200) { m_IsOpen = false; QuitCommandReceived?.Invoke(); } return(result.UnityInput); #else throw new UnityAgentsException( "You cannot perform training on this platform."); #endif }
/// <summary> /// Send a UnityOutput and receives a UnityInput. /// </summary> /// <returns>The next UnityInput.</returns> /// <param name="unityOutput">The UnityOutput to be sent.</param> UnityInputProto Exchange(UnityOutputProto unityOutput) { if (!m_IsOpen) { return(null); } try { var message = m_Client.Exchange(WrapMessage(unityOutput, 200)); if (message.Header.Status == 200) { return(message.UnityInput); } m_IsOpen = false; // Not sure if the quit command is actually sent when a // non 200 message is received. Notify that we are indeed // quitting. QuitCommandReceived?.Invoke(); return(message.UnityInput); } catch (Exception ex) { if (ex is RpcException rpcException) { // Log more verbose errors if they're something the user can possibly do something about. switch (rpcException.Status.StatusCode) { case StatusCode.Unavailable: // This can happen when python disconnects. Ignore it to avoid noisy logs. break; case StatusCode.ResourceExhausted: // This happens is the message body is too large. There's no way to // gracefully handle this, but at least we can show the message and the // user can try to reduce the number of agents or observation sizes. Debug.LogError($"GRPC Exception: {rpcException.Message}. Disconnecting from trainer."); break; default: // Other unknown errors. Log at INFO level. Debug.Log($"GRPC Exception: {rpcException.Message}. Disconnecting from trainer."); break; } } else { // Fall-through for other error types Debug.LogError($"Communication Exception: {ex.Message}. Disconnecting from trainer."); } m_IsOpen = false; QuitCommandReceived?.Invoke(); return(null); } }
void NotifyQuitAndShutDownChannel() { QuitCommandReceived?.Invoke(); try { m_Channel.ShutdownAsync().Wait(); } catch (Exception) { // do nothing } }
UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInputProto unityInput) { m_IsOpen = true; var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure); m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel); var result = m_Client.Exchange(WrapMessage(unityOutput, 200)); var inputMessage = m_Client.Exchange(WrapMessage(null, 200)); unityInput = inputMessage.UnityInput; #if UNITY_EDITOR EditorApplication.playModeStateChanged += HandleOnPlayModeChanged; #endif if (result.Header.Status != 200 || inputMessage.Header.Status != 200) { m_IsOpen = false; QuitCommandReceived?.Invoke(); } return(result.UnityInput); }
void NotifyQuitAndShutDownChannel() { QuitCommandReceived?.Invoke(); m_Channel.ShutdownAsync().Wait(); }