private static void CompleteDownload(LocalStorageFileState state)
        {
            bool flag = true;

            LocalStorageAPI.s_log.LogDebug("Download completed for State={0}", new object[] { state });
            HTTPHeader hTTPHeader = LocalStorageAPI.ParseHTTPHeader(state.FileData);

            if (hTTPHeader != null)
            {
                byte[] numArray = new byte[hTTPHeader.ContentLength];
                Array.Copy(state.FileData, hTTPHeader.ContentStart, numArray, 0, hTTPHeader.ContentLength);
                if (LocalStorageAPI.ComputeSHA256(numArray) == state.CH.Sha256Digest)
                {
                    flag = false;
                    LocalStorageAPI.DecompressStateIfPossible(state, numArray);
                }
                else
                {
                    LocalStorageAPI.s_log.LogWarning("Integrity check failed for State={0}", new object[] { state });
                }
            }
            else
            {
                LocalStorageAPI.s_log.LogWarning("Parsinig of HTTP header failed for State={0}", new object[] { state });
            }
            if (flag || state.FileData == null)
            {
                LocalStorageAPI.ExecuteFailedDownload(state);
            }
            else
            {
                LocalStorageAPI.ExecuteSucessfulDownload(state);
            }
        }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            LocalStorageFileState asyncState = (LocalStorageFileState)ar.AsyncState;

            try
            {
                LocalStorageAPI.s_log.LogDebug("ReceiveCallback called for State={0}", new object[] { asyncState });
                int num = asyncState.Socket.EndReceive(ar);
                if (num <= 0)
                {
                    LocalStorageAPI.CompleteDownload(asyncState);
                }
                else
                {
                    int          num1         = (asyncState.FileData != null ? (int)asyncState.FileData.Length : 0) + num;
                    MemoryStream memoryStream = new MemoryStream(new byte[num1], 0, num1, true, true);
                    if (asyncState.FileData != null)
                    {
                        memoryStream.Write(asyncState.FileData, 0, (int)asyncState.FileData.Length);
                    }
                    memoryStream.Write(asyncState.ReceiveBuffer, 0, num);
                    asyncState.FileData = memoryStream.GetBuffer();
                    asyncState.Socket.BeginReceive(asyncState.ReceiveBuffer, 0, LocalStorageAPI.m_bufferSize, SocketFlags.None, new AsyncCallback(LocalStorageAPI.ReceiveCallback), asyncState);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[] { exception.Message });
                asyncState.FailureMessage = exception.Message;
                LocalStorageAPI.ExecuteFailedDownload(asyncState);
            }
        }
 private static void StoreStateToDrive(LocalStorageFileState state)
 {
     try
     {
         LocalStorageAPI.s_log.LogDebug("StoreState State={0}", new object[] { state });
         string str = LocalStorageAPI.MakeFullPathFromState(state);
         LocalStorageAPI.s_log.LogDebug("Attempting to save {0}", new object[] { str });
         if (!File.Exists(str))
         {
             FileStream fileStream = File.Create(str, (int)state.FileData.Length);
             if (state.CompressedData != null)
             {
                 LocalStorageAPI.s_log.LogDebug("Writing compressed file to disk");
                 fileStream.Write(state.CompressedData, 0, (int)state.CompressedData.Length);
             }
             else
             {
                 LocalStorageAPI.s_log.LogDebug("Writing uncompressed file to disk");
                 fileStream.Write(state.FileData, 0, (int)state.FileData.Length);
             }
             fileStream.Flush();
             fileStream.Close();
             LocalStorageAPI.s_log.LogDebug("Writing completed");
         }
         else
         {
             LocalStorageAPI.s_log.LogDebug("Unable to save the file, it already exists");
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         LocalStorageAPI.s_log.LogWarning("EXCEPTION (StoreStateToDrive): {0}", new object[] { exception.Message });
     }
 }
 private static HTTPHeader ParseHTTPHeader(byte[] data)
 {
     try
     {
         int num = LocalStorageAPI.SearchForBytePattern(data, new byte[]
         {
             13,
             10,
             13,
             10
         });
         HTTPHeader result;
         if (num == -1)
         {
             result = null;
             return(result);
         }
         int num2 = num + 1;
         if (num2 >= data.Length)
         {
             result = null;
             return(result);
         }
         string @string = Encoding.get_ASCII().GetString(data, 0, num);
         if (@string.IndexOf("200 OK") == -1)
         {
             result = null;
             return(result);
         }
         Regex regex = new Regex("(?<=Content-Length:\\s)\\d+", 1);
         Match match = regex.Match(@string);
         if (!match.get_Success())
         {
             result = null;
             return(result);
         }
         int num3 = (int)uint.Parse(match.get_Value());
         int num4 = data.Length - num2;
         if (num3 != num4)
         {
             result = null;
             return(result);
         }
         result = new HTTPHeader
         {
             ContentLength = num3,
             ContentStart  = num2
         };
         return(result);
     }
     catch (Exception ex)
     {
         LocalStorageAPI.s_log.LogWarning("EXCEPTION (ParseHTTPHeader): {0}", new object[]
         {
             ex.get_Message()
         });
     }
     return(null);
 }
        private void DownloadFromDepot(LocalStorageFileState state)
        {
            string str = string.Format("{0}.depot.battle.net", state.CH.Region);

            LocalStorageAPI.s_log.LogDebug("Starting download from {0}", new object[] { str });
            state.Connection.LogDebug   = new Action <string>(LocalStorageAPI.s_log.LogDebug);
            state.Connection.LogWarning = new Action <string>(LocalStorageAPI.s_log.LogWarning);
            state.Connection.OnFailure  = () => LocalStorageAPI.ExecuteFailedDownload(state);
            state.Connection.OnSuccess  = () => LocalStorageAPI.ConnectCallback(state);
            state.Connection.Connect(str, this.DepotPort);
        }
        private static void DecompressStateIfPossible(LocalStorageFileState state, byte[] data)
        {
            ulong num;

            if (!LocalStorageAPI.IsCompressedStream(data, out num))
            {
                state.FileData = data;
            }
            else
            {
                state.CompressedData = data;
                MemoryStream memoryStream = new MemoryStream(data, 16, (int)data.Length - 16);
                state.FileData = LocalStorageAPI.Inflate(memoryStream, (int)num);
            }
        }
 private bool LoadStateFromDrive(LocalStorageFileState state)
 {
     try
     {
         LocalStorageAPI.s_log.LogDebug("LoadState State={0}", new object[]
         {
             state
         });
         string text = LocalStorageAPI.MakeFullPathFromState(state);
         LocalStorageAPI.s_log.LogDebug("Attempting to load {0}", new object[]
         {
             text
         });
         if (!File.Exists(text))
         {
             LocalStorageAPI.s_log.LogDebug("File does not exist, unable to load from disk.");
             bool result = false;
             return(result);
         }
         FileStream fileStream = File.OpenRead(text);
         byte[]     array      = new byte[fileStream.get_Length()];
         fileStream.Read(array, 0, array.Length);
         fileStream.Close();
         if (LocalStorageAPI.ComputeSHA256(array) != state.CH.Sha256Digest)
         {
             LocalStorageAPI.s_log.LogDebug("File was loaded but integrity check failed, attempting to delete ...");
             File.Delete(text);
             bool result = false;
             return(result);
         }
         LocalStorageAPI.DecompressStateIfPossible(state, array);
         LocalStorageAPI.s_log.LogDebug("Loading completed");
         LocalStorageAPI.FinalizeState(state);
     }
     catch (Exception ex)
     {
         LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[]
         {
             ex.get_Message()
         });
         bool result = false;
         return(result);
     }
     return(true);
 }
        private bool LoadStateFromDrive(LocalStorageFileState state)
        {
            bool flag;

            try
            {
                LocalStorageAPI.s_log.LogDebug("LoadState State={0}", new object[] { state });
                string str = LocalStorageAPI.MakeFullPathFromState(state);
                LocalStorageAPI.s_log.LogDebug("Attempting to load {0}", new object[] { str });
                if (File.Exists(str))
                {
                    FileStream fileStream = File.OpenRead(str);
                    byte[]     numArray   = new byte[checked ((IntPtr)fileStream.Length)];
                    fileStream.Read(numArray, 0, (int)numArray.Length);
                    fileStream.Close();
                    if (LocalStorageAPI.ComputeSHA256(numArray) == state.CH.Sha256Digest)
                    {
                        LocalStorageAPI.DecompressStateIfPossible(state, numArray);
                        LocalStorageAPI.s_log.LogDebug("Loading completed");
                        LocalStorageAPI.FinalizeState(state);
                        return(true);
                    }
                    else
                    {
                        LocalStorageAPI.s_log.LogDebug("File was loaded but integrity check failed, attempting to delete ...");
                        File.Delete(str);
                        flag = false;
                    }
                }
                else
                {
                    LocalStorageAPI.s_log.LogDebug("File does not exist, unable to load from disk.");
                    flag = false;
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[] { exception.Message });
                flag = false;
            }
            return(flag);
        }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            LocalStorageFileState localStorageFileState = (LocalStorageFileState)ar.get_AsyncState();

            try
            {
                LocalStorageAPI.s_log.LogDebug("ReceiveCallback called for State={0}", new object[]
                {
                    localStorageFileState
                });
                int num = localStorageFileState.Socket.EndReceive(ar);
                if (num > 0)
                {
                    int          num2         = (localStorageFileState.FileData != null) ? localStorageFileState.FileData.Length : 0;
                    int          num3         = num2 + num;
                    MemoryStream memoryStream = new MemoryStream(new byte[num3], 0, num3, true, true);
                    if (localStorageFileState.FileData != null)
                    {
                        memoryStream.Write(localStorageFileState.FileData, 0, localStorageFileState.FileData.Length);
                    }
                    memoryStream.Write(localStorageFileState.ReceiveBuffer, 0, num);
                    localStorageFileState.FileData = memoryStream.GetBuffer();
                    localStorageFileState.Socket.BeginReceive(localStorageFileState.ReceiveBuffer, 0, LocalStorageAPI.m_bufferSize, 0, new AsyncCallback(LocalStorageAPI.ReceiveCallback), localStorageFileState);
                }
                else
                {
                    LocalStorageAPI.CompleteDownload(localStorageFileState);
                }
            }
            catch (Exception ex)
            {
                LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[]
                {
                    ex.get_Message()
                });
                localStorageFileState.FailureMessage = ex.get_Message();
                LocalStorageAPI.ExecuteFailedDownload(localStorageFileState);
            }
        }
        private static HTTPHeader ParseHTTPHeader(byte[] data)
        {
            HTTPHeader hTTPHeader;

            try
            {
                int num = LocalStorageAPI.SearchForBytePattern(data, new byte[] { 13, 10, 13, 10 });
                if (num != -1)
                {
                    int num1 = num + 1;
                    if (num1 < (int)data.Length)
                    {
                        string str = Encoding.ASCII.GetString(data, 0, num);
                        if (str.IndexOf("200 OK") != -1)
                        {
                            Match match = (new Regex("(?<=Content-Length:\\s)\\d+", RegexOptions.IgnoreCase)).Match(str);
                            if (match.Success)
                            {
                                int num2 = (int)uint.Parse(match.Value);
                                if (num2 == (int)data.Length - num1)
                                {
                                    HTTPHeader hTTPHeader1 = new HTTPHeader()
                                    {
                                        ContentLength = num2,
                                        ContentStart  = num1
                                    };
                                    hTTPHeader = hTTPHeader1;
                                }
                                else
                                {
                                    hTTPHeader = null;
                                }
                            }
                            else
                            {
                                hTTPHeader = null;
                            }
                        }
                        else
                        {
                            hTTPHeader = null;
                        }
                    }
                    else
                    {
                        hTTPHeader = null;
                    }
                }
                else
                {
                    hTTPHeader = null;
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                LocalStorageAPI.s_log.LogWarning("EXCEPTION (ParseHTTPHeader): {0}", new object[] { exception.Message });
                return(null);
            }
            return(hTTPHeader);
        }
 private static void FinalizeDownload(LocalStorageFileState state)
 {
     state.Connection.Disconnect();
     state.ReceiveBuffer = null;
     LocalStorageAPI.FinalizeState(state);
 }
 private static void ExecuteSucessfulDownload(LocalStorageFileState state)
 {
     LocalStorageAPI.StoreStateToDrive(state);
     LocalStorageAPI.FinalizeDownload(state);
 }
 private static void ExecuteFailedDownload(LocalStorageFileState state)
 {
     state.FileData = null;
     LocalStorageAPI.FinalizeDownload(state);
 }
Esempio n. 14
0
        public void SetConnectionMeteringContentHandles(ConnectionMeteringContentHandles handles, LocalStorageAPI localStorage)
        {
            if (handles == null || !handles.IsInitialized || handles.ContentHandleCount == 0)
            {
                this.m_cmLogSource.LogWarning("Invalid connection metering content handle received.");
                return;
            }
            if (handles.ContentHandleCount != 1)
            {
                this.m_cmLogSource.LogWarning("More than 1 connection metering content handle specified!");
            }
            bnet.protocol.ContentHandle contentHandle = handles.ContentHandle.get_Item(0);
            if (contentHandle == null || !contentHandle.IsInitialized)
            {
                this.m_cmLogSource.LogWarning("The content handle received is not valid!");
                return;
            }
            this.m_cmLogSource.LogDebug("Received request to enable connection metering.");
            ContentHandle contentHandle2 = ContentHandle.FromProtocol(contentHandle);

            this.m_cmLogSource.LogDebug("Requesting file from local storage. ContentHandle={0}", new object[]
            {
                contentHandle2
            });
            localStorage.GetFile(contentHandle2, new LocalStorageAPI.DownloadCompletedCallback(this.DownloadCompletedCallback), null);
        }