/// <summary> /// Parses the given messages and invokes the ConnectionNewValidMessage event for each message. /// </summary> /// <param name="data">The messages string to parse.</param> private async Task parseMessageAsync(string data) { // Parse message: List <AbstractMessage> messages = null; try { messages = parser.parseMessages(data); } catch (Exception e) { Logger.Error("Error during message parsing." + e); return; } // Process messages: foreach (AbstractMessage msg in messages) { // Filter IQ messages which ids are not valid: if (msg is IQMessage) { IQMessage iq = msg as IQMessage; if (iq.GetType().Equals(IQMessage.RESULT) && messageIdCache.getTimed(iq.ID) != null) { Logger.Warn("Invalid message id received!"); return; } } // Invoke message processors: NewValidMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); // Should restart connection? if (msg.getRestartConnection() != AbstractMessage.NO_RESTART) { if (msg.getRestartConnection() == AbstractMessage.SOFT_RESTART) { await softRestartAsync(); } else if (msg.getRestartConnection() == AbstractMessage.HARD_RESTART) { await hardRestartAsync(); } else { throw new ArgumentException("Invalid restart type: " + msg.getRestartConnection()); } } // Filter already processed messages: if (msg.isProcessed()) { return; } // -------------------------------------------------------------------- // Open stream: if (msg is OpenStreamAnswerMessage) { OpenStreamAnswerMessage oA = msg as OpenStreamAnswerMessage; if (oA.ID == null) { // TODO Handle OpenStreamAnswerMessage id == null //Error throw exception?! return; } streamId = oA.ID; } // Close stream message: else if (msg is CloseStreamMessage) { switch (state) { case ConnectionState.CONNECTING: case ConnectionState.CONNECTED: await internalDisconnectAsync(); break; } } // Rooster: else if (msg is RosterMessage) { NewRoosterMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); } // Presence: else if (msg is PresenceMessage && (msg as PresenceMessage).getFrom() != null) { NewPresenceMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); } // Bookmarks: else if (msg is BookmarksResultMessage) { NewBookmarksResultMessage?.Invoke(this, new NewBookmarksResultMessageEventArgs(msg as BookmarksResultMessage)); } else if (msg is OmemoDeviceListEventMessage deviceListEvent) { OMEMO_HELPER.onOmemoDeviceListEventMessage(deviceListEvent); } } }
/// <summary> /// Parses the given messages and invokes the ConnectionNewValidMessage event for each message. /// </summary> /// <param name="data">The messages string to parse.</param> private async Task ParseMessageAsync(string data) { // Parse message: List <AbstractMessage> messages = null; try { messages = PARSER.parseMessages(ref data); } catch (Exception e) { Logger.Error("[XMPPConnection2]: Error during message parsing." + e); return; } // Process messages: foreach (AbstractMessage msg in messages) { // Stream error messages: if (msg is StreamErrorMessage errorMessage) { Logger.Warn("[XMPPConnection2]: Received stream error message: " + errorMessage.ToString()); lastConnectionError = new ConnectionError(ConnectionErrorCode.SERVER_ERROR, errorMessage.ToString()); await OnConnectionErrorAsync(); await SendStreamCloseMessageAsync(); return; } // Filter IQ messages which ids are not valid: if (msg is IQMessage iq) { if (iq.GetType().Equals(IQMessage.RESULT) && messageIdCache.GetTimed(iq.ID) != null) { Logger.Warn("[XMPPConnection2]: Invalid message id received!"); return; } } // Respond to XEP-0199 (XMPP Ping) messages: if (msg is PingMessage pingMsg) { Logger.Debug("[XMPPConnection2]: XMPP ping received from " + pingMsg.getFrom()); await SendAsync(pingMsg.generateResponse(), true); } // Invoke message processors: NewValidMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); // Should restart connection? if (msg.getRestartConnection() != AbstractMessage.NO_RESTART) { if (msg.getRestartConnection() == AbstractMessage.SOFT_RESTART) { await SoftRestartAsync(); } else if (msg.getRestartConnection() == AbstractMessage.HARD_RESTART) { await HardRestartAsync(); } else { throw new ArgumentException("[XMPPConnection2]: Invalid restart type: " + msg.getRestartConnection()); } } // Filter already processed messages: if (msg.isProcessed()) { return; } // -------------------------------------------------------------------- // Open stream: if (msg is OpenStreamAnswerMessage oA) { if (oA.ID is null) { // TODO Handle OpenStreamAnswerMessage id is null //Error throw exception?! return; } } // Close stream message: else if (msg is CloseStreamMessage) { switch (state) { case ConnectionState.CONNECTING: case ConnectionState.CONNECTED: await InternalDisconnectAsync(false); break; } } // Rooster: else if (msg is RosterResultMessage) { NewRoosterMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); } // Presence: else if (msg is PresenceMessage presenceMessage && !(presenceMessage.getFrom() is null)) { NewPresenceMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); }
//--------------------------------------------------------Events:---------------------------------------------------------------------\\ #region --Events-- private void Connection_ConnectionNewRoosterMessage(IMessageSender sender, NewValidMessageEventArgs args) { NewRoosterMessage?.Invoke(this, args); }