Modified sending (Removed SendAsync method)

This commit is contained in:
sta 2013-10-08 20:17:01 +09:00
parent ff390b4136
commit d6f21d31cd
3 changed files with 384 additions and 303 deletions

View File

@ -29,6 +29,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using WebSocketSharp.Net; using WebSocketSharp.Net;
@ -225,6 +226,11 @@ namespace WebSocketSharp.Server
IsBound = true; IsBound = true;
} }
internal void Start ()
{
_websocket.Connect ();
}
#endregion #endregion
#region Protected Methods #region Protected Methods
@ -235,7 +241,7 @@ namespace WebSocketSharp.Server
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains an error message. /// A <see cref="string"/> that contains an error message.
/// </param> /// </param>
protected virtual void Error (string message) protected void Error (string message)
{ {
if (!message.IsNullOrEmpty ()) if (!message.IsNullOrEmpty ())
OnError (new ErrorEventArgs (message)); OnError (new ErrorEventArgs (message));
@ -282,6 +288,246 @@ namespace WebSocketSharp.Server
{ {
} }
/// <summary>
/// Sends a Ping to the client of the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <returns>
/// <c>true</c> if the current <see cref="WebSocketService"/> instance receives a Pong
/// from the client in a time; otherwise, <c>false</c>.
/// </returns>
protected bool Ping ()
{
return IsBound
? _websocket.Ping ()
: false;
}
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client of
/// the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <returns>
/// <c>true</c> if the current <see cref="WebSocketService"/> instance receives a Pong
/// from the client in a time; otherwise, <c>false</c>.
/// </returns>
/// <param name="message">
/// A <see cref="string"/> that contains a message to send.
/// </param>
protected bool Ping (string message)
{
return IsBound
? _websocket.Ping (message)
: false;
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
protected void Send (byte [] data)
{
if (IsBound)
_websocket.Send (data, null);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
protected void Send (string data)
{
if (IsBound)
_websocket.Send (data, null);
}
/// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> to
/// the client of the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="file">
/// A <see cref="FileInfo"/> from which contains a binary data to send.
/// </param>
protected void Send (FileInfo file)
{
if (IsBound)
_websocket.Send (file, null);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; 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>
protected void Send (byte [] data, Action<bool> completed)
{
if (IsBound)
_websocket.Send (data, completed);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; 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>
protected void Send (string data, Action<bool> completed)
{
if (IsBound)
_websocket.Send (data, completed);
}
/// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> to
/// the client of the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="file">
/// A <see cref="FileInfo"/> from which contains a binary data to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; 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>
protected void Send (FileInfo file, Action<bool> completed)
{
if (IsBound)
_websocket.Send (file, completed);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to
/// the client of the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send.
/// </param>
/// <param name="dispose">
/// <c>true</c> if <paramref name="stream"/> is disposed after a binary data read;
/// otherwise, <c>false</c>.
/// </param>
protected void Send (Stream stream, int length, bool dispose)
{
if (IsBound)
_websocket.Send (stream, length, dispose, null);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to
/// the client of the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send.
/// </param>
/// <param name="dispose">
/// <c>true</c> if <paramref name="stream"/> is disposed after a binary data read;
/// otherwise, <c>false</c>.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; 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>
protected void Send (Stream stream, int length, bool dispose, Action<bool> completed)
{
if (IsBound)
_websocket.Send (stream, length, dispose, completed);
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance.
/// </summary>
protected void Stop ()
{
if (IsBound)
_websocket.Close ();
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the specified
/// <see cref="ushort"/> and <see cref="string"/>.
/// </summary>
/// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for stop.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop.
/// </param>
protected void Stop (ushort code, string reason)
{
if (IsBound)
_websocket.Close (code, reason);
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the specified
/// <see cref="CloseStatusCode"/> and <see cref="string"/>.
/// </summary>
/// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicates a status code
/// indicating the reason for stop.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop.
/// </param>
protected void Stop (CloseStatusCode code, string reason)
{
if (IsBound)
_websocket.Close (code, reason);
}
/// <summary> /// <summary>
/// Validates the cookies used in the WebSocket connection request. /// Validates the cookies used in the WebSocket connection request.
/// </summary> /// </summary>
@ -306,152 +552,5 @@ namespace WebSocketSharp.Server
} }
#endregion #endregion
#region Public Methods
/// <summary>
/// Sends a Ping to the client of the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <returns>
/// <c>true</c> if the current <see cref="WebSocketService"/> instance receives a Pong
/// from the client in a time; otherwise, <c>false</c>.
/// </returns>
public bool Ping ()
{
return IsBound
? _websocket.Ping ()
: false;
}
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client of
/// the current <see cref="WebSocketService"/> instance.
/// </summary>
/// <returns>
/// <c>true</c> if the current <see cref="WebSocketService"/> instance receives a Pong
/// from the client in a time; otherwise, <c>false</c>.
/// </returns>
/// <param name="message">
/// A <see cref="string"/> that contains a message to send.
/// </param>
public bool Ping (string message)
{
return IsBound
? _websocket.Ping (message)
: false;
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
public virtual void Send (byte [] data)
{
if (IsBound)
_websocket.Send (data);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
public virtual void Send (string data)
{
if (IsBound)
_websocket.Send (data);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance asynchronously.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when
/// the asynchronous operation completes.
/// </param>
public virtual void SendAsync (byte [] data, Action completed)
{
if (IsBound)
_websocket.SendAsync (data, completed);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance asynchronously.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when
/// the asynchronous operation completes.
/// </param>
public virtual void SendAsync (string data, Action completed)
{
if (IsBound)
_websocket.SendAsync (data, completed);
}
/// <summary>
/// Starts the current <see cref="WebSocketService"/> instance.
/// </summary>
public void Start ()
{
if (IsBound)
_websocket.Connect ();
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance.
/// </summary>
public void Stop ()
{
if (IsBound)
_websocket.Close ();
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the specified
/// <see cref="ushort"/> and <see cref="string"/>.
/// </summary>
/// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for stop.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop.
/// </param>
public void Stop (ushort code, string reason)
{
if (IsBound)
_websocket.Close (code, reason);
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the specified
/// <see cref="CloseStatusCode"/> and <see cref="string"/>.
/// </summary>
/// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicates a status code
/// indicating the reason for stop.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop.
/// </param>
public void Stop (CloseStatusCode code, string reason)
{
if (IsBound)
_websocket.Close (code, reason);
}
#endregion
} }
} }

View File

@ -649,14 +649,14 @@ namespace WebSocketSharp.Server
return; return;
} }
WebSocketService service; WebSocketService session;
if (!TryGetServiceInstance (id, out service)) if (!TryGetServiceInstance (id, out session))
{ {
_logger.Error ("The WebSocket session with the specified ID not found.\nID: " + id); _logger.Error ("The WebSocket session with the specified ID not found.\nID: " + id);
return; return;
} }
service.Send (data); session.Context.WebSocket.Send (data, null);
} }
/// <summary> /// <summary>
@ -678,14 +678,14 @@ namespace WebSocketSharp.Server
return; return;
} }
WebSocketService service; WebSocketService session;
if (!TryGetServiceInstance (id, out service)) if (!TryGetServiceInstance (id, out session))
{ {
_logger.Error ("The WebSocket session with the specified ID not found.\nID: " + id); _logger.Error ("The WebSocket session with the specified ID not found.\nID: " + id);
return; return;
} }
service.Send (data); session.Context.WebSocket.Send (data, null);
} }
/// <summary> /// <summary>

View File

@ -1028,65 +1028,72 @@ namespace WebSocketSharp
return _stream.Write (frame.ToByteArray ()); return _stream.Write (frame.ToByteArray ());
} }
private void send (Opcode opcode, byte [] data) private bool send (Opcode opcode, byte [] data)
{ {
lock (_forSend) lock (_forSend)
{ {
var sent = false;
try { try {
var comped = false; var compressed = false;
if (_compression != CompressionMethod.NONE) if (_compression != CompressionMethod.NONE)
{ {
data = data.Compress (_compression); data = data.Compress (_compression);
comped = true; compressed = true;
} }
send (WsFrame.CreateFrame ( sent = send (WsFrame.CreateFrame (
Fin.FINAL, opcode, _client ? Mask.MASK : Mask.UNMASK, data, comped)); Fin.FINAL, opcode, _client ? Mask.MASK : Mask.UNMASK, data, compressed));
} }
catch (Exception ex) { catch (Exception ex) {
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
error ("An exception has occured."); error ("An exception has occured.");
} }
return sent;
} }
} }
private void send (Opcode opcode, Stream stream) private bool send (Opcode opcode, Stream stream)
{ {
lock (_forSend) lock (_forSend)
{ {
var comp = stream; var sent = false;
var comped = false;
var src = stream;
var compressed = false;
try { try {
if (_compression != CompressionMethod.NONE) if (_compression != CompressionMethod.NONE)
{ {
comp = stream.Compress (_compression); stream = stream.Compress (_compression);
comped = true; compressed = true;
} }
sendFragmented (opcode, comp, _client ? Mask.MASK : Mask.UNMASK, comped); sent = sendFragmented (opcode, stream, _client ? Mask.MASK : Mask.UNMASK, compressed);
} }
catch (Exception ex) { catch (Exception ex) {
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
error ("An exception has occured."); error ("An exception has occured.");
} }
finally { finally {
if (comped) if (compressed)
comp.Dispose ();
stream.Dispose (); stream.Dispose ();
src.Dispose ();
} }
return sent;
} }
} }
private void sendAsync (Opcode opcode, byte [] data, Action completed) private void send (Opcode opcode, byte [] data, Action<bool> completed)
{ {
Action<Opcode, byte []> sender = send; Func<Opcode, byte [], bool> sender = send;
AsyncCallback callback = ar => AsyncCallback callback = ar =>
{ {
try { try {
sender.EndInvoke (ar); var sent = sender.EndInvoke (ar);
if (completed != null) if (completed != null)
completed (); completed (sent);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1098,15 +1105,15 @@ namespace WebSocketSharp
sender.BeginInvoke (opcode, data, callback, null); sender.BeginInvoke (opcode, data, callback, null);
} }
private void sendAsync (Opcode opcode, Stream stream, Action completed) private void send (Opcode opcode, Stream stream, Action<bool> completed)
{ {
Action<Opcode, Stream> sender = send; Func<Opcode, Stream, bool> sender = send;
AsyncCallback callback = ar => AsyncCallback callback = ar =>
{ {
try { try {
sender.EndInvoke (ar); var sent = sender.EndInvoke (ar);
if (completed != null) if (completed != null)
completed (); completed (sent);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1585,10 +1592,62 @@ namespace WebSocketSharp
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> using the WebSocket connection. /// Sends a binary <paramref name="data"/> using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send. /// An array of <see cref="byte"/> that contains a binary data to send.
/// </param> /// </param>
public void Send (byte[] data) public void Send (byte[] data)
{
Send (data, null);
}
/// <summary>
/// Sends a text <paramref name="data"/> using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
public void Send (string data)
{
Send (data, null);
}
/// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/>
/// using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="file">
/// A <see cref="FileInfo"/> from which contains a binary data to send.
/// </param>
public void Send (FileInfo file)
{
Send (file, null);
}
/// <summary>
/// Sends a binary <paramref name="data"/> using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; 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 Send (byte [] data, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData (); var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
if (msg != null) if (msg != null)
@ -1600,18 +1659,27 @@ namespace WebSocketSharp
} }
if (data.LongLength <= FragmentLength) if (data.LongLength <= FragmentLength)
send (Opcode.BINARY, data); send (Opcode.BINARY, data, completed);
else else
send (Opcode.BINARY, new MemoryStream (data)); send (Opcode.BINARY, new MemoryStream (data), completed);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> using the WebSocket connection. /// Sends a text <paramref name="data"/> using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains a text data to send.
/// </param> /// </param>
public void Send (string data) /// <param name="completed">
/// An Action&lt;bool&gt; 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 Send (string data, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData (); var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
if (msg != null) if (msg != null)
@ -1624,18 +1692,28 @@ namespace WebSocketSharp
var rawData = Encoding.UTF8.GetBytes (data); var rawData = Encoding.UTF8.GetBytes (data);
if (rawData.LongLength <= FragmentLength) if (rawData.LongLength <= FragmentLength)
send (Opcode.TEXT, rawData); send (Opcode.TEXT, rawData, completed);
else else
send (Opcode.TEXT, new MemoryStream (rawData)); send (Opcode.TEXT, new MemoryStream (rawData), completed);
} }
/// <summary> /// <summary>
/// Sends a binary data using the WebSocket connection. /// Sends a binary data from the specified <see cref="FileInfo"/>
/// using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> that contains a binary data to send. /// A <see cref="FileInfo"/> from which contains a binary data to send.
/// </param> /// </param>
public void Send (FileInfo file) /// <param name="completed">
/// An Action&lt;bool&gt; 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 Send (FileInfo file, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? var msg = _readyState.CheckIfOpen () ??
(file == null ? "'file' must not be null." : null); (file == null ? "'file' must not be null." : null);
@ -1648,12 +1726,16 @@ namespace WebSocketSharp
return; return;
} }
send (Opcode.BINARY, file.OpenRead ()); send (Opcode.BINARY, file.OpenRead (), completed);
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> using the WebSocket connection. /// Sends a binary data from the specified <see cref="Stream"/>
/// using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to send. /// A <see cref="Stream"/> object from which contains a binary data to send.
/// </param> /// </param>
@ -1666,120 +1748,16 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Send (Stream stream, int length, bool dispose) public void Send (Stream stream, int length, bool dispose)
{ {
byte [] data = null; Send (stream, length, dispose, null);
int readLen = 0;
var msg = _readyState.CheckIfOpen () ??
stream.CheckIfCanRead () ??
(length < 1 ? "'length' must be greater than 0." : null) ??
((readLen = (data = stream.ReadBytes (length)).Length) == 0
? "A data cannot be read from 'stream'." : null);
if (msg != null)
{
_logger.Error (msg);
error (msg);
return;
}
if (readLen != length)
_logger.Warn (String.Format (
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}", length, readLen));
if (dispose)
stream.Dispose ();
if (readLen <= FragmentLength)
send (Opcode.BINARY, data);
else
send (Opcode.BINARY, new MemoryStream (data));
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously using the WebSocket connection. /// Sends a binary data from the specified <see cref="Stream"/>
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when
/// the asynchronous operation completes.
/// </param>
public void SendAsync (byte [] data, Action completed)
{
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
if (msg != null)
{
_logger.Error (msg);
error (msg);
return;
}
if (data.LongLength <= FragmentLength)
sendAsync (Opcode.BINARY, data, completed);
else
sendAsync (Opcode.BINARY, new MemoryStream (data), completed);
}
/// <summary>
/// Sends a text <paramref name="data"/> asynchronously using the WebSocket connection.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when
/// the asynchronous operation completes.
/// </param>
public void SendAsync (string data, Action completed)
{
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
if (msg != null)
{
_logger.Error (msg);
error (msg);
return;
}
var rawData = Encoding.UTF8.GetBytes (data);
if (rawData.LongLength <= FragmentLength)
sendAsync (Opcode.TEXT, rawData, completed);
else
sendAsync (Opcode.TEXT, new MemoryStream (rawData), completed);
}
/// <summary>
/// Sends a binary data asynchronously using the WebSocket connection.
/// </summary>
/// <param name="file">
/// A <see cref="FileInfo"/> that contains a binary data to send.
/// </param>
/// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when
/// the asynchronous operation completes.
/// </param>
public void SendAsync (FileInfo file, Action completed)
{
var msg = _readyState.CheckIfOpen () ??
(file == null ? "'file' must not be null." : null);
if (msg != null)
{
_logger.Error (msg);
error (msg);
return;
}
sendAsync (Opcode.BINARY, file.OpenRead (), completed);
}
/// <summary>
/// Sends a binary data asynchronously from the specified <see cref="Stream"/>
/// using the WebSocket connection. /// using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to send. /// A <see cref="Stream"/> object from which contains a binary data to send.
/// </param> /// </param>
@ -1791,10 +1769,12 @@ namespace WebSocketSharp
/// otherwise, <c>false</c>. /// otherwise, <c>false</c>.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the asynchronous operation completes. /// 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> /// </param>
public void SendAsync (Stream stream, int length, bool dispose, Action completed) public void Send (Stream stream, int length, bool dispose, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? var msg = _readyState.CheckIfOpen () ??
stream.CheckIfCanRead () ?? stream.CheckIfCanRead () ??
@ -1829,10 +1809,12 @@ namespace WebSocketSharp
if (dispose) if (dispose)
stream.Dispose (); stream.Dispose ();
if (readLen <= FragmentLength) var sent = readLen <= FragmentLength
sendAsync (Opcode.BINARY, data, completed); ? send (Opcode.BINARY, data)
else : send (Opcode.BINARY, new MemoryStream (data));
sendAsync (Opcode.BINARY, new MemoryStream (data), completed);
if (completed != null)
completed (sent);
}; };
Action<Exception> exception = ex => Action<Exception> exception = ex =>