Ejemplo n.º 1
0
        /// <summary>
        /// Tries to get the host instance for a WebSocket service with
        /// the specified path.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the service is successfully found; otherwise,
        /// <c>false</c>.
        /// </returns>
        /// <param name="path">
        ///   <para>
        ///   A <see cref="string"/> that represents an absolute path to
        ///   the service to find.
        ///   </para>
        ///   <para>
        ///   / is trimmed from the end of the string if present.
        ///   </para>
        /// </param>
        /// <param name="host">
        ///   <para>
        ///   When this method returns, a <see cref="WebSocketServiceHost"/>
        ///   instance or <see langword="null"/> if not found.
        ///   </para>
        ///   <para>
        ///   The host instance provides the function to access
        ///   the information in the service.
        ///   </para>
        /// </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 TryGetServiceHost(string path, out WebSocketServiceHost host)
        {
            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");
            }

            return(InternalTryGetServiceHost(path, out host));
        }
Ejemplo n.º 2
0
        internal void Add <TBehavior> (string path, Func <TBehavior> creator)
            where TBehavior : WebSocketBehavior
        {
            path = 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, _log
                    );

                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
        {
            lock (_sync)
            {
                path = HttpUtility.UrlDecode(path).TrimEndSlash();

                WebSocketServiceHost host;
                if (_hosts.TryGetValue(path, out host))
                {
                    _logger.Error(
                        "A WebSocket service with the specified path already exists:\n  path: " + path);

                    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);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a WebSocket service with the specified behavior, path,
        /// and delegate.
        /// </summary>
        /// <param name="path">
        ///   <para>
        ///   A <see cref="string"/> that represents an absolute path to
        ///   the service to add.
        ///   </para>
        ///   <para>
        ///   / is trimmed from the end of the string if present.
        ///   </para>
        /// </param>
        /// <param name="initializer">
        ///   <para>
        ///   An <c>Action&lt;TBehavior&gt;</c> delegate or
        ///   <see langword="null"/> if not needed.
        ///   </para>
        ///   <para>
        ///   The delegate invokes the method called when initializing
        ///   a new session instance for the service.
        ///   </para>
        /// </param>
        /// <typeparam name="TBehavior">
        ///   <para>
        ///   The type of the behavior for the service.
        ///   </para>
        ///   <para>
        ///   It must inherit the <see cref="WebSocketBehavior"/> class.
        ///   </para>
        ///   <para>
        ///   And also, it must have a public parameterless constructor.
        ///   </para>
        /// </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 = path.TrimSlashFromEnd();

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

                host = new WebSocketServiceHost <TBehavior> (
                    path, () => new TBehavior(), initializer, _log
                    );

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

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

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

                _hosts.Add(path, host);
            }
        }
Ejemplo n.º 5
0
        internal bool InternalTryGetServiceHost(
            string path, out WebSocketServiceHost host
            )
        {
            path = path.TrimSlashFromEnd();

            lock (_sync)
                return(_hosts.TryGetValue(path, out host));
        }
Ejemplo n.º 6
0
        internal bool InternalTryGetServiceHost(
            string path, out WebSocketServiceHost host
            )
        {
            path = HttpUtility.UrlDecode(path).TrimSlashFromEnd();

            lock (_sync)
                return(_hosts.TryGetValue(path, out host));
        }
        public void AddService(
            string path, WebSocketBehavior webSocketBehavior)
        {
            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)
            {
                WebSocketServiceHostBase host;
                if (_hosts.TryGetValue(path, out host))
                {
                    throw new ArgumentException("Already in use.", "path");
                }

                host = new WebSocketServiceHost(
                    path, webSocketBehavior, _log
                    );

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

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

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

                _hosts.Add(path, host);
            }
        }
        /// <summary>
        /// Tries to get the WebSocket service host with the specified <paramref name="path"/>.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the service is successfully found; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="path">
        /// A <see cref="string"/> that represents the absolute path to the service to find.
        /// </param>
        /// <param name="host">
        /// When this method returns, a <see cref="WebSocketServiceHost"/> instance that
        /// provides the access to the information in the service, or <see langword="null"/>
        /// if it's not found. This parameter is passed uninitialized.
        /// </param>
        public bool TryGetServiceHost(string path, out WebSocketServiceHost host)
        {
            var msg = _state.CheckIfAvailable(false, true, false) ?? path.CheckIfValidServicePath();

            if (msg != null)
            {
                _logger.Error(msg);
                host = null;

                return(false);
            }

            return(InternalTryGetServiceHost(path, out host));
        }
        internal bool InternalTryGetServiceHost(string path, out WebSocketServiceHost host)
        {
            bool ret;

            lock (_sync)
            {
                path = HttpUtility.UrlDecode(path).TrimEndSlash();
                ret  = _hosts.TryGetValue(path, out host);
            }

            if (!ret)
            {
                _logger.Error(
                    "A WebSocket service with the specified path isn't found:\n  path: " + path);
            }

            return(ret);
        }