#region License /* * WebSocketServiceHostManager.cs * * The MIT License * * Copyright (c) 2012-2014 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 System.Collections.Generic; using System.IO; using System.Text; using System.Threading; using WebSocketSharp.Net; namespace WebSocketSharp.Server { /// /// Manages the WebSocket services provided by the or /// . /// public class WebSocketServiceHostManager { #region Private Fields private Dictionary _hosts; private volatile bool _keepClean; private Logger _logger; private volatile ServerState _state; private object _sync; #endregion #region Internal Constructors internal WebSocketServiceHostManager () : this (new Logger ()) { } internal WebSocketServiceHostManager (Logger logger) { _logger = logger; _hosts = new Dictionary (); _keepClean = true; _state = ServerState.READY; _sync = new object (); } #endregion #region Public Properties /// /// Gets 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 _hosts.Count; } } } /// /// Gets the collection of the WebSocket service hosts. /// /// /// An IEnumerable<WebSocketServiceHost> that contains the collection of the WebSocket /// service hosts. /// 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 path] { get { WebSocketServiceHost host; TryGetServiceHost (path, out host); return host; } } /// /// 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 the inactive sessions every 60 seconds; otherwise, /// false. /// public bool KeepClean { get { return _keepClean; } internal set { lock (_sync) { if (!(value ^ _keepClean)) return; _keepClean = value; foreach (var host in _hosts.Values) host.KeepClean = value; } } } /// /// 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. /// public IEnumerable Paths { get { return copyHosts ().Keys; } } /// /// Gets the number of the WebSocket sessions in the server. /// /// /// An that represents the number of the sessions in the server. /// public int SessionCount { get { var count = 0; foreach (var host in Hosts) { if (_state != ServerState.START) break; count += host.SessionCount; } return count; } } #endregion #region Private Methods private void broadcast (Opcode opcode, byte [] data, Action completed) { var cache = new Dictionary (); try { foreach (var host in Hosts) { if (_state != ServerState.START) break; host.Sessions.Broadcast (opcode, data, cache); } if (completed != null) completed (); } catch (Exception ex) { _logger.Fatal (ex.ToString ()); } finally { cache.Clear (); } } private void broadcast (Opcode opcode, Stream stream, Action completed) { var cache = new Dictionary (); try { foreach (var host in Hosts) { if (_state != ServerState.START) break; host.Sessions.Broadcast (opcode, stream, cache); } if (completed != null) completed (); } catch (Exception ex) { _logger.Fatal (ex.ToString ()); } finally { foreach (var cached in cache.Values) cached.Dispose (); cache.Clear (); } } private void broadcastAsync (Opcode opcode, byte [] data, Action completed) { ThreadPool.QueueUserWorkItem ( state => broadcast (opcode, data, completed)); } private void broadcastAsync (Opcode opcode, Stream stream, Action completed) { ThreadPool.QueueUserWorkItem ( state => broadcast (opcode, stream, completed)); } private Dictionary> broadping ( byte [] frame, int millisecondsTimeout) { var result = new Dictionary> (); foreach (var host in Hosts) { if (_state != ServerState.START) break; result.Add (host.ServicePath, host.Sessions.Broadping (frame, millisecondsTimeout)); } return result; } private Dictionary copyHosts () { lock (_sync) { return new Dictionary (_hosts); } } #endregion #region Internal Methods internal void Add (string path, WebSocketServiceHost host) { lock (_sync) { WebSocketServiceHost find; if (_hosts.TryGetValue (path, out find)) { _logger.Error ( "A WebSocket service with the specified path already exists.\npath: " + path); return; } if (_state == ServerState.START) host.Sessions.Start (); _hosts.Add (path, host); } } internal bool Remove (string path) { path = HttpUtility.UrlDecode (path).TrimEndSlash (); WebSocketServiceHost host; lock (_sync) { if (!_hosts.TryGetValue (path, out host)) { _logger.Error ("A WebSocket service with the specified path not found.\npath: " + path); return false; } _hosts.Remove (path); } if (host.Sessions.State == ServerState.START) host.Sessions.Stop ( ((ushort) CloseStatusCode.AWAY).ToByteArrayInternally (ByteOrder.BIG), true); return true; } internal void Start () { lock (_sync) { foreach (var host in _hosts.Values) host.Sessions.Start (); _state = ServerState.START; } } internal void Stop (byte [] data, bool send) { lock (_sync) { _state = ServerState.SHUTDOWN; var payload = new PayloadData (data); var args = new CloseEventArgs (payload); var frameAsBytes = send ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () : null; foreach (var host in _hosts.Values) host.Sessions.Stop (args, frameAsBytes); _hosts.Clear (); _state = ServerState.STOP; } } internal bool TryGetServiceHostInternally (string path, out WebSocketServiceHost host) { path = HttpUtility.UrlDecode (path).TrimEndSlash (); bool result; lock (_sync) { result = _hosts.TryGetValue (path, out host); } if (!result) _logger.Error ("A WebSocket service with the specified path not found.\npath: " + path); return result; } #endregion #region Public Methods /// /// Broadcasts a binary to every client in the WebSocket services /// provided by the server. /// /// /// An array of that represents the binary data to broadcast. /// public void Broadcast (byte [] data) { var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; } if (data.LongLength <= WebSocket.FragmentLength) broadcast (Opcode.BINARY, data, null); else broadcast (Opcode.BINARY, new MemoryStream (data), null); } /// /// 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 = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; } var rawData = Encoding.UTF8.GetBytes (data); if (rawData.LongLength <= WebSocket.FragmentLength) broadcast (Opcode.TEXT, rawData, null); else broadcast (Opcode.TEXT, new MemoryStream (rawData), null); } /// /// 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. /// /// /// A delegate that references the method(s) called when the broadcast is /// complete. /// public void BroadcastAsync (byte [] data, Action completed) { var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; } if (data.LongLength <= WebSocket.FragmentLength) broadcastAsync (Opcode.BINARY, data, completed); else broadcastAsync (Opcode.BINARY, new MemoryStream (data), completed); } /// /// 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. /// /// /// A that represents the text data to broadcast. /// /// /// A delegate that references the method(s) called when the broadcast is /// complete. /// public void BroadcastAsync (string data, Action completed) { var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData (); if (msg != null) { _logger.Error (msg); return; } var rawData = Encoding.UTF8.GetBytes (data); if (rawData.LongLength <= WebSocket.FragmentLength) broadcastAsync (Opcode.TEXT, rawData, completed); else broadcastAsync (Opcode.TEXT, new MemoryStream (rawData), completed); } /// /// 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. /// /// /// 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 BroadcastAsync (Stream stream, int length, Action completed) { var msg = _state.CheckIfStart () ?? stream.CheckIfCanRead () ?? (length < 1 ? "'length' must be greater than 0." : null); if (msg != null) { _logger.Error (msg); return; } stream.ReadBytesAsync ( length, data => { var len = data.Length; if (len == 0) { _logger.Error ("A data cannot be read from 'stream'."); return; } if (len < length) _logger.Warn ( String.Format ( "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}", length, len)); if (len <= WebSocket.FragmentLength) broadcast (Opcode.BINARY, data, completed); else broadcast (Opcode.BINARY, new MemoryStream (data), completed); }, ex => _logger.Fatal (ex.ToString ())); } /// /// 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 /// 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 () { var msg = _state.CheckIfStart (); if (msg != null) { _logger.Error (msg); return null; } return broadping (WsFrame.EmptyUnmaskPingData, 1000); } /// /// 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 /// 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 send. /// public Dictionary> Broadping (string message) { if (message == null || message.Length == 0) return Broadping (); byte [] data = null; 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); } /// /// Tries to get a WebSocket service host with the specified . /// /// /// true if the WebSocket service is successfully found; otherwise, false. /// /// /// 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. /// public bool TryGetServiceHost (string path, out WebSocketServiceHost host) { var msg = _state.CheckIfStart () ?? path.CheckIfValidServicePath (); if (msg != null) { _logger.Error (msg); host = null; return false; } return TryGetServiceHostInternally (path, out host); } #endregion } }