Example #1
0
        private Task <uint> createSaveBufferTask(IBuffer buffer)
        {
            if (OnDataWriting != null)
            {
                var arg = new BufferWriteEventArgs(BufferActions.Write, _position, buffer.Length);
                OnDataWriting(this, ref arg);

                if (!arg.IsAllowed)
                {
                    return(new Task <uint>(() => { return buffer.Length; }));
                }
            }

            return(new Task <uint>(() =>
            {
                uint len = buffer.Length;
                var data = new byte[len];
                buffer.CopyTo(data);

                try
                {
                    doSend(data);
                    if (_position + len > _streamSize)
                    {
                        _streamSize = _position + len;
                    }
                    return len;
                }
                catch
                {
                    DataSendingFailed?.Invoke(this, new EventArgs());
                    return 0;
                }
            }));
        }
Example #2
0
        public void Seek(ulong position)
        {
            BufferWriteEventArgs arg = new BufferWriteEventArgs(BufferActions.Seek, position, 0);

            OnSeekCalled?.Invoke(this, ref arg);

            _position = position;

            if (_position >= _streamSize)
            {
                _streamSize = _position + 1;
            }
        }
Example #3
0
        public IAsyncOperation <bool> FlushAsync()
        {
            BufferWriteEventArgs arg = new BufferWriteEventArgs(BufferActions.Flush, _position, 0);

            OnFlushCalled?.Invoke(this, ref arg);

            var t = new Task <bool>(() =>
            {
                return(true);
            });

            t.RunSynchronously();

            Func <CancellationToken, Task <bool> > tp = (token) => t;

            return(AsyncInfo.Run(tp));
        }
Example #4
0
        public IAsyncOperationWithProgress <IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
        {
            BufferWriteEventArgs arg = new BufferWriteEventArgs(BufferActions.Read, _position, count);

            OnReadCalled?.Invoke(this, ref arg);

            var t = new Task <IBuffer>(() =>
            {
                var d = _virtualStream.ReadAsync(buffer, count, options);
                while (d.Status != AsyncStatus.Completed)
                {
                    ;
                }

                return(buffer);
            });

            t.RunSynchronously();

            Func <CancellationToken, IProgress <uint>, Task <IBuffer> > tp = (token, progress) => t;

            return(AsyncInfo.Run(tp));
        }
Example #5
0
        public IAsyncOperationWithProgress <uint, uint> WriteAsync(IBuffer buffer)
        {
            BufferWriteEventArgs arg = new BufferWriteEventArgs(BufferActions.Write, _position, buffer.Length);

            OnWriteCalled?.Invoke(this, ref arg);

            Task <uint> aTask = new Task <uint>(() =>
            {
                uint len = buffer.Length;
                // Calculate new size of the stream.
                if (_position + len > _streamSize)
                {
                    _streamSize = _position + len;
                }

                return(len);
            });

            aTask.RunSynchronously();

            Func <CancellationToken, IProgress <uint>, Task <uint> > aTaskProvider = (token, progress) => aTask;

            return(AsyncInfo.Run(aTaskProvider));
        }