Added SendAsync methods to the WebSocket class. Existing Send methods were changed to behave synchronously

This commit is contained in:
sta 2014-01-10 05:04:46 +09:00
parent 11d3311f4f
commit 418ec8bc2f
4 changed files with 387 additions and 445 deletions

View File

@ -5,7 +5,7 @@
* The MIT License * The MIT License
* *
* Copyright (c) 2012-2014 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
@ -15,7 +15,7 @@
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -182,8 +182,7 @@ namespace WebSocketSharp.Server
private void onOpen (object sender, EventArgs e) private void onOpen (object sender, EventArgs e)
{ {
ID = _sessions.Add (this); ID = _sessions.Add (this);
if (ID == null) if (ID == null) {
{
_websocket.Close (CloseStatusCode.AWAY); _websocket.Close (CloseStatusCode.AWAY);
return; return;
} }
@ -303,59 +302,50 @@ namespace WebSocketSharp.Server
/// Sends a binary <paramref name="data"/> to the client of the current /// Sends a binary <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance. /// <see cref="WebSocketService"/> instance.
/// </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 the binary data to send.
/// </param> /// </param>
protected void Send (byte [] data) protected void Send (byte [] data)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (data, null); _websocket.Send (data);
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> to /// Sends a binary data from the specified <see cref="FileInfo"/> to
/// the client of the current <see cref="WebSocketService"/> instance. /// the client of the current <see cref="WebSocketService"/> instance.
/// </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"/> from which contains a binary data to send. /// A <see cref="FileInfo"/> from which contains the binary data to send.
/// </param> /// </param>
protected void Send (FileInfo file) protected void Send (FileInfo file)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (file, null); _websocket.Send (file);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client of the current /// Sends a text <paramref name="data"/> to the client of the current
/// <see cref="WebSocketService"/> instance. /// <see cref="WebSocketService"/> instance.
/// </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 represents the text data to send.
/// </param> /// </param>
protected void Send (string data) protected void Send (string data)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (data, null); _websocket.Send (data);
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client of the current /// Sends a binary <paramref name="data"/> asynchronously to the client of the
/// <see cref="WebSocketService"/> instance. /// current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </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 the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -363,21 +353,22 @@ namespace WebSocketSharp.Server
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// 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> /// </param>
protected void Send (byte [] data, Action<bool> completed) protected void SendAsync (byte [] data, Action<bool> completed)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (data, completed); _websocket.SendAsync (data, completed);
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> to /// Sends a binary data from the specified <see cref="FileInfo"/>
/// the client of the current <see cref="WebSocketService"/> instance. /// asynchronously to the client of the current <see cref="WebSocketService"/>
/// instance.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> from which contains a binary data to send. /// A <see cref="FileInfo"/> from which contains the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -385,21 +376,21 @@ namespace WebSocketSharp.Server
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// 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> /// </param>
protected void Send (FileInfo file, Action<bool> completed) protected void SendAsync (FileInfo file, Action<bool> completed)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (file, completed); _websocket.SendAsync (file, completed);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client of the current /// Sends a text <paramref name="data"/> asynchronously to the client of the
/// <see cref="WebSocketService"/> instance. /// current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains the text data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -407,43 +398,24 @@ namespace WebSocketSharp.Server
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// 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> /// </param>
protected void Send (string data, Action<bool> completed) protected void SendAsync (string data, Action<bool> completed)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (data, completed); _websocket.SendAsync (data, completed);
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to /// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// the client of the current <see cref="WebSocketService"/> instance. /// to the client of the current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </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 the binary data to send.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
protected void Send (Stream stream, int length)
{
if (_websocket != null)
_websocket.Send (stream, length, 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>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -451,10 +423,10 @@ namespace WebSocketSharp.Server
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// 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> /// </param>
protected void Send (Stream stream, int length, Action<bool> completed) protected void SendAsync (Stream stream, int length, Action<bool> completed)
{ {
if (_websocket != null) if (_websocket != null)
_websocket.Send (stream, length, completed); _websocket.SendAsync (stream, length, completed);
} }
/// <summary> /// <summary>

View File

@ -4,8 +4,8 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2012-2013 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
@ -15,7 +15,7 @@
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -868,166 +868,149 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified /// Sends a binary <paramref name="data"/> to the client associated with the
/// <paramref name="servicePath"/> and <paramref name="id"/>. /// specified <paramref name="servicePath"/> and <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <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 the binary data to send.
/// </param> /// </param>
public void SendTo (string servicePath, string id, byte [] data) public void SendTo (string servicePath, string id, byte [] data)
{ {
SendTo (servicePath, id, data, null); WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, data);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified /// Sends a text <paramref name="data"/> to the client associated with the
/// <paramref name="servicePath"/> and <paramref name="id"/>. /// specified <paramref name="servicePath"/> and <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that represents the text data to send.
/// </param> /// </param>
public void SendTo (string servicePath, string id, string data) public void SendTo (string servicePath, string id, string data)
{ {
SendTo (servicePath, id, data, null); WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, data);
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified /// Sends a binary <paramref name="data"/> asynchronously to the client
/// <paramref name="servicePath"/> and <paramref name="id"/>. /// associated with the specified <paramref name="servicePath"/> and
/// <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <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 the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// 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
/// successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendTo (string servicePath, string id, byte [] data, Action<bool> completed) public void SendToAsync (
string servicePath, string id, byte [] data, Action<bool> completed)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host)) if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, data, completed); host.Sessions.SendToAsync (id, data, completed);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified /// Sends a text <paramref name="data"/> asynchronously to the client
/// <paramref name="servicePath"/> and <paramref name="id"/>. /// associated with the specified <paramref name="servicePath"/> and
/// <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that represents the text data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// 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
/// successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendTo (string servicePath, string id, string data, Action<bool> completed) public void SendToAsync (
string servicePath, string id, string data, Action<bool> completed)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host)) if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, data, completed); host.Sessions.SendToAsync (id, data, completed);
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to the client associated with /// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// the specified <paramref name="servicePath"/> and <paramref name="id"/>. /// to the client associated with the specified <paramref name="servicePath"/>
/// and <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <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 the binary data to send.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
public void SendTo (string servicePath, string id, Stream stream, int length)
{
SendTo (servicePath, id, stream, length, null);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to the client associated with
/// the specified <paramref name="servicePath"/> and <paramref name="id"/>.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination
/// for the data.
/// </param>
/// <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>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// 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
/// successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendTo ( public void SendToAsync (
string servicePath, string id, Stream stream, int length, Action<bool> completed) string servicePath, string id, Stream stream, int length, Action<bool> completed)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host)) if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, stream, length, completed); host.Sessions.SendToAsync (id, stream, length, completed);
} }
/// <summary> /// <summary>

