aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/Net
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2017-03-13 15:01:49 -0400
committerGitHub <noreply@github.com>2017-03-13 15:01:49 -0400
commitafb4a08bfe94da3b29d6f962665f93824e94f203 (patch)
treecf21a536f930f54e3847bf243afb130276c3097c /Emby.Common.Implementations/Net
parent7ae1de52b45267e399b6cbbaa423665bb006bf79 (diff)
parenta9b61af1549770b5a3c613c6b552f8bb698e9870 (diff)
Merge pull request #2525 from MediaBrowser/dev
Dev
Diffstat (limited to 'Emby.Common.Implementations/Net')
-rw-r--r--Emby.Common.Implementations/Net/NetAcceptSocket.cs2
-rw-r--r--Emby.Common.Implementations/Net/SocketFactory.cs27
-rw-r--r--Emby.Common.Implementations/Net/UdpSocket.cs18
3 files changed, 35 insertions, 12 deletions
diff --git a/Emby.Common.Implementations/Net/NetAcceptSocket.cs b/Emby.Common.Implementations/Net/NetAcceptSocket.cs
index e21ffe553..3721709e6 100644
--- a/Emby.Common.Implementations/Net/NetAcceptSocket.cs
+++ b/Emby.Common.Implementations/Net/NetAcceptSocket.cs
@@ -100,7 +100,7 @@ namespace Emby.Common.Implementations.Net
#if NET46
public Task SendFile(string path, byte[] preBuffer, byte[] postBuffer, CancellationToken cancellationToken)
{
- var options = TransmitFileOptions.Disconnect | TransmitFileOptions.ReuseSocket | TransmitFileOptions.UseKernelApc;
+ var options = TransmitFileOptions.UseKernelApc;
var completionSource = new TaskCompletionSource<bool>();
diff --git a/Emby.Common.Implementations/Net/SocketFactory.cs b/Emby.Common.Implementations/Net/SocketFactory.cs
index 021613e57..0f4306a6b 100644
--- a/Emby.Common.Implementations/Net/SocketFactory.cs
+++ b/Emby.Common.Implementations/Net/SocketFactory.cs
@@ -97,10 +97,31 @@ namespace Emby.Common.Implementations.Net
}
}
+ public ISocket CreateUdpBroadcastSocket(int localPort)
+ {
+ if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
+
+ var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
+ try
+ {
+ retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
+
+ return new UdpSocket(retVal, localPort, IPAddress.Any);
+ }
+ catch
+ {
+ if (retVal != null)
+ retVal.Dispose();
+
+ throw;
+ }
+ }
+
/// <summary>
- /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
- /// </summary>
- /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
+ /// Creates a new UDP acceptSocket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
+ /// </summary>
+ /// <returns>An implementation of the <see cref="ISocket"/> interface used by RSSDP components to perform acceptSocket operations.</returns>
public ISocket CreateSsdpUdpSocket(IpAddressInfo localIpAddress, int localPort)
{
if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs
index 8e2a1da6f..b85245ba1 100644
--- a/Emby.Common.Implementations/Net/UdpSocket.cs
+++ b/Emby.Common.Implementations/Net/UdpSocket.cs
@@ -60,6 +60,8 @@ namespace Emby.Common.Implementations.Net
var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
state.TaskCompletionSource = tcs;
+ cancellationToken.Register(() => tcs.TrySetCanceled());
+
#if NETSTANDARD1_6
_Socket.ReceiveFromAsync(new ArraySegment<Byte>(state.Buffer), SocketFlags.None, state.RemoteEndPoint)
.ContinueWith((task, asyncState) =>
@@ -160,7 +162,7 @@ namespace Emby.Common.Implementations.Net
var bytesRead = receiveData();
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
- state.TaskCompletionSource.SetResult(
+ state.TaskCompletionSource.TrySetResult(
new SocketReceiveResult
{
Buffer = state.Buffer,
@@ -172,18 +174,18 @@ namespace Emby.Common.Implementations.Net
}
catch (ObjectDisposedException)
{
- state.TaskCompletionSource.SetCanceled();
+ state.TaskCompletionSource.TrySetCanceled();
}
catch (SocketException se)
{
if (se.SocketErrorCode != SocketError.Interrupted && se.SocketErrorCode != SocketError.OperationAborted && se.SocketErrorCode != SocketError.Shutdown)
- state.TaskCompletionSource.SetException(se);
+ state.TaskCompletionSource.TrySetException(se);
else
- state.TaskCompletionSource.SetCanceled();
+ state.TaskCompletionSource.TrySetCanceled();
}
catch (Exception ex)
{
- state.TaskCompletionSource.SetException(ex);
+ state.TaskCompletionSource.TrySetException(ex);
}
}
@@ -206,7 +208,7 @@ namespace Emby.Common.Implementations.Net
var bytesRead = state.Socket.EndReceiveFrom(asyncResult, ref state.RemoteEndPoint);
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
- state.TaskCompletionSource.SetResult(
+ state.TaskCompletionSource.TrySetResult(
new SocketReceiveResult
{
Buffer = state.Buffer,
@@ -218,11 +220,11 @@ namespace Emby.Common.Implementations.Net
}
catch (ObjectDisposedException)
{
- state.TaskCompletionSource.SetCanceled();
+ state.TaskCompletionSource.TrySetCanceled();
}
catch (Exception ex)
{
- state.TaskCompletionSource.SetException(ex);
+ state.TaskCompletionSource.TrySetException(ex);
}
#endif
}