Пример #1
0
        /// <summary>
        /// 移除服务器节点
        /// </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>
        /// 当与服务器端断开连接时发生的事件处理
        /// </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>
        /// 注册服务器节点
        /// </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>
        /// 当客户端与服务器建立连接成功时发生的事件处理
        /// </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);
            }
        }