Пример #1
0
        /// <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);
            }
        }
Пример #2
0
        /// <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="sendIfNotConnected">Sends the message if the underlaying TCP connection is connected to the server and ignores the connection state of the XMPPConnection.</param>
        /// <returns>True if the message got send and didn't got cached.</returns>
        public async Task <bool> SendAsync(AbstractMessage msg, bool sendIfNotConnected)
        {
            if (state == ConnectionState.CONNECTING)
            {
                ResetConnectionTimer();
            }

            if (state != ConnectionState.CONNECTED && !sendIfNotConnected)
            {
                if (Logger.logLevel >= LogLevel.DEBUG)
                {
                    Logger.Warn("[XMPPConnection2]: Did not send message, due to connection state is " + state + "\n" + msg.toXmlString());
                }
                else
                {
                    Logger.Warn("[XMPPConnection2]: Did not send message, due to connection state is " + state);
                }

                if (!sendIfNotConnected)
                {
                    return(false);
                }
            }

            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(true);
                }
                else
                {
                    Logger.Error("[XMPPConnection2]: Error during sending message for account: " + account.getBareJid());
                }
            }
            catch (Exception e)
            {
                Logger.Error("[XMPPConnection2]: Error during sending message for account: " + account.getBareJid(), e);
            }
            return(false);
        }
Пример #3
0
        //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
        #region --Set-, Get- Methods--


        #endregion
        //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
        #region --Misc Methods (Public)--
        public void addMessage(string accountId, AbstractMessage msg)
        {
            MessageCacheTable mT = new MessageCacheTable()
            {
                accountId = accountId,
                messageId = msg.ID,
            };

            if (msg is MessageMessage)
            {
                MessageMessage message = msg as MessageMessage;
                message.addDelay();
                mT.message       = message.toXmlString();
                mT.isChatMessage = true;
                mT.chatMessageId = message.chatMessageId;
            }
            else
            {
                mT.message       = msg.toXmlString();
                mT.isChatMessage = false;
            }
            dB.InsertOrReplace(mT);
        }