aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/Net
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2017-03-12 15:28:28 -0400
committerGitHub <noreply@github.com>2017-03-12 15:28:28 -0400
commit7ae1de52b45267e399b6cbbaa423665bb006bf79 (patch)
tree227cb95db1ddc171c078ce9bc6f8f717cc2e9e20 /Emby.Common.Implementations/Net
parent3962361ad9940e0c8b2e6d4aea4fc6b1cdd88553 (diff)
parent5e821947491c07eb65db4653db674af55bc4e90b (diff)
Merge pull request #2523 from MediaBrowser/dev
Dev
Diffstat (limited to 'Emby.Common.Implementations/Net')
-rw-r--r--Emby.Common.Implementations/Net/NetAcceptSocket.cs43
-rw-r--r--Emby.Common.Implementations/Net/SocketFactory.cs1
2 files changed, 43 insertions, 1 deletions
diff --git a/Emby.Common.Implementations/Net/NetAcceptSocket.cs b/Emby.Common.Implementations/Net/NetAcceptSocket.cs
index 731ad1b74..e21ffe553 100644
--- a/Emby.Common.Implementations/Net/NetAcceptSocket.cs
+++ b/Emby.Common.Implementations/Net/NetAcceptSocket.cs
@@ -2,6 +2,7 @@
using System.Net;
using System.Net.Sockets;
using System.Threading;
+using System.Threading.Tasks;
using Emby.Common.Implementations.Networking;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Logging;
@@ -59,7 +60,7 @@ namespace Emby.Common.Implementations.Net
#if NET46
Socket.Close();
#else
- Socket.Dispose();
+ Socket.Dispose();
#endif
}
@@ -96,6 +97,46 @@ namespace Emby.Common.Implementations.Net
_acceptor.StartAccept();
}
+#if NET46
+ public Task SendFile(string path, byte[] preBuffer, byte[] postBuffer, CancellationToken cancellationToken)
+ {
+ var options = TransmitFileOptions.Disconnect | TransmitFileOptions.ReuseSocket | TransmitFileOptions.UseKernelApc;
+
+ var completionSource = new TaskCompletionSource<bool>();
+
+ var result = Socket.BeginSendFile(path, preBuffer, postBuffer, options, new AsyncCallback(FileSendCallback), new Tuple<Socket, string, TaskCompletionSource<bool>>(Socket, path, completionSource));
+
+ return completionSource.Task;
+ }
+
+ private void FileSendCallback(IAsyncResult ar)
+ {
+ // Retrieve the socket from the state object.
+ Tuple<Socket, string, TaskCompletionSource<bool>> data = (Tuple<Socket, string, TaskCompletionSource<bool>>)ar.AsyncState;
+
+ var client = data.Item1;
+ var path = data.Item2;
+ var taskCompletion = data.Item3;
+
+ // Complete sending the data to the remote device.
+ try {
+ client.EndSendFile(ar);
+ taskCompletion.TrySetResult(true);
+}
+ catch(SocketException ex){
+ _logger.Info("Socket.SendFile failed for {0}. error code {1}", path, ex.SocketErrorCode);
+ taskCompletion.TrySetException(ex);
+}catch(Exception ex){
+ taskCompletion.TrySetException(ex);
+}
+ }
+#else
+ public Task SendFile(string path, byte[] preBuffer, byte[] postBuffer, CancellationToken cancellationToken)
+ {
+ throw new NotImplementedException();
+ }
+#endif
+
public void Dispose()
{
Socket.Dispose();
diff --git a/Emby.Common.Implementations/Net/SocketFactory.cs b/Emby.Common.Implementations/Net/SocketFactory.cs
index 523f4da85..021613e57 100644
--- a/Emby.Common.Implementations/Net/SocketFactory.cs
+++ b/Emby.Common.Implementations/Net/SocketFactory.cs
@@ -59,6 +59,7 @@ namespace Emby.Common.Implementations.Net
if (remotePort < 0) throw new ArgumentException("remotePort cannot be less than zero.", "remotePort");
var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
+
try
{
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);