Commit 2a4c54c3 authored by Christian Schudt's avatar Christian Schudt Committed by daryl herzmann

Gracefully close the stream with </stream:stream> (#770)

Fixes OF-1308
parent 34971f95
......@@ -216,22 +216,22 @@ public class NIOConnection implements Connection {
@Override
public void close() {
if (state.compareAndSet(State.OPEN, State.CLOSED)) {
if (state.compareAndSet(State.OPEN, State.CLOSED)) {
// Ensure that the state of this connection, its session and the MINA context are eventually closed.
if ( session != null ) {
session.setStatus( Session.STATUS_CLOSED );
}
if (session != null) {
session.setStatus(Session.STATUS_CLOSED);
}
try {
deliverRawText( flashClient ? "</flash:stream>" : "</stream:stream>" );
} catch ( Exception e ) {
deliverRawText0(flashClient ? "</flash:stream>" : "</stream:stream>");
} catch (Exception e) {
Log.error("Failed to deliver stream close tag: " + e.getMessage());
}
}
try {
ioSession.close( true );
ioSession.close(true);
} catch (Exception e) {
Log.error("Exception while closing MINA session", e);
}
......@@ -317,34 +317,38 @@ public class NIOConnection implements Connection {
@Override
public void deliverRawText(String text) {
if (!isClosed()) {
boolean errorDelivering = false;
IoBuffer buffer = IoBuffer.allocate(text.length());
buffer.setAutoExpand(true);
deliverRawText0(text);
}
}
private void deliverRawText0(String text){
boolean errorDelivering = false;
IoBuffer buffer = IoBuffer.allocate(text.length());
buffer.setAutoExpand(true);
try {
//Charset charset = Charset.forName(CHARSET);
//buffer.putString(text, charset.newEncoder());
buffer.put(text.getBytes(StandardCharsets.UTF_8));
if (flashClient) {
buffer.put((byte) '\0');
}
buffer.flip();
ioSessionLock.lock();
try {
//Charset charset = Charset.forName(CHARSET);
//buffer.putString(text, charset.newEncoder());
buffer.put(text.getBytes(StandardCharsets.UTF_8));
if (flashClient) {
buffer.put((byte) '\0');
}
buffer.flip();
ioSessionLock.lock();
try {
ioSession.write(buffer);
}
finally {
ioSessionLock.unlock();
}
ioSession.write(buffer);
}
catch (Exception e) {
Log.debug("Error delivering raw text:\n" + text, e);
errorDelivering = true;
finally {
ioSessionLock.unlock();
}
}
catch (Exception e) {
Log.debug("Error delivering raw text:\n" + text, e);
errorDelivering = true;
}
// Attempt to close the connection if delivering text fails.
if (errorDelivering) {
close();
}
// Attempt to close the connection if delivering text fails.
if (errorDelivering) {
close();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment