public void MergeFrom(UnityMessageProto other)
 {
     if (other == null)
     {
         return;
     }
     if (other.header_ != null)
     {
         if (header_ == null)
         {
             header_ = new global::Unity.MLAgents.CommunicatorObjects.HeaderProto();
         }
         Header.MergeFrom(other.Header);
     }
     if (other.unityOutput_ != null)
     {
         if (unityOutput_ == null)
         {
             unityOutput_ = new global::Unity.MLAgents.CommunicatorObjects.UnityOutputProto();
         }
         UnityOutput.MergeFrom(other.UnityOutput);
     }
     if (other.unityInput_ != null)
     {
         if (unityInput_ == null)
         {
             unityInput_ = new global::Unity.MLAgents.CommunicatorObjects.UnityInputProto();
         }
         UnityInput.MergeFrom(other.UnityInput);
     }
     _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
 }
 /// <summary>
 /// Send a UnityOutput and receives a UnityInput.
 /// </summary>
 /// <returns>The next UnityInput.</returns>
 /// <param name="unityOutput">The UnityOutput to be sent.</param>
 public UnityInput Exchange(UnityOutput unityOutput)
 {
     if (!m_isOpen)
     {
         return(null);
     }
     try
     {
         var message = m_client.Exchange(WrapMessage(unityOutput, 200));
         if (message.Header.Status == 200)
         {
             return(message.UnityInput);
         }
         else
         {
             m_isOpen = false;
             return(null);
         }
     }
     catch
     {
         m_isOpen = false;
         return(null);
     }
 }
 /// <summary>
 /// Wraps the UnityOuptut into a message with the appropriate status.
 /// </summary>
 /// <returns>The UnityMessage corresponding.</returns>
 /// <param name="content">The UnityOutput to be wrapped.</param>
 /// <param name="status">The status of the message.</param>
 private static UnityMessage WrapMessage(UnityOutput content, int status)
 {
     return(new UnityMessage
     {
         Header = new Header {
             Status = status
         },
         UnityOutput = content
     });
 }
        /// <summary>
        /// Initialize the communicator by sending the first UnityOutput and receiving the
        /// first UnityInput. The second UnityInput is stored in the unityInput argument.
        /// </summary>
        /// <returns>The first Unity Input.</returns>
        /// <param name="unityOutput">The first Unity Output.</param>
        /// <param name="unityInput">The second Unity input.</param>
        public UnityInput Initialize(UnityOutput unityOutput,
                                     out UnityInput unityInput)
        {
            m_isOpen = true;
            var channel = new Channel(
                "localhost:" + m_communicatorParameters.port,
                ChannelCredentials.Insecure);

            m_client = new UnityToExternal.UnityToExternalClient(channel);
            var result = m_client.Exchange(WrapMessage(unityOutput, 200));

            unityInput = m_client.Exchange(WrapMessage(null, 200)).UnityInput;
#if UNITY_EDITOR
            EditorApplication.playModeStateChanged += HandleOnPlayModeChanged;
#endif
            return(result.UnityInput);
        }
        /// <summary>
        /// Send a UnityOutput and receives a UnityInput.
        /// </summary>
        /// <returns>The next UnityInput.</returns>
        /// <param name="unityOutput">The UnityOutput to be sent.</param>
        public UnityInput Exchange(UnityOutput unityOutput)
        {
            Send(WrapMessage(unityOutput, 200).ToByteArray());
            byte[] received = null;
            var    task     = Task.Run(() => received = Receive());

            if (!task.Wait(System.TimeSpan.FromSeconds(TimeOut)))
            {
                throw new UnityAgentsException(
                          "The communicator took too long to respond.");
            }

            var message = UnityMessage.Parser.ParseFrom(received);

            if (message.Header.Status != 200)
            {
                return(null);
            }
            return(message.UnityInput);
        }
Beispiel #6
0
        /// <summary>
        /// Initialize the communicator by sending the first UnityOutput and receiving the
        /// first UnityInput. The second UnityInput is stored in the unityInput argument.
        /// </summary>
        /// <returns>The first Unity Input.</returns>
        /// <param name="unityOutput">The first Unity Output.</param>
        /// <param name="unityInput">The second Unity input.</param>
        public UnityInput Initialize(UnityOutput unityOutput,
                                     out UnityInput unityInput)
        {
            m_sender = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);
            m_sender.Connect("localhost", communicatorParameters.port);

            UnityMessage initializationInput =
                UnityMessage.Parser.ParseFrom(Receive());

            Send(WrapMessage(unityOutput, 200).ToByteArray());

            unityInput = UnityMessage.Parser.ParseFrom(Receive()).UnityInput;
#if UNITY_EDITOR
            EditorApplication.playModeStateChanged += HandleOnPlayModeChanged;
#endif
            return(initializationInput.UnityInput);
        }
        public override int GetHashCode()
        {
            int hash = 1;

            if (header_ != null)
            {
                hash ^= Header.GetHashCode();
            }
            if (unityOutput_ != null)
            {
                hash ^= UnityOutput.GetHashCode();
            }
            if (unityInput_ != null)
            {
                hash ^= UnityInput.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }