diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-12-13 18:38:26 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-12-13 18:38:26 -0500 |
| commit | 524e7facc87e746745af9095a3f100dcec1799b6 (patch) | |
| tree | 3711beec7e1e9aa937e86883e38505beddded382 /SocketHttpListener.Portable/WebSocket.cs | |
| parent | dcd06597a7cc1d4a7829ed75e0a6b1e52fd5c10e (diff) | |
fix socket errors on linux under .net core
Diffstat (limited to 'SocketHttpListener.Portable/WebSocket.cs')
| -rw-r--r-- | SocketHttpListener.Portable/WebSocket.cs | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/SocketHttpListener.Portable/WebSocket.cs b/SocketHttpListener.Portable/WebSocket.cs index 889880387..9966d3fcf 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))); } #endregion |
