From 33014f77aaacb91cfbd820052f1a49af66a214e3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 26 May 2017 02:48:54 -0400 Subject: update GetItems method --- Emby.Common.Implementations/BaseApplicationHost.cs | 1 - .../Diagnostics/CommonProcess.cs | 5 + .../Emby.Common.Implementations.csproj | 1 + .../HttpClientManager/HttpClientManager.cs | 2 +- Emby.Common.Implementations/IO/ProgressStream.cs | 240 +++++++++++++++++++++ Emby.Common.Implementations/Net/NetAcceptSocket.cs | 12 ++ .../Serialization/XmlSerializer.cs | 1 - 7 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 Emby.Common.Implementations/IO/ProgressStream.cs (limited to 'Emby.Common.Implementations') diff --git a/Emby.Common.Implementations/BaseApplicationHost.cs b/Emby.Common.Implementations/BaseApplicationHost.cs index 835088feae..8d0ee993f1 100644 --- a/Emby.Common.Implementations/BaseApplicationHost.cs +++ b/Emby.Common.Implementations/BaseApplicationHost.cs @@ -31,7 +31,6 @@ using Emby.Common.Implementations.Net; using Emby.Common.Implementations.EnvironmentInfo; using Emby.Common.Implementations.Threading; using MediaBrowser.Common; -using MediaBrowser.Common.IO; using MediaBrowser.Model.Cryptography; using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.Net; diff --git a/Emby.Common.Implementations/Diagnostics/CommonProcess.cs b/Emby.Common.Implementations/Diagnostics/CommonProcess.cs index f6ca6cf9c8..afd30bc47f 100644 --- a/Emby.Common.Implementations/Diagnostics/CommonProcess.cs +++ b/Emby.Common.Implementations/Diagnostics/CommonProcess.cs @@ -98,6 +98,11 @@ namespace Emby.Common.Implementations.Diagnostics return _process.WaitForExit(timeMs); } + public Task WaitForExitAsync(int timeMs) + { + return Task.FromResult(_process.WaitForExit(timeMs)); + } + public void Dispose() { _process.Dispose(); diff --git a/Emby.Common.Implementations/Emby.Common.Implementations.csproj b/Emby.Common.Implementations/Emby.Common.Implementations.csproj index 7ee6a264bc..de2a872cd5 100644 --- a/Emby.Common.Implementations/Emby.Common.Implementations.csproj +++ b/Emby.Common.Implementations/Emby.Common.Implementations.csproj @@ -72,6 +72,7 @@ + diff --git a/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs index c2a310c0e1..5bd18cb808 100644 --- a/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -1,7 +1,6 @@ using System.Net.Sockets; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Net; @@ -17,6 +16,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Emby.Common.Implementations.HttpClientManager; +using Emby.Common.Implementations.IO; using MediaBrowser.Model.IO; using MediaBrowser.Common; diff --git a/Emby.Common.Implementations/IO/ProgressStream.cs b/Emby.Common.Implementations/IO/ProgressStream.cs new file mode 100644 index 0000000000..fb8cf86dfc --- /dev/null +++ b/Emby.Common.Implementations/IO/ProgressStream.cs @@ -0,0 +1,240 @@ +using System; +using System.IO; + +namespace Emby.Common.Implementations.IO +{ + /// + /// Measures progress when reading from a stream or writing to one + /// + public class ProgressStream : Stream + { + /// + /// Gets the base stream. + /// + /// The base stream. + public Stream BaseStream { get; private set; } + + /// + /// Gets or sets the bytes processed. + /// + /// The bytes processed. + private long BytesProcessed { get; set; } + /// + /// Gets or sets the length of the write. + /// + /// The length of the write. + private long WriteLength { get; set; } + + /// + /// Gets or sets the length of the read. + /// + /// The length of the read. + private long? ReadLength { get; set; } + + /// + /// Gets or sets the progress action. + /// + /// The progress action. + private Action ProgressAction { get; set; } + + /// + /// Creates the read progress stream. + /// + /// The base stream. + /// The progress action. + /// Length of the read. + /// ProgressStream. + public static ProgressStream CreateReadProgressStream(Stream baseStream, Action progressAction, long? readLength = null) + { + return new ProgressStream + { + BaseStream = baseStream, + ProgressAction = progressAction, + ReadLength = readLength + }; + } + + /// + /// Creates the write progress stream. + /// + /// The base stream. + /// The progress action. + /// Length of the write. + /// ProgressStream. + public static ProgressStream CreateWriteProgressStream(Stream baseStream, Action progressAction, long writeLength) + { + return new ProgressStream + { + BaseStream = baseStream, + ProgressAction = progressAction, + WriteLength = writeLength + }; + } + + /// + /// When overridden in a derived class, gets a value indicating whether the current stream supports reading. + /// + /// true if this instance can read; otherwise, false. + /// true if the stream supports reading; otherwise, false. + public override bool CanRead + { + get { return BaseStream.CanRead; } + } + + /// + /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking. + /// + /// true if this instance can seek; otherwise, false. + /// true if the stream supports seeking; otherwise, false. + public override bool CanSeek + { + get { return BaseStream.CanSeek; } + } + + /// + /// When overridden in a derived class, gets a value indicating whether the current stream supports writing. + /// + /// true if this instance can write; otherwise, false. + /// true if the stream supports writing; otherwise, false. + public override bool CanWrite + { + get { return BaseStream.CanWrite; } + } + + /// + /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. + /// + public override void Flush() + { + BaseStream.Flush(); + } + + /// + /// When overridden in a derived class, gets the length in bytes of the stream. + /// + /// The length. + /// A long value representing the length of the stream in bytes. + public override long Length + { + get { return BaseStream.Length; } + } + + /// + /// When overridden in a derived class, gets or sets the position within the current stream. + /// + /// The position. + /// The current position within the stream. + public override long Position + { + get { return BaseStream.Position; } + set + { + BaseStream.Position = value; + } + } + + /// + /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. + /// + /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. + /// The zero-based byte offset in at which to begin storing the data read from the current stream. + /// The maximum number of bytes to be read from the current stream. + /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. + public override int Read(byte[] buffer, int offset, int count) + { + var read = BaseStream.Read(buffer, offset, count); + + BytesProcessed += read; + + double percent = BytesProcessed; + percent /= ReadLength ?? BaseStream.Length; + percent *= 100; + + ProgressAction(percent); + + return read; + } + + public override int EndRead(IAsyncResult asyncResult) + { + var read = base.EndRead(asyncResult); + + BytesProcessed += read; + + double percent = BytesProcessed; + percent /= ReadLength ?? BaseStream.Length; + percent *= 100; + + ProgressAction(percent); + + return read; + } + + /// + /// When overridden in a derived class, sets the position within the current stream. + /// + /// A byte offset relative to the parameter. + /// A value of type indicating the reference point used to obtain the new position. + /// The new position within the current stream. + public override long Seek(long offset, SeekOrigin origin) + { + return BaseStream.Seek(offset, origin); + } + + /// + /// When overridden in a derived class, sets the length of the current stream. + /// + /// The desired length of the current stream in bytes. + public override void SetLength(long value) + { + BaseStream.SetLength(value); + } + + /// + /// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. + /// + /// An array of bytes. This method copies bytes from to the current stream. + /// The zero-based byte offset in at which to begin copying bytes to the current stream. + /// The number of bytes to be written to the current stream. + public override void Write(byte[] buffer, int offset, int count) + { + BaseStream.Write(buffer, offset, count); + + BytesProcessed += count; + + double percent = BytesProcessed; + percent /= WriteLength; + percent *= 100; + + ProgressAction(percent); + } + + public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) + { + var result = base.BeginWrite(buffer, offset, count, callback, state); + + BytesProcessed += count; + + double percent = BytesProcessed; + percent /= WriteLength; + percent *= 100; + + ProgressAction(percent); + + return result; + } + + /// + /// Releases the unmanaged resources used by the and optionally releases the managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected override void Dispose(bool disposing) + { + if (disposing) + { + BaseStream.Dispose(); + } + base.Dispose(disposing); + } + } +} diff --git a/Emby.Common.Implementations/Net/NetAcceptSocket.cs b/Emby.Common.Implementations/Net/NetAcceptSocket.cs index 5f97fd8547..5e831ac7ab 100644 --- a/Emby.Common.Implementations/Net/NetAcceptSocket.cs +++ b/Emby.Common.Implementations/Net/NetAcceptSocket.cs @@ -108,6 +108,18 @@ namespace Emby.Common.Implementations.Net return completionSource.Task; } + public IAsyncResult BeginSendFile(string path, byte[] preBuffer, byte[] postBuffer, AsyncCallback callback, object state) + { + var options = TransmitFileOptions.UseDefaultWorkerThread; + + return Socket.BeginSendFile(path, preBuffer, postBuffer, options, new AsyncCallback(FileSendCallback), state); + } + + public void EndSendFile(IAsyncResult result) + { + Socket.EndSendFile(result); + } + private void FileSendCallback(IAsyncResult ar) { // Retrieve the socket from the state object. diff --git a/Emby.Common.Implementations/Serialization/XmlSerializer.cs b/Emby.Common.Implementations/Serialization/XmlSerializer.cs index ad2708387b..b5896e6b0e 100644 --- a/Emby.Common.Implementations/Serialization/XmlSerializer.cs +++ b/Emby.Common.Implementations/Serialization/XmlSerializer.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Serialization; -using MediaBrowser.Common.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; -- cgit v1.2.3