/// <summary> /// Sends the given message to the server or stores it until there is a connection to the server. /// </summary> /// <param name="msg">The message to send.</param> /// <param name="cacheIfNotConnected">Cache the message if the connection state does not equals 'CONNECTED', to ensure the message doesn't get lost.</param> /// <param name="sendIfNotConnected">Sends the message if the underlaying TCP connection is connected to the server and ignores the connection state of the XMPPConnection.</param> public async Task sendAsync(AbstractMessage msg, bool cacheIfNotConnected, bool sendIfNotConnected) { if (state == ConnectionState.CONNECTING) { resetConnectionTimer(); } if (state != ConnectionState.CONNECTED && !sendIfNotConnected) { if (Logger.logLevel >= LogLevel.DEBUG) { Logger.Warn("Did not send message, due to connection state is " + state + "\n" + msg.toXmlString()); } else { Logger.Warn("Did not send message, due to connection state is " + state); } if ((cacheIfNotConnected || msg.shouldSaveUntilSend())) { MessageCacheDBManager.INSTANCE.addMessage(account.getIdAndDomain(), msg); } if (!sendIfNotConnected) { return; } } if (msg is IQMessage && msg.ID != null) { messageIdCache.addTimed(msg.ID); } try { if (await TCP_CONNECTION.sendAsync(msg.toXmlString())) { // Only trigger onMessageSend(...) for chat messages: if (msg is MessageMessage m) { onMessageSend(msg.ID, m.chatMessageId, false); return; } } else { Logger.Error("Error during sending message for account: " + account.getIdAndDomain()); } } catch (Exception e) { Logger.Error("Error during sending message for account: " + account.getIdAndDomain(), e); } if ((cacheIfNotConnected || msg.shouldSaveUntilSend())) { MessageCacheDBManager.INSTANCE.addMessage(account.getIdAndDomain(), msg); } }