示例#1
0
        /// <summary>
        /// remove server node
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">name is null or empty</exception>
        public bool UnRegisterNode(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            SocketConnector node = null;

            SocketBase.IConnection connection = null;
            lock (this)
            {
                //remove node by name,
                if (this._dicNodes.TryGetValue(name, out node))
                {
                    this._dicNodes.Remove(name);
                }
                //get connection by name.
                this._dicConnections.TryGetValue(name, out connection);
            }

            if (node != null)
            {
                node.Stop();
            }
            if (connection != null)
            {
                connection.BeginDisconnect();
            }
            return(node != null);
        }
示例#2
0
        /// <summary>
        /// on disconnected
        /// </summary>
        /// <param name="node"></param>
        /// <param name="connection"></param>
        private void OnDisconnected(SocketConnector node, SocketBase.IConnection connection)
        {
            lock (this)
            {
                if (!this._dicConnections.Remove(node.Name))
                {
                    return;
                }

                this._arrConnections  = this._dicConnections.Values.ToArray();
                this._hashConnections = new ConsistentHashContainer <SocketBase.IConnection>(this._dicConnections);
            }
        }
示例#3
0
        /// <summary>
        /// try register server node.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        public bool TryRegisterNode(string name, EndPoint endPoint)
        {
            SocketConnector node = null;

            lock (this)
            {
                if (this._dicNodes.ContainsKey(name))
                {
                    return(false);
                }
                this._dicNodes[name] = node = new SocketConnector(name, endPoint, this._host,
                                                                  this.OnConnected, this.OnDisconnected);
            }
            node.Start();
            return(true);
        }
示例#4
0
        /// <summary>
        /// on connected
        /// </summary>
        /// <param name="node"></param>
        /// <param name="connection"></param>
        private void OnConnected(SocketConnector node, SocketBase.IConnection connection)
        {
            //fire connected event.
            this.Connected(node.Name, connection);

            bool isActive = false;

            SocketBase.IConnection oldConnection = null;
            lock (this)
            {
                //remove exists connection by name.
                if (this._dicConnections.TryGetValue(node.Name, out oldConnection))
                {
                    this._dicConnections.Remove(node.Name);
                }
                //add curr connection to list if node is active
                if (isActive = this._dicNodes.ContainsKey(node.Name))
                {
                    this._dicConnections[node.Name] = connection;
                }

                this._arrConnections  = this._dicConnections.Values.ToArray();
                this._hashConnections = new ConsistentHashContainer <SocketBase.IConnection>(this._dicConnections);
            }
            //disconect old connection.
            if (oldConnection != null)
            {
                oldConnection.BeginDisconnect();
            }
            //disconnect not active node connection.
            if (!isActive)
            {
                connection.BeginDisconnect();
            }
            //fire server available event.
            if (isActive && this.ServerAvailable != null)
            {
                this.ServerAvailable(node.Name, connection);
            }
        }