aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2017-05-25 00:26:34 -0400
committerGitHub <noreply@github.com>2017-05-25 00:26:34 -0400
commit2715db6ad794588614a2585a51d1cfde6d8e2941 (patch)
tree698657f7a0b1b5891b7e8d3b61175684512f9bf8 /Emby.Common.Implementations
parent5652f2534888199adbfc5c8146b03b794a1f57e4 (diff)
parentd035d7eaec937b1ad43af6a95f723070c1e847ea (diff)
Merge pull request #2662 from MediaBrowser/dev
Dev
Diffstat (limited to 'Emby.Common.Implementations')
-rw-r--r--Emby.Common.Implementations/BaseApplicationHost.cs1
-rw-r--r--Emby.Common.Implementations/Net/UdpSocket.cs69
2 files changed, 46 insertions, 24 deletions
diff --git a/Emby.Common.Implementations/BaseApplicationHost.cs b/Emby.Common.Implementations/BaseApplicationHost.cs
index dd4be9aae4..835088feae 100644
--- a/Emby.Common.Implementations/BaseApplicationHost.cs
+++ b/Emby.Common.Implementations/BaseApplicationHost.cs
@@ -438,7 +438,6 @@ namespace Emby.Common.Implementations
var assemblyFilePath = Path.Combine(ApplicationPaths.PluginsPath, assemblyFileName);
assemblyPlugin.SetAttributes(assemblyFilePath, assemblyFileName, assemblyName.Version, assemblyId);
- return null;
}
var isFirstRun = !File.Exists(plugin.ConfigurationFilePath);
diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs
index 678cf6f035..5e110e4645 100644
--- a/Emby.Common.Implementations/Net/UdpSocket.cs
+++ b/Emby.Common.Implementations/Net/UdpSocket.cs
@@ -116,8 +116,6 @@ namespace Emby.Common.Implementations.Net
private set;
}
- private readonly AsyncCallback _defaultAsyncCallback = (i) => { };
-
public IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback)
{
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
@@ -145,9 +143,30 @@ namespace Emby.Common.Implementations.Net
public Task<SocketReceiveResult> ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
- var result = BeginReceive(buffer, offset, count, _defaultAsyncCallback);
+ var taskCompletion = new TaskCompletionSource<SocketReceiveResult>();
+
+ Action<IAsyncResult> callback = callbackResult =>
+ {
+ try
+ {
+ taskCompletion.TrySetResult(EndReceive(callbackResult));
+ }
+ catch (Exception ex)
+ {
+ taskCompletion.TrySetException(ex);
+ }
+ };
+
+ var result = BeginReceive(buffer, offset, count, new AsyncCallback(callback));
- return Task.Factory.FromAsync(result, EndReceive);
+ if (result.CompletedSynchronously)
+ {
+ callback(result);
+ }
+
+ cancellationToken.Register(() => taskCompletion.TrySetCanceled());
+
+ return taskCompletion.Task;
}
public Task<SocketReceiveResult> ReceiveAsync(CancellationToken cancellationToken)
@@ -159,9 +178,30 @@ namespace Emby.Common.Implementations.Net
public Task SendToAsync(byte[] buffer, int offset, int size, IpEndPointInfo endPoint, CancellationToken cancellationToken)
{
- var result = BeginSendTo(buffer, offset, size, endPoint, _defaultAsyncCallback, null);
+ var taskCompletion = new TaskCompletionSource<int>();
+
+ Action<IAsyncResult> callback = callbackResult =>
+ {
+ try
+ {
+ taskCompletion.TrySetResult(EndSendTo(callbackResult));
+ }
+ catch (Exception ex)
+ {
+ taskCompletion.TrySetException(ex);
+ }
+ };
- return Task.Factory.FromAsync(result, EndSendTo);
+ var result = BeginSendTo(buffer, offset, size, endPoint, new AsyncCallback(callback), null);
+
+ if (result.CompletedSynchronously)
+ {
+ callback(result);
+ }
+
+ cancellationToken.Register(() => taskCompletion.TrySetCanceled());
+
+ return taskCompletion.Task;
}
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, IpEndPointInfo endPoint, AsyncCallback callback, object state)
@@ -208,22 +248,5 @@ namespace Emby.Common.Implementations.Net
return NetworkManager.ToIpEndPointInfo(endpoint);
}
-
- private class AsyncReceiveState
- {
- public AsyncReceiveState(Socket socket, EndPoint remoteEndPoint)
- {
- this.Socket = socket;
- this.RemoteEndPoint = remoteEndPoint;
- }
-
- public EndPoint RemoteEndPoint;
- public byte[] Buffer = new byte[8192];
-
- public Socket Socket { get; private set; }
-
- public TaskCompletionSource<SocketReceiveResult> TaskCompletionSource { get; set; }
-
- }
}
}