Example #1
0
        private void AgentStarterThread()
        {
            // Read configuration from file
            if (!SettingsManager.ReadConfigFile())
            {
                _notifyIconComponent.ShowNotifiction("Error while reading configuration file.");
                return;
            }

            if (_newServerEndPoint != null)
            {
                SettingsManager.ServerEndPoint = _newServerEndPoint;       //override configuration file
            }
            EncryptionAdapter.SetEncryption(SettingsManager.SecretKey);

            Logger.CreateLogFile("logs", SettingsManager.LogFilenamePattern);       // creates "logs" directory in binaries folder and set log filename
            Logger.WriteStr("+++ Starting Agent by user request... +++");

            AgentStarted          = true;
            _retryIntervalCurrent = RetryIntervalInitial; //set to initial value

            while (true)                                  // always reconnect untill canceled
            {
                _client = new GeneralSocketClient();
                _client.Connect(SettingsManager.ServerEndPoint, OnConnectToServerCompleted);

                // pause here untill disconnected from server
                _clientDisconnected.Reset();
                _clientDisconnected.WaitOne();


                if (AgentStarted)
                {
                    Logger.WriteStr("Should be running so will try to reconnect to server in " + _retryIntervalCurrent + "seconds...");
                    _notifyIconComponent.ShowNotifiction("Will reconnect in " + _retryIntervalCurrent + "sec...");
                    Thread.Sleep(_retryIntervalCurrent * 1000);

                    //increase interval between reconnects up to retryIntervalMaximum value.
                    // it needed in case of frequent disconnects / when server is unreacheble
                    if (_retryIntervalCurrent < RetryIntervalMaximum)
                    {
                        _retryIntervalCurrent += 5;
                    }
                }
                else
                {
                    break;  //exit loop
                }
            }

            Logger.WriteStr("Client terminated");
        }
Example #2
0
 public void Connect()
 {
     _messageClient.Connect(Settings.Messaging.ServerAddress);
 }