public void Open()
 {
     try
     {
         this.RequestFileStream = new FileStream(this.FilePath, FileMode.Open, FileAccess.Read);
         this.Request           = (HttpWebRequest)WebRequest.Create(AssetStoreClient.APIUri(this.URI, this.m_extraParams));
         this.Request.AllowWriteStreamBuffering = false;
         this.Request.Timeout = 36000000;
         this.Request.Headers.Set("X-Unity-Session", AssetStoreClient.ActiveOrUnauthSessionID);
         this.Request.KeepAlive     = false;
         this.Request.ContentLength = this.RequestFileStream.Length;
         this.Request.Method        = "PUT";
         this.BytesToSend           = this.RequestFileStream.Length;
         this.BytesSent             = 0L;
         this.RequestStream         = this.Request.GetRequestStream();
         if (this.Buffer == null)
         {
             this.Buffer = new byte[32768];
         }
     }
     catch (Exception ex)
     {
         AssetStoreResponse job = AssetStoreClient.parseAssetStoreResponse(null, null, ex, null);
         this.RequestDoneCallback(job);
         this.Close();
         throw ex;
     }
 }
예제 #2
0
 private static bool Parse(AssetStoreResponse response, out string error, out JSONValue jval)
 {
     jval  = default(JSONValue);
     error = null;
     if (response.failed)
     {
         error = string.Format("Error receiving response from server ({0}): {1}", response.HttpStatusCode, response.HttpErrorMessage ?? "n/a");
         return(true);
     }
     try
     {
         JSONParser jsonparser = new JSONParser(response.data);
         jval = jsonparser.Parse();
     }
     catch (JSONParseException ex)
     {
         error = "Error parsing reply from AssetStore";
         DebugUtils.LogError("Error parsing server reply: " + response.data);
         DebugUtils.LogError(ex.Message);
         return(true);
     }
     if (jval.ContainsKey("error"))
     {
         error = jval["error"].AsString(true);
     }
     else if (jval.ContainsKey("status") && jval["status"].AsString(true) != "ok")
     {
         error = jval["message"].AsString(true);
     }
     return(error != null);
 }
 public static void AbortLargeFilesUpload()
 {
     if (AssetStoreClient.s_PendingLargeFiles.Count == 0)
     {
         return;
     }
     AssetStoreClient.s_PendingLargeFiles.RemoveAll(delegate(AssetStoreClient.LargeFilePending assetUpload)
     {
         if (assetUpload == null)
         {
             return(true);
         }
         AssetStoreResponse job = AssetStoreClient.parseAssetStoreResponse(null, null, null, null);
         job.HttpStatusCode     = -2;
         if (assetUpload.RequestDoneCallback != null)
         {
             assetUpload.RequestDoneCallback(job);
         }
         assetUpload.Close();
         return(true);
     });
 }
        private static AssetStoreResponse parseAssetStoreResponse(string data, byte[] binData, Exception ex, WebHeaderCollection responseHeaders)
        {
            AssetStoreResponse result = default(AssetStoreResponse);

            result.data             = data;
            result.binData          = binData;
            result.ok               = true;
            result.HttpErrorMessage = null;
            result.HttpStatusCode   = -1;
            if (ex != null)
            {
                WebException ex2 = null;
                try
                {
                    ex2 = (WebException)ex;
                }
                catch (Exception)
                {
                }
                if (ex2 == null || ex2.Response == null || ex2.Response.Headers == null)
                {
                    DebugUtils.LogError("Invalid server response " + ex.Message);
                    DebugUtils.LogError("Stacktrace:" + ex.StackTrace);
                }
                else
                {
                    result.HttpHeaders      = ex2.Response.Headers;
                    result.HttpStatusCode   = (int)((HttpWebResponse)ex2.Response).StatusCode;
                    result.HttpErrorMessage = ex2.Message;
                    if (result.HttpStatusCode != 401 && AssetStoreManager.sDbg)
                    {
                        WebHeaderCollection headers = ex2.Response.Headers;
                        DebugUtils.LogError("\nDisplaying ex the response headers\n");
                        for (int i = 0; i < headers.Count; i++)
                        {
                            DebugUtils.LogError("\t" + headers.GetKey(i) + " = " + headers.Get(i));
                        }
                        DebugUtils.Log("status code: " + result.HttpStatusCode.ToString());
                    }
                }
            }
            else
            {
                result.HttpStatusCode = 200;
                result.HttpHeaders    = responseHeaders;
            }
            if (result.HttpStatusCode / 100 != 2)
            {
                result.ok = false;
                if (AssetStoreManager.sDbg)
                {
                    DebugUtils.LogError("Request statusCode: " + result.HttpStatusCode.ToString());
                }
                if (ex != null)
                {
                    result.HttpErrorMessage = ex.Message;
                }
                else
                {
                    result.HttpErrorMessage = "Request status: " + result.HttpStatusCode.ToString();
                }
            }
            if (ex != null)
            {
                result.ok = false;
                if (AssetStoreManager.sDbg)
                {
                    DebugUtils.LogError("Request exception: " + ex.GetType().ToString() + " - " + ex.Message);
                }
                result.HttpErrorMessage = ex.Message;
            }
            return(result);
        }
        public static void Update()
        {
            List <AssetStoreClient.Pending> obj = AssetStoreClient.pending;

            lock (obj)
            {
                AssetStoreClient.pending.RemoveAll(delegate(AssetStoreClient.Pending p)
                {
                    if (p.conn == null)
                    {
                        if (p.queueDelegate == null)
                        {
                            DebugUtils.LogWarning("Invalid pending state while communicating with asset store");
                            return(true);
                        }
                        if (!p.queueDelegate() && p.conn == null)
                        {
                            return(false);
                        }
                        p.queueDelegate = null;
                    }
                    if (!p.conn.IsBusy)
                    {
                        if (p.ex == null && p.data == null)
                        {
                            if (p.binData == null)
                            {
                                goto IL_19F;
                            }
                        }
                        try
                        {
                            AssetStoreResponse job = AssetStoreClient.parseAssetStoreResponse(p.data, p.binData, p.ex, (p.conn != null) ? p.conn.ResponseHeaders : null);
                            if (AssetStoreManager.sDbg)
                            {
                                DebugUtils.Log(string.Concat(new string[]
                                {
                                    "Pending done: ",
                                    Thread.CurrentThread.ManagedThreadId.ToString(),
                                    " ",
                                    p.id,
                                    " ",
                                    job.data ?? "<nodata>"
                                }));
                                if (job.HttpHeaders != null && job.HttpHeaders.Get("X-Unity-Reason") != null)
                                {
                                    DebugUtils.LogWarning("X-Unity-Reason: " + job.HttpHeaders.Get("X-Unity-Reason"));
                                }
                            }
                            p.callback(job);
                        }
                        catch (Exception ex)
                        {
                            DebugUtils.LogError("Uncaught exception in async net callback: " + ex.Message);
                            DebugUtils.LogError(ex.StackTrace);
                        }
                        AssetStoreClient.ReleaseClient(p.conn);
                        p.conn = null;
                        return(true);
                    }
                    IL_19F:
                    if (p.progressCallback != null && p.statsUpdated)
                    {
                        p.statsUpdated = false;
                        double pctUp   = (p.totalBytesToSend <= 0)
                            ? 0
                            : p.bytesSend / p.totalBytesToSend * 100.0;

                        double pctDown = (p.totalBytesToReceive <= 0)
                            ? 0
                            : p.bytesReceived / p.totalBytesToReceive * 100.0;

                        try
                        {
                            p.progressCallback(pctUp, pctDown);
                        }
                        catch (Exception ex2)
                        {
                            DebugUtils.LogError("Uncaught exception in async net progress callback: " + ex2.Message);
                        }
                    }
                    return(false);
                });
            }
            AssetStoreClient.UpdateLargeFilesUpload();
        }
        private static string UpdateLargeFilesUpload()
        {
            if (AssetStoreClient.s_UploadingLargeFile == null)
            {
                if (AssetStoreClient.s_PendingLargeFiles.Count == 0)
                {
                    return(null);
                }
                AssetStoreClient.s_UploadingLargeFile = AssetStoreClient.s_PendingLargeFiles[0];
                try
                {
                    AssetStoreClient.s_UploadingLargeFile.Open();
                }
                catch (Exception ex)
                {
                    DebugUtils.LogError("Unable to start uploading:" + AssetStoreClient.s_UploadingLargeFile.FilePath + " Reason: " + ex.Message);
                    AssetStoreClient.s_PendingLargeFiles.Remove(AssetStoreClient.s_UploadingLargeFile);
                    AssetStoreClient.s_PendingLargeFiles = null;
                    return(null);
                }
            }
            AssetStoreClient.LargeFilePending largeFilePending = AssetStoreClient.s_UploadingLargeFile;
            StreamReader streamReader = null;
            WebResponse  webResponse  = null;

            try
            {
                if (largeFilePending == null || largeFilePending.Request == null)
                {
                    return(null);
                }
                byte[] buffer = largeFilePending.Buffer;
                int    num    = 0;
                for (int i = 0; i < 2; i++)
                {
                    num = largeFilePending.RequestFileStream.Read(buffer, 0, buffer.Length);
                    if (num == 0)
                    {
                        break;
                    }
                    largeFilePending.RequestStream.Write(buffer, 0, num);
                    largeFilePending.BytesSent += (long)num;
                }
                if (num != 0)
                {
                    try
                    {
                        double num2  = (double)largeFilePending.BytesSent;
                        double num3  = (double)largeFilePending.BytesToSend;
                        double pctUp = num2 / num3 * 100.0;
                        if (largeFilePending.RequestProgressCallback != null)
                        {
                            largeFilePending.RequestProgressCallback(pctUp, 0.0);
                        }
                    }
                    catch (Exception ex2)
                    {
                        DebugUtils.LogWarning("Progress update error " + ex2.Message);
                    }
                    return(null);
                }
                AssetStoreClient.s_PendingLargeFiles.Remove(largeFilePending);
                AssetStoreClient.s_UploadingLargeFile = null;
                DebugUtils.Log("Finished Uploading: " + largeFilePending.Id);
                webResponse = largeFilePending.Request.GetResponse();
                Stream responseStream = webResponse.GetResponseStream();
                string text;
                try
                {
                    streamReader = new StreamReader(responseStream);
                    text         = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                catch (Exception ex3)
                {
                    DebugUtils.LogError("StreamReader sr");
                    throw ex3;
                }
                AssetStoreResponse job = AssetStoreClient.parseAssetStoreResponse(text, null, null, webResponse.Headers);
                largeFilePending.Close();
                largeFilePending.RequestDoneCallback(job);
                return(text);
            }
            catch (Exception ex4)
            {
                DebugUtils.LogError("UploadingLarge Files Exception:" + ex4.Source);
                if (streamReader != null)
                {
                    streamReader.Close();
                }
                AssetStoreResponse job2 = AssetStoreClient.parseAssetStoreResponse(null, null, ex4, (webResponse == null) ? null : webResponse.Headers);
                largeFilePending.RequestDoneCallback(job2);
                largeFilePending.Close();
                AssetStoreClient.s_PendingLargeFiles.Remove(largeFilePending);
            }
            return(null);
        }