diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java | 99 |
1 files changed, 54 insertions, 45 deletions
diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 35c89b45f..5ac089766 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -26,7 +26,6 @@ import java.net.IDN; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; -import java.net.SocketAddress; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -35,6 +34,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -81,7 +81,6 @@ public class XmppConnection implements Runnable { private static final int PACKET_IQ = 0; private static final int PACKET_MESSAGE = 1; private static final int PACKET_PRESENCE = 2; - private final Context applicationContext; protected Account account; private final WakeLock wakeLock; private Socket socket; @@ -95,7 +94,7 @@ public class XmppConnection implements Runnable { private String streamId = null; private int smVersion = 3; - private final SparseArray<String> messageReceipts = new SparseArray<>(); + private final SparseArray<String> mStanzaReceipts = new SparseArray<>(); private int stanzasReceived = 0; private int stanzasSent = 0; @@ -123,7 +122,6 @@ public class XmppConnection implements Runnable { PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString()); tagWriter = new TagWriter(); mXmppConnectionService = service; - applicationContext = service.getApplicationContext(); } protected void changeStatus(final Account.State nextStatus) { @@ -165,6 +163,9 @@ public class XmppConnection implements Runnable { } } else { final Bundle result = DNSHelper.getSRVRecord(account.getServer()); + if (result == null) { + throw new IOException("unhandled exception in DNS resolver"); + } final ArrayList<Parcelable> values = result.getParcelableArrayList("values"); if ("timeout".equals(result.getString("error"))) { throw new IOException("timeout in dns"); @@ -338,23 +339,24 @@ public class XmppConnection implements Runnable { + ": session resumed with lost packages"); stanzasSent = serverCount; } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() - + ": session resumed"); + Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed"); } - if (acknowledgedListener != null) { - for (int i = 0; i < messageReceipts.size(); ++i) { - if (serverCount >= messageReceipts.keyAt(i)) { - acknowledgedListener.onMessageAcknowledged( - account, messageReceipts.valueAt(i)); - } + acknowledgeStanzaUpTo(serverCount); + ArrayList<IqPacket> failedIqPackets = new ArrayList<>(); + for(int i = 0; i < this.mStanzaReceipts.size(); ++i) { + String id = mStanzaReceipts.valueAt(i); + Pair<IqPacket,OnIqPacketReceived> pair = id == null ? null : this.packetCallbacks.get(id); + if (pair != null) { + failedIqPackets.add(pair.first); } } - messageReceipts.clear(); + mStanzaReceipts.clear(); + Log.d(Config.LOGTAG,"resending "+failedIqPackets.size()+" iq stanza"); + for(IqPacket packet : failedIqPackets) { + sendUnmodifiedIqPacket(packet,null); + } } catch (final NumberFormatException ignored) { } - sendServiceDiscoveryInfo(account.getServer()); - sendServiceDiscoveryInfo(account.getJid().toBareJid()); - sendServiceDiscoveryItems(account.getServer()); sendInitialPing(); } else if (nextTag.isStart("r")) { tagReader.readElement(nextTag); @@ -368,17 +370,7 @@ public class XmppConnection implements Runnable { lastPacketReceived = SystemClock.elapsedRealtime(); try { final int serverSequence = Integer.parseInt(ack.getAttribute("h")); - if (Config.EXTENDED_SM_LOGGING) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server acknowledged stanza #" + serverSequence); - } - final String msgId = this.messageReceipts.get(serverSequence); - if (msgId != null) { - if (this.acknowledgedListener != null) { - this.acknowledgedListener.onMessageAcknowledged( - account, msgId); - } - this.messageReceipts.remove(serverSequence); - } + acknowledgeStanzaUpTo(serverSequence); } catch (NumberFormatException e) { Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server send ack without sequence number"); } @@ -406,6 +398,22 @@ public class XmppConnection implements Runnable { } } + private void acknowledgeStanzaUpTo(int serverCount) { + for (int i = 0; i < mStanzaReceipts.size(); ++i) { + if (serverCount >= mStanzaReceipts.keyAt(i)) { + if (Config.EXTENDED_SM_LOGGING) { + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server acknowledged stanza #" + mStanzaReceipts.keyAt(i)); + } + String id = mStanzaReceipts.valueAt(i); + if (acknowledgedListener != null) { + acknowledgedListener.onMessageAcknowledged(account, id); + } + mStanzaReceipts.removeAt(i); + i--; + } + } + } + private void sendInitialPing() { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping"); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); @@ -521,14 +529,6 @@ public class XmppConnection implements Runnable { tagWriter.writeTag(startTLS); } - private SharedPreferences getPreferences() { - return PreferenceManager.getDefaultSharedPreferences(applicationContext); - } - - private boolean enableLegacySSL() { - return getPreferences().getBoolean("enable_legacy_ssl", false); - } - private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException { tagReader.readTag(); try { @@ -707,6 +707,7 @@ public class XmppConnection implements Runnable { } catch (final InterruptedException ignored) { } } + clearIqCallbacks(); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET); iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind") .addChild("resource").setContent(account.getResource()); @@ -737,9 +738,20 @@ public class XmppConnection implements Runnable { }); } + private void clearIqCallbacks() { + Log.d(Config.LOGTAG,account.getJid().toBareJid()+": clearing iq iq callbacks"); + final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.ERROR); + Iterator<Entry<String, Pair<IqPacket, OnIqPacketReceived>>> iterator = this.packetCallbacks.entrySet().iterator(); + while(iterator.hasNext()) { + Entry<String, Pair<IqPacket, OnIqPacketReceived>> entry = iterator.next(); + entry.getValue().second.onIqPacketReceived(account,failurePacket); + iterator.remove(); + } + } + private void sendStartSession() { final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET); - startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session"); + startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session"); this.sendUnmodifiedIqPacket(startSession, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { @@ -763,7 +775,7 @@ public class XmppConnection implements Runnable { final EnablePacket enable = new EnablePacket(smVersion); tagWriter.writeStanzaAsync(enable); stanzasSent = 0; - messageReceipts.clear(); + mStanzaReceipts.clear(); } features.carbonsEnabled = false; features.blockListRequested = false; @@ -895,7 +907,7 @@ public class XmppConnection implements Runnable { public void sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) { packet.setFrom(account.getJid()); - this.sendUnmodifiedIqPacket(packet,callback); + this.sendUnmodifiedIqPacket(packet, callback); } @@ -905,9 +917,6 @@ public class XmppConnection implements Runnable { packet.setAttribute("id", id); } if (callback != null) { - if (packet.getId() == null) { - packet.setId(nextRandomId()); - } packetCallbacks.put(packet.getId(), new Pair<>(packet, callback)); } this.sendPacket(packet); @@ -932,11 +941,11 @@ public class XmppConnection implements Runnable { ++stanzasSent; } tagWriter.writeStanzaAsync(packet); - if (packet instanceof MessagePacket && packet.getId() != null && getFeatures().sm()) { + if ((packet instanceof MessagePacket || packet instanceof IqPacket) && packet.getId() != null && this.streamId != null) { if (Config.EXTENDED_SM_LOGGING) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for message stanza #" + stanzasSent); + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for stanza #" + stanzasSent); } - this.messageReceipts.put(stanzasSent, packet.getId()); + this.mStanzaReceipts.put(stanzasSent, packet.getId()); tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion)); } } @@ -1005,7 +1014,7 @@ public class XmppConnection implements Runnable { if (tagWriter.isActive()) { tagWriter.finish(); try { - while (!tagWriter.finished()) { + while (!tagWriter.finished() && socket.isConnected()) { Log.d(Config.LOGTAG, "not yet finished"); Thread.sleep(100); } |