Exemple #1
0
        internal void Add <TBehavior> (string path, Func <TBehavior> creator)
            where TBehavior : WebSocketBehavior
        {
            path = HttpUtility.UrlDecode(path).TrimSlashFromEnd();

            lock (_sync)
            {
                WebSocketServiceHost host;
                if (_hosts.TryGetValue(path, out host))
                {
                    throw new ArgumentException("Already in use.", "path");
                }

                host = new WebSocketServiceHost <TBehavior> (path, creator, null, _logger);

                if (!_clean)
                {
                    host.KeepClean = false;
                }

                if (_waitTime != host.WaitTime)
                {
                    host.WaitTime = _waitTime;
                }

                if (_state == ServerState.Start)
                {
                    host.Start();
                }

                _hosts.Add(path, host);
            }
        }
        internal void Add <TBehavior> (string path, Func <TBehavior> initializer)
            where TBehavior : WebSocketBehavior
        {
            path = HttpUtility.UrlDecode(path).TrimEndSlash();

            lock (_sync)
            {
                WebSocketServiceHost host;
                if (_hosts.TryGetValue(path, out host))
                {
                    _logger.Error("A WebSocket service with the specified path has already existed.");

                    return;
                }

                host = new WebSocketServiceHost <TBehavior> (path, initializer, _logger);
                if (!_clean)
                {
                    host.KeepClean = false;
                }

                if (_waitTime != host.WaitTime)
                {
                    host.WaitTime = _waitTime;
                }

                if (_state == ServerState.Start)
                {
                    host.Start();
                }

                _hosts.Add(path, host);
            }
        }
Exemple #3
0
        internal bool InternalTryGetServiceHost(string path, out WebSocketServiceHost host)
        {
            path = HttpUtility.UrlDecode(path).TrimSlashFromEnd();

            lock (_sync)
            {
                return(_hosts.TryGetValue(path, out host));
            }
        }
Exemple #4
0
        /// <summary>
        /// Removes a WebSocket service with the specified <paramref name="path"/>.
        /// </summary>
        /// <remarks>
        ///   <para>
        ///   <paramref name="path"/> is converted to a URL-decoded string and
        ///   / is trimmed from the end of the converted string if any.
        ///   </para>
        ///   <para>
        ///   The service is stopped with close status 1001 (going away)
        ///   if it has already started.
        ///   </para>
        /// </remarks>
        /// <returns>
        /// <c>true</c> if the service is successfully found and removed;
        /// otherwise, <c>false</c>.
        /// </returns>
        /// <param name="path">
        /// A <see cref="string"/> that represents an absolute path to
        /// the service to remove.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="path"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <para>
        ///   <paramref name="path"/> is empty.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   <paramref name="path"/> is not an absolute path.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   <paramref name="path"/> includes either or both
        ///   query and fragment components.
        ///   </para>
        /// </exception>
        public bool RemoveService(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException("An empty string.", "path");
            }

            if (path[0] != '/')
            {
                throw new ArgumentException("Not an absolute path.", "path");
            }

            if (path.IndexOfAny(new[] { '?', '#' }) > -1)
            {
                var msg = "It includes either or both query and fragment components.";
                throw new ArgumentException(msg, "path");
            }

            path = HttpUtility.UrlDecode(path).TrimSlashFromEnd();

            WebSocketServiceHost host;

            lock (_sync)
            {
                if (!_hosts.TryGetValue(path, out host))
                {
                    return(false);
                }

                _hosts.Remove(path);
            }

            if (host.State == ServerState.Start)
            {
                host.Stop(1001, String.Empty);
            }

            return(true);
        }
Exemple #5
0
        internal bool Remove(string path)
        {
            path = HttpUtility.UrlDecode(path).TrimSlashFromEnd();

            WebSocketServiceHost host;

            lock (_sync)
            {
                if (!_hosts.TryGetValue(path, out host))
                {
                    return(false);
                }

                _hosts.Remove(path);
            }

            if (host.State == ServerState.Start)
            {
                host.Stop(1001, String.Empty);
            }

            return(true);
        }
        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 could not be found.");
                    return(false);
                }

                _hosts.Remove(path);
            }

            if (host.State == ServerState.Start)
            {
                host.Stop(1001, String.Empty);
            }

            return(true);
        }
Exemple #7
0
        /// <summary>
        /// Adds a WebSocket service with the specified behavior,
        /// <paramref name="path"/>, and <paramref name="initializer"/>.
        /// </summary>
        /// <remarks>
        /// <paramref name="path"/> is converted to a URL-decoded string and
        /// / is trimmed from the end of the converted string if any.
        /// </remarks>
        /// <param name="path">
        /// A <see cref="string"/> that represents an absolute path to
        /// the service to add.
        /// </param>
        /// <param name="initializer">
        /// An <c>Action&lt;TBehavior&gt;</c> delegate that invokes
        /// the method used to initialize a new session instance for
        /// the service or <see langword="null"/> if not needed.
        /// </param>
        /// <typeparam name="TBehavior">
        /// The type of the behavior for the service. It must inherit
        /// the <see cref="WebSocketBehavior"/> class and it must have
        /// a public parameterless constructor.
        /// </typeparam>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="path"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <para>
        ///   <paramref name="path"/> is empty.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   <paramref name="path"/> is not an absolute path.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   <paramref name="path"/> includes either or both
        ///   query and fragment components.
        ///   </para>
        ///   <para>
        ///   -or-
        ///   </para>
        ///   <para>
        ///   <paramref name="path"/> is already in use.
        ///   </para>
        /// </exception>
        public void AddService <TBehavior> (
            string path, Action <TBehavior> initializer
            )
            where TBehavior : WebSocketBehavior, new ()
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException("An empty string.", "path");
            }

            if (path[0] != '/')
            {
                throw new ArgumentException("Not an absolute path.", "path");
            }

            if (path.IndexOfAny(new[] { '?', '#' }) > -1)
            {
                var msg = "It includes either or both query and fragment components.";
                throw new ArgumentException(msg, "path");
            }

            path = HttpUtility.UrlDecode(path).TrimSlashFromEnd();

            lock (_sync)
            {
                WebSocketServiceHost host;
                if (_hosts.TryGetValue(path, out host))
                {
                    throw new ArgumentException("Already in use.", "path");
                }

                host = new WebSocketServiceHost <TBehavior> (
#if SSHARP
                    path, Activator.CreateInstance <TBehavior>, initializer, _logger
#else
                    path, () => new TBehavior(), initializer, _logger
#endif
                    );

                if (!_clean)
                {
                    host.KeepClean = false;
                }

                if (_waitTime != host.WaitTime)
                {
                    host.WaitTime = _waitTime;
                }

                if (_state == ServerState.Start)
                {
                    host.Start();
                }

                _hosts.Add(path, host);
            }
        }