示例#1
0
        /// <summary>
        /// Initializes the Server to receive follower connections
        /// </summary>
        internal static void ServerInitialize()
        {
            if (!AutoFollow.Enabled)
            {
                return;
            }

            if (Service.ConnectionMode != ConnectionMode.Server)
            {
                return;
            }

            if (DateTime.UtcNow < DontAttemptServerModeUntil)
            {
                return;
            }

            if (DateTime.UtcNow.Subtract(LastFailTime).TotalSeconds < 5)
            {
                return;
            }

            ServerStartAttempts++;

            try
            {
                var portIsTaken = true;
                ServerUri = new Uri(ServerUri.AbsoluteUri.Replace(_basePort.ToString(), Settings.Network.ServerPort.ToString()));
                _basePort = Settings.Network.ServerPort;
                var serverPort = _basePort;

                while (portIsTaken && AutoFollow.Enabled)
                {
                    var ipgp         = IPGlobalProperties.GetIPGlobalProperties();
                    var tcpListeners = ipgp.GetActiveTcpListeners();

                    if (tcpListeners.Any(listner => listner.Port == serverPort))
                    {
                        portIsTaken = false;
                        serverPort += 1;
                    }
                    else
                    {
                        portIsTaken = false;
                    }
                }

                Log.Info("Initializing Server Service @ {0} Attempt={1}", ServerUri.AbsoluteUri, ServerStartAttempts);
                ServiceHost = new ServiceHost(typeof(Service), ServerUri);
                ServiceHost.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Follow");
                ServiceHost.Open();

                ServerInitialized = true;

                if (OnServerInitialized != null)
                {
                    OnServerInitialized.Invoke();
                }
            }
            catch (AddressAlreadyInUseException ex)
            {
                Log.Verbose("Address already in use. Attempt={0}", ServerStartAttempts);
                Log.Debug(ex.ToString());
                DontAttemptServerModeUntil = DateTime.UtcNow.Add(TimeSpan.FromMinutes(1));
                LastFailTime = DateTime.UtcNow;
                Server.ShutdownServer();
                Client.ShutdownClient();
                Service.ConnectionMode = ConnectionMode.Client;
            }
            catch (Exception ex)
            {
                Log.Verbose("Could not initialize service. Do you already have a leader started? Attempt={0}", ServerStartAttempts);
                Log.Debug(ex.ToString());
                LastFailTime = DateTime.UtcNow;
            }

            if (ServerStartAttempts > 5 && !IsValid)
            {
                Service.ConnectionMode = ConnectionMode.Client;
            }
        }
示例#2
0
 internal static void RunServerInitialized() => OnServerInitialized?.Invoke();