Moved some methods (e.g. Broadcast method) from WebSocketService.cs and WebSocketServiceHost.cs to WebSocketSessionManager.cs

This commit is contained in:
sta 2013-09-16 14:33:26 +09:00
parent e3d5dea096
commit 42a44226d5
8 changed files with 354 additions and 802 deletions

View File

@ -28,12 +28,12 @@ namespace Example2
protected override void OnMessage (MessageEventArgs e)
{
Broadcast (String.Format ("{0}: {1}", _name, e.Data));
Sessions.Broadcast (String.Format ("{0}: {1}", _name, e.Data));
}
protected override void OnClose (CloseEventArgs e)
{
Broadcast (String.Format ("{0} got logged off...", _name));
Sessions.Broadcast (String.Format ("{0} got logged off...", _name));
}
}
}

View File

@ -28,12 +28,12 @@ namespace Example3
protected override void OnMessage (MessageEventArgs e)
{
Broadcast (String.Format ("{0}: {1}", _name, e.Data));
Sessions.Broadcast (String.Format ("{0}: {1}", _name, e.Data));
}
protected override void OnClose (CloseEventArgs e)
{
Broadcast (String.Format ("{0} got logged off...", _name));
Sessions.Broadcast (String.Format ("{0} got logged off...", _name));
}
}
}

View File

