The Free Software Media System - Server Backend & API
fix socket errors on linux under .net core
Luke Pulverenti524e7facc87e
diff --git a/Emby.Common.Implementations/Net/SocketFactory.cs b/Emby.Common.Implementations/Net/SocketFactory.csindex 1f41ffff5c..70c7ba8458 100644--- a/Emby.Common.Implementations/Net/SocketFactory.cs+++ b/Emby.Common.Implementations/Net/SocketFactory.cs@@ -125,15 +125,15 @@ namespace Emby.Common.Implementations.Nettry{-#if NETSTANDARD1_3- // The ExclusiveAddressUse socket option is a Windows-specific option that, when set to "true," tells Windows not to allow another socket to use the same local address as this socket- // See https://github.com/dotnet/corefx/pull/11509 for more details- if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))+#if NET46+ retVal.ExclusiveAddressUse = false;+#else+ // The ExclusiveAddressUse socket option is a Windows-specific option that, when set to "true," tells Windows not to allow another socket to use the same local address as this socket+ // See https://github.com/dotnet/corefx/pull/11509 for more details+ if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)){retVal.ExclusiveAddressUse = false;}-#else- retVal.ExclusiveAddressUse = false;#endif//retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);diff --git a/Emby.Common.Implementations/Networking/NetworkManager.cs b/Emby.Common.Implementations/Networking/NetworkManager.csindex a4e6d47d40..4485e8b14e 100644--- a/Emby.Common.Implementations/Networking/NetworkManager.cs+++ b/Emby.Common.Implementations/Networking/NetworkManager.cs@@ -245,7 +245,7 @@ namespace Emby.Common.Implementations.Networking//}return ipProperties.UnicastAddresses- .Where(i => i.IsDnsEligible)+ //.Where(i => i.IsDnsEligible).Select(i => i.Address).Where(i => i.AddressFamily == AddressFamily.InterNetwork).ToList();diff --git a/Emby.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs b/Emby.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.csindex 0a312f7b96..9823a2ff50 100644--- a/Emby.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs+++ b/Emby.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs@@ -102,11 +102,7 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp/// <returns>Task.</returns>public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken){- var completionSource = new TaskCompletionSource<bool>();-- WebSocket.SendAsync(bytes, res => completionSource.TrySetResult(true));-- return completionSource.Task;+ return WebSocket.SendAsync(bytes);}/// <summary>@@ -118,11 +114,7 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp/// <returns>Task.</returns>public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken){- var completionSource = new TaskCompletionSource<bool>();-- WebSocket.SendAsync(text, res => completionSource.TrySetResult(true));-- return completionSource.Task;+ return WebSocket.SendAsync(text);}/// <summary>diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.csindex 7a8951396a..07ec6d955a 100644--- a/MediaBrowser.Api/BaseApiService.cs+++ b/MediaBrowser.Api/BaseApiService.cs@@ -151,9 +151,10 @@ namespace MediaBrowser.Api}if (client.IndexOf("web", StringComparison.OrdinalIgnoreCase) == -1 &&++ // covers both emby mobile and emby for android mobileclient.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) == -1 &&client.IndexOf("ios", StringComparison.OrdinalIgnoreCase) == -1 &&- client.IndexOf("android", StringComparison.OrdinalIgnoreCase) == -1 &&client.IndexOf("theater", StringComparison.OrdinalIgnoreCase) == -1){options.Fields.Add(Model.Querying.ItemFields.ChildCount);diff --git a/SocketHttpListener.Portable/WebSocket.cs b/SocketHttpListener.Portable/WebSocket.csindex 889880387e..9966d3fcf9 100644--- a/SocketHttpListener.Portable/WebSocket.cs+++ b/SocketHttpListener.Portable/WebSocket.cs@@ -5,6 +5,7 @@ using System.IO;using System.Net;using System.Text;using System.Threading;+using System.Threading.Tasks;using MediaBrowser.Model.Cryptography;using MediaBrowser.Model.IO;using SocketHttpListener.Net.WebSockets;@@ -621,26 +622,22 @@ namespace SocketHttpListener}}- private void sendAsync(Opcode opcode, Stream stream, Action<bool> completed)+ private Task sendAsync(Opcode opcode, Stream stream){- Func<Opcode, Stream, bool> sender = send;- sender.BeginInvoke(- opcode,- stream,- ar =>- {- try- {- var sent = sender.EndInvoke(ar);- if (completed != null)- completed(sent);- }- catch (Exception ex)- {- error("An exception has occurred while callback.", ex);- }- },- null);+ var completionSource = new TaskCompletionSource<bool>();+ Task.Run(() =>+ {+ try+ {+ send(opcode, stream);+ completionSource.TrySetResult(true);+ }+ catch (Exception ex)+ {+ completionSource.TrySetException(ex);+ }+ });+ return completionSource.Task;}// As server@@ -833,22 +830,18 @@ namespace SocketHttpListener/// <param name="data">/// An array of <see cref="byte"/> that represents the binary data to send./// </param>- /// <param name="completed">/// An Action<bool> delegate that references the method(s) called when the send is/// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is/// complete successfully; otherwise, <c>false</c>.- /// </param>- public void SendAsync(byte[] data, Action<bool> completed)+ public Task SendAsync(byte[] data){var msg = _readyState.CheckIfOpen() ?? data.CheckIfValidSendData();if (msg != null){- error(msg);-- return;+ throw new Exception(msg);}- sendAsync(Opcode.Binary, _memoryStreamFactory.CreateNew(data), completed);+ return sendAsync(Opcode.Binary, _memoryStreamFactory.CreateNew(data));}/// <summary>@@ -860,22 +853,18 @@ namespace SocketHttpListener/// <param name="data">/// A <see cref="string"/> that represents the text data to send./// </param>- /// <param name="completed">/// An Action<bool> delegate that references the method(s) called when the send is/// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is/// complete successfully; otherwise, <c>false</c>.- /// </param>- public void SendAsync(string data, Action<bool> completed)+ public Task SendAsync(string data){var msg = _readyState.CheckIfOpen() ?? data.CheckIfValidSendData();if (msg != null){- error(msg);-- return;+ throw new Exception(msg);}- sendAsync(Opcode.Text, _memoryStreamFactory.CreateNew(Encoding.UTF8.GetBytes(data)), completed);+ return sendAsync(Opcode.Text, _memoryStreamFactory.CreateNew(Encoding.UTF8.GetBytes(data)));}#endregiondiff --git a/src/Emby.Server/Program.cs b/src/Emby.Server/Program.csindex 24e391c739..26141a0ce9 100644--- a/src/Emby.Server/Program.cs+++ b/src/Emby.Server/Program.cs@@ -215,9 +215,12 @@ namespace Emby.Servervar initProgress = new Progress<double>();- // Not crazy about this but it's the only way to suppress ffmpeg crash dialog boxes- SetErrorMode(ErrorModes.SEM_FAILCRITICALERRORS | ErrorModes.SEM_NOALIGNMENTFAULTEXCEPT |- ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);+ if (environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows)+ {+ // Not crazy about this but it's the only way to suppress ffmpeg crash dialog boxes+ SetErrorMode(ErrorModes.SEM_FAILCRITICALERRORS | ErrorModes.SEM_NOALIGNMENTFAULTEXCEPT |+ ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);+ }var task = _appHost.Init(initProgress);Task.WaitAll(task);