コード例 #1
0
            // TODO: Change Get and Put to byte arrays?

            public void WriteContent(byte[] data)
            {
                if (!this.MetaWritten)
                {
                    throw new DownloaderException("File content must be written after metadata");
                }

                if (data.Length > this.RemainingLength)
                {
                    throw new DownloaderException("Tried to write more bytes than were remaining in the file.");
                }

                this._dataStream.Write(data, 0, data.Length);

                if (this.RemainingLength == 0)
                {
                    // Append file version if possible
                    if (this._cacheFileInfo?.Version != null)
                    {
                        var writer = new BinaryWriter(this._dataStream);
                        writer.WriteUInt16BigEndian((ushort)this._cacheFileInfo.Version);
                    }

                    this.Completed = true;

                    var file = new BinaryFile
                    {
                        Info = this._cacheFileInfo
                    };

                    file.Decode(this._dataStream.ToArray());

                    this._completionSource.SetResult(file);
                }
            }
コード例 #2
0
        protected override BinaryFile GetBinaryFile(CacheFileInfo fileInfo)
        {
            var file = new BinaryFile
            {
                Info = fileInfo
            };

            file.Decode(this._fileStore.ReadFileData(fileInfo.Index, fileInfo.FileId.Value));

            return(file);
        }
コード例 #3
0
        public async Task <BinaryFile> DownloadFileAsync(Index index, int fileId, CacheFileInfo fileInfo)
        {
            var webRequest = WebRequest.CreateHttp($"{this._baseUrl}/ms?m=0&a={(int)index}&g={fileId}&c={fileInfo.Crc}&v={fileInfo.Version}");

            try
            {
                using (var response = (HttpWebResponse)await webRequest.GetResponseAsync())
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new DownloaderException($"HTTP interface responded with status code: {response.StatusCode}.");
                    }

                    if (response.ContentLength != fileInfo.CompressedSize)
                    {
                        throw new DownloaderException($"Downloaded file size {response.ContentLength} does not match expected {fileInfo.CompressedSize}.");
                    }

                    var dataStream = new MemoryStream();
                    var dataWriter = new BinaryWriter(dataStream);

                    var responseReader = new BinaryReader(response.GetResponseStream());
                    dataWriter.Write(responseReader.ReadBytes((int)response.ContentLength));

                    // Append version
                    dataWriter.WriteUInt16BigEndian((ushort)fileInfo.Version);

                    var file = new BinaryFile
                    {
                        Info = fileInfo
                    };

                    file.Decode(dataStream.ToArray());

                    return(file);
                }
            }
            catch (WebException exception)
            {
                throw new DownloaderException(
                          $"Could not download {(int)index}/{fileId} due to a request error.",
                          exception
                          );
            }
        }