/// <summary>
        /// Try to send request string and return the response string, or return null if not successful
        /// </summary>
        /// <param name="address">a string denoting the address to connect to</param>
        /// <param name="requestString">The request string</param>
        /// <param name="numTries">The number of times to try</param>
        /// <param name="requestTimeout">The timeout for each request</param>
        /// <param name="progressPublisher">Report topics: Failure, Retry, Send, Success</param>
        /// <returns>the response message, or null if not successful</returns>
        public static string RequestResponseStringWithRetry([NotNull] string address, [NotNull] string requestString,
                                                            int numTries, TimeSpan requestTimeout, PublisherSocket progressPublisher = null)
        {
            while (numTries-- > 0)
            {
                using (var requestSocket = new RequestSocket(address))
                {
                    progressPublisher?.SendFrame(ProgressTopic.Send.ToString());

                    requestSocket.SendFrame(requestString);

                    if (requestSocket.TryReceiveFrameString(requestTimeout, out string frameString))
                    {
                        progressPublisher?.SendFrame(ProgressTopic.Success.ToString());

                        return(frameString);
                    }

                    progressPublisher?.SendFrame(ProgressTopic.Retry.ToString());
                }
            }

            progressPublisher?.SendFrame(ProgressTopic.Failure.ToString());

            return(null);
        }
    public void SendReq(GameState gameState, out string recievedMessage)
    {
        byte[] bytes = new byte[gameState.CalculateSize()];
        Google.Protobuf.CodedOutputStream codedOutputStream = new Google.Protobuf.CodedOutputStream(bytes);
        gameState.WriteTo(codedOutputStream);
        codedOutputStream.Flush();

        client.SendFrame(bytes, false);
        //recievedMessage = client.ReceiveFrameString();
        if (!client.TryReceiveFrameString(new TimeSpan(0, 0, 1), out recievedMessage))
        {
            recievedMessage = "";
            UnityEditor.EditorApplication.isPlaying = false;
        }
    }
Exemple #3
0
        /// <summary>
        /// Try to send request string and return the response string, or return null if not successful
        /// </summary>
        /// <param name="address">a string denoting the address to connect to</param>
        /// <param name="requestString">The request string</param>
        /// <param name="numTries">The number of times to try</param>
        /// <param name="requestTimeout">The timeout for each request</param>
        /// <param name="progressPublisher">Report topics: Failure, Retry, Send, Success</param>
        /// <returns>the response message, or null if not successful</returns>
        public static string RequestResponseStringWithRetry([NotNull] string address, [NotNull] string requestString,
            int numTries, TimeSpan requestTimeout, PublisherSocket progressPublisher = null)
        {
            while (numTries-- > 0)
            {
                using (var requestSocket = new RequestSocket(address))
                {
                    if (progressPublisher != null)
                    {
                        progressPublisher.SendFrame(ProgressTopic.Send.ToString());
                    }

                    requestSocket.SendFrame(requestString);

                    string frameString;
                    if (requestSocket.TryReceiveFrameString(requestTimeout, out frameString))
                    {
                        if (progressPublisher != null)
                        {
                            progressPublisher.SendFrame(ProgressTopic.Success.ToString());
                        }

                        return frameString;
                    }
                    if (progressPublisher != null)
                    {
                        progressPublisher.SendFrame(ProgressTopic.Retry.ToString());
                    }
                }
            }

            if (progressPublisher != null)
            {
                progressPublisher.SendFrame(ProgressTopic.Failure.ToString());
            }

            return null;
        }