示例#1
0
        /// <summary>
        /// Starts the upload if it is not yet running, completed, cancelled or failed
        /// </summary>
        /// <returns></returns>
        public Task StartUploadAsync()
        {
            if (Completed || Failed || Cancelled)
            {
                UploadOperationEnded?.Invoke(this);
                return(null);
            }

            if (_uploadTask == null)
            {
                _uploadTask = Task.Run(DoUpload);
                Running     = true;
            }
            return(_uploadTask);
        }
示例#2
0
        private void DoUpload()
        {
            try
            {
                if (_byteStreamToUpload != null)
                {
                    Running = true;
                    int bytesToUploadCount = 0;
                    do
                    {
                        byte[] bytesToUpload = new byte[262144];

                        bytesToUploadCount = _byteStreamToUpload.Read(bytesToUpload, 0, 262144);
                        if (bytesToUploadCount > 0)
                        {
                            byte[] targetArray = bytesToUpload.Take((int)bytesToUploadCount).ToArray();
                            fixed(byte *arrayPtr = targetArray)
                            {
                                using (SWIG.UplinkWriteResult sentResult = SWIG.storj_uplink.uplink_upload_write(_upload, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)bytesToUploadCount))
                                {
                                    if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message))
                                    {
                                        _errorMessage = sentResult.error.message;
                                        Failed        = true;
                                        Running       = false;
                                        UploadOperationEnded?.Invoke(this);
                                        return;
                                    }
                                    else
                                    {
                                        BytesSent += sentResult.bytes_written;
                                    }
                                }
                                if (_cancelled)
                                {
                                    using (SWIG.UplinkError abortError = SWIG.storj_uplink.uplink_upload_abort(_upload))
                                    {
                                        if (abortError != null && !string.IsNullOrEmpty(abortError.message))
                                        {
                                            Failed        = true;
                                            _errorMessage = abortError.message;
                                        }
                                        else
                                        {
                                            Cancelled = true;
                                        }
                                    }

                                    Running = false;
                                    UploadOperationEnded?.Invoke(this);
                                    return;
                                }
                                UploadOperationProgressChanged?.Invoke(this);
                                if (!string.IsNullOrEmpty(_errorMessage))
                                {
                                    Failed = true;
                                    UploadOperationEnded?.Invoke(this);
                                    return;
                                }
                            }
                        }
                    } while (bytesToUploadCount > 0);

                    if (_customMetadata != null)
                    {
                        if (customMetadataMutex.WaitOne(1000))
                        {
                            try
                            {
                                _customMetadata.ToSWIG(); //Appends the customMetadata in the go-layer to a global field
                                using (SWIG.UplinkError customMetadataError = SWIG.storj_uplink.upload_set_custom_metadata2(_upload))
                                {
                                    if (customMetadataError != null && !string.IsNullOrEmpty(customMetadataError.message))
                                    {
                                        _errorMessage = customMetadataError.message;
                                        Failed        = true;
                                        UploadOperationEnded?.Invoke(this);
                                        return;
                                    }
                                }
                            }
                            finally
                            {
                                customMetadataMutex.ReleaseMutex();
                            }
                        }
                    }

                    using (SWIG.UplinkError commitError = SWIG.storj_uplink.uplink_upload_commit(_upload))
                    {
                        if (commitError != null && !string.IsNullOrEmpty(commitError.message))
                        {
                            _errorMessage = commitError.message;
                            Failed        = true;
                            UploadOperationEnded?.Invoke(this);
                            return;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(_errorMessage))
                {
                    Failed = true;
                    UploadOperationEnded?.Invoke(this);
                    return;
                }
                Completed = true;
                UploadOperationEnded?.Invoke(this);
            }
            catch (Exception ex)
            {
                Failed        = true;
                _errorMessage = ex.Message;
                return;
            }
            finally
            {
                Running = false;
                UploadOperationEnded?.Invoke(this);
            }
        }
示例#3
0
        private void DoUpload()
        {
            try
            {
                if (_byteStreamToUpload != null)
                {
                    Running = true;
                    int bytesToUploadCount = 0;
                    do
                    {
                        byte[] bytesToUpload = new byte[262144];

                        bytesToUploadCount = _byteStreamToUpload.Read(bytesToUpload, 0, 262144);
                        if (bytesToUploadCount > 0)
                        {
                            SWIG.WriteResult sentResult = SWIG.storj_uplink.upload_write(_uploadResult.upload, new SWIG.SWIGTYPE_p_void(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(bytesToUpload.Take((int)bytesToUploadCount).ToArray(), 0), true), (uint)bytesToUploadCount);
                            if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message))
                            {
                                _errorMessage = sentResult.error.message;
                                Failed        = true;
                                Running       = false;
                                UploadOperationEnded?.Invoke(this);
                                return;
                            }
                            else
                            {
                                BytesSent += sentResult.bytes_written;
                            }

                            SWIG.storj_uplink.free_write_result(sentResult);
                            if (_cancelled)
                            {
                                SWIG.Error abortError = SWIG.storj_uplink.upload_abort(_uploadResult.upload);
                                if (abortError != null && !string.IsNullOrEmpty(abortError.message))
                                {
                                    Failed        = true;
                                    _errorMessage = abortError.message;
                                }
                                else
                                {
                                    Cancelled = true;
                                }
                                SWIG.storj_uplink.free_error(abortError);

                                Running = false;
                                UploadOperationEnded?.Invoke(this);
                                return;
                            }
                            UploadOperationProgressChanged?.Invoke(this);
                            if (!string.IsNullOrEmpty(_errorMessage))
                            {
                                Failed = true;
                                UploadOperationEnded?.Invoke(this);
                                return;
                            }
                        }
                    } while (bytesToUploadCount > 0);

                    if (_customMetadata != null)
                    {
                        if (customMetadataMutex.WaitOne(1000))
                        {
                            try
                            {
                                _customMetadata.ToSWIG(); //Appends the customMetadata in the go-layer to a global field
                                SWIG.Error customMetadataError = SWIG.storj_uplink.upload_set_custom_metadata2(_uploadResult.upload);
                                if (customMetadataError != null && !string.IsNullOrEmpty(customMetadataError.message))
                                {
                                    _errorMessage = customMetadataError.message;
                                    Failed        = true;
                                    UploadOperationEnded?.Invoke(this);
                                    return;
                                }
                                SWIG.storj_uplink.free_error(customMetadataError);
                            }
                            finally
                            {
                                customMetadataMutex.ReleaseMutex();
                            }
                        }
                    }

                    SWIG.Error commitError = SWIG.storj_uplink.upload_commit(_uploadResult.upload);
                    if (commitError != null && !string.IsNullOrEmpty(commitError.message))
                    {
                        _errorMessage = commitError.message;
                        Failed        = true;
                        UploadOperationEnded?.Invoke(this);
                        SWIG.storj_uplink.free_error(commitError);
                        return;
                    }
                    SWIG.storj_uplink.free_error(commitError);
                }
                if (!string.IsNullOrEmpty(_errorMessage))
                {
                    Failed = true;
                    UploadOperationEnded?.Invoke(this);
                    return;
                }
                Completed = true;
                UploadOperationEnded?.Invoke(this);
            }
            catch (Exception ex)
            {
                Failed        = true;
                _errorMessage = ex.Message;
                return;
            }
            finally
            {
                Running = false;
                UploadOperationEnded?.Invoke(this);
            }
        }