From d07265dd8e7897af925013392b3e8bc5f9c80e78 Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 13 Feb 2014 14:38:13 +0900 Subject: [PATCH] Refactored WebSocketServiceHostManager.cs --- Example2/Program.cs | 2 +- Example3/Program.cs | 2 +- .../Server/WebSocketServiceHostManager.cs | 692 +++--------------- 3 files changed, 122 insertions(+), 574 deletions(-) diff --git a/Example2/Program.cs b/Example2/Program.cs index fa387677..686701c4 100644 --- a/Example2/Program.cs +++ b/Example2/Program.cs @@ -50,7 +50,7 @@ namespace Example2 Console.WriteLine ( "A WebSocket server listening on port: {0} service paths:", wssv.Port); - foreach (var path in wssv.WebSocketServices.ServicePaths) + foreach (var path in wssv.WebSocketServices.Paths) Console.WriteLine (" {0}", path); } diff --git a/Example3/Program.cs b/Example3/Program.cs index 4c811dde..78133dd9 100644 --- a/Example3/Program.cs +++ b/Example3/Program.cs @@ -55,7 +55,7 @@ namespace Example3 "An HTTP server listening on port: {0} WebSocket service paths:", _httpsv.Port); - foreach (var path in _httpsv.WebSocketServices.ServicePaths) + foreach (var path in _httpsv.WebSocketServices.Paths) Console.WriteLine (" {0}", path); } diff --git a/websocket-sharp/Server/WebSocketServiceHostManager.cs b/websocket-sharp/Server/WebSocketServiceHostManager.cs index 90a72fe1..45bce088 100644 --- a/websocket-sharp/Server/WebSocketServiceHostManager.cs +++ b/websocket-sharp/Server/WebSocketServiceHostManager.cs @@ -29,7 +29,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using System.Threading; using WebSocketSharp.Net; @@ -44,9 +43,9 @@ namespace WebSocketSharp.Server { #region Private Fields + private Dictionary _hosts; private volatile bool _keepClean; private Logger _logger; - private Dictionary _serviceHosts; private volatile ServerState _state; private object _sync; @@ -62,8 +61,8 @@ namespace WebSocketSharp.Server internal WebSocketServiceHostManager (Logger logger) { _logger = logger; + _hosts = new Dictionary (); _keepClean = true; - _serviceHosts = new Dictionary (); _state = ServerState.READY; _sync = new object (); } @@ -76,45 +75,55 @@ namespace WebSocketSharp.Server /// Gets the number of the WebSocket services provided by the server. /// /// - /// An that represents the number of the WebSocket services - /// provided by the server. + /// An that represents the number of the WebSocket services. /// public int Count { get { lock (_sync) { - return _serviceHosts.Count; + return _hosts.Count; } } } /// - /// Gets a WebSocket service host with the specified . + /// Gets the collection of the WebSocket service hosts. /// /// - /// A instance that represents the - /// WebSocket service host if it's successfully found; otherwise, - /// . + /// An IEnumerable<WebSocketServiceHost> that contains the collection of the WebSocket + /// service hosts. /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. + public IEnumerable Hosts { + get { + return copyHosts ().Values; + } + } + + /// + /// Gets a WebSocket service host with the specified . + /// + /// + /// A instance that represents the WebSocket service host if + /// it's successfully found; otherwise, . + /// + /// + /// A that represents the absolute path to the WebSocket service to find. /// - public WebSocketServiceHost this [string servicePath] { + public WebSocketServiceHost this [string path] { get { WebSocketServiceHost host; - TryGetServiceHost (servicePath, out host); + TryGetServiceHost (path, out host); return host; } } /// - /// Gets a value indicating whether the manager cleans up periodically the - /// every inactive session to the WebSocket services provided by the server. + /// Gets a value indicating whether the manager cleans up periodically the inactive sessions in + /// the WebSocket services provided by the server. /// /// - /// true if the manager cleans up periodically the every inactive - /// session to the WebSocket services; otherwise, false. + /// true if the manager cleans up the inactive sessions every 60 seconds; otherwise, + /// false. /// public bool KeepClean { get { @@ -127,54 +136,35 @@ namespace WebSocketSharp.Server return; _keepClean = value; - foreach (var host in _serviceHosts.Values) + foreach (var host in _hosts.Values) host.KeepClean = value; } } } /// - /// Gets the collection of the WebSocket service hosts managed by the manager. - /// - /// - /// An IEnumerable<WebSocketServiceHost> that contains the collection of - /// the WebSocket service hosts. - /// - public IEnumerable ServiceHosts { - get { - lock (_sync) { - return _serviceHosts.Values.ToList (); - } - } - } - - /// - /// Gets the collection of every path to the WebSocket services provided by - /// the server. + /// Gets the collection of every path to the WebSocket services provided by the server. /// /// - /// An IEnumerable<string> that contains the collection of every path to - /// the WebSocket services. + /// An IEnumerable<string> that contains the collection of every path to the WebSocket + /// services. /// - public IEnumerable ServicePaths { + public IEnumerable Paths { get { - lock (_sync) { - return _serviceHosts.Keys.ToList (); - } + return copyHosts ().Keys; } } /// - /// Gets the number of the WebSocket sessions to the server. + /// Gets the number of the WebSocket sessions in the server. /// /// - /// An that represents the number of the sessions to the - /// server. + /// An that represents the number of the sessions in the server. /// public int SessionCount { get { var count = 0; - foreach (var host in ServiceHosts) { + foreach (var host in Hosts) { if (_state != ServerState.START) break; @@ -193,7 +183,7 @@ namespace WebSocketSharp.Server { var cache = new Dictionary (); try { - foreach (var host in ServiceHosts) { + foreach (var host in Hosts) { if (_state != ServerState.START) break; @@ -215,7 +205,7 @@ namespace WebSocketSharp.Server { var cache = new Dictionary (); try { - foreach (var host in ServiceHosts) { + foreach (var host in Hosts) { if (_state != ServerState.START) break; @@ -249,59 +239,60 @@ namespace WebSocketSharp.Server } private Dictionary> broadping ( - byte [] frameAsBytes, int timeOut) + byte [] frame, int millisecondsTimeout) { var result = new Dictionary> (); - foreach (var host in ServiceHosts) { + foreach (var host in Hosts) { if (_state != ServerState.START) break; - result.Add ( - host.ServicePath, host.Sessions.Broadping (frameAsBytes, timeOut)); + result.Add (host.ServicePath, host.Sessions.Broadping (frame, millisecondsTimeout)); } return result; } - private string checkIfCanSend (Func checkParams) + private Dictionary copyHosts () { - return _state.CheckIfStart () ?? checkParams (); + lock (_sync) { + return new Dictionary (_hosts); + } } #endregion #region Internal Methods - internal void Add (string servicePath, WebSocketServiceHost serviceHost) + internal void Add (string path, WebSocketServiceHost host) { lock (_sync) { - WebSocketServiceHost host; - if (_serviceHosts.TryGetValue (servicePath, out host)) { + WebSocketServiceHost find; + if (_hosts.TryGetValue (path, out find)) { _logger.Error ( - "A WebSocket service with the specified path already exists.\npath: " + servicePath); + "A WebSocket service with the specified path already exists.\npath: " + path); + return; } if (_state == ServerState.START) - serviceHost.Sessions.Start (); + host.Sessions.Start (); - _serviceHosts.Add (servicePath, serviceHost); + _hosts.Add (path, host); } } - internal bool Remove (string servicePath) + internal bool Remove (string path) { - servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); + path = HttpUtility.UrlDecode (path).TrimEndSlash (); WebSocketServiceHost host; lock (_sync) { - if (!_serviceHosts.TryGetValue (servicePath, out host)) { - _logger.Error ( - "A WebSocket service with the specified path not found.\npath: " + servicePath); + if (!_hosts.TryGetValue (path, out host)) { + _logger.Error ("A WebSocket service with the specified path not found.\npath: " + path); return false; } - _serviceHosts.Remove (servicePath); + _hosts.Remove (path); } if (host.Sessions.State == ServerState.START) @@ -314,7 +305,7 @@ namespace WebSocketSharp.Server internal void Start () { lock (_sync) { - foreach (var host in _serviceHosts.Values) + foreach (var host in _hosts.Values) host.Sessions.Start (); _state = ServerState.START; @@ -328,33 +319,30 @@ namespace WebSocketSharp.Server var payload = new PayloadData (data); var args = new CloseEventArgs (payload); - var frameAsBytes = - send - ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () - : null; + var frameAsBytes = send + ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () + : null; - foreach (var host in _serviceHosts.Values) + foreach (var host in _hosts.Values) host.Sessions.Stop (args, frameAsBytes); - _serviceHosts.Clear (); + _hosts.Clear (); _state = ServerState.STOP; } } - internal bool TryGetServiceHostInternally ( - string servicePath, out WebSocketServiceHost serviceHost) + internal bool TryGetServiceHostInternally (string path, out WebSocketServiceHost host) { - servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); + path = HttpUtility.UrlDecode (path).TrimEndSlash (); bool result; lock (_sync) { - result = _serviceHosts.TryGetValue (servicePath, out serviceHost); + result = _hosts.TryGetValue (path, out host); } if (!result) - _logger.Error ( - "A WebSocket service with the specified path not found.\npath: " + servicePath); + _logger.Error ("A WebSocket service with the specified path not found.\npath: " + path); return result; } @@ -364,16 +352,15 @@ namespace WebSocketSharp.Server #region Public Methods /// - /// Broadcasts a binary to all clients - /// of the WebSocket services provided by the server. + /// Broadcasts a binary to every client in the WebSocket services + /// provided by the server. /// /// - /// An array of that represents the binary data - /// to broadcast. + /// An array of that represents the binary data to broadcast. /// public void Broadcast (byte [] data) { - var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); + var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; @@ -386,15 +373,15 @@ namespace WebSocketSharp.Server } /// - /// Broadcasts a text to all clients - /// of the WebSocket services provided by the server. + /// Broadcasts a text to every client in the WebSocket services + /// provided by the server. /// /// /// A that represents the text data to broadcast. /// public void Broadcast (string data) { - var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); + var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; @@ -408,23 +395,22 @@ namespace WebSocketSharp.Server } /// - /// Broadcasts a binary asynchronously to all clients - /// of the WebSocket services provided by the server. + /// Broadcasts a binary asynchronously to every client in the WebSocket + /// services provided by the server. /// /// /// This method doesn't wait for the broadcast to be complete. /// /// - /// An array of that represents the binary data - /// to broadcast. + /// An array of that represents the binary data to broadcast. /// /// - /// A delegate that references the method(s) called when - /// the broadcast is complete. + /// A delegate that references the method(s) called when the broadcast is + /// complete. /// public void BroadcastAsync (byte [] data, Action completed) { - var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); + var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; @@ -437,8 +423,8 @@ namespace WebSocketSharp.Server } /// - /// Broadcasts a text asynchronously to all clients - /// of the WebSocket services provided by the server. + /// Broadcasts a text asynchronously to every client in the WebSocket + /// services provided by the server. /// /// /// This method doesn't wait for the broadcast to be complete. @@ -447,12 +433,12 @@ namespace WebSocketSharp.Server /// A that represents the text data to broadcast. /// /// - /// A delegate that references the method(s) called when - /// the broadcast is complete. + /// A delegate that references the method(s) called when the broadcast is + /// complete. /// public void BroadcastAsync (string data, Action completed) { - var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); + var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; @@ -466,9 +452,8 @@ namespace WebSocketSharp.Server } /// - /// Broadcasts a binary data from the specified - /// asynchronously to all clients of the WebSocket services provided - /// by the server. + /// Broadcasts a binary data from the specified asynchronously to every + /// client in the WebSocket services provided by the server. /// /// /// This method doesn't wait for the broadcast to be complete. @@ -480,14 +465,14 @@ namespace WebSocketSharp.Server /// An that represents the number of bytes to broadcast. /// /// - /// A delegate that references the method(s) called when - /// the broadcast is complete. + /// A delegate that references the method(s) called when the broadcast is + /// complete. /// public void BroadcastAsync (Stream stream, int length, Action completed) { - var msg = checkIfCanSend ( - () => stream.CheckIfCanRead () ?? - (length < 1 ? "'length' must be greater than 0." : null)); + var msg = _state.CheckIfStart () ?? + stream.CheckIfCanRead () ?? + (length < 1 ? "'length' must be greater than 0." : null); if (msg != null) { _logger.Error (msg); @@ -519,134 +504,13 @@ namespace WebSocketSharp.Server } /// - /// Broadcasts a binary to all clients of the - /// WebSocket service with the specified . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// An array of that represents the binary data - /// to broadcast. - /// - public void BroadcastTo (string servicePath, byte [] data) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.Broadcast (data); - } - - /// - /// Broadcasts a text to all clients of the - /// WebSocket service with the specified . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the text data to broadcast. - /// - public void BroadcastTo (string servicePath, string data) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.Broadcast (data); - } - - /// - /// Broadcasts a binary asynchronously to all clients - /// of the WebSocket service with the specified . - /// - /// - /// This method doesn't wait for the broadcast to be complete. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// An array of that represents the binary data - /// to broadcast. - /// - /// - /// A delegate that references the method(s) called when - /// the broadcast is complete. - /// - public void BroadcastToAsync ( - string servicePath, byte [] data, Action completed) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.BroadcastAsync (data, completed); - } - - /// - /// Broadcasts a text asynchronously to all clients - /// of the WebSocket service with the specified . - /// - /// - /// This method doesn't wait for the broadcast to be complete. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the text data to broadcast. - /// - /// - /// A delegate that references the method(s) called when - /// the broadcast is complete. - /// - public void BroadcastToAsync ( - string servicePath, string data, Action completed) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.BroadcastAsync (data, completed); - } - - /// - /// Broadcasts a binary data from the specified - /// asynchronously to all clients of the WebSocket service with the - /// specified . - /// - /// - /// This method doesn't wait for the broadcast to be complete. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A from which contains the binary data to broadcast. - /// - /// - /// An that represents the number of bytes to broadcast. - /// - /// - /// A delegate that references the method(s) called when - /// the broadcast is complete. - /// - public void BroadcastToAsync ( - string servicePath, Stream stream, int length, Action completed) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.BroadcastAsync (stream, length, completed); - } - - /// - /// Sends Pings to all clients of the WebSocket services provided by the - /// server. + /// Sends a Ping to every client in the WebSocket services provided by the 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. + /// A Dictionary<string, Dictionary<string, bool>> that contains the collection of + /// pairs of service path and collection of pairs of session ID and value indicating whether + /// the manager received a Pong from every client in a time. If this method isn't available, + /// returns . /// public Dictionary> Broadping () { @@ -660,17 +524,17 @@ namespace WebSocketSharp.Server } /// - /// Sends Pings with the specified to all clients - /// of the WebSocket services provided by the server. + /// Sends a Ping with the specified to every client in the WebSocket + /// services provided by the 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 Dictionary<string, Dictionary<string, bool>> that contains the collection of + /// pairs of service path and collection of pairs of session ID and value indicating whether + /// the manager received a Pong from every client in a time. If this method isn't available or + /// is invalid, returns . /// /// - /// A that represents the message to broadcast. + /// A that represents the message to send. /// public Dictionary> Broadping (string message) { @@ -678,358 +542,42 @@ namespace WebSocketSharp.Server return Broadping (); byte [] data = null; - var msg = checkIfCanSend ( - () => (data = Encoding.UTF8.GetBytes (message)) - .CheckIfValidControlData ("message")); + var msg = _state.CheckIfStart () ?? + (data = Encoding.UTF8.GetBytes (message)).CheckIfValidControlData ("message"); if (msg != null) { _logger.Error (msg); return null; } - return broadping ( - WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000); - } - - /// - /// Sends Pings to all clients of the WebSocket service with the specified - /// . - /// - /// - /// A Dictionary<string, bool> 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. If the WebSocket service not found, - /// returns . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - public Dictionary BroadpingTo (string servicePath) - { - WebSocketServiceHost host; - return TryGetServiceHost (servicePath, out host) - ? host.Sessions.Broadping () - : null; - } - - /// - /// Sends Pings with the specified to all clients - /// of the WebSocket service with the specified . - /// - /// - /// A Dictionary<string, bool> 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. If the WebSocket service not found, - /// returns . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the message to send. - /// - public Dictionary BroadpingTo ( - string servicePath, string message) - { - WebSocketServiceHost host; - return TryGetServiceHost (servicePath, out host) - ? host.Sessions.Broadping (message) - : null; - } - - /// - /// Closes the session with the specified and - /// . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to close. - /// - public void CloseSession (string servicePath, string id) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.CloseSession (id); - } - - /// - /// Closes the session with the specified , - /// , and . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to close. - /// - /// - /// A that represents the status code for closure. - /// - /// - /// A that represents the reason for closure. - /// - public void CloseSession ( - string servicePath, string id, ushort code, string reason) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.CloseSession (id, code, reason); - } - - /// - /// Closes the session with the specified , - /// , and . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to close. - /// - /// - /// One of the values that indicate the status - /// codes for closure. - /// - /// - /// A that represents the reason for closure. - /// - public void CloseSession ( - string servicePath, string id, CloseStatusCode code, string reason) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.CloseSession (id, code, reason); - } - - /// - /// Sends a Ping to the client associated with the specified - /// and . - /// - /// - /// true if the WebSocket service with - /// receives a Pong from the client in a time; otherwise, false. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to send the - /// Ping to. - /// - public bool PingTo (string servicePath, string id) - { - WebSocketServiceHost host; - return TryGetServiceHost (servicePath, out host) && - host.Sessions.PingTo (id); - } - - /// - /// Sends a Ping with the specified to the client - /// associated with the specified and - /// . - /// - /// - /// true if the WebSocket service with - /// receives a Pong from the client in a time; otherwise, false. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to send the - /// Ping to. - /// - /// - /// A that represents the message to send. - /// - public bool PingTo (string servicePath, string id, string message) - { - WebSocketServiceHost host; - return TryGetServiceHost (servicePath, out host) && - host.Sessions.PingTo (id, message); - } - - /// - /// Sends a binary to the client on the session with - /// the specified , in the WebSocket service with the - /// specified . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to find. - /// - /// - /// An array of that represents the binary data to send. - /// - public void SendTo (string servicePath, string id, byte [] data) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.SendTo (id, data); - } - - /// - /// Sends a text to the client on the session with - /// the specified , in the WebSocket service with the - /// specified . - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to find. - /// - /// - /// A that represents the text data to send. - /// - public void SendTo (string servicePath, string id, string data) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.SendTo (id, data); - } - - /// - /// Sends a binary asynchronously to the client on the - /// session with the specified , in the WebSocket service - /// with the specified . - /// - /// - /// This method doesn't wait for the send to be complete. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to find. - /// - /// - /// An array of that represents the binary data to send. - /// - /// - /// An Action<bool> delegate that references the method(s) called when - /// the send is complete. - /// A passed to this delegate is true if the send is - /// complete successfully; otherwise, false. - /// - public void SendToAsync ( - string servicePath, string id, byte [] data, Action completed) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.SendToAsync (id, data, completed); - } - - /// - /// Sends a text asynchronously to the client on the - /// session with the specified , in the WebSocket service - /// with the specified . - /// - /// - /// This method doesn't wait for the send to be complete. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to find. - /// - /// - /// A that represents the text data to send. - /// - /// - /// An Action<bool> delegate that references the method(s) called when - /// the send is complete. - /// A passed to this delegate is true if the send is - /// complete successfully; otherwise, false. - /// - public void SendToAsync ( - string servicePath, string id, string data, Action completed) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.SendToAsync (id, data, completed); - } - - /// - /// Sends a binary data from the specified asynchronously - /// to the client on the session with the specified , in - /// the WebSocket service with the specified . - /// - /// - /// This method doesn't wait for the send to be complete. - /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. - /// - /// - /// A that represents the ID of the session to find. - /// - /// - /// A from which contains the binary data to send. - /// - /// - /// An that represents the number of bytes to send. - /// - /// - /// An Action<bool> delegate that references the method(s) called when - /// the send is complete. - /// A passed to this delegate is true if the send is - /// complete successfully; otherwise, false. - /// - public void SendToAsync ( - string servicePath, string id, Stream stream, int length, Action completed) - { - WebSocketServiceHost host; - if (TryGetServiceHost (servicePath, out host)) - host.Sessions.SendToAsync (id, stream, length, completed); + return broadping (WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000); } /// - /// Tries to get a WebSocket service host with the specified - /// . + /// Tries to get a WebSocket service host with the specified . /// /// - /// true if the WebSocket service host is successfully found; - /// otherwise, false. + /// true if the WebSocket service is successfully found; otherwise, false. /// - /// - /// A that represents the absolute path to the WebSocket - /// service to find. + /// + /// A that represents the absolute path to the WebSocket service to find. /// - /// - /// When this method returns, a instance - /// that represents the WebSocket service host if it's successfully found; - /// otherwise, . This parameter is passed uninitialized. + /// + /// When this method returns, a instance that represents the + /// WebSocket service host if it's successfully found; otherwise, . This + /// parameter is passed uninitialized. /// - public bool TryGetServiceHost ( - string servicePath, out WebSocketServiceHost serviceHost) + public bool TryGetServiceHost (string path, out WebSocketServiceHost host) { - var msg = _state.CheckIfStart () ?? servicePath.CheckIfValidServicePath (); + var msg = _state.CheckIfStart () ?? path.CheckIfValidServicePath (); if (msg != null) { _logger.Error (msg); - serviceHost = null; + host = null; return false; } - return TryGetServiceHostInternally (servicePath, out serviceHost); + return TryGetServiceHostInternally (path, out host); } #endregion