use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.
the class DeflateDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
if (decoder == null) {
if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
}
decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
}
boolean readable = msg.content().isReadable();
decoder.writeInbound(msg.content().retain());
if (appendFrameTail(msg)) {
decoder.writeInbound(Unpooled.wrappedBuffer(FRAME_TAIL));
}
CompositeByteBuf compositeUncompressedContent = ctx.alloc().compositeBuffer();
for (; ; ) {
ByteBuf partUncompressedContent = decoder.readInbound();
if (partUncompressedContent == null) {
break;
}
if (!partUncompressedContent.isReadable()) {
partUncompressedContent.release();
continue;
}
compositeUncompressedContent.addComponent(true, partUncompressedContent);
}
// See https://github.com/netty/netty/issues/4348
if (readable && compositeUncompressedContent.numComponents() <= 0) {
compositeUncompressedContent.release();
throw new CodecException("cannot read uncompressed buffer");
}
if (msg.isFinalFragment() && noContext) {
cleanup();
}
WebSocketFrame outMsg;
if (msg instanceof TextWebSocketFrame) {
outMsg = new TextWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
} else if (msg instanceof BinaryWebSocketFrame) {
outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
} else if (msg instanceof ContinuationWebSocketFrame) {
outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
} else {
throw new CodecException("unexpected frame type: " + msg.getClass().getName());
}
out.add(outMsg);
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project async-http-client by AsyncHttpClient.
the class NettyWebSocket method sendMessage.
@Override
public WebSocket sendMessage(byte[] message, WebSocketWriteCompleteListener listener) {
final ChannelPromise channelPromise = channel.newPromise();
channelPromise.addListener(listener);
channel.writeAndFlush(new BinaryWebSocketFrame(wrappedBuffer(message)), channelPromise);
return this;
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project async-http-client by AsyncHttpClient.
the class NettyWebSocket method stream.
@Override
public WebSocket stream(final byte[] fragment, final int offset, final int len, final boolean last, final WebSocketWriteCompleteListener listener) {
final ChannelPromise channelPromise = channel.newPromise();
channelPromise.addListener(listener);
channel.writeAndFlush(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment, offset, len)), channelPromise);
return this;
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project undertow by undertow-io.
the class AbstractWebSocketServerTest method testBinary.
@Test
public void testBinary() throws Exception {
if (getVersion() == WebSocketVersion.V00) {
// ignore 00 tests for now
return;
}
final AtomicBoolean connected = new AtomicBoolean(false);
DefaultServer.setRootHandler(new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {
@Override
public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
connected.set(true);
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
final Pooled<ByteBuffer[]> data = message.getData();
WebSockets.sendBinary(data.getResource(), channel, new WebSocketCallback<Void>() {
@Override
public void complete(WebSocketChannel channel, Void context) {
data.close();
}
@Override
public void onError(WebSocketChannel channel, Void context, Throwable throwable) {
data.close();
}
});
}
});
channel.resumeReceives();
}
}));
final FutureResult latch = new FutureResult();
final byte[] payload = "payload".getBytes();
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
latch.getIoFuture().get();
client.destroy();
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testBinaryWithByteArrayUsingStream.
@org.junit.Test
public void testBinaryWithByteArrayUsingStream() throws Exception {
final byte[] payload = "payload".getBytes();
final AtomicReference<Throwable> cause = new AtomicReference<>();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult latch = new FutureResult();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
session.addMessageHandler(new MessageHandler.Whole<byte[]>() {
@Override
public void onMessage(final byte[] message) {
DefaultServer.getWorker().execute(new Runnable() {
@Override
public void run() {
try {
OutputStream out = session.getBasicRemote().getSendStream();
out.write(message);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
cause.set(e);
latch.setException(e);
}
}
});
}
});
}
}
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();
Assert.assertNull(cause.get());
client.destroy();
}
Aggregations