@ -237,7 +237,7 @@ public class Chat : WebSocketService
{
protected override void OnMessage (MessageEventArgs e)
{
Broadcast (e.Data);
Sessions.Broadcast (e.Data);
}
}
```

View File

@ -70,151 +70,5 @@ namespace WebSocketSharp.Server
/// A <see cref="WebSocketContext"/> that contains the WebSocket connection request objects to bind.
/// </param>
void BindWebSocket (WebSocketContext context);
/// <summary>
/// Broadcasts the specified array of <see cref="byte"/> to all clients of the WebSocket service host.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> to broadcast.
/// </param>
void Broadcast (byte [] data);
/// <summary>
/// Broadcasts the specified <see cref="string"/> to all clients of the WebSocket service host.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> to broadcast.
/// </param>
void Broadcast (string data);
/// <summary>
/// Sends Pings to all clients of the WebSocket service host.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service host received a Pong from each client in a time.
/// </returns>
Dictionary<string, bool> Broadping ();
/// <summary>
/// Sends Pings with the specified <paramref name="data"/> to all clients of the WebSocket service host.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service host received a Pong from each client in a time.
/// </returns>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a message data to send.
/// </param>
Dictionary<string, bool> Broadping (byte [] data);
/// <summary>
/// Close the session with the specified <paramref name="id"/>.
/// </summary>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
void CloseSession (string id);
/// <summary>
/// Close the session with the specified <paramref name="code"/>, <paramref name="reason"/>
/// and <paramref name="id"/>.
/// </summary>
/// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
void CloseSession (ushort code, string reason, string id);
/// <summary>
/// Close the session with the specified <paramref name="code"/>, <paramref name="reason"/>
/// and <paramref name="id"/>.
/// </summary>
/// <param name="code">
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
void CloseSession (CloseStatusCode code, string reason, string id);
/// <summary>
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
bool PingTo (string id);
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service host 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>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
bool PingTo (string message, string id);
/// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
bool SendTo (byte [] data, string id);
/// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
bool SendTo (string data, string id);
/// <summary>
/// Stops the WebSocket service host.
/// </summary>
void Stop ();
/// <summary>
/// Stops the WebSocket service host with the specified array of <see cref="byte"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> that contains the reason for stop.
/// </param>
void Stop (byte [] data);
}
}

View File

@ -95,8 +95,7 @@ namespace WebSocketSharp.Server
/// Gets the manager of the sessions to the WebSocket service.
/// </summary>
/// <value>
/// A <see cref="WebSocketSessionManager"/> that manages the sessions
/// to the WebSocket service.
/// A <see cref="WebSocketSessionManager"/> that manages the sessions to the WebSocket service.
/// </value>
protected WebSocketSessionManager Sessions {
get {
@ -226,111 +225,10 @@ namespace WebSocketSharp.Server
IsBound = true;
}
internal bool Ping (byte [] data)
{
return _websocket.Ping (data);
}
internal void Stop (byte [] data)
{
_websocket.Close (data);
}
#endregion
#region Protected Methods
/// <summary>
/// Broadcasts the specified array of <see cref="byte"/> to all clients of the WebSocket service.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> to broadcast.
/// </param>
protected virtual void Broadcast (byte [] data)
{
if (!IsBound)
return;
var msg = data.CheckIfValidSendData ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return;
}
_sessions.Broadcast (data);
}
/// <summary>
/// Broadcasts the specified <see cref="string"/> to all clients of the WebSocket service.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> to broadcast.
/// </param>
protected virtual void Broadcast (string data)
{
if (!IsBound)
return;
var msg = data.CheckIfValidSendData ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return;
}
_sessions.Broadcast (data);
}
/// <summary>
/// Sends Pings to all clients of the WebSocket service.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service received a Pong from each client in a time.
/// </returns>
protected virtual Dictionary<string, bool> Broadping ()
{
return IsBound
? _sessions.Broadping (new byte [] {})
: null;
}
/// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket service.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service received a Pong from each client in a time.
/// </returns>
/// <param name="message">
/// A <see cref="string"/> that contains a message to send.
/// </param>
protected virtual Dictionary<string, bool> Broadping (string message)
{
if (!IsBound)
return null;
if (message == null || message.Length == 0)
return _sessions.Broadping (new byte [] {});
var data = Encoding.UTF8.GetBytes (message);
var msg = data.CheckIfValidPingData ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return null;
}
return _sessions.Broadping (data);
}
/// <summary>
/// Calls the <see cref="OnError"/> method with the specified <paramref name="message"/>.
/// </summary>
@ -384,124 +282,6 @@ namespace WebSocketSharp.Server
{
}
/// <summary>
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service receives a Pong from the client in a time;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
protected virtual bool PingTo (string id)
{
if (!IsBound)
return false;
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return false;
}
return _sessions.PingTo (id);
}
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service 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>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
protected virtual bool PingTo (string message, string id)
{
if (!IsBound)
return false;
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return false;
}
return _sessions.PingTo (message, id);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
protected virtual bool SendTo (byte [] data, string id)
{
if (!IsBound)
return false;
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return false;
}
return _sessions.SendTo (data, id);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
protected virtual bool SendTo (string data, string id)
{
if (!IsBound)
return false;
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
Error (msg);
return false;
}
return _sessions.SendTo (data, id);
}
/// <summary>
/// Validates the cookies used in the WebSocket connection request.
/// </summary>
@ -536,7 +316,7 @@ namespace WebSocketSharp.Server
/// <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 virtual bool Ping ()
public bool Ping ()
{
return IsBound
? _websocket.Ping ()
@ -554,7 +334,7 @@ namespace WebSocketSharp.Server
/// <param name="message">
/// A <see cref="string"/> that contains a message to send.
/// </param>
public virtual bool Ping (string message)
public bool Ping (string message)
{
return IsBound
? _websocket.Ping (message)

View File

@ -315,246 +315,6 @@ namespace WebSocketSharp.Server
#region Public Methods
/// <summary>
/// Broadcasts the specified array of <see cref="byte"/> to all clients.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> to broadcast.
/// </param>
public void Broadcast (byte [] data)
{
var msg = data.CheckIfValidSendData ();
if (msg != null)
{
Log.Error (msg);
return;
}
_sessions.Broadcast (data);
}
/// <summary>
/// Broadcasts the specified <see cref="string"/> to all clients.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> to broadcast.
/// </param>
public void Broadcast (string data)
{
var msg = data.CheckIfValidSendData ();
if (msg != null)
{
Log.Error (msg);
return;
}
_sessions.Broadcast (data);
}
/// <summary>
/// Sends Pings to all clients.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service host received a Pong from each client in a time.
/// </returns>
public Dictionary<string, bool> Broadping ()
{
return _sessions.Broadping (new byte [] {});
}
/// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service host received a Pong from each client in a time.
/// </returns>
/// <param name="message">
/// A <see cref="string"/> that contains a message to send.
/// </param>
public Dictionary<string, bool> Broadping (string message)
{
if (message == null || message.Length == 0)
return _sessions.Broadping (new byte [] {});
var data = Encoding.UTF8.GetBytes (message);
var msg = data.CheckIfValidPingData ();
if (msg != null)
{
Log.Error (msg);
return null;
}
return _sessions.Broadping (data);
}
/// <summary>
/// Close the session with the specified <paramref name="id"/>.
/// </summary>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
public void CloseSession (string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return;
}
_sessions.StopServiceInstance (id);
}
/// <summary>
/// Close the session with the specified <paramref name="code"/>, <paramref name="reason"/>
/// and <paramref name="id"/>.
/// </summary>
/// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
public void CloseSession (ushort code, string reason, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return;
}
_sessions.StopServiceInstance (code, reason, id);
}
/// <summary>
/// Close the session with the specified <paramref name="code"/>, <paramref name="reason"/>
/// and <paramref name="id"/>.
/// </summary>
/// <param name="code">
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
public void CloseSession (CloseStatusCode code, string reason, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return;
}
_sessions.StopServiceInstance (code, reason, id);
}
/// <summary>
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
public bool PingTo (string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return false;
}
return _sessions.PingTo (id);
}
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service host 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>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
public bool PingTo (string message, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return false;
}
return _sessions.PingTo (message, id);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
public bool SendTo (byte [] data, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return false;
}
return _sessions.SendTo (data, id);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
public bool SendTo (string data, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
Log.Error (msg);
return false;
}
return _sessions.SendTo (data, id);
}
/// <summary>
/// Stops receiving the WebSocket connection requests.
/// </summary>
@ -618,55 +378,6 @@ namespace WebSocketSharp.Server
service.Start ();
}
/// <summary>
/// Broadcasts the specified array of <see cref="byte"/> to all clients.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> to broadcast.
/// </param>
void IWebSocketServiceHost.Broadcast (byte [] data)
{
_sessions.Broadcast (data);
}
/// <summary>
/// Broadcasts the specified <see cref="string"/> to all clients.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> to broadcast.
/// </param>
void IWebSocketServiceHost.Broadcast (string data)
{
_sessions.Broadcast (data);
}
/// <summary>
/// Sends Pings with the specified <paramref name="data"/> to all clients.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service host received a Pong from each client in a time.
/// </returns>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a message data to send.
/// </param>
Dictionary<string, bool> IWebSocketServiceHost.Broadping (byte [] data)
{
return _sessions.Broadping (data);
}
/// <summary>
/// Stops receiving the WebSocket connection requests with the specified array of <see cref="byte"/>.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> that contains the reason for stop.
/// </param>
void IWebSocketServiceHost.Stop (byte [] data)
{
base.Stop ();
_sessions.Stop (data);
}
#endregion
}
}

View File

@ -131,10 +131,10 @@ namespace WebSocketSharp.Server
}
/// <summary>
/// Gets the collection of each path to the WebSocket services provided by the WebSocket server.
/// Gets the collection of every path to the WebSocket services provided by the WebSocket server.
/// </summary>
/// <value>
/// An IEnumerable&lt;string&gt; that contains the collection of each path to the WebSocket services.
/// An IEnumerable&lt;string&gt; that contains the collection of every path to the WebSocket services.
/// </value>
public IEnumerable<string> ServicePaths {
get {
@ -149,6 +149,15 @@ namespace WebSocketSharp.Server
#region Private Methods
private Dictionary<string, Dictionary<string, bool>> broadping (byte [] data)
{
var result = new Dictionary<string, Dictionary<string, bool>> ();
foreach (var service in copy ())
result.Add (service.Key, service.Value.Sessions.BroadpingInternally (data));
return result;
}
private Dictionary<string, IWebSocketServiceHost> copy ()
{
lock (_sync)
@ -178,15 +187,6 @@ namespace WebSocketSharp.Server
}
}
internal Dictionary<string, Dictionary<string, bool>> Broadping (byte [] data)
{
var result = new Dictionary<string, Dictionary<string, bool>> ();
foreach (var service in copy ())
result.Add (service.Key, service.Value.Broadping (data));
return result;
}
internal bool Remove (string servicePath)
{
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash ();
@ -204,7 +204,7 @@ namespace WebSocketSharp.Server
_serviceHosts.Remove (servicePath);
}
host.Stop (((ushort) CloseStatusCode.AWAY).ToByteArray (ByteOrder.BIG));
host.Sessions.Stop (((ushort) CloseStatusCode.AWAY).ToByteArray (ByteOrder.BIG));
return true;
}
@ -213,7 +213,7 @@ namespace WebSocketSharp.Server
lock (_sync)
{
foreach (var host in _serviceHosts.Values)
host.Stop ();
host.Sessions.Stop ();
_serviceHosts.Clear ();
}
@ -224,7 +224,7 @@ namespace WebSocketSharp.Server
lock (_sync)
{
foreach (var host in _serviceHosts.Values)
host.Stop (data);
host.Sessions.Stop (data);
_serviceHosts.Clear ();
}
@ -260,7 +260,7 @@ namespace WebSocketSharp.Server
}
foreach (var host in ServiceHosts)
host.Broadcast (data);
host.Sessions.BroadcastInternally (data);
}
/// <summary>
@ -280,7 +280,7 @@ namespace WebSocketSharp.Server
}
foreach (var host in ServiceHosts)
host.Broadcast (data);
host.Sessions.BroadcastInternally (data);
}
/// <summary>
@ -312,7 +312,7 @@ namespace WebSocketSharp.Server
return false;
}
host.Broadcast (data);
host.Sessions.BroadcastInternally (data);
return true;
}
@ -345,7 +345,7 @@ namespace WebSocketSharp.Server
return false;
}
host.Broadcast (data);
host.Sessions.BroadcastInternally (data);
return true;
}
@ -359,7 +359,7 @@ namespace WebSocketSharp.Server
/// </returns>
public Dictionary<string, Dictionary<string, bool>> Broadping ()
{
return Broadping (new byte [] {});
return broadping (new byte [] {});
}
/// <summary>
@ -378,7 +378,7 @@ namespace WebSocketSharp.Server
public Dictionary<string, Dictionary<string, bool>> Broadping (string message)
{
if (message == null || message.Length == 0)
return Broadping (new byte [] {});
return broadping (new byte [] {});
var data = Encoding.UTF8.GetBytes (message);
var msg = data.CheckIfValidPingData ();
@ -388,7 +388,7 @@ namespace WebSocketSharp.Server
return null;
}
return Broadping (data);
return broadping (data);
}
/// <summary>
@ -418,7 +418,7 @@ namespace WebSocketSharp.Server
return null;
}
return host.Broadping (new byte [] {});
return host.Sessions.BroadpingInternally (new byte [] {});
}
/// <summary>
@ -456,11 +456,11 @@ namespace WebSocketSharp.Server
return null;
}
return host.Broadping (data);
return host.Sessions.BroadpingInternally (data);
}
/// <summary>
/// Close the session with the specified <paramref name="id"/> and
/// Closes the session with the specified <paramref name="id"/> and
/// <paramref name="servicePath"/>.
/// </summary>
/// <param name="id">
@ -485,11 +485,11 @@ namespace WebSocketSharp.Server
return;
}
host.CloseSession (id);
host.Sessions.CloseSession (id);
}
/// <summary>
/// Close the session with the specified <paramref name="code"/>, <paramref name="reason"/>,
/// Closes the session with the specified <paramref name="code"/>, <paramref name="reason"/>,
/// <paramref name="id"/> and <paramref name="servicePath"/>.
/// </summary>
/// <param name="code">
@ -520,11 +520,11 @@ namespace WebSocketSharp.Server
return;
}
host.CloseSession (code, reason, id);
host.Sessions.CloseSession (code, reason, id);
}
/// <summary>
/// Close the session with the specified <paramref name="code"/>, <paramref name="reason"/>,
/// Closes the session with the specified <paramref name="code"/>, <paramref name="reason"/>,
/// <paramref name="id"/> and <paramref name="servicePath"/>.
/// </summary>
/// <param name="code">
@ -555,7 +555,7 @@ namespace WebSocketSharp.Server
return;
}
host.CloseSession (code, reason, id);
host.Sessions.CloseSession (code, reason, id);
}
/// <summary>
@ -646,7 +646,7 @@ namespace WebSocketSharp.Server
return false;
}
return host.PingTo (id);
return host.Sessions.PingTo (id);
}
/// <summary>
@ -682,7 +682,7 @@ namespace WebSocketSharp.Server
return false;
}
return host.PingTo (message, id);
return host.Sessions.PingTo (message, id);
}
/// <summary>
@ -717,7 +717,7 @@ namespace WebSocketSharp.Server
return false;
}
return host.SendTo (data, id);
return host.Sessions.SendTo (data, id);
}
/// <summary>
@ -752,7 +752,7 @@ namespace WebSocketSharp.Server
return false;
}
return host.SendTo (data, id);
return host.Sessions.SendTo (data, id);
}
#endregion

View File

@ -29,6 +29,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace WebSocketSharp.Server
@ -95,7 +96,7 @@ namespace WebSocketSharp.Server
/// </value>
public IEnumerable<string> ActiveIDs {
get {
return from result in Broadping (new byte [] {})
return from result in BroadpingInternally (new byte [] {})
where result.Value
select result.Key;
}
@ -139,7 +140,7 @@ namespace WebSocketSharp.Server
/// </value>
public IEnumerable<string> InactiveIDs {
get {
return from result in Broadping (new byte [] {})
return from result in BroadpingInternally (new byte [] {})
where !result.Value
select result.Key;
}
@ -170,7 +171,8 @@ namespace WebSocketSharp.Server
return _sessions [id];
}
catch {
_logger.Error ("'id' not found.\nid: " + id);
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return null;
}
}
@ -301,7 +303,7 @@ namespace WebSocketSharp.Server
}
}
internal void Broadcast (byte [] data)
internal void BroadcastInternally (byte [] data)
{
if (_stopped)
broadcast (data);
@ -309,7 +311,7 @@ namespace WebSocketSharp.Server
broadcastAsync (data);
}
internal void Broadcast (string data)
internal void BroadcastInternally (string data)
{
if (_stopped)
broadcast (data);
@ -317,41 +319,15 @@ namespace WebSocketSharp.Server
broadcastAsync (data);
}
internal Dictionary<string, bool> Broadping (byte [] data)
internal Dictionary<string, bool> BroadpingInternally (byte [] data)
{
var result = new Dictionary<string, bool> ();
foreach (var service in ServiceInstances)
result.Add (service.ID, service.Ping (data));
foreach (var session in ServiceInstances)
result.Add (session.ID, session.Context.WebSocket.Ping (data));
return result;
}
internal bool PingTo (string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
return service.Ping ();
}
internal bool PingTo (string message, string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
return service.Ping (message);
}
internal bool Remove (string id)
{
lock (_sync)
@ -360,34 +336,6 @@ namespace WebSocketSharp.Server
}
}
internal bool SendTo (byte [] data, string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
service.Send (data);
return true;
}
internal bool SendTo (string data, string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
service.Send (data);
return true;
}
internal void Stop ()
{
stopSweepTimer ();
@ -397,8 +345,8 @@ namespace WebSocketSharp.Server
return;
_stopped = true;
foreach (var service in ServiceInstances)
service.Stop ();
foreach (var session in ServiceInstances)
session.Context.WebSocket.Close ();
}
}
@ -411,50 +359,11 @@ namespace WebSocketSharp.Server
return;
_stopped = true;
foreach (var service in ServiceInstances)
service.Stop (data);
foreach (var session in ServiceInstances)
session.Context.WebSocket.Close (data);
}
}
internal void StopServiceInstance (string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return;
}
service.Stop ();
}
internal void StopServiceInstance (ushort code, string reason, string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return;
}
service.Stop (code, reason);
}
internal void StopServiceInstance (CloseStatusCode code, string reason, string id)
{
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return;
}
service.Stop (code, reason);
}
internal bool TryGetServiceInstance (string id, out WebSocketService service)
{
lock (_sync)
@ -467,6 +376,304 @@ namespace WebSocketSharp.Server
#region Public Methods
/// <summary>
/// Broadcasts the specified array of <see cref="byte"/> to all clients of the WebSocket service.
/// </summary>
/// <param name="data">
/// An array of <see cref="byte"/> to broadcast.
/// </param>
public void Broadcast (byte [] data)
{
var msg = data.CheckIfValidSendData ();
if (msg != null)
{
_logger.Error (msg);
return;
}
BroadcastInternally (data);
}
/// <summary>
/// Broadcasts the specified <see cref="string"/> to all clients of the WebSocket service.
/// </summary>
/// <param name="data">
/// A <see cref="string"/> to broadcast.
/// </param>
public void Broadcast (string data)
{
var msg = data.CheckIfValidSendData ();
if (msg != null)
{
_logger.Error (msg);
return;
}
BroadcastInternally (data);
}
/// <summary>
/// Sends Pings to all clients of the WebSocket service.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service received a Pong from each client in a time.
/// </returns>
public Dictionary<string, bool> Broadping ()
{
return BroadpingInternally (new byte [] {});
}
/// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket service.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service received a Pong from each client in a time.
/// </returns>
/// <param name="message">
/// A <see cref="string"/> that contains a message to send.
/// </param>
public Dictionary<string, bool> Broadping (string message)
{
if (message == null || message.Length == 0)
return BroadpingInternally (new byte [] {});
var data = Encoding.UTF8.GetBytes (message);
var msg = data.CheckIfValidPingData ();
if (msg != null)
{
_logger.Error (msg);
return null;
}
return BroadpingInternally (data);
}
/// <summary>
/// Closes the session with the specified <paramref name="id"/>.
/// </summary>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
public void CloseSession (string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return;
}
WebSocketService session;
if (!TryGetServiceInstance (id, out session))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return;
}
session.Context.WebSocket.Close ();
}
/// <summary>
/// Closes the session with the specified <paramref name="code"/>, <paramref name="reason"/>
/// and <paramref name="id"/>.
/// </summary>
/// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
public void CloseSession (ushort code, string reason, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return;
}
WebSocketService session;
if (!TryGetServiceInstance (id, out session))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return;
}
session.Context.WebSocket.Close (code, reason);
}
/// <summary>
/// Closes the session with the specified <paramref name="code"/>, <paramref name="reason"/>
/// and <paramref name="id"/>.
/// </summary>
/// <param name="code">
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID to find.
/// </param>
public void CloseSession (CloseStatusCode code, string reason, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return;
}
WebSocketService session;
if (!TryGetServiceInstance (id, out session))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return;
}
session.Context.WebSocket.Close (code, reason);
}
/// <summary>
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service receives a Pong from the client in a time;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
public bool PingTo (string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return false;
}
WebSocketService session;
if (!TryGetServiceInstance (id, out session))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
return session.Context.WebSocket.Ping ();
}
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service 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>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
/// </param>
public bool PingTo (string message, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return false;
}
WebSocketService session;
if (!TryGetServiceInstance (id, out session))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
return session.Context.WebSocket.Ping (message);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
public bool SendTo (byte [] data, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return false;
}
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
service.Send (data);
return true;
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the specified
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns>
/// <param name="data">
/// A <see cref="string"/> that contains a text data to send.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
/// </param>
public bool SendTo (string data, string id)
{
var msg = id.CheckIfValidSessionID ();
if (msg != null)
{
_logger.Error (msg);
return false;
}
WebSocketService service;
if (!TryGetServiceInstance (id, out service))
{
_logger.Error (
"The WebSocket session with the specified ID not found.\nID: " + id);
return false;
}
service.Send (data);
return true;
}
/// <summary>
/// Cleans up the inactive sessions.
/// </summary>
@ -490,7 +697,7 @@ namespace WebSocketSharp.Server
{
var state = service.State;
if (state == WebSocketState.OPEN)
service.Stop (((ushort) CloseStatusCode.ABNORMAL).ToByteArray (ByteOrder.BIG));
service.Context.WebSocket.Close (CloseStatusCode.ABNORMAL);
else if (state == WebSocketState.CLOSING)
continue;
else