Esempio n. 1
0
        public unsafe override int Read(byte[] buffer, int offset, int count)
        {
            int readBytes = 0;
            int remaining = count;

            byte[] tmpBuffer = new byte[count];
            fixed(byte *arrayPtr = tmpBuffer)
            {
                while (readBytes < count)
                {
                    using (SWIG.UplinkReadResult readResult = SWIG.storj_uplink.uplink_download_read(_result.download, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)remaining))
                    {
                        Array.Copy(tmpBuffer, 0, buffer, readBytes, (int)readResult.bytes_read);
                        remaining -= (int)readResult.bytes_read;
                        Position  += (int)readResult.bytes_read;
                        readBytes += (int)readResult.bytes_read;
                        if (readResult.error != null && readResult.error.code == -1)
                        {
                            return(readBytes);
                        }
                    }
                }
                return(readBytes);
            }
        }
Esempio n. 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);
        }