View File

@ -4,8 +4,8 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2012-2013 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
@ -15,7 +15,7 @@
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -706,148 +706,124 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified /// Sends a binary <paramref name="data"/> to the client associated with the
/// <paramref name="id"/>. /// specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <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 the binary data to send.
/// </param> /// </param>
public void SendTo (string id, byte [] data) public void SendTo (string id, byte [] data)
{ {
SendTo (id, data, null); IWebSocketSession session;
if (TryGetSession (id, out session))
session.Context.WebSocket.Send (data);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified /// Sends a text <paramref name="data"/> to the client associated with the
/// <paramref name="id"/>. /// specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that represents the text data to send.
/// </param> /// </param>
public void SendTo (string id, string data) public void SendTo (string id, string data)
{
SendTo (id, data, null);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination
/// for the data.
/// </param>
/// <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 SendTo (string id, byte [] data, Action<bool> completed)
{ {
IWebSocketSession session; IWebSocketSession session;
if (TryGetSession (id, out session)) if (TryGetSession (id, out session))
session.Context.WebSocket.Send (data, completed); session.Context.WebSocket.Send (data);
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified /// Sends a binary <paramref name="data"/> asynchronously to the client
/// <paramref name="id"/>.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination
/// for the data.
/// </param>
/// <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>
public void SendTo (string id, string data, Action<bool> completed)
{
IWebSocketSession session;
if (TryGetSession (id, out session))
session.Context.WebSocket.Send (data, completed);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to the client
/// associated with the specified <paramref name="id"/>. /// associated with the specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the data. /// destination for the data.
/// </param> /// </param>
/// <param name="stream"> /// <param name="data">
/// A <see cref="Stream"/> object from which contains a binary data to send. /// An array of <see cref="byte"/> that contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send.
/// </param>
public void SendTo (string id, Stream stream, int length)
{
SendTo (id, stream, length, null);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> to the client
/// associated with the specified <paramref name="id"/>.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination
/// for the data.
/// </param>
/// <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>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// 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
/// successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendTo ( public void SendToAsync (string id, byte [] data, Action<bool> completed)
{
IWebSocketSession session;
if (TryGetSession (id, out session))
session.Context.WebSocket.SendAsync (data, completed);
}
/// <summary>
/// Sends a text <paramref name="data"/> asynchronously to the client
/// associated with the specified <paramref name="id"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="id">
/// A <see cref="string"/> that represents the session ID that represents the
/// destination for the data.
/// </param>
/// <param name="data">
/// A <see cref="string"/> that represents the 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>
public void SendToAsync (string id, string data, Action<bool> completed)
{
IWebSocketSession session;
if (TryGetSession (id, out session))
session.Context.WebSocket.SendAsync (data, completed);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// to the client associated with the specified <paramref name="id"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="id">
/// A <see cref="string"/> that represents the session ID that represents the
/// destination for the data.
/// </param>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes 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 SendToAsync (
string id, Stream stream, int length, Action<bool> completed) string id, Stream stream, int length, Action<bool> completed)
{ {
IWebSocketSession session; IWebSocketSession session;
if (TryGetSession (id, out session)) if (TryGetSession (id, out session))
session.Context.WebSocket.Send (stream, length, completed); session.Context.WebSocket.SendAsync (stream, length, completed);
} }
/// <summary> /// <summary>

View File

@ -1055,8 +1055,7 @@ namespace WebSocketSharp
private bool send (byte [] frameAsBytes) private bool send (byte [] frameAsBytes)
{ {
if (_readyState != WebSocketState.OPEN) if (_readyState != WebSocketState.OPEN) {
{
var msg = "A WebSocket connection isn't established or has been closed."; var msg = "A WebSocket connection isn't established or has been closed.";
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1070,22 +1069,25 @@ namespace WebSocketSharp
// As client // As client
private void send (HandshakeRequest request) private void send (HandshakeRequest request)
{ {
_logger.Debug (String.Format ( _logger.Debug (
"A WebSocket connection request to {0}:\n{1}", _uri, request)); String.Format (
"A WebSocket connection request to {0}:\n{1}", _uri, request));
_stream.WriteHandshake (request); _stream.WriteHandshake (request);
} }
// As server // As server
private bool send (HandshakeResponse response) private bool send (HandshakeResponse response)
{ {
_logger.Debug ("A response to a WebSocket connection request:\n" + response.ToString ()); _logger.Debug (
"A response to the WebSocket connection request:\n" + response.ToString ());
return _stream.WriteHandshake (response); return _stream.WriteHandshake (response);
} }
private bool send (WsFrame frame) private bool send (WsFrame frame)
{ {
if (_readyState != WebSocketState.OPEN) if (_readyState != WebSocketState.OPEN) {
{
var msg = "A WebSocket connection isn't established or has been closed."; var msg = "A WebSocket connection isn't established or has been closed.";
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1098,23 +1100,22 @@ namespace WebSocketSharp
private bool send (Opcode opcode, byte [] data) private bool send (Opcode opcode, byte [] data)
{ {
lock (_forSend) lock (_forSend) {
{
var sent = false; var sent = false;
try { try {
var compressed = false; var compressed = false;
if (_compression != CompressionMethod.NONE) if (_compression != CompressionMethod.NONE) {
{
data = data.Compress (_compression); data = data.Compress (_compression);
compressed = true; compressed = true;
} }
sent = send (WsFrame.CreateFrame ( var mask = _client ? Mask.MASK : Mask.UNMASK;
Fin.FINAL, opcode, _client ? Mask.MASK : Mask.UNMASK, data, compressed)); sent = send (
WsFrame.CreateFrame (Fin.FINAL, opcode, mask, data, compressed));
} }
catch (Exception ex) { catch (Exception ex) {
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
error ("An exception has occurred."); error ("An exception has occurred while sending a data.");
} }
return sent; return sent;
@ -1123,24 +1124,22 @@ namespace WebSocketSharp
private bool send (Opcode opcode, Stream stream) private bool send (Opcode opcode, Stream stream)
{ {
lock (_forSend) lock (_forSend) {
{
var sent = false; var sent = false;
var src = stream; var src = stream;
var compressed = false; var compressed = false;
try { try {
if (_compression != CompressionMethod.NONE) if (_compression != CompressionMethod.NONE) {
{
stream = stream.Compress (_compression); stream = stream.Compress (_compression);
compressed = true; compressed = true;
} }
sent = sendFragmented (opcode, stream, _client ? Mask.MASK : Mask.UNMASK, compressed); var mask = _client ? Mask.MASK : Mask.UNMASK;
sent = sendFragmented (opcode, stream, mask, compressed);
} }
catch (Exception ex) { catch (Exception ex) {
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
error ("An exception has occurred."); error ("An exception has occurred while sending a data.");
} }
finally { finally {
if (compressed) if (compressed)
@ -1153,47 +1152,48 @@ namespace WebSocketSharp
} }
} }
private void send (Opcode opcode, byte [] data, Action<bool> completed) private void sendAsync (Opcode opcode, byte [] data, Action<bool> completed)
{ {
Func<Opcode, byte [], bool> sender = send; Func<Opcode, byte [], bool> sender = send;
AsyncCallback callback = ar => sender.BeginInvoke (
{ opcode,
try { data,
var sent = sender.EndInvoke (ar); ar => {
if (completed != null) try {
completed (sent); var sent = sender.EndInvoke (ar);
} if (completed != null)
catch (Exception ex) completed (sent);
{ }
_logger.Fatal (ex.ToString ()); catch (Exception ex) {
error ("An exception has occurred."); _logger.Fatal (ex.ToString ());
} error ("An exception has occurred while callback.");
}; }
},
sender.BeginInvoke (opcode, data, callback, null); null);
} }
private void send (Opcode opcode, Stream stream, Action<bool> completed) private void sendAsync (Opcode opcode, Stream stream, Action<bool> completed)
{ {
Func<Opcode, Stream, bool> sender = send; Func<Opcode, Stream, bool> sender = send;
AsyncCallback callback = ar => sender.BeginInvoke (
{ opcode,
try { stream,
var sent = sender.EndInvoke (ar); ar => {
if (completed != null) try {
completed (sent); var sent = sender.EndInvoke (ar);
} if (completed != null)
catch (Exception ex) completed (sent);
{ }
_logger.Fatal (ex.ToString ()); catch (Exception ex) {
error ("An exception has occurred."); _logger.Fatal (ex.ToString ());
} error ("An exception has occurred while callback.");
}; }
},
sender.BeginInvoke (opcode, stream, callback, null); null);
} }
private bool sendFragmented (Opcode opcode, Stream stream, Mask mask, bool compressed) private bool sendFragmented (
Opcode opcode, Stream stream, Mask mask, bool compressed)
{ {
var len = stream.Length; var len = stream.Length;
if (sendFragmented (opcode, stream, len, mask, compressed) == len) if (sendFragmented (opcode, stream, len, mask, compressed) == len)
@ -1219,12 +1219,12 @@ namespace WebSocketSharp
byte [] buffer = null; byte [] buffer = null;
// Not fragment // Not fragment
if (quo == 0) if (quo == 0) {
{
buffer = new byte [rem]; buffer = new byte [rem];
readLen = stream.Read (buffer, 0, rem); readLen = stream.Read (buffer, 0, rem);
if (readLen == rem && if (readLen == rem &&
send (WsFrame.CreateFrame (Fin.FINAL, opcode, mask, buffer, compressed))) send (
WsFrame.CreateFrame (Fin.FINAL, opcode, mask, buffer, compressed)))
sentLen = readLen; sentLen = readLen;
return sentLen; return sentLen;
@ -1235,17 +1235,18 @@ namespace WebSocketSharp
// First // First
readLen = stream.Read (buffer, 0, FragmentLength); readLen = stream.Read (buffer, 0, FragmentLength);
if (readLen == FragmentLength && if (readLen == FragmentLength &&
send (WsFrame.CreateFrame (Fin.MORE, opcode, mask, buffer, compressed))) send (
WsFrame.CreateFrame (Fin.MORE, opcode, mask, buffer, compressed)))
sentLen = readLen; sentLen = readLen;
else else
return sentLen; return sentLen;
// Mid // Mid
for (long i = 0; i < count; i++) for (long i = 0; i < count; i++) {
{
readLen = stream.Read (buffer, 0, FragmentLength); readLen = stream.Read (buffer, 0, FragmentLength);
if (readLen == FragmentLength && if (readLen == FragmentLength &&
send (WsFrame.CreateFrame (Fin.MORE, Opcode.CONT, mask, buffer, compressed))) send (
WsFrame.CreateFrame (Fin.MORE, Opcode.CONT, mask, buffer, compressed)))
sentLen += readLen; sentLen += readLen;
else else
return sentLen; return sentLen;
@ -1258,7 +1259,8 @@ namespace WebSocketSharp
readLen = stream.Read (buffer, 0, tmpLen); readLen = stream.Read (buffer, 0, tmpLen);
if (readLen == tmpLen && if (readLen == tmpLen &&
send (WsFrame.CreateFrame (Fin.FINAL, Opcode.CONT, mask, buffer, compressed))) send (
WsFrame.CreateFrame (Fin.FINAL, Opcode.CONT, mask, buffer, compressed)))
sentLen += readLen; sentLen += readLen;
return sentLen; return sentLen;
@ -1864,66 +1866,13 @@ 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 the binary data to send.
/// </param> /// </param>
public void Send (byte[] data) public void Send (byte [] 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 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 <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) {
{
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1934,64 +1883,45 @@ namespace WebSocketSharp
if (len <= FragmentLength) if (len <= FragmentLength)
send ( send (
Opcode.BINARY, Opcode.BINARY,
len > 0 && _client && _compression == CompressionMethod.NONE ? data.Copy (len) : data, len > 0 && _client && _compression == CompressionMethod.NONE
completed); ? data.Copy (len)
: data);
else else
send (Opcode.BINARY, new MemoryStream (data), completed); send (Opcode.BINARY, new MemoryStream (data));
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> /// Sends a binary data from the specified <see cref="FileInfo"/> using the
/// using the WebSocket connection. /// 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"/> from which contains a binary data to send. /// A <see cref="FileInfo"/> from which contains the binary data to send.
/// </param> /// </param>
/// <param name="completed"> public void Send (FileInfo file)
/// 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);
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
return; return;
} }
send (Opcode.BINARY, file.OpenRead (), completed); send (Opcode.BINARY, file.OpenRead ());
} }
/// <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 represents the text data to send.
/// </param> /// </param>
/// <param name="completed"> public void Send (string data)
/// 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) {
{
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -2000,41 +1930,20 @@ 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, completed); send (Opcode.TEXT, rawData);
else else
send (Opcode.TEXT, new MemoryStream (rawData), completed); send (Opcode.TEXT, new MemoryStream (rawData));
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> /// Sends a binary <paramref name="data"/> asynchronously using the WebSocket
/// using the WebSocket connection. /// connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="stream"> /// <param name="data">
/// A <see cref="Stream"/> object from which contains a binary data to send. /// An array of <see cref="byte"/> that contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send.
/// </param>
public void Send (Stream stream, int length)
{
Send (stream, length, null);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/>
/// using the WebSocket connection.
/// </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>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -2042,14 +1951,118 @@ namespace WebSocketSharp
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// 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> /// </param>
public void Send (Stream stream, int length, Action<bool> completed) public void SendAsync (byte [] data, Action<bool> completed)
{
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
error (msg);
return;
}
var len = data.LongLength;
if (len <= FragmentLength)
sendAsync (
Opcode.BINARY,
len > 0 && _client && _compression == CompressionMethod.NONE
? data.Copy (len)
: data,
completed);
else
sendAsync (Opcode.BINARY, new MemoryStream (data), completed);
}
/// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/>
/// asynchronously using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="file">
/// A <see cref="FileInfo"/> from which contains the 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 SendAsync (FileInfo file, Action<bool> 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 text <paramref name="data"/> asynchronously using the WebSocket
/// connection.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="data">
/// A <see cref="string"/> that represents the 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>
public void SendAsync (string data, Action<bool> 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 from the specified <see cref="Stream"/> asynchronously
/// using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes 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 SendAsync (Stream stream, int length, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? var msg = _readyState.CheckIfOpen () ??
stream.CheckIfCanRead () ?? stream.CheckIfCanRead () ??
(length < 1 ? "'length' must be greater than 0." : null); (length < 1 ? "'length' must be greater than 0." : null);
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -2058,11 +2071,9 @@ namespace WebSocketSharp
stream.ReadBytesAsync ( stream.ReadBytesAsync (
length, length,
data => data => {
{
var len = data.Length; var len = data.Length;
if (len == 0) if (len == 0) {
{
var err = "A data cannot be read from 'stream'."; var err = "A data cannot be read from 'stream'.";
_logger.Error (err); _logger.Error (err);
error (err); error (err);
@ -2071,10 +2082,11 @@ namespace WebSocketSharp
} }
if (len < length) if (len < length)
_logger.Warn (String.Format ( _logger.Warn (
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}", String.Format (
length, "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
len)); length,
len));
var sent = len <= FragmentLength var sent = len <= FragmentLength
? send (Opcode.BINARY, data) ? send (Opcode.BINARY, data)
@ -2083,10 +2095,9 @@ namespace WebSocketSharp
if (completed != null) if (completed != null)
completed (sent); completed (sent);
}, },
ex => ex => {
{
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
error ("An exception has occurred."); error ("An exception has occurred while sending a data.");
}); });
} }