Exemplo n.º 1
0
 private static void IsErrorToExitProgram(bool isNotError = true)
 {
     if (!isNotError)
     {
         RunType = ServerStateType.Closing;
         Process.GetCurrentProcess().Close();
     }
 }
Exemplo n.º 2
0
        public void SendHartbeat(ServerStateType type)
        {
            if (_isDispose) return;

            var term = _election.CurrentTerm;
            var votedFor = _election.VotedFor;
            
            foreach (var node in _nodes)
            {
                if (_isDispose) return;

                if (!NextIndex.ContainsKey(node))
                {
                    // Adds a node to our record and assumes it's log state is the same as our's
                    // If it's not the same the follower will reject the message and send 
                    // back it's current state for the next heartbeat
                    NextIndex.TryAdd(node, _logReplication.LastApplied);
                    MatchIndex.TryAdd(node, 0);
                }

                var next = NextIndex[node];
                var previousLogIndex = next - 1;
                var entries = _logReplication.Replicate(next);

                var logEntries = entries as IList<LogEntry> ?? entries.ToList();
                var message = new AppendEntryMessage()
                {
                    To = node,
                    Term = term,
                    Leader = votedFor,
                    Entries = logEntries,
                    LeaderCommit = _logReplication.CommitIndex,
                    PreviousLogIndex = previousLogIndex,
                    PreviousLogTerm = _logReplication.Term(previousLogIndex)
                };

                if (_options.UseLogging)
                    _logger.LogInformation($"Sending {nameof(AppendEntryMessage)} \n\t\t Term {message.Term} \n\t\t To {message.To} \n\t\t Leader {message.Leader} \n\t\t Entries {logEntries.Count()}");
                
                _append.OnNext(message);
            }

            _hartbeat.Subscribe(SendHartbeat);
        }
Exemplo n.º 3
0
        private static bool cmd_exit(string cmdStr)
        {
            RunType = ServerStateType.Closing;

            return true;
        }
Exemplo n.º 4
0
        /// <summary>
        ///     停止游戏
        /// </summary>
        public static void StopGame()
        {
            RunType = ServerStateType.Closing;

            if (BeforeStopListen != null)
                BeforeStopListen();

            if (s_world != null)
                s_world.StopWorld();

            if (AfterStopListen != null)
                AfterStopListen();

            Logs.Info("wait thread queue exit.");
            //  等待所有的线程队列退出
            while (ThreadQueueEntity.HasActionInAllQueue)
            {
                Thread.Sleep(100);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     开启游戏
        /// </summary>
        /// <remarks>
        ///     游戏启动执行顺序
        ///     1.FirstInit
        ///     2.InitTemplate
        ///     3.LoadDefaultGameData
        ///     4.BeforeModuleInit
        ///     5.world.Start()
        ///     6.AfterModuleInit
        /// </remarks>
        /// <param name="world"></param>
        public static void StartGame(WorldBase world)
        {
            if (world == null)
                throw new NullReferenceException("world");

            s_world = world;

            RunType = ServerStateType.Starting;

            //  先加载最基础的配置文件
            StaticConfigFileManager.LoadData();

            if (FirstInit != null)
                IsErrorToExitProgram(FirstInit());

            if (InitTemplate != null)
                IsErrorToExitProgram(InitTemplate());

            if (LoadDefaultGameData != null)
                IsErrorToExitProgram(LoadDefaultGameData());

            if (BeforeModuleInit != null)
                IsErrorToExitProgram(BeforeModuleInit());

            world.StartWorld();

            if (AfterModuleInit != null)
                IsErrorToExitProgram(AfterModuleInit());

            RunType = ServerStateType.Runing;

            if (IsConsoleRun)
            {
                StartCommandlinesDisposal();
            }
        }
Exemplo n.º 6
0
        public void ElectionResults(RequestVoteResultMessage result)
        {
            if (_serverIdentifier.Equals(result.From))
                return;

            if (isDispose) return;

            if (_options.UseLogging)            
                _logger.LogInformation($"Processing {nameof(ElectionResults)} \n\t\t Candidate {result.Candidate} \n\t\t Term {result.Term} \n\t\t VoteGranted {result.VoteGranted}");
            
            // We are not intrested in old terms
            if (result.Term < _election.CurrentTerm)
                return;

            // A candidate is already a term ahead so we follow that _election 
            if (result.Term > _election.CurrentTerm)
            {
                _election.CurrentTerm = result.Term;
                _election.VotedFor = result.Candidate;
                ChangeState = ServerStateType.Follower;
                return;
            }

            if (!_nodes.Contains(result.From))
            {
                _nodes.Add(result.From);
            }
            
            
            // We collect our votes for the _election to determining if we are the leader
            _votingSystem.AddVote(result.From, result.Candidate);
        }
Exemplo n.º 7
0
        public void SetElectionTimeout()
        {
            if (isDispose) return;

            _electionTimer.ResetElectionTimeout(() =>
            {
                _voteReplyDispose?.Dispose();
                ChangeState = _votingSystem.Result();
            });
        }