From e3d5dea09634d51f39628d53da111e4b72ea68cd Mon Sep 17 00:00:00 2001 From: sta Date: Fri, 13 Sep 2013 16:54:41 +0900 Subject: [PATCH] Renamed WebSocketServiceManager.cs to WebSocketSessionManager.cs, added IWebSocketSession.cs and modified WebSocketService.cs --- Example2/Chat.cs | 29 +- Example2/Echo.cs | 6 +- Example3/Chat.cs | 29 +- Example3/Echo.cs | 14 +- .../Net/WebSockets/WebSocketContext.cs | 2 +- websocket-sharp/Server/HttpServer.cs | 6 +- .../Server/IWebSocketServiceHost.cs | 26 +- websocket-sharp/Server/IWebSocketSession.cs | 71 +++++ websocket-sharp/Server/WebSocketServer.cs | 9 +- websocket-sharp/Server/WebSocketService.cs | 198 ++++++++------ .../Server/WebSocketServiceHost.cs | 50 ++-- .../Server/WebSocketServiceHostManager.cs | 70 +++-- ...eManager.cs => WebSocketSessionManager.cs} | 248 ++++++------------ websocket-sharp/websocket-sharp.csproj | 3 +- 14 files changed, 418 insertions(+), 343 deletions(-) create mode 100644 websocket-sharp/Server/IWebSocketSession.cs rename websocket-sharp/Server/{WebSocketServiceManager.cs => WebSocketSessionManager.cs} (53%) diff --git a/Example2/Chat.cs b/Example2/Chat.cs index c7d0c011..a87bc389 100644 --- a/Example2/Chat.cs +++ b/Example2/Chat.cs @@ -3,42 +3,37 @@ using System.Threading; using WebSocketSharp; using WebSocketSharp.Server; -namespace Example2 { - +namespace Example2 +{ public class Chat : WebSocketService { private static int _num = 0; private string _name; - private string getName() + private string getName () { - return QueryString.Contains("name") - ? QueryString["name"] - : "anon#" + getNum(); + return Context.QueryString ["name"] ?? ("anon#" + getNum ()); } - private int getNum() + private int getNum () { - return Interlocked.Increment(ref _num); + return Interlocked.Increment (ref _num); } - protected override void OnOpen() + protected override void OnOpen () { - _name = getName(); + _name = getName (); } - protected override void OnMessage(MessageEventArgs e) + protected override void OnMessage (MessageEventArgs e) { - - var msg = String.Format("{0}: {1}", _name, e.Data); - Broadcast(msg); + Broadcast (String.Format ("{0}: {1}", _name, e.Data)); } - protected override void OnClose(CloseEventArgs e) + protected override void OnClose (CloseEventArgs e) { - var msg = String.Format("{0} got logged off...", _name); - Broadcast(msg); + Broadcast (String.Format ("{0} got logged off...", _name)); } } } diff --git a/Example2/Echo.cs b/Example2/Echo.cs index e3fa6ba6..011ed2f4 100644 --- a/Example2/Echo.cs +++ b/Example2/Echo.cs @@ -9,9 +9,11 @@ namespace Example2 { protected override void OnMessage (MessageEventArgs e) { - var msg = QueryString.Contains ("name") - ? String.Format ("Returns '{0}' to {1}", e.Data, QueryString ["name"]) + var name = Context.QueryString ["name"]; + var msg = name != null + ? String.Format ("Returns '{0}' to {1}", e.Data, name) : e.Data; + Send (msg); } diff --git a/Example3/Chat.cs b/Example3/Chat.cs index c6e887e7..b806693c 100644 --- a/Example3/Chat.cs +++ b/Example3/Chat.cs @@ -3,42 +3,37 @@ using System.Threading; using WebSocketSharp; using WebSocketSharp.Server; -namespace Example3 { - +namespace Example3 +{ public class Chat : WebSocketService { private static int _num = 0; private string _name; - private string getName() + private string getName () { - return QueryString.Contains("name") - ? QueryString["name"] - : "anon#" + getNum(); + return Context.QueryString ["name"] ?? ("anon#" + getNum ()); } - private int getNum() + private int getNum () { - return Interlocked.Increment(ref _num); + return Interlocked.Increment (ref _num); } - protected override void OnOpen() + protected override void OnOpen () { - _name = getName(); + _name = getName (); } - protected override void OnMessage(MessageEventArgs e) + protected override void OnMessage (MessageEventArgs e) { - - var msg = String.Format("{0}: {1}", _name, e.Data); - Broadcast(msg); + Broadcast (String.Format ("{0}: {1}", _name, e.Data)); } - protected override void OnClose(CloseEventArgs e) + protected override void OnClose (CloseEventArgs e) { - var msg = String.Format("{0} got logged off...", _name); - Broadcast(msg); + Broadcast (String.Format ("{0} got logged off...", _name)); } } } diff --git a/Example3/Echo.cs b/Example3/Echo.cs index 365e1194..b2515433 100644 --- a/Example3/Echo.cs +++ b/Example3/Echo.cs @@ -2,16 +2,18 @@ using System; using WebSocketSharp; using WebSocketSharp.Server; -namespace Example3 { - +namespace Example3 +{ public class Echo : WebSocketService { - protected override void OnMessage(MessageEventArgs e) + protected override void OnMessage (MessageEventArgs e) { - var msg = QueryString.Contains("name") - ? String.Format("'{0}' returns to {1}", e.Data, QueryString["name"]) + var name = Context.QueryString ["name"]; + var msg = name != null + ? String.Format ("Returns '{0}' to {1}", e.Data, name) : e.Data; - Send(msg); + + Send (msg); } } } diff --git a/websocket-sharp/Net/WebSockets/WebSocketContext.cs b/websocket-sharp/Net/WebSockets/WebSocketContext.cs index 9e98e57b..5dbe98ec 100644 --- a/websocket-sharp/Net/WebSockets/WebSocketContext.cs +++ b/websocket-sharp/Net/WebSockets/WebSocketContext.cs @@ -44,7 +44,7 @@ namespace WebSocketSharp.Net.WebSockets #region Protected Constructors /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// protected WebSocketContext () { diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index f62c1e5b..76f10ca4 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -171,11 +171,11 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets a value indicating whether the server cleans up the inactive WebSocket service - /// instances periodically. + /// Gets or sets a value indicating whether the server cleans up the inactive WebSocket sessions + /// periodically. /// /// - /// true if the server cleans up the inactive WebSocket service instances every 60 seconds; + /// true if the server cleans up the inactive WebSocket sessions every 60 seconds; /// otherwise, false. The default value is true. /// public bool KeepClean { diff --git a/websocket-sharp/Server/IWebSocketServiceHost.cs b/websocket-sharp/Server/IWebSocketServiceHost.cs index 3b5f68ab..02ac649c 100644 --- a/websocket-sharp/Server/IWebSocketServiceHost.cs +++ b/websocket-sharp/Server/IWebSocketServiceHost.cs @@ -46,15 +46,23 @@ namespace WebSocketSharp.Server int ConnectionCount { get; } /// - /// Gets or sets a value indicating whether the WebSocket service host cleans up the inactive service - /// instances periodically. + /// Gets or sets a value indicating whether the WebSocket service host cleans up + /// the inactive sessions periodically. /// /// - /// true if the WebSocket service host cleans up the inactive service instances periodically; + /// true if the WebSocket service host cleans up the inactive sessions periodically; /// otherwise, false. /// bool KeepClean { get; set; } + /// + /// Gets the manager of the sessions to the WebSocket service host. + /// + /// + /// A that manages the sessions. + /// + WebSocketSessionManager Sessions { get; } + /// /// Binds the specified to a instance. /// @@ -101,7 +109,7 @@ namespace WebSocketSharp.Server Dictionary Broadping (byte [] data); /// - /// Close the WebSocket session with the specified . + /// Close the session with the specified . /// /// /// A that contains a session ID to find. @@ -109,7 +117,7 @@ namespace WebSocketSharp.Server void CloseSession (string id); /// - /// Close the WebSocket session with the specified , + /// Close the session with the specified , /// and . /// /// @@ -124,7 +132,7 @@ namespace WebSocketSharp.Server void CloseSession (ushort code, string reason, string id); /// - /// Close the WebSocket session with the specified , + /// Close the session with the specified , /// and . /// /// @@ -167,7 +175,8 @@ namespace WebSocketSharp.Server bool PingTo (string message, string id); /// - /// Sends a binary data to the client associated with the specified . + /// Sends a binary to the client associated with the specified + /// . /// /// /// true if is successfully sent; otherwise, false. @@ -181,7 +190,8 @@ namespace WebSocketSharp.Server bool SendTo (byte [] data, string id); /// - /// Sends a text data to the client associated with the specified . + /// Sends a text to the client associated with the specified + /// . /// /// /// true if is successfully sent; otherwise, false. diff --git a/websocket-sharp/Server/IWebSocketSession.cs b/websocket-sharp/Server/IWebSocketSession.cs new file mode 100644 index 00000000..9aae92f7 --- /dev/null +++ b/websocket-sharp/Server/IWebSocketSession.cs @@ -0,0 +1,71 @@ +#region License +/* + * IWebSocketSession.cs + * + * The MIT License + * + * Copyright (c) 2013 sta.blockhead + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#endregion + +using System; +using WebSocketSharp.Net.WebSockets; + +namespace WebSocketSharp.Server +{ + /// + /// Exposes the properties for the session to the WebSocket service. + /// + public interface IWebSocketSession + { + /// + /// Gets the WebSocket connection request information. + /// + /// + /// A that contains the WebSocket connection request information. + /// + WebSocketContext Context { get; } + + /// + /// Gets the unique ID of the session to the WebSocket service. + /// + /// + /// A that contains the unique ID of the session. + /// + string ID { get; } + + /// + /// Gets the time that the session has been started. + /// + /// + /// A that represents the time that the session has been started. + /// + DateTime StartTime { get; } + + /// + /// Gets the state of the WebSocket connection. + /// + /// + /// One of the values. + /// + WebSocketState State { get; } + } +} diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index 0ea0cf94..b4dabce9 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -146,11 +146,10 @@ namespace WebSocketSharp.Server #region Public Properties /// - /// Gets or sets a value indicating whether the server cleans up the inactive WebSocket service - /// instances periodically. + /// Gets or sets a value indicating whether the server cleans up the inactive sessions periodically. /// /// - /// true if the server cleans up the inactive WebSocket service instances every 60 seconds; + /// true if the server cleans up the inactive sessions every 60 seconds; /// otherwise, false. The default value is true. /// public bool KeepClean { @@ -164,10 +163,10 @@ namespace WebSocketSharp.Server } /// - /// Gets the collection of paths to the WebSocket services that the server provides. + /// Gets the collection of each path to the WebSocket services that the server provides. /// /// - /// An IEnumerable<string> that contains the collection of paths. + /// An IEnumerable<string> that contains the collection of each path to the WebSocket services. /// public IEnumerable ServicePaths { get { diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index e710723c..0d3f6ae7 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -37,18 +37,19 @@ using WebSocketSharp.Net.WebSockets; namespace WebSocketSharp.Server { /// - /// Provides the basic functions of the WebSocket service managed by the WebSocket service host. + /// Provides the basic functions of the WebSocket service used by the WebSocket service host. /// /// /// The WebSocketService class is an abstract class. /// - public abstract class WebSocketService + public abstract class WebSocketService : IWebSocketSession { #region Private Fields private WebSocket _websocket; private WebSocketContext _context; - private WebSocketServiceManager _sessions; + private WebSocketSessionManager _sessions; + private DateTime _start; #endregion @@ -59,18 +60,8 @@ namespace WebSocketSharp.Server /// public WebSocketService () { - ID = String.Empty; IsBound = false; - } - - #endregion - - #region Internal Properties - - internal WebSocket WebSocket { - get { - return _websocket; - } + _start = DateTime.MaxValue; } #endregion @@ -99,32 +90,17 @@ namespace WebSocketSharp.Server _websocket.Log = value; } } - + /// - /// Gets the collection of query string variables used in the WebSocket connection request. + /// Gets the manager of the sessions to the WebSocket service. /// /// - /// A that contains the collection of query string variables. + /// A that manages the sessions + /// to the WebSocket service. /// - protected NameValueCollection QueryString { + protected WebSocketSessionManager Sessions { get { - return IsBound - ? _context.QueryString - : null; - } - } - - /// - /// Gets the collection of the WebSocket sessions managed by the WebSocket service host. - /// - /// - /// A that contains a collection of the WebSocket sessions. - /// - protected WebSocketServiceManager Sessions { - get { - return IsBound - ? _sessions - : null; + return _sessions; } } @@ -133,10 +109,22 @@ namespace WebSocketSharp.Server #region Public Properties /// - /// Gets the session ID of the current instance. + /// Gets the WebSocket connection request information. /// /// - /// A that contains a session ID. + /// A that contains the WebSocket connection request information. + /// + public WebSocketContext Context { + get { + return _context; + } + } + + /// + /// Gets the unique ID of the current instance. + /// + /// + /// A that contains the unique ID. /// public string ID { get; private set; @@ -154,12 +142,42 @@ namespace WebSocketSharp.Server get; private set; } + /// + /// Gets the time that the current instance has been started. + /// + /// + /// A that represents the time that the current + /// instance has been started. + /// + public DateTime StartTime { + get { + return _start; + } + } + + /// + /// Gets the state of the WebSocket connection. + /// + /// + /// One of the values. + /// + public WebSocketState State { + get { + return IsBound + ? _websocket.ReadyState + : WebSocketState.CONNECTING; + } + } + #endregion #region Private Methods private void onClose (object sender, CloseEventArgs e) { + if (ID == null) + return; + _sessions.Remove (ID); OnClose (e); } @@ -177,6 +195,13 @@ namespace WebSocketSharp.Server private void onOpen (object sender, EventArgs e) { ID = _sessions.Add (this); + if (ID == null) + { + _websocket.Close (CloseStatusCode.AWAY); + return; + } + + _start = DateTime.Now; OnOpen (); } @@ -184,7 +209,7 @@ namespace WebSocketSharp.Server #region Internal Methods - internal void Bind (WebSocketContext context, WebSocketServiceManager sessions) + internal void Bind (WebSocketContext context, WebSocketSessionManager sessions) { if (IsBound) return; @@ -206,16 +231,6 @@ namespace WebSocketSharp.Server return _websocket.Ping (data); } - internal void SendAsync (byte [] data, Action completed) - { - _websocket.SendAsync (data, completed); - } - - internal void SendAsync (string data, Action completed) - { - _websocket.SendAsync (data, completed); - } - internal void Stop (byte [] data) { _websocket.Close (data); @@ -226,8 +241,7 @@ namespace WebSocketSharp.Server #region Protected Methods /// - /// Broadcasts the specified array of to the clients of every - /// instances in the . + /// Broadcasts the specified array of to all clients of the WebSocket service. /// /// /// An array of to broadcast. @@ -250,8 +264,7 @@ namespace WebSocketSharp.Server } /// - /// Broadcasts the specified to the clients of every - /// instances in the . + /// Broadcasts the specified to all clients of the WebSocket service. /// /// /// A to broadcast. @@ -274,12 +287,11 @@ namespace WebSocketSharp.Server } /// - /// Sends Pings to the clients of every instances - /// in the . + /// Sends Pings to all clients of the WebSocket service. /// /// /// A Dictionary<string, bool> that contains the collection of pairs of session ID and value - /// indicating whether the each instance received a Pong in a time. + /// indicating whether the WebSocket service received a Pong from each client in a time. /// protected virtual Dictionary Broadping () { @@ -289,12 +301,11 @@ namespace WebSocketSharp.Server } /// - /// Sends Pings with the specified to the clients of every - /// instances in the . + /// Sends Pings with the specified to all clients of the WebSocket service. /// /// /// A Dictionary<string, bool> that contains the collection of pairs of session ID and value - /// indicating whether the each instance received a Pong in a time. + /// indicating whether the WebSocket service received a Pong from each client in a time. /// /// /// A that contains a message to send. @@ -374,12 +385,11 @@ namespace WebSocketSharp.Server } /// - /// Sends a Ping to the client of the instance - /// with the specified . + /// Sends a Ping to the client associated with the specified . /// /// - /// true if the instance with receives - /// a Pong in a time; otherwise, false. + /// true if the WebSocket service receives a Pong from the client in a time; + /// otherwise, false. /// /// /// A that contains a session ID that represents the destination for the Ping. @@ -402,12 +412,12 @@ namespace WebSocketSharp.Server } /// - /// Sends a Ping with the specified to the client of - /// the instance with the specified . + /// Sends a Ping with the specified to the client associated with + /// the specified . /// /// - /// true if the instance with receives - /// a Pong in a time; otherwise, false. + /// true if the WebSocket service receives a Pong from the client in a time; + /// otherwise, false. /// /// /// A that contains a message to send. @@ -433,8 +443,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a binary data to the client of the instance - /// with the specified . + /// Sends a binary to the client associated with the specified + /// . /// /// /// true if is successfully sent; otherwise, false. @@ -463,8 +473,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a text data to the client of the instance - /// with the specified . + /// Sends a text to the client associated with the specified + /// . /// /// /// true if is successfully sent; otherwise, false. @@ -523,8 +533,8 @@ namespace WebSocketSharp.Server /// Sends a Ping to the client of the current instance. /// /// - /// true if the current instance receives a Pong in a time; - /// otherwise, false. + /// true if the current instance receives a Pong + /// from the client in a time; otherwise, false. /// public virtual bool Ping () { @@ -538,8 +548,8 @@ namespace WebSocketSharp.Server /// the current instance. /// /// - /// true if the current instance receives a Pong in a time; - /// otherwise, false. + /// true if the current instance receives a Pong + /// from the client in a time; otherwise, false. /// /// /// A that contains a message to send. @@ -552,7 +562,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a binary data to the client of the current instance. + /// Sends a binary to the client of the current + /// instance. /// /// /// An array of that contains a binary data to send. @@ -564,7 +575,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a text data to the client of the current instance. + /// Sends a text to the client of the current + /// instance. /// /// /// A that contains a text data to send. @@ -575,6 +587,40 @@ namespace WebSocketSharp.Server _websocket.Send (data); } + /// + /// Sends a binary to the client of the current + /// instance asynchronously. + /// + /// + /// An array of that contains a binary data to send. + /// + /// + /// An delegate that references the method(s) called when + /// the asynchronous operation completes. + /// + public virtual void SendAsync (byte [] data, Action completed) + { + if (IsBound) + _websocket.SendAsync (data, completed); + } + + /// + /// Sends a text to the client of the current + /// instance asynchronously. + /// + /// + /// A that contains a text data to send. + /// + /// + /// An delegate that references the method(s) called when + /// the asynchronous operation completes. + /// + public virtual void SendAsync (string data, Action completed) + { + if (IsBound) + _websocket.SendAsync (data, completed); + } + /// /// Starts the current instance. /// diff --git a/websocket-sharp/Server/WebSocketServiceHost.cs b/websocket-sharp/Server/WebSocketServiceHost.cs index 6fc7c843..f74855e5 100644 --- a/websocket-sharp/Server/WebSocketServiceHost.cs +++ b/websocket-sharp/Server/WebSocketServiceHost.cs @@ -53,7 +53,7 @@ namespace WebSocketSharp.Server #region Private Fields private string _servicePath; - private WebSocketServiceManager _sessions; + private WebSocketSessionManager _sessions; #endregion @@ -62,7 +62,7 @@ namespace WebSocketSharp.Server internal WebSocketServiceHost (Logger logger) : base (logger) { - _sessions = new WebSocketServiceManager (logger); + _sessions = new WebSocketSessionManager (logger); } #endregion @@ -91,7 +91,7 @@ namespace WebSocketSharp.Server public WebSocketServiceHost (string url) : base (url) { - _sessions = new WebSocketServiceManager (Log); + _sessions = new WebSocketSessionManager (Log); } /// @@ -185,7 +185,7 @@ namespace WebSocketSharp.Server public WebSocketServiceHost (System.Net.IPAddress address, int port, string servicePath, bool secure) : base (address, port, servicePath, secure) { - _sessions = new WebSocketServiceManager (Log); + _sessions = new WebSocketSessionManager (Log); } #endregion @@ -206,10 +206,10 @@ namespace WebSocketSharp.Server /// /// Gets or sets a value indicating whether the WebSocket service host cleans up - /// the inactive instances periodically. + /// the inactive sessions periodically. /// /// - /// true if the WebSocket service host cleans up the inactive WebSocket service instances + /// true if the WebSocket service host cleans up the inactive sessions /// every 60 seconds; otherwise, false. The default value is true. /// public bool KeepClean { @@ -222,18 +222,6 @@ namespace WebSocketSharp.Server } } - /// - /// Gets the collection of the WebSocket sessions managed by the WebSocket service host. - /// - /// - /// A that contains a collection of the WebSocket sessions. - /// - public WebSocketServiceManager Sessions { - get { - return _sessions; - } - } - /// /// Gets the path to the WebSocket service that the WebSocket service host provides. /// @@ -249,6 +237,18 @@ namespace WebSocketSharp.Server } } + /// + /// Gets the manager of the sessions to the WebSocket service host. + /// + /// + /// A that manages the sessions. + /// + public WebSocketSessionManager Sessions { + get { + return _sessions; + } + } + /// /// Gets the WebSocket URL on which to listen for incoming connection attempts. /// @@ -390,7 +390,7 @@ namespace WebSocketSharp.Server } /// - /// Close the WebSocket session with the specified . + /// Close the session with the specified . /// /// /// A that contains a session ID to find. @@ -408,7 +408,7 @@ namespace WebSocketSharp.Server } /// - /// Close the WebSocket session with the specified , + /// Close the session with the specified , /// and . /// /// @@ -433,7 +433,7 @@ namespace WebSocketSharp.Server } /// - /// Close the WebSocket session with the specified , + /// Close the session with the specified , /// and . /// /// @@ -506,7 +506,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a binary data to the client associated with the specified . + /// Sends a binary to the client associated with the specified + /// . /// /// /// true if is successfully sent; otherwise, false. @@ -530,7 +531,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a text data to the client associated with the specified . + /// Sends a text to the client associated with the specified + /// . /// /// /// true if is successfully sent; otherwise, false. @@ -654,7 +656,7 @@ namespace WebSocketSharp.Server } /// - /// Stops the WebSocket service host with the specified array of . + /// Stops receiving the WebSocket connection requests with the specified array of . /// /// /// An array of that contains the reason for stop. diff --git a/websocket-sharp/Server/WebSocketServiceHostManager.cs b/websocket-sharp/Server/WebSocketServiceHostManager.cs index 79fefc6c..adbeb296 100644 --- a/websocket-sharp/Server/WebSocketServiceHostManager.cs +++ b/websocket-sharp/Server/WebSocketServiceHostManager.cs @@ -28,13 +28,15 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using WebSocketSharp.Net; namespace WebSocketSharp.Server { /// - /// Manages the collection of the WebSocket service hosts. + /// Manages the WebSocket services provided by the and + /// . /// public class WebSocketServiceHostManager { @@ -88,7 +90,7 @@ namespace WebSocketSharp.Server get { lock (_sync) { - return _serviceHosts.Values; + return _serviceHosts.Values.ToList (); } } } @@ -98,10 +100,10 @@ namespace WebSocketSharp.Server #region Public Properties /// - /// Gets the connection count to the WebSocket services managed by the . + /// Gets the connection count to the WebSocket services provided by the WebSocket server. /// /// - /// An that contains the connection count. + /// An that contains the connection count to the WebSocket services. /// public int ConnectionCount { get { @@ -114,7 +116,7 @@ namespace WebSocketSharp.Server } /// - /// Gets the number of the WebSocket services managed by the . + /// Gets the number of the WebSocket services provided by the WebSocket server. /// /// /// An that contains the number of the WebSocket services. @@ -129,16 +131,16 @@ namespace WebSocketSharp.Server } /// - /// Gets the collection of paths to the WebSocket services managed by the . + /// Gets the collection of each path to the WebSocket services provided by the WebSocket server. /// /// - /// An IEnumerable<string> that contains the collection of paths. + /// An IEnumerable<string> that contains the collection of each path to the WebSocket services. /// public IEnumerable ServicePaths { get { lock (_sync) { - return _serviceHosts.Keys; + return _serviceHosts.Keys.ToList (); } } } @@ -243,7 +245,7 @@ namespace WebSocketSharp.Server /// /// Broadcasts the specified array of to all clients of the WebSocket services - /// managed by the . + /// provided by the WebSocket server. /// /// /// An array of to broadcast. @@ -263,7 +265,7 @@ namespace WebSocketSharp.Server /// /// Broadcasts the specified to all clients of the WebSocket services - /// managed by the . + /// provided by the WebSocket server. /// /// /// A to broadcast. @@ -348,7 +350,7 @@ namespace WebSocketSharp.Server } /// - /// Sends Pings to all clients of the WebSocket services managed by the . + /// Sends Pings to all clients of the WebSocket services provided by the WebSocket server. /// /// /// A Dictionary<string, Dictionary<string, bool>> that contains the collection of @@ -362,12 +364,13 @@ namespace WebSocketSharp.Server /// /// Sends Pings with the specified to all clients of the WebSocket services - /// managed by the . + /// provided by the WebSocket server. /// /// /// A Dictionary<string, Dictionary<string, bool>> that contains the collection of /// service paths and pairs of session ID and value indicating whether each WebSocket service /// received a Pong from each client in a time. + /// If is invalid, returns . /// /// /// A that contains a message to send. @@ -457,7 +460,7 @@ namespace WebSocketSharp.Server } /// - /// Close the WebSocket session with the specified and + /// Close the session with the specified and /// . /// /// @@ -486,7 +489,7 @@ namespace WebSocketSharp.Server } /// - /// Close the WebSocket session with the specified , , + /// Close the session with the specified , , /// and . /// /// @@ -521,7 +524,7 @@ namespace WebSocketSharp.Server } /// - /// Close the WebSocket session with the specified , , + /// Close the session with the specified , , /// and . /// /// @@ -584,6 +587,35 @@ namespace WebSocketSharp.Server return host.ConnectionCount; } + /// + /// Gets the manager of the sessions to the WebSocket service with the specified . + /// + /// + /// A if the WebSocket service is successfully found; + /// otherwise, . + /// + /// + /// A that contains an absolute path to the WebSocket service to find. + /// + public WebSocketSessionManager GetSessions (string servicePath) + { + var msg = servicePath.CheckIfValidServicePath (); + if (msg != null) + { + _logger.Error (msg); + return null; + } + + IWebSocketServiceHost host; + if (!TryGetServiceHost (servicePath, out host)) + { + _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); + return null; + } + + return host.Sessions; + } + /// /// Sends a Ping to the client associated with the specified and /// . @@ -654,8 +686,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a binary data to the client associated with the specified and - /// . + /// Sends a binary to the client associated with the specified + /// and . /// /// /// true if is successfully sent; otherwise, false. @@ -689,8 +721,8 @@ namespace WebSocketSharp.Server } /// - /// Sends a text data to the client associated with the specified and - /// . + /// Sends a text to the client associated with the specified + /// and . /// /// /// true if is successfully sent; otherwise, false. diff --git a/websocket-sharp/Server/WebSocketServiceManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs similarity index 53% rename from websocket-sharp/Server/WebSocketServiceManager.cs rename to websocket-sharp/Server/WebSocketSessionManager.cs index ce39722d..8b912a1c 100644 --- a/websocket-sharp/Server/WebSocketServiceManager.cs +++ b/websocket-sharp/Server/WebSocketSessionManager.cs @@ -1,6 +1,6 @@ #region License /* - * WebSocketServiceManager.cs + * WebSocketSessionManager.cs * * The MIT License * @@ -34,15 +34,15 @@ using System.Timers; namespace WebSocketSharp.Server { /// - /// Manages the collection of instances. + /// Manages the sessions to the Websocket service. /// - public class WebSocketServiceManager + public class WebSocketSessionManager { #region Private Fields private object _forSweep; private Logger _logger; - private Dictionary _services; + private Dictionary _sessions; private volatile bool _stopped; private volatile bool _sweeping; private Timer _sweepTimer; @@ -52,16 +52,16 @@ namespace WebSocketSharp.Server #region Internal Constructors - internal WebSocketServiceManager () + internal WebSocketSessionManager () : this (new Logger ()) { } - internal WebSocketServiceManager (Logger logger) + internal WebSocketSessionManager (Logger logger) { _logger = logger; _forSweep = new object (); - _services = new Dictionary (); + _sessions = new Dictionary (); _stopped = false; _sweeping = false; _sync = new object (); @@ -72,15 +72,26 @@ namespace WebSocketSharp.Server #endregion + #region Internal Properties + + internal IEnumerable ServiceInstances { + get { + lock (_sync) + { + return _sessions.Values.ToList (); + } + } + } + + #endregion + #region Public Properties /// - /// Gets the collection of IDs of active instances - /// managed by the . + /// Gets the collection of every ID of the active sessions to the Websocket service. /// /// - /// An IEnumerable<string> that contains the collection of IDs - /// of active instances. + /// An IEnumerable<string> that contains the collection of every ID of the active sessions. /// public IEnumerable ActiveIDs { get { @@ -91,46 +102,40 @@ namespace WebSocketSharp.Server } /// - /// Gets the number of instances - /// managed by the . + /// Gets the number of the sessions to the Websocket service. /// /// - /// An that contains the number of instances - /// managed by the . + /// An that contains the number of the sessions. /// public int Count { get { lock (_sync) { - return _services.Count; + return _sessions.Count; } } } /// - /// Gets the collection of IDs of instances - /// managed by the . + /// Gets the collection of every ID of the sessions to the Websocket service. /// /// - /// An IEnumerable<string> that contains the collection of IDs - /// of instances. + /// An IEnumerable<string> that contains the collection of every ID of the sessions. /// public IEnumerable IDs { get { lock (_sync) { - return _services.Keys; + return _sessions.Keys.ToList (); } } } /// - /// Gets the collection of IDs of inactive instances - /// managed by the . + /// Gets the collection of every ID of the inactive sessions to the Websocket service. /// /// - /// An IEnumerable<string> that contains the collection of IDs - /// of inactive instances. + /// An IEnumerable<string> that contains the collection of every ID of the inactive sessions. /// public IEnumerable InactiveIDs { get { @@ -141,17 +146,16 @@ namespace WebSocketSharp.Server } /// - /// Gets the instance with the specified - /// from the . + /// Gets the session information with the specified . /// /// - /// A instance with if it is successfully found; + /// A instance with if it is successfully found; /// otherwise, . /// /// - /// A that contains an ID to find. + /// A that contains a session ID to find. /// - public WebSocketService this [string id] { + public IWebSocketSession this [string id] { get { var msg = id.CheckIfValidSessionID (); if (msg != null) @@ -163,7 +167,7 @@ namespace WebSocketSharp.Server lock (_sync) { try { - return _services [id]; + return _sessions [id]; } catch { _logger.Error ("'id' not found.\nid: " + id); @@ -174,12 +178,11 @@ namespace WebSocketSharp.Server } /// - /// Gets a value indicating whether the cleans up - /// the inactive instances periodically. + /// Gets a value indicating whether the manager cleans up the inactive sessions periodically. /// /// - /// true if the cleans up the inactive - /// instances every 60 seconds; otherwise, false. + /// true if the manager cleans up the inactive sessions every 60 seconds; + /// otherwise, false. /// public bool KeepClean { get { @@ -198,19 +201,15 @@ namespace WebSocketSharp.Server } /// - /// Gets the collection of the instances - /// managed by the . + /// Gets the collection of the session informations to the Websocket service. /// /// - /// An IEnumerable<WebSocketService> that contains the collection of - /// the instances. + /// An IEnumerable<IWebSocketSession> that contains the collection of the session informations. /// - public IEnumerable ServiceInstances { + public IEnumerable Sessions { get { - lock (_sync) - { - return _services.Values; - } + return from IWebSocketSession session in ServiceInstances + select session; } } @@ -232,9 +231,7 @@ namespace WebSocketSharp.Server private void broadcastAsync (byte [] data) { - var copied = copy (); - var services = copied.Values.GetEnumerator (); - + var services = ServiceInstances.GetEnumerator (); Action completed = null; completed = () => { @@ -248,9 +245,7 @@ namespace WebSocketSharp.Server private void broadcastAsync (string data) { - var copied = copy (); - var services = copied.Values.GetEnumerator (); - + var services = ServiceInstances.GetEnumerator (); Action completed = null; completed = () => { @@ -262,14 +257,6 @@ namespace WebSocketSharp.Server services.Current.SendAsync (data, completed); } - private Dictionary copy () - { - lock (_sync) - { - return new Dictionary (_services); - } - } - private static string createID () { return Guid.NewGuid ().ToString ("N"); @@ -300,7 +287,7 @@ namespace WebSocketSharp.Server #region Internal Methods - internal string Add (WebSocketService service) + internal string Add (WebSocketService session) { lock (_sync) { @@ -308,19 +295,12 @@ namespace WebSocketSharp.Server return null; var id = createID (); - _services.Add (id, service); + _sessions.Add (id, session); return id; } } - /// - /// Broadcasts the specified array of to the clients of every - /// instances managed by the . - /// - /// - /// An array of to broadcast. - /// internal void Broadcast (byte [] data) { if (_stopped) @@ -329,13 +309,6 @@ namespace WebSocketSharp.Server broadcastAsync (data); } - /// - /// Broadcasts the specified to the clients of every - /// instances managed by the . - /// - /// - /// A to broadcast. - /// internal void Broadcast (string data) { if (_stopped) @@ -344,71 +317,35 @@ namespace WebSocketSharp.Server broadcastAsync (data); } - /// - /// Sends Pings with the specified to the clients of every - /// instances managed by the . - /// - /// - /// A Dictionary<string, bool> that contains the collection of pairs of ID and value indicating - /// whether each instance received a Pong from the client in a time. - /// - /// - /// An array of that contains a message data to send. - /// internal Dictionary Broadping (byte [] data) { var result = new Dictionary (); - foreach (var session in copy ()) - result.Add (session.Key, session.Value.Ping (data)); + foreach (var service in ServiceInstances) + result.Add (service.ID, service.Ping (data)); return result; } - /// - /// Sends a Ping to the client of the instance - /// with the specified . - /// - /// - /// true if the instance receives a Pong from the client - /// in a time; otherwise, false. - /// - /// - /// A that contains an ID that represents the destination for the Ping. - /// internal bool PingTo (string id) { WebSocketService service; if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "The WebSocket session with the specified ID not found.\nID: " + id); return false; } return service.Ping (); } - /// - /// Sends a Ping with the specified to the client of the - /// instance with the specified . - /// - /// - /// true if the instance receives a Pong from the client - /// in a time; otherwise, false. - /// - /// - /// A that contains a message to send. - /// - /// - /// A that contains an ID that represents the destination for the Ping. - /// internal bool PingTo (string message, string id) { WebSocketService service; if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "The WebSocket session with the specified ID not found.\nID: " + id); return false; } @@ -419,30 +356,17 @@ namespace WebSocketSharp.Server { lock (_sync) { - return _services.Remove (id); + return _sessions.Remove (id); } } - /// - /// Sends a binary data to the client of the instance - /// with the specified . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// An array of that contains a binary data to send. - /// - /// - /// A that contains an ID that represents the destination for the data. - /// internal bool SendTo (byte [] data, string id) { WebSocketService service; if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "The WebSocket session with the specified ID not found.\nID: " + id); return false; } @@ -450,26 +374,13 @@ namespace WebSocketSharp.Server return true; } - /// - /// Sends a text data to the client of the instance - /// with the specified . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// A that contains a text data to send. - /// - /// - /// A that contains an ID that represents the destination for the data. - /// internal bool SendTo (string data, string id) { WebSocketService service; if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "The WebSocket session with the specified ID not found.\nID: " + id); return false; } @@ -486,7 +397,7 @@ namespace WebSocketSharp.Server return; _stopped = true; - foreach (var service in copy ().Values) + foreach (var service in ServiceInstances) service.Stop (); } } @@ -500,7 +411,7 @@ namespace WebSocketSharp.Server return; _stopped = true; - foreach (var service in copy ().Values) + foreach (var service in ServiceInstances) service.Stop (data); } } @@ -511,7 +422,7 @@ namespace WebSocketSharp.Server if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "The WebSocket session with the specified ID not found.\nID: " + id); return; } @@ -524,7 +435,7 @@ namespace WebSocketSharp.Server if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "The WebSocket session with the specified ID not found.\nID: " + id); return; } @@ -537,19 +448,27 @@ namespace WebSocketSharp.Server if (!TryGetServiceInstance (id, out service)) { _logger.Error ( - "The WebSocket service instance with the specified ID not found.\nID: " + id); + "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) + { + return _sessions.TryGetValue (id, out service); + } + } + #endregion #region Public Methods /// - /// Cleans up the inactive instances. + /// Cleans up the inactive sessions. /// public void Sweep () { @@ -567,15 +486,15 @@ namespace WebSocketSharp.Server break; WebSocketService service; - if (_services.TryGetValue (id, out service)) + if (_sessions.TryGetValue (id, out service)) { - var state = service.WebSocket.ReadyState; + var state = service.State; if (state == WebSocketState.OPEN) service.Stop (((ushort) CloseStatusCode.ABNORMAL).ToByteArray (ByteOrder.BIG)); else if (state == WebSocketState.CLOSING) continue; else - _services.Remove (id); + _sessions.Remove (id); } } } @@ -585,25 +504,26 @@ namespace WebSocketSharp.Server } /// - /// Tries to get the instance with the specified . + /// Tries to get the session information with the specified . /// /// - /// true if the instance with - /// is successfully found; otherwise, false. + /// true if the session information is successfully found; + /// otherwise, false. /// /// - /// A that contains an ID to find. + /// A that contains a session ID to find. /// - /// - /// When this method returns, contains a instance with - /// if it is successfully found; otherwise, . + /// + /// When this method returns, a instance that contains the session + /// information if it is successfully found; otherwise, . /// - public bool TryGetServiceInstance (string id, out WebSocketService service) + public bool TryGetSession (string id, out IWebSocketSession session) { - lock (_sync) - { - return _services.TryGetValue (id, out service); - } + WebSocketService service; + var result = TryGetServiceInstance (id, out service); + session = service; + + return result; } #endregion diff --git a/websocket-sharp/websocket-sharp.csproj b/websocket-sharp/websocket-sharp.csproj index 4dee7e0c..0774bf83 100644 --- a/websocket-sharp/websocket-sharp.csproj +++ b/websocket-sharp/websocket-sharp.csproj @@ -110,7 +110,6 @@ - @@ -128,6 +127,8 @@ + +