Created
February 1, 2017 18:30
-
-
Save anonymous/cbb6004adb1a30d4274622814a69284c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sends a complete binary message, invoking the callback when complete. | |
* Automatically frees the pooled byte buffer when done. | |
* | |
* @param pooledData The buffer to be sent, it will be freed when done. | |
* @param wsChannel | |
* @param callback | |
*/ | |
public static void sendBinary(final PooledByteBuffer pooledData, final WebSocketChannel wsChannel, final WebSocketCallback<Void> callback) { | |
sendInternal(pooledData, WebSocketFrameType.BINARY, wsChannel, callback, -1); | |
} | |
/** | |
* Sends a complete binary message, invoking the callback when complete. | |
* Automatically frees the pooled byte buffer when done. | |
* | |
* @param pooledData The buffer to be sent, it will be freed when done. | |
* @param wsChannel | |
* @param callback | |
*/ | |
public static void sendBinary(final PooledByteBuffer pooledData, final WebSocketChannel wsChannel, final WebSocketCallback<Void> callback, long timeoutmillis) { | |
sendInternal(pooledData, WebSocketFrameType.BINARY, wsChannel, callback, timeoutmillis); | |
} | |
private static void sendInternal(final ByteBuffer data, WebSocketFrameType type, final WebSocketChannel wsChannel, final WebSocketCallback<Void> callback, long timeoutmillis) { | |
sendInternal(new ImmediatePooledByteBuffer(data), type, wsChannel, callback, timeoutmillis); | |
} | |
private static void sendInternal(final PooledByteBuffer pooledData, WebSocketFrameType type, final WebSocketChannel wsChannel, final WebSocketCallback<Void> callback, long timeoutmillis) { | |
boolean closeData = true; | |
try { | |
StreamSinkFrameChannel channel = wsChannel.send(type); | |
// TODO chunk data into some MTU-like thing to control packet size | |
closeData = false; // channel.send takes ownership of pooledData, so it no longer needs to be closed. | |
if(!channel.send(pooledData)) { | |
throw WebSocketMessages.MESSAGES.unableToSendOnNewChannel(); | |
} | |
flushChannelAsync(wsChannel, callback, channel, null, timeoutmillis); | |
} catch (IOException e) { | |
if (callback != null) { | |
callback.onError(wsChannel, null, e); | |
} else { | |
IoUtils.safeClose(wsChannel); | |
} | |
}finally { | |
if ( closeData ) { | |
pooledData.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment