Exemplo n.º 1
0
 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;
     }
 }
Exemplo n.º 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);
 }
Exemplo n.º 3
0
 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);
     });
 }
Exemplo n.º 4
0
    private static bool Parse(AssetStoreResponse response, out string error, out JSONValue jval)
    {
        bool flag;

        jval  = new 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
        {
            jval = (new JSONParser(response.data)).Parse();
            if (jval.ContainsKey("error"))
            {
                JSONValue item = jval["error"];
                error = item.AsString(true);
            }
            else if (jval.ContainsKey("status") && jval["status"].AsString(true) != "ok")
            {
                JSONValue jSONValue = jval["message"];
                error = jSONValue.AsString(true);
            }

            return(error != null);
        }
        catch (JSONParseException jSONParseException1)
        {
            JSONParseException jSONParseException = jSONParseException1;
            error = "Error parsing reply from AssetStore";
            DebugUtils.LogError(string.Concat("Error parsing server reply: ", response.data));
            DebugUtils.LogError(jSONParseException.Message);
            flag = true;
        }

        return(flag);
    }
Exemplo n.º 5
0
    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;
                string result = null;
                return(result);
            }
        }
        AssetStoreClient.LargeFilePending largeFilePending = AssetStoreClient.s_UploadingLargeFile;
        StreamReader streamReader = null;
        WebResponse  webResponse  = null;

        try
        {
            string result;
            if (largeFilePending == null || largeFilePending.Request == null)
            {
                result = null;
                return(result);
            }
            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);
                }
                result = null;
                return(result);
            }
            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);
            result = text;
            return(result);
        }
        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);
    }
Exemplo n.º 6
0
    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);
    }
Exemplo n.º 7
0
    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.bytesSend / p.totalBytesToSend * 100.0;
                    double pctDown = 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();
    }
Exemplo n.º 8
0
    private static string UpdateLargeFilesUpload()
    {
        string end;
        string str;
        WebHeaderCollection headers;

        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 exception1)
            {
                Exception exception = exception1;
                DebugUtils.LogError(string.Concat("Unable to start uploading:", AssetStoreClient.s_UploadingLargeFile.FilePath, " Reason: ", exception.Message));
                AssetStoreClient.s_PendingLargeFiles.Remove(AssetStoreClient.s_UploadingLargeFile);
                AssetStoreClient.s_PendingLargeFiles = null;
                str = null;
                return(str);
            }
        }
        AssetStoreClient.LargeFilePending sUploadingLargeFile = AssetStoreClient.s_UploadingLargeFile;
        StreamReader streamReader = null;
        WebResponse  response     = null;

        try
        {
            if (sUploadingLargeFile == null || sUploadingLargeFile.Request == null)
            {
                str = null;
            }
            else
            {
                byte[] buffer = sUploadingLargeFile.Buffer;
                int    num    = 0;
                int    num1   = 0;
                while (num1 < 2)
                {
                    num = sUploadingLargeFile.RequestFileStream.Read(buffer, 0, (int)buffer.Length);
                    if (num != 0)
                    {
                        sUploadingLargeFile.RequestStream.Write(buffer, 0, num);
                        sUploadingLargeFile.BytesSent += (long)num;
                        num1++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (num == 0)
                {
                    AssetStoreClient.s_PendingLargeFiles.Remove(sUploadingLargeFile);
                    AssetStoreClient.s_UploadingLargeFile = null;
                    DebugUtils.Log(string.Concat("Finished Uploading: ", sUploadingLargeFile.Id));
                    response = sUploadingLargeFile.Request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    try
                    {
                        streamReader = new StreamReader(responseStream);
                        end          = streamReader.ReadToEnd();
                        streamReader.Close();
                    }
                    catch (Exception exception3)
                    {
                        Exception exception2 = exception3;
                        DebugUtils.LogError("StreamReader sr");
                        throw exception2;
                    }
                    AssetStoreResponse assetStoreResponse = AssetStoreClient.parseAssetStoreResponse(end, null, null, response.Headers);
                    sUploadingLargeFile.Close();
                    sUploadingLargeFile.RequestDoneCallback(assetStoreResponse);
                    str = end;
                }
                else
                {
                    try
                    {
                        double bytesSent   = (double)sUploadingLargeFile.BytesSent;
                        double bytesToSend = bytesSent / (double)sUploadingLargeFile.BytesToSend * 100;
                        if (sUploadingLargeFile.RequestProgressCallback != null)
                        {
                            sUploadingLargeFile.RequestProgressCallback(bytesToSend, 0);
                        }
                    }
                    catch (Exception exception4)
                    {
                        DebugUtils.LogWarning(string.Concat("Progress update error ", exception4.Message));
                    }
                    str = null;
                }
            }
        }
        catch (Exception exception7)
        {
            Exception exception5 = exception7;
            DebugUtils.LogError(string.Concat("UploadingLarge Files Exception:", exception5.Source));
            if (streamReader != null)
            {
                streamReader.Close();
            }
            Exception exception6 = exception5;
            if (response == null)
            {
                headers = null;
            }
            else
            {
                headers = response.Headers;
            }
            AssetStoreResponse assetStoreResponse1 = AssetStoreClient.parseAssetStoreResponse(null, null, exception6, headers);
            sUploadingLargeFile.RequestDoneCallback(assetStoreResponse1);
            sUploadingLargeFile.Close();
            AssetStoreClient.s_PendingLargeFiles.Remove(sUploadingLargeFile);
            return(null);
        }
        return(str);
    }
Exemplo n.º 9
0
    public static void Update()
    {
        List <AssetStoreClient.Pending> pendings = AssetStoreClient.pending;

        Monitor.Enter(pendings);
        try
        {
            AssetStoreClient.pending.RemoveAll((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 || p.ex == null && p.data == null && p.binData == null)
                {
                    if (p.progressCallback != null && p.statsUpdated)
                    {
                        p.statsUpdated = false;
                        double num     = ((double)((float)p.totalBytesToSend) <= 0 ? 0 : (double)((float)p.bytesSend) / (double)((float)p.totalBytesToSend) * 100);
                        double num1    = ((double)((float)p.totalBytesToReceive) <= 0 ? 0 : (double)((float)p.bytesReceived) / (double)((float)p.totalBytesToReceive) * 100);
                        try
                        {
                            p.progressCallback(num, num1);
                        }
                        catch (Exception exception)
                        {
                            DebugUtils.LogError(string.Concat("Uncaught exception in async net progress callback: ", exception.Message));
                        }
                    }
                    return(false);
                }
                try
                {
                    AssetStoreResponse assetStoreResponse = AssetStoreClient.parseAssetStoreResponse(p.data, p.binData, p.ex, (p.conn != null ? p.conn.ResponseHeaders : null));
                    if (AssetStoreManager.sDbg)
                    {
                        string[] str = new string[] { "Pending done: ", null, null, null, null, null };
                        str[1]       = Thread.CurrentThread.ManagedThreadId.ToString();
                        str[2]       = " ";
                        str[3]       = p.id;
                        str[4]       = " ";
                        str[5]       = assetStoreResponse.data ?? "<nodata>";
                        DebugUtils.Log(string.Concat(str));
                        if (assetStoreResponse.HttpHeaders != null && assetStoreResponse.HttpHeaders.Get("X-Unity-Reason") != null)
                        {
                            DebugUtils.LogWarning(string.Concat("X-Unity-Reason: ", assetStoreResponse.HttpHeaders.Get("X-Unity-Reason")));
                        }
                    }
                    p.callback(assetStoreResponse);
                }
                catch (Exception exception2)
                {
                    Exception exception1 = exception2;
                    DebugUtils.LogError(string.Concat("Uncaught exception in async net callback: ", exception1.Message));
                    DebugUtils.LogError(exception1.StackTrace);
                }
                AssetStoreClient.ReleaseClient(p.conn);
                p.conn = null;
                return(true);
            });
        }
        finally
        {
            Monitor.Exit(pendings);
        }
        AssetStoreClient.UpdateLargeFilesUpload();
    }
