/// <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;

            lock (this)
                if (this._dicNodes.ContainsKey(node.Name))
                {
                    isActive = true;
                    this._dicConnections[node.Name] = connection;
                    this._arrConnections            = this._dicConnections.Values.ToArray();
                    this._hashConnections           = new ConsistentHashContainer <SocketBase.IConnection>(this._dicConnections);
                }

            if (isActive)
            {
                this.ServerAvailable(node.Name, connection);
            }
            else
            {
                connection.BeginDisconnect();
            }
        }
Пример #2
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);
        }
Пример #3
0
 /// <summary>
 /// begin connect
 /// </summary>
 private void Connect()
 {
     SocketConnector.Connect(this.Info.ArrRemoteEP.Length == 1 ?
                             this.Info.ArrRemoteEP[0] :
                             this.Info.ArrRemoteEP[(Guid.NewGuid().GetHashCode() & int.MaxValue) % this.Info.ArrRemoteEP.Length])
     .ContinueWith(t => this.ConnectCallback(t));
 }
Пример #4
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;
 }
 /// <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);
     }
 }
Пример #6
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);
        }
        /// <summary>
        /// try register server node.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        public bool TryRegisterNode(string name, EndPoint endPoint)
        {
            SocketConnector connector = null;

            lock (this)
            {
                if (this._dicNodes.ContainsKey(name))
                {
                    return(false);
                }

                connector               = new SocketConnector(name, endPoint, this._host);
                connector.Connected    += this.OnConnected;
                connector.Disconnected += this.OnDisconnected;
                this._dicNodes[name]    = connector;
            }
            connector.Start();
            return(true);
        }
Пример #8
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);
            }
        }
Пример #9
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);
            }
        }
Пример #10
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);
        }
Пример #11
0
        /// <summary>
        /// on connected
        /// </summary>
        /// <param name="node"></param>
        /// <param name="connection"></param>
        private void OnConnected(SocketConnector node, SocketBase.IConnection connection)
        {
            SocketBase.IConnection oldConnection = null;
            bool isActive = false; //node is active

            lock (this)
            {
                //remove exists connection by name.
                if (this._dicConnections.TryGetValue(node.Name, out oldConnection)) this._dicConnections.Remove(node.Name);

                isActive = this._dicNodes.ContainsKey(node.Name);
                if (isActive) 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(); return; }
            //fire connected event.
            this.Connected(node.Name, connection);
        }