コード例 #1
0
        /// <summary>
        /// connect to zookeeper
        /// </summary>
        /// <param name="name"></param>
        /// <param name="connection"></param>
        private void ConnectToZookeeper(string name, IConnection connection)
        {
            var connectRequest = new Data.ConnectRequest(this._protocolVersion,
                                                         this._zkClient._lastZxid,
                                                         (int)this._zkClient._sessionTimeout.TotalMilliseconds,
                                                         this._sessionID,
                                                         this._sessionPassword);

            var request = new Request <ZookResponse>(this._zkClient.NextRequestSeqID(), "connect",
                                                     Utils.Marshaller.Serialize(connectRequest, true),
                                                     ex => connection.BeginDisconnect(ex),
                                                     response =>
            {
                Data.ConnectResponse result = null;
                try { result = Utils.Marshaller.Deserialize <Data.ConnectResponse>(response.Payload); }
                catch (Exception ex) { connection.BeginDisconnect(ex); return; }

                if (result.SessionTimeOut <= 0)    //session expired
                {
                    this._protocolVersion          = 0;
                    this._sessionID                = 0;
                    this._negotiatedSessionTimeout = 0;
                    this._sessionPassword          = new byte[16];

                    connection.BeginDisconnect(new ApplicationException("zookeeper session expired"));
                    this._zkClient.SetKeeperState(Data.KeeperState.Expired); return;
                }

                this._protocolVersion          = result.ProtocolVersion;
                this._negotiatedSessionTimeout = result.SessionTimeOut;
                this._sessionID       = result.SessionID;
                this._sessionPassword = result.SessionPassword;

                this._currConnection = connection;
                this._zkClient.SetKeeperState(Data.KeeperState.SyncConnected);
                this.FireServerAvailable(name, connection);
            })
            {
                Tag = "connect"
            };

            connection.UserData = request;
            connection.BeginSend(request);
        }
コード例 #2
0
        /// <summary>
        /// connecto to zookeeper
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        private Task ConnectToZookeeper(IConnection connection)
        {
            var taskSource = new TaskCompletionSource <bool>();

            var request = base.NewRequest("zk.connect", Utils.Marshaller.Serialize(new Data.ConnectRequest(
                                                                                       this._protocolVersion, this._lastZxid, (int)this._sessionTimeout.TotalMilliseconds, this._sessionID, this._sessionPassword)),
                                          base.MillisecondsReceiveTimeout,
                                          ex => taskSource.TrySetException(ex),
                                          message =>
            {
                Data.ConnectResponse result = null;
                try { result = Utils.Marshaller.Deserialize <Data.ConnectResponse>(message.Payload); }
                catch (Exception ex) { taskSource.TrySetException(ex); return; }

                if (result.SessionTimeOut <= 0)    //session expired
                {
                    this._protocolVersion          = 0;
                    this._sessionID                = 0;
                    this._negotiatedSessionTimeout = 0;
                    this._sessionPassword          = new byte[16];

                    this.SetKeeperState(Data.KeeperState.Expired);
                    taskSource.TrySetException(new ApplicationException("zookeeper session expired"));
                    return;
                }

                this._protocolVersion          = result.ProtocolVersion;
                this._negotiatedSessionTimeout = result.SessionTimeOut;
                this._sessionID       = result.SessionID;
                this._sessionPassword = result.SessionPassword;
                this.SetKeeperState(Data.KeeperState.SyncConnected);

                taskSource.TrySetResult(true);
            });

            connection.UserData = request;
            connection.BeginSend(request);

            return(taskSource.Task);
        }