Exemplo n.º 10
0
    private static AssetStoreResponse parseAssetStoreResponse(string data, byte[] binData, Exception ex, WebHeaderCollection responseHeaders)
    {
        AssetStoreResponse headers = new AssetStoreResponse()
        {
            data             = data,
            binData          = binData,
            ok               = true,
            HttpErrorMessage = null,
            HttpStatusCode   = -1
        };

        if (ex == null)
        {
            headers.HttpStatusCode = 200;
            headers.HttpHeaders    = responseHeaders;
        }
        else
        {
            WebException webException = null;
            try
            {
                webException = (WebException)ex;
            }
            catch (Exception exception)
            {
            }
            if (webException == null || webException.Response == null || webException.Response.Headers == null)
            {
                DebugUtils.LogError(string.Concat("Invalid server response ", ex.Message));
                DebugUtils.LogError(string.Concat("Stacktrace:", ex.StackTrace));
            }
            else
            {
                headers.HttpHeaders      = webException.Response.Headers;
                headers.HttpStatusCode   = (int)((HttpWebResponse)webException.Response).StatusCode;
                headers.HttpErrorMessage = webException.Message;
                if (headers.HttpStatusCode != 401 && AssetStoreManager.sDbg)
                {
                    WebHeaderCollection webHeaderCollection = webException.Response.Headers;
                    DebugUtils.LogError("\nDisplaying ex the response headers\n");
                    for (int i = 0; i < webHeaderCollection.Count; i++)
                    {
                        DebugUtils.LogError(string.Concat("\t", webHeaderCollection.GetKey(i), " = ", webHeaderCollection.Get(i)));
                    }
                    DebugUtils.Log(string.Concat("status code: ", headers.HttpStatusCode.ToString()));
                }
            }
        }
        if (headers.HttpStatusCode / 100 != 2)
        {
            headers.ok = false;
            if (AssetStoreManager.sDbg)
            {
                DebugUtils.LogError(string.Concat("Request statusCode: ", headers.HttpStatusCode.ToString()));
            }
            if (ex == null)
            {
                headers.HttpErrorMessage = string.Concat("Request status: ", headers.HttpStatusCode.ToString());
            }
            else
            {
                headers.HttpErrorMessage = ex.Message;
            }
        }
        if (ex != null)
        {
            headers.ok = false;
            if (AssetStoreManager.sDbg)
            {
                DebugUtils.LogError(string.Concat("Request exception: ", ex.GetType().ToString(), " - ", ex.Message));
            }
            headers.HttpErrorMessage = ex.Message;
        }
        return(headers);
    }