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 { ...@@ -216,22 +216,22 @@ public class NIOConnection implements Connection {
@Override @Override
public void close() { 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. // Ensure that the state of this connection, its session and the MINA context are eventually closed.
if ( session != null ) { if (session != null) {
session.setStatus( Session.STATUS_CLOSED ); session.setStatus(Session.STATUS_CLOSED);
} }
try { try {
deliverRawText( flashClient ? "</flash:stream>" : "</stream:stream>" ); deliverRawText0(flashClient ? "</flash:stream>" : "</stream:stream>");
} catch ( Exception e ) { } catch (Exception e) {
Log.error("Failed to deliver stream close tag: " + e.getMessage()); Log.error("Failed to deliver stream close tag: " + e.getMessage());
} }
try { try {
ioSession.close( true ); ioSession.close(true);
} catch (Exception e) { } catch (Exception e) {
Log.error("Exception while closing MINA session", e); Log.error("Exception while closing MINA session", e);
} }
...@@ -317,34 +317,38 @@ public class NIOConnection implements Connection { ...@@ -317,34 +317,38 @@ public class NIOConnection implements Connection {
@Override @Override
public void deliverRawText(String text) { public void deliverRawText(String text) {
if (!isClosed()) { if (!isClosed()) {
boolean errorDelivering = false; deliverRawText0(text);
IoBuffer buffer = IoBuffer.allocate(text.length()); }
buffer.setAutoExpand(true); }
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 { try {
//Charset charset = Charset.forName(CHARSET); ioSession.write(buffer);
//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();
}
} }
catch (Exception e) { finally {
Log.debug("Error delivering raw text:\n" + text, e); ioSessionLock.unlock();
errorDelivering = true;
} }
}
catch (Exception e) {
Log.debug("Error delivering raw text:\n" + text, e);
errorDelivering = true;
}
// Attempt to close the connection if delivering text fails. // Attempt to close the connection if delivering text fails.
if (errorDelivering) { if (errorDelivering) {
close(); 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