Пример #1
0
        public bool WriteBytes(byte[] buffer)
        {
            uint bytesSent = 0;

            while (bytesSent != buffer.Length)
            {
                var uploadChunk = buffer.Take((int)(buffer.Length - bytesSent)).ToArray();
                fixed(byte *arrayPtr = uploadChunk)
                {
                    SWIG.WriteResult sentResult = SWIG.storj_uplink.upload_write(_uploadResult.upload, new SWIG.SWIGTYPE_p_void(new IntPtr(arrayPtr), true), (uint)uploadChunk.Length);
                    if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message))
                    {
                        _errorMessage = sentResult.error.message;
                        return(false);
                    }
                    else
                    {
                        bytesSent += sentResult.bytes_written;
                    }

                    SWIG.storj_uplink.free_write_result(sentResult);
                }
            }

            return(true);
        }
        public bool WriteBytes(byte[] buffer)
        {
            uint bytesSent = 0;

            while (bytesSent != buffer.Length)
            {
                var uploadChunk             = buffer.Take((int)(buffer.Length - bytesSent)).ToArray();
                SWIG.WriteResult sentResult = SWIG.storj_uplink.upload_write(_uploadResult.upload, new SWIG.SWIGTYPE_p_void(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(uploadChunk, 0), true), (uint)uploadChunk.Length);
                if (sentResult.error != null && !string.IsNullOrEmpty(sentResult.error.message))
                {
                    _errorMessage = sentResult.error.message;
                    return(false);
                }
                else
                {
                    bytesSent += sentResult.bytes_written;
                }

                SWIG.storj_uplink.free_write_result(sentResult);
            }

            return(true);
        }
Пример #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)
                        {
                            byte[] targetArray = bytesToUpload.Take((int)bytesToUploadCount).ToArray();
                            fixed(byte *arrayPtr = targetArray)
                            {
                                SWIG.WriteResult sentResult = SWIG.storj_uplink.upload_write(_uploadResult.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;
                                }

                                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);
            }
        }