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.");
}
});
}
}