Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XEP-0047 Send ‘close’ stream only if it was not closed #556

Open
wants to merge 2 commits into
base: 4.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* automatically if one of them is closed.
*
* @author Henning Staib
* @author Eng Chong Meng
*/
public class InBandBytestreamSession implements BytestreamSession {

Expand Down Expand Up @@ -93,6 +94,9 @@ public class InBandBytestreamSession implements BytestreamSession {
/* flag to indicate if session is closed */
private boolean isClosed = false;

/* flag to indicate if session is already closed by peer */
private volatile boolean closedByPeer = false;

/**
* Constructor.
*
Expand Down Expand Up @@ -188,6 +192,7 @@ protected void closeByPeer(Close closeRequest) throws NotConnectedException, Int
this.inputStream.closeInternal();
this.inputStream.cleanup();
this.outputStream.closeInternal(false);
this.closedByPeer = true;

// acknowledge close request
IQ confirmClose = IQ.createResultIQ(closeRequest);
Expand Down Expand Up @@ -224,18 +229,19 @@ protected synchronized void closeByLocal(boolean in) throws IOException {
if (this.inputStream.isClosed && this.outputStream.isClosed) {
this.isClosed = true;

// send close request
Close close = new Close(this.byteStreamRequest.getSessionID());
close.setTo(this.remoteJID);
try {
connection.createStanzaCollectorAndSend(close).nextResultOrThrow();
}
catch (Exception e) {
// Sadly we are unable to use the IOException(Throwable) constructor because this
// constructor is only supported from Android API 9 on.
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
// send close stream request if not already closed by peer
if (!closedByPeer) {
Close close = new Close(this.byteStreamRequest.getSessionID());
close.setTo(this.remoteJID);
try {
connection.createStanzaCollectorAndSend(close).nextResultOrThrow();
} catch (Exception e) {
// Sadly we are unable to use the IOException(Throwable) constructor because this
// constructor is only supported from Android API 9 on.
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
}
}

this.inputStream.cleanup();
Expand Down