synchronize packetCallbacks

This commit is contained in:
Daniel Gultsch 2015-08-16 12:12:22 +02:00
parent c082066118
commit 51a2645349
1 changed files with 30 additions and 25 deletions

View File

@ -152,7 +152,6 @@ public class XmppConnection implements Runnable {
shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER); shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
tagReader = new XmlReader(wakeLock); tagReader = new XmlReader(wakeLock);
tagWriter = new TagWriter(); tagWriter = new TagWriter();
packetCallbacks.clear();
this.changeStatus(Account.State.CONNECTING); this.changeStatus(Account.State.CONNECTING);
if (DNSHelper.isIp(account.getServer().toString())) { if (DNSHelper.isIp(account.getServer().toString())) {
socket = new Socket(); socket = new Socket();
@ -489,26 +488,28 @@ public class XmppConnection implements Runnable {
this.jingleListener.onJinglePacketReceived(account,(JinglePacket) packet); this.jingleListener.onJinglePacketReceived(account,(JinglePacket) packet);
} }
} else { } else {
if (packetCallbacks.containsKey(packet.getId())) { synchronized (this.packetCallbacks) {
final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId()); if (packetCallbacks.containsKey(packet.getId())) {
// Packets to the server should have responses from the server final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
if (packetCallbackDuple.first.toServer(account)) { // Packets to the server should have responses from the server
if (packet.fromServer(account)) { if (packetCallbackDuple.first.toServer(account)) {
packetCallbackDuple.second.onIqPacketReceived(account, packet); if (packet.fromServer(account)) {
packetCallbacks.remove(packet.getId()); packetCallbackDuple.second.onIqPacketReceived(account, packet);
packetCallbacks.remove(packet.getId());
} else {
Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet");
}
} else { } else {
Log.e(Config.LOGTAG,account.getJid().toBareJid().toString()+": ignoring spoofed iq packet"); if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
} packetCallbackDuple.second.onIqPacketReceived(account, packet);
} else { packetCallbacks.remove(packet.getId());
if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) { } else {
packetCallbackDuple.second.onIqPacketReceived(account, packet); Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet");
packetCallbacks.remove(packet.getId()); }
} else {
Log.e(Config.LOGTAG,account.getJid().toBareJid().toString()+": ignoring spoofed iq packet");
} }
} else if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
this.unregisteredIqListener.onIqPacketReceived(account, packet);
} }
} else if (packet.getType() == IqPacket.TYPE.GET|| packet.getType() == IqPacket.TYPE.SET) {
this.unregisteredIqListener.onIqPacketReceived(account, packet);
} }
} }
} }
@ -739,13 +740,15 @@ public class XmppConnection implements Runnable {
} }
private void clearIqCallbacks() { private void clearIqCallbacks() {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": clearing iq iq callbacks"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": clearing iq iq callbacks");
final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.ERROR); final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.ERROR);
Iterator<Entry<String, Pair<IqPacket, OnIqPacketReceived>>> iterator = this.packetCallbacks.entrySet().iterator(); synchronized (this.packetCallbacks) {
while(iterator.hasNext()) { Iterator<Entry<String, Pair<IqPacket, OnIqPacketReceived>>> iterator = this.packetCallbacks.entrySet().iterator();
Entry<String, Pair<IqPacket, OnIqPacketReceived>> entry = iterator.next(); while (iterator.hasNext()) {
entry.getValue().second.onIqPacketReceived(account,failurePacket); Entry<String, Pair<IqPacket, OnIqPacketReceived>> entry = iterator.next();
iterator.remove(); entry.getValue().second.onIqPacketReceived(account, failurePacket);
iterator.remove();
}
} }
} }
@ -917,7 +920,9 @@ public class XmppConnection implements Runnable {
packet.setAttribute("id", id); packet.setAttribute("id", id);
} }
if (callback != null) { if (callback != null) {
packetCallbacks.put(packet.getId(), new Pair<>(packet, callback)); synchronized (this.packetCallbacks) {
packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
}
} }
this.sendPacket(packet); this.sendPacket(packet);
} }