public async void Update(ConnectionParameters parameters) { bool disconnected = false; bool errorState = false; _updateMutex.WaitOne(4000); _updateMutex.Reset(); _updating = true; // Copy new parameters _currentParameters = parameters; // Connection break up if (_XMPP.Connected && !IsInternetAvailable) { PushEvent(ErrorType.NoInternet, ErrorPolicyType.Informative); Disconnect(); disconnected = true; } // Settings changed if (_currentParameters.UpdatedSettings) { if (!disconnected) { #if DEBUG PushEvent(LogType.Info, "Settings changed, disconnecting"); #endif Disconnect(); disconnected = true; } if (string.IsNullOrEmpty(_currentParameters.Hostname)) { PushEvent(ErrorType.InvalidHostname, ErrorPolicyType.Deactivate); errorState = true; } if (string.IsNullOrEmpty(_currentParameters.JID)) { PushEvent(ErrorType.InvalidJID, ErrorPolicyType.Deactivate); errorState = true; } if (string.IsNullOrEmpty(_currentParameters.Password)) { PushEvent(ErrorType.MissingPassword, ErrorPolicyType.Deactivate); errorState = true; } _XMPP.Settings.Account = this.Id; _XMPP.Settings.Id = _currentParameters.JID; _XMPP.Settings.Password = _currentParameters.Password; _XMPP.Settings.Hostname = _currentParameters.Hostname; _XMPP.Settings.Port = _currentParameters.Port; _XMPP.Settings.SSL = _currentParameters.UseSSL; _XMPP.Settings.OldSSL = _currentParameters.OldStyleSSL; _XMPP.Settings.AuthenticationTypes = MechanismType.None; if (_currentParameters.AuthPlain) _XMPP.Settings.AuthenticationTypes |= MechanismType.Plain; if (_currentParameters.AuthMD5) _XMPP.Settings.AuthenticationTypes |= MechanismType.DigestMD5; if (_currentParameters.AuthSCRAM) _XMPP.Settings.AuthenticationTypes |= MechanismType.SCRAM; if (_currentParameters.AuthOAUTH2) _XMPP.Settings.AuthenticationTypes |= MechanismType.XOAUTH2; } if (!errorState) { // Set offline if (_currentParameters.State == AccountState.Disabled) { _lastError = ErrorType.None; if (!disconnected) { #if DEBUG PushEvent(LogType.Info, "Disconnecting"); #endif Disconnect(); disconnected = true; } _lastError = ErrorType.None; } // Set online else if (_currentParameters.State == AccountState.Enabled) { if (_XMPP.Connected) { #if DEBUG PushEvent(LogType.Info, "Already connected"); #endif } else if (!IsInternetAvailable) { PushEvent(ErrorType.NoInternet, ErrorPolicyType.Informative); } else { #if DEBUG PushEvent(LogType.Info, "Connecting"); #endif Connect(); } } } // Unset changed status _currentParameters.UpdatedSettings = false; _updateMutex.Set(); if (_tryReconnect) { await Task.Delay(Runtime._eventDelayMS); _tryReconnect = false; _currentParameters.UpdatedSettings = true; Update(_currentParameters); } _updating = false; }
public void Clean() { Disconnect(); _currentParameters = null; _lastError = ErrorType.None; }
public void Update() { _settingMutex.WaitOne(4000); _settingMutex.Reset(); Accounts accounts = new Accounts(); Status status = new Status(); // get obsolete List<string> removeList = new List<string>(); foreach( var connection in this._connectionList ) { if( accounts[connection.Key] == null ) removeList.Add(connection.Key); } // Remove obsolete foreach (var item in removeList) { _connectionList[item].Clean(); _connectionList.Remove(item); } // Add new and update existing foreach (Account server in accounts) { if (server.IsValid()) { // Create new parameters var parameters = new ConnectionParameters(); parameters.Hostname = server.host; parameters.JID = server.jid; parameters.Password = server.password; parameters.UseSSL = server.usesssl; parameters.OldStyleSSL = server.oldstylessl; parameters.AuthPlain = server.authplain; parameters.AuthMD5 = server.authmd5; parameters.AuthSCRAM = server.authscram; parameters.AuthOAUTH2= server.authoauth2; parameters.RequestConnectedStandby = server.requestConnectedStandby; parameters.Port = server.port; // Messanger is offline? Do so! if (status.status == StatusType.Offline) parameters.State = AccountState.Disabled; else parameters.State = server.persistantState; parameters.UpdatedSettings = server.ResetChangedState(); if (parameters.RequestConnectedStandby && BackgroundExecutionManager.GetAccessStatus() != BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity) _backend.OnConnectionEvent(this, new ConnectionEvent(new BackendEventError(parameters.JID, ErrorType.NoHardwareSlotsAllowed, ErrorPolicyType.Deactivate))); // Add new server if (!_connectionList.ContainsKey(server.jid)) { try { Connection newConnection = new Connection(server.jid); newConnection.OnEvent += _backend.OnConnectionEvent; _connectionList[server.jid] = newConnection; parameters.UpdatedSettings = true; #if DEBUG _backend.OnConnectionEvent(this, new ConnectionEvent(new BackendEventLog("", LogType.Info, "Server with name " + server.title + " added"))); #endif } catch { #if DEBUG _backend.OnConnectionEvent(this, new ConnectionEvent(new BackendEventLog("", LogType.Error, "Adding server with name " + server.title + " failed"))); #endif _settingMutex.Set(); } } // Get connection Connection currentConnection = _connectionList[server.jid]; // Update currentConnection.Update(parameters); } else if( !server.forceDisabled ) { _backend.OnConnectionEvent(this, new ConnectionEvent(new BackendEventError(server.jid, ErrorType.InvalidSettings, ErrorPolicyType.Deactivate))); #if DEBUG _backend.OnConnectionEvent(this, new ConnectionEvent(new BackendEventLog("", LogType.Error, "Server with name " + server.title + " is invalid"))); #endif } } _settingMutex.Set(); }