This class is used to wrap around UnityEngine.Experimental.Networking.UnityWebRequest so that we can be backward compatible with unity 4.6
Inheritance: IDisposable
Ejemplo n.º 1
0
 private IEnumerator InvokeRequest(IUnityHttpRequest request)
 {
     if ((ServiceFactory.Instance.GetService <INetworkReachability>() as NetworkReachability).NetworkStatus != 0)
     {
         if (request is UnityWwwRequest)
         {
             WWW wwwRequest = new WWW((request as UnityWwwRequest).RequestUri.AbsoluteUri, request.RequestContent, request.Headers);
             if (wwwRequest == null)
             {
                 yield return((object)null);
             }
             bool uploadCompleted2 = false;
             while (!wwwRequest.get_isDone())
             {
                 float uploadProgress = wwwRequest.get_uploadProgress();
                 if (!uploadCompleted2)
                 {
                     request.OnUploadProgressChanged(uploadProgress);
                     if (uploadProgress == 1f)
                     {
                         uploadCompleted2 = true;
                     }
                 }
                 yield return((object)null);
             }
             request.WwwRequest = (IDisposable)wwwRequest;
             request.Response   = new UnityWebResponseData(wwwRequest);
         }
         else
         {
             UnityRequest unityRequest = request as UnityRequest;
             if (unityRequest == null)
             {
                 yield return((object)null);
             }
             UnityWebRequestWrapper unityWebRequest = new UnityWebRequestWrapper(unityRequest.RequestUri.AbsoluteUri, unityRequest.Method)
             {
                 DownloadHandler = new DownloadHandlerBufferWrapper()
             };
             if (request.RequestContent != null && request.RequestContent.Length != 0)
             {
                 unityWebRequest.UploadHandler = new UploadHandlerRawWrapper(request.RequestContent);
             }
             bool uploadCompleted = false;
             foreach (KeyValuePair <string, string> header in request.Headers)
             {
                 unityWebRequest.SetRequestHeader(header.Key, header.Value);
             }
             AsyncOperation operation = unityWebRequest.Send();
             while (!operation.get_isDone())
             {
                 float progress = operation.get_progress();
                 if (!uploadCompleted)
                 {
                     request.OnUploadProgressChanged(progress);
                     if (progress == 1f)
                     {
                         uploadCompleted = true;
                     }
                 }
                 yield return((object)null);
             }
             request.WwwRequest = unityWebRequest;
             request.Response   = new UnityWebResponseData(unityWebRequest);
         }
     }
     else
     {
         request.Exception = new WebException("Network Unavailable", WebExceptionStatus.ConnectFailure);
     }
     if (request.IsSync)
     {
         if (request.Response != null && !request.Response.IsSuccessStatusCode)
         {
             request.Exception = new HttpErrorResponseException(request.Response);
         }
         request.WaitHandle.Set();
     }
     else
     {
         if (request.Response != null && !request.Response.IsSuccessStatusCode)
         {
             request.Exception = new HttpErrorResponseException(request.Response);
         }
         ThreadPool.QueueUserWorkItem(delegate
         {
             try
             {
                 request.Callback(request.AsyncResult);
             }
             catch (Exception exception)
             {
                 _logger.Error(exception, "An exception was thrown from the callback method executed fromUnityMainThreadDispatcher.InvokeRequest method.");
             }
         });
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Makes a single web request using the WWW or UnityWebRequest API.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>IEnumerator which indicated if the operation is pending.</returns>
        IEnumerator InvokeRequest(IUnityHttpRequest request)
        {
            // Fire the request
            var nr = ServiceFactory.Instance.GetService <INetworkReachability>() as Amazon.Util.Internal.PlatformServices.NetworkReachability;

            if (nr.NetworkStatus != NetworkStatus.NotReachable)
            {
                bool isWwwRequest = (request as UnityWwwRequest) != null;
                if (isWwwRequest)
                {
                    var wwwRequest = new WWW((request as UnityWwwRequest).RequestUri.AbsoluteUri, request.RequestContent, request.Headers);
                    if (wwwRequest == null)
                    {
                        yield return(null);
                    }
                    bool  uploadCompleted = false;
                    float prevProgress    = 0;
                    float currentProgress = 0;
                    while (!wwwRequest.isDone)
                    {
                        if (request.RequestContent == null)
                        {
                            currentProgress = wwwRequest.progress;
                            if (prevProgress != currentProgress)
                            {
                                if (DownloadProgress != null)
                                {
                                    DownloadProgress(currentProgress);
                                }
                            }
                            prevProgress = currentProgress;
                        }

                        var uploadProgress = wwwRequest.uploadProgress;
                        if (!uploadCompleted)
                        {
                            request.OnUploadProgressChanged(uploadProgress);

                            if (uploadProgress == 1)
                            {
                                uploadCompleted = true;
                            }
                        }
                        yield return(null);
                    }
                    request.WwwRequest = wwwRequest;
                    request.Response   = new UnityWebResponseData(wwwRequest);
                }
                else
                {
                    var unityRequest = request as UnityRequest;

                    if (unityRequest == null)
                    {
                        yield return(null);
                    }

                    var unityWebRequest = new UnityWebRequestWrapper(
                        unityRequest.RequestUri.AbsoluteUri,
                        unityRequest.Method);

                    unityWebRequest.DownloadHandler = new DownloadHandlerBufferWrapper();

                    if (request.RequestContent != null && request.RequestContent.Length > 0)
                    {
                        unityWebRequest.UploadHandler = new UploadHandlerRawWrapper(request.RequestContent);
                    }

                    bool uploadCompleted = false;

                    foreach (var header in request.Headers)
                    {
                        unityWebRequest.SetRequestHeader(header.Key, header.Value);
                    }

                    var   operation       = unityWebRequest.Send();
                    float prevProgress    = 0;
                    float currentProgress = 0;
                    while (!operation.isDone)
                    {
                        if (request.RequestContent == null)
                        {
                            currentProgress = unityWebRequest.DownloadProgress;
                            if (prevProgress != currentProgress)
                            {
                                if (DownloadProgress != null)
                                {
                                    DownloadProgress(currentProgress);
                                }
                            }
                            prevProgress = currentProgress;
                        }

                        var uploadProgress = operation.progress;
                        if (!uploadCompleted)
                        {
                            request.OnUploadProgressChanged(uploadProgress);

                            if (uploadProgress == 1)
                            {
                                uploadCompleted = true;
                            }
                        }
                        yield return(null);
                    }
                    request.WwwRequest = unityWebRequest;
                    request.Response   = new UnityWebResponseData(unityWebRequest);
                }
            }
            else
            {
                request.Exception = new WebException("Network Unavailable", WebExceptionStatus.ConnectFailure);
            }

            if (request.IsSync)
            {
                // For synchronous calls, signal the wait handle
                // so that the calling thread which waits on the wait handle
                // is unblocked.
                if (request.Response != null && !request.Response.IsSuccessStatusCode)
                {
                    request.Exception = new HttpErrorResponseException(request.Response);
                }
                request.WaitHandle.Set();
            }
            else
            {
                if (request.Response != null && !request.Response.IsSuccessStatusCode)
                {
                    request.Exception = new HttpErrorResponseException(request.Response);
                }
                // For asychronous calls invoke the callback method with the
                // state object that was originally passed in.

                // Invoke the callback method for the request on the thread pool
                // after the web request is executed. This callback triggers the
                // post processing of the response from the server.
                ThreadPool.QueueUserWorkItem((state) =>
                {
                    try
                    {
                        request.Callback(request.AsyncResult);
                    }
                    catch (Exception exception)
                    {
                        // The callback method (HttpHandler.GetResponseCallback) and
                        // subsequent calls to handler callbacks capture any exceptions
                        // thrown from the runtime pipeline during post processing.

                        // Log the exception, in case we get an unhandled exception
                        // from the callback.
                        _logger.Error(exception,
                                      "An exception was thrown from the callback method executed from"
                                      + "UnityMainThreadDispatcher.InvokeRequest method.");
                    }
                });
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Makes a single web request using the WWW or UnityWebRequest API.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>IEnumerator which indicated if the operation is pending.</returns>
        IEnumerator InvokeRequest(IUnityHttpRequest request)
        {
            // Fire the request
            var nr = ServiceFactory.Instance.GetService<INetworkReachability>() as Amazon.Util.Internal.PlatformServices.NetworkReachability;
            if (nr.NetworkStatus != NetworkStatus.NotReachable)
            {
                bool isWwwRequest = (request as UnityWwwRequest) != null;
                if (isWwwRequest)
                {
                    var wwwRequest = new WWW((request as UnityWwwRequest).RequestUri.AbsoluteUri, request.RequestContent, request.Headers);
                    if (wwwRequest == null)
                    {
                        yield return null;
                    }
                    bool uploadCompleted = false;
                    while (!wwwRequest.isDone)
                    {
                        var uploadProgress = wwwRequest.uploadProgress;
                        if (!uploadCompleted)
                        {
                            request.OnUploadProgressChanged(uploadProgress);

                            if (uploadProgress == 1)
                                uploadCompleted = true;
                        }
                        yield return null;
                    }
                    request.WwwRequest = wwwRequest;
                    request.Response = new UnityWebResponseData(wwwRequest);
                }
                else
                {
                    var unityRequest = request as UnityRequest;
                    if (unityRequest == null)
                    {
                        yield return null;
                    }
                    
                    var unityWebRequest = new UnityWebRequestWrapper(
                        unityRequest.RequestUri.AbsoluteUri,
                        unityRequest.Method);

                    unityWebRequest.DownloadHandler = new DownloadHandlerBufferWrapper();

                    if (request.RequestContent != null && request.RequestContent.Length > 0)
                        unityWebRequest.UploadHandler = new UploadHandlerRawWrapper(request.RequestContent);

                    bool uploadCompleted = false;

                    foreach (var header in request.Headers)
                    {
                        unityWebRequest.SetRequestHeader(header.Key, header.Value);
                    }

                    var operation = unityWebRequest.Send();
                    while (!operation.isDone)
                    {
                        var uploadProgress = operation.progress;
                        if (!uploadCompleted)
                        {
                            request.OnUploadProgressChanged(uploadProgress);

                            if (uploadProgress == 1)
                                uploadCompleted = true;
                        }
                        yield return null;
                    }
                    request.WwwRequest = unityWebRequest;
                    request.Response = new UnityWebResponseData(unityWebRequest);
                }
            }
            else
            {
                request.Exception = new WebException("Network Unavailable", WebExceptionStatus.ConnectFailure);
            }

            if (request.IsSync)
            {
                // For synchronous calls, signal the wait handle 
                // so that the calling thread which waits on the wait handle
                // is unblocked.
                if (request.Response != null && !request.Response.IsSuccessStatusCode)
                {
                    request.Exception = new HttpErrorResponseException(request.Response);
                }
                request.WaitHandle.Set();
            }
            else
            {
                if (request.Response != null && !request.Response.IsSuccessStatusCode)
                {
                    request.Exception = new HttpErrorResponseException(request.Response);
                }
                // For asychronous calls invoke the callback method with the
                // state object that was originally passed in.

                // Invoke the callback method for the request on the thread pool
                // after the web request is executed. This callback triggers the 
                // post processing of the response from the server.
                ThreadPool.QueueUserWorkItem((state) =>
                {
                    try
                    {
                        request.Callback(request.AsyncResult);
                    }
                    catch (Exception exception)
                    {
                        // The callback method (HttpHandler.GetResponseCallback) and 
                        // subsequent calls to handler callbacks capture any exceptions
                        // thrown from the runtime pipeline during post processing.

                        // Log the exception, in case we get an unhandled exception 
                        // from the callback.
                        _logger.Error(exception,
                    "An exception was thrown from the callback method executed from"
                    + "UnityMainThreadDispatcher.InvokeRequest method.");

                    }
                });
            }
        }