private void DoDownload()
        {
            Running = true;
            while (BytesReceived < TotalBytes)
            {
                var tenth = _bytesToDownload.Length / 10;
                if (TotalBytes - BytesReceived > tenth)
                {
                    //Fetch next bytes in batch
                    byte[]          part       = new byte[tenth];
                    SWIG.ReadResult readResult = SWIG.storj_uplink.download_read(_downloadResult.download, new SWIG.SWIGTYPE_p_void(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(part, 0), true), (uint)tenth);
                    if (readResult.error != null && !string.IsNullOrEmpty(readResult.error.message))
                    {
                        _errorMessage = readResult.error.message;
                        Failed        = true;
                        Running       = false;
                        DownloadOperationEnded?.Invoke(this);
                        return;
                    }
                    if (readResult.bytes_read != 0)
                    {
                        Array.Copy(part, 0, _bytesToDownload, (long)BytesReceived, readResult.bytes_read);
                        BytesReceived += readResult.bytes_read;
                    }
                    SWIG.storj_uplink.free_read_result(readResult);
                }
                else
                {
                    //Fetch only the remaining bytes
                    var    remaining = TotalBytes - BytesReceived;
                    byte[] part      = new byte[remaining];

                    SWIG.ReadResult readResult = SWIG.storj_uplink.download_read(_downloadResult.download, new SWIG.SWIGTYPE_p_void(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(part, 0), true), (uint)remaining);

                    if (readResult.error != null && !string.IsNullOrEmpty(readResult.error.message))
                    {
                        _errorMessage = readResult.error.message;
                        Failed        = true;
                        Running       = false;
                        DownloadOperationEnded?.Invoke(this);
                        return;
                    }
                    if (readResult.bytes_read != 0)
                    {
                        Array.Copy(part, 0, _bytesToDownload, (long)BytesReceived, readResult.bytes_read);
                        BytesReceived += readResult.bytes_read;
                    }
                    SWIG.storj_uplink.free_read_result(readResult);
                }

                if (_cancelled)
                {
                    SWIG.Error cancelError = SWIG.storj_uplink.close_download(_downloadResult.download);
                    if (cancelError != null && !string.IsNullOrEmpty(cancelError.message))
                    {
                        _errorMessage = cancelError.message;
                        Failed        = true;
                    }
                    else
                    {
                        Cancelled = true;
                    }
                    SWIG.storj_uplink.free_error(cancelError);

                    Running = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
                DownloadOperationProgressChanged?.Invoke(this);
                if (!string.IsNullOrEmpty(_errorMessage))
                {
                    Failed  = true;
                    Running = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
            }

            SWIG.Error closeError = SWIG.storj_uplink.close_download(_downloadResult.download);

            if (closeError != null && !string.IsNullOrEmpty(closeError.message))
            {
                _errorMessage = closeError.message;
                Failed        = true;
                Running       = false;
                DownloadOperationEnded?.Invoke(this);
                SWIG.storj_uplink.free_error(closeError);
                return;
            }
            SWIG.storj_uplink.free_error(closeError);

            Completed = true;
            Running   = false;
            DownloadOperationEnded?.Invoke(this);
        }
Example #2
0
        private void DoDownload()
        {
            Running = true;
            while (BytesReceived < TotalBytes)
            {
                var tenth = _bytesToDownload.Length / 10;
                if (TotalBytes - BytesReceived > tenth)
                {
                    //Fetch next bytes in batch
                    byte[] part = new byte[tenth];
                    fixed(byte *arrayPtr = part)
                    {
                        using (SWIG.UplinkReadResult readResult = SWIG.storj_uplink.uplink_download_read(_download, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)tenth))
                        {
                            if (readResult.error != null && !string.IsNullOrEmpty(readResult.error.message))
                            {
                                _errorMessage = readResult.error.message;
                                Failed        = true;
                                Running       = false;
                                DownloadOperationEnded?.Invoke(this);
                                return;
                            }
                            if (readResult.bytes_read != 0)
                            {
                                Array.Copy(part, 0, _bytesToDownload, (long)BytesReceived, readResult.bytes_read);
                                BytesReceived += readResult.bytes_read;
                            }
                        }
                    }
                }
                else
                {
                    //Fetch only the remaining bytes
                    var    remaining = TotalBytes - BytesReceived;
                    byte[] part      = new byte[remaining];
                    fixed(byte *arrayPtr = part)
                    {
                        using (SWIG.UplinkReadResult readResult = SWIG.storj_uplink.uplink_download_read(_download, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)remaining))
                        {
                            if (readResult.error != null && !string.IsNullOrEmpty(readResult.error.message))
                            {
                                _errorMessage = readResult.error.message;
                                Failed        = true;
                                Running       = false;
                                DownloadOperationEnded?.Invoke(this);
                                return;
                            }
                            if (readResult.bytes_read != 0)
                            {
                                Array.Copy(part, 0, _bytesToDownload, (long)BytesReceived, readResult.bytes_read);
                                BytesReceived += readResult.bytes_read;
                            }
                        }
                    }
                }

                if (_cancelled)
                {
                    using (SWIG.UplinkError cancelError = SWIG.storj_uplink.uplink_close_download(_download))
                    {
                        if (cancelError != null && !string.IsNullOrEmpty(cancelError.message))
                        {
                            _errorMessage = cancelError.message;
                            Failed        = true;
                        }
                        else
                        {
                            Cancelled = true;
                        }
                    }

                    Running = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
                DownloadOperationProgressChanged?.Invoke(this);
                if (!string.IsNullOrEmpty(_errorMessage))
                {
                    Failed  = true;
                    Running = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
            }

            using (SWIG.UplinkError closeError = SWIG.storj_uplink.uplink_close_download(_download))
            {
                if (closeError != null && !string.IsNullOrEmpty(closeError.message))
                {
                    _errorMessage = closeError.message;
                    Failed        = true;
                    Running       = false;
                    DownloadOperationEnded?.Invoke(this);
                    return;
                }
            }

            Completed = true;
            Running   = false;
            DownloadOperationEnded?.Invoke(this);
        }