use of javax.websocket.SendResult in project atmosphere by Atmosphere.
the class JSR356WebSocketTest method test_semaphore_is_released_in_case_of_failing_write.
@Test(timeOut = 1000)
public void test_semaphore_is_released_in_case_of_failing_write() throws Exception {
mockWriteResult(new SendResult(new RuntimeException("Fails")));
webSocket.write("Hello");
webSocket.write("Hello");
verify(asyncRemoteEndpoint, times(2)).sendText(eq("Hello"), any(SendHandler.class));
}
use of javax.websocket.SendResult in project undertow by undertow-io.
the class SendHandlerAdapter method complete.
@Override
public void complete(WebSocketChannel channel, Void context) {
if (done) {
return;
}
done = true;
handler.onResult(new SendResult());
}
use of javax.websocket.SendResult in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testBinaryWithByteBufferByCompletion.
@org.junit.Test
public void testBinaryWithByteBufferByCompletion() throws Exception {
final byte[] payload = "payload".getBytes();
final AtomicReference<SendResult> sendResult = new AtomicReference<>();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult latch = new FutureResult();
final FutureResult latch2 = new FutureResult();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer message) {
ByteBuffer buf = ByteBuffer.allocate(message.remaining());
buf.put(message);
buf.flip();
session.getAsyncRemote().sendBinary(buf, new SendHandler() {
@Override
public void onResult(SendResult result) {
sendResult.set(result);
if (result.getException() != null) {
latch2.setException(new IOException(result.getException()));
} else {
latch2.setResult(null);
}
}
});
}
});
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
latch.getIoFuture().get();
latch2.getIoFuture().get();
SendResult result = sendResult.get();
Assert.assertNotNull(result);
Assert.assertNull(result.getException());
client.destroy();
}
use of javax.websocket.SendResult in project tomcat by apache.
the class WsRemoteEndpointImplClient method doWrite.
@Override
protected void doWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... data) {
long timeout;
for (ByteBuffer byteBuffer : data) {
if (blockingWriteTimeoutExpiry == -1) {
timeout = getSendTimeout();
if (timeout < 1) {
timeout = Long.MAX_VALUE;
}
} else {
timeout = blockingWriteTimeoutExpiry - System.currentTimeMillis();
if (timeout < 0) {
SendResult sr = new SendResult(new IOException("Blocking write timeout"));
handler.onResult(sr);
}
}
try {
channel.write(byteBuffer).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
handler.onResult(new SendResult(e));
return;
}
}
handler.onResult(SENDRESULT_OK);
}
use of javax.websocket.SendResult in project tomcat by apache.
the class WsRemoteEndpointImplBase method startMessage.
void startMessage(byte opCode, ByteBuffer payload, boolean last, SendHandler handler) {
wsSession.updateLastActive();
List<MessagePart> messageParts = new ArrayList<>();
messageParts.add(new MessagePart(last, 0, opCode, payload, intermediateMessageHandler, new EndMessageHandler(this, handler), -1));
messageParts = transformation.sendMessagePart(messageParts);
// trigger the supplied SendHandler
if (messageParts.size() == 0) {
handler.onResult(new SendResult());
return;
}
MessagePart mp = messageParts.remove(0);
boolean doWrite = false;
synchronized (messagePartLock) {
if (Constants.OPCODE_CLOSE == mp.getOpCode() && getBatchingAllowed()) {
// Should not happen. To late to send batched messages now since
// the session has been closed. Complain loudly.
log.warn(sm.getString("wsRemoteEndpoint.flushOnCloseFailed"));
}
if (messagePartInProgress.tryAcquire()) {
doWrite = true;
} else {
// When a control message is sent while another message is being
// sent, the control message is queued. Chances are the
// subsequent data message part will end up queued while the
// control message is sent. The logic in this class (state
// machine, EndMessageHandler, TextMessageSendHandler) ensures
// that there will only ever be one data message part in the
// queue. There could be multiple control messages in the queue.
// Add it to the queue
messagePartQueue.add(mp);
}
// Add any remaining messages to the queue
messagePartQueue.addAll(messageParts);
}
if (doWrite) {
// Actual write has to be outside sync block to avoid possible
// deadlock between messagePartLock and writeLock in
// o.a.coyote.http11.upgrade.AbstractServletOutputStream
writeMessagePart(mp);
}
}
Aggregations