@ -5,6 +5,7 @@ using System.IO;
using System.Net ;
using System.Net ;
using System.Text ;
using System.Text ;
using System.Threading ;
using System.Threading ;
using System.Threading.Tasks ;
using MediaBrowser.Model.Cryptography ;
using MediaBrowser.Model.Cryptography ;
using MediaBrowser.Model.IO ;
using MediaBrowser.Model.IO ;
using SocketHttpListener.Net.WebSockets ;
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 ;
var completionSource = new TaskCompletionSource < bool > ( ) ;
sender . BeginInvoke (
Task . Run ( ( ) = >
opcode ,
stream ,
ar = >
{
{
try
try
{
{
var sent = sender . EndInvoke ( ar ) ;
send ( opcode , stream ) ;
if ( completed ! = null )
completionSource . TrySetResult ( true ) ;
completed ( sent ) ;
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
error ( "An exception has occurred while callback." , ex ) ;
completionSource . TrySetException ( ex ) ;
}
}
} ,
} ) ;
null ) ;
return completionSource . Task ;
}
}
// As server
// As server
@ -833,22 +830,18 @@ namespace SocketHttpListener
/// <param name="data">
/// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send.
/// An array of <see cref="byte"/> that represents the binary data to send.
/// </param>
/// </param>
/// <param name="completed">
/// An Action<bool> delegate that references the method(s) called when the send is
/// 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. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param>
public Task SendAsync ( byte [ ] data )
public void SendAsync ( byte [ ] data , Action < bool > completed )
{
{
var msg = _readyState . CheckIfOpen ( ) ? ? data . CheckIfValidSendData ( ) ;
var msg = _readyState . CheckIfOpen ( ) ? ? data . CheckIfValidSendData ( ) ;
if ( msg ! = null )
if ( msg ! = null )
{
{
error ( msg ) ;
throw new Exception ( msg ) ;
return ;
}
}
sendAsync ( Opcode . Binary , _memoryStreamFactory . CreateNew ( data ) , completed ) ;
return sendAsync ( Opcode . Binary , _memoryStreamFactory . CreateNew ( data ) ) ;
}
}
/// <summary>
/// <summary>
@ -860,22 +853,18 @@ namespace SocketHttpListener
/// <param name="data">
/// <param name="data">
/// A <see cref="string"/> that represents the text data to send.
/// A <see cref="string"/> that represents the text data to send.
/// </param>
/// </param>
/// <param name="completed">
/// An Action<bool> delegate that references the method(s) called when the send is
/// 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. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param>
public Task SendAsync ( string data )
public void SendAsync ( string data , Action < bool > completed )
{
{
var msg = _readyState . CheckIfOpen ( ) ? ? data . CheckIfValidSendData ( ) ;
var msg = _readyState . CheckIfOpen ( ) ? ? data . CheckIfValidSendData ( ) ;
if ( msg ! = null )
if ( msg ! = null )
{
{
error ( msg ) ;
throw new Exception ( msg ) ;
return ;
}
}
sendAsync ( Opcode . Text , _memoryStreamFactory . CreateNew ( Encoding . UTF8 . GetBytes ( data ) ) , completed ) ;
return sendAsync ( Opcode . Text , _memoryStreamFactory . CreateNew ( Encoding . UTF8 . GetBytes ( data ) ) ) ;
}
}
# endregion
# endregion