use of io.vertx.core.net.impl.NetSocketInternal in project vert.x by eclipse.
the class WebSocketTest method testServerCloseHandshake.
@Test
public void testServerCloseHandshake() {
short status = (short) (4000 + TestUtils.randomPositiveInt() % 100);
waitFor(2);
server = vertx.createHttpServer();
server.webSocketHandler(ws -> {
ws.closeHandler(sc -> {
assertEquals((Short) (short) status, ws.closeStatusCode());
complete();
});
});
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v1 -> {
client = vertx.createHttpClient();
handshake(client, req -> {
req.send(onSuccess(resp -> {
assertEquals(101, resp.statusCode());
Http1xClientConnection conn = (Http1xClientConnection) req.connection();
NetSocketInternal soi = conn.toNetSocket();
soi.channelHandlerContext().pipeline().addBefore("handler", "encoder", new WebSocket13FrameEncoder(true));
soi.channelHandlerContext().pipeline().addBefore("handler", "decoder", new WebSocket13FrameDecoder(false, false, 1000));
String reason = randomAlphaString(10);
soi.writeMessage(new CloseWebSocketFrame(status, reason));
AtomicBoolean closeFrameReceived = new AtomicBoolean();
soi.messageHandler(msg -> {
try {
if (msg instanceof CloseWebSocketFrame) {
CloseWebSocketFrame frame = (CloseWebSocketFrame) msg;
assertEquals(status, frame.statusCode());
assertEquals(reason, frame.reasonText());
closeFrameReceived.set(true);
}
} finally {
ReferenceCountUtil.release(msg);
}
});
soi.closeHandler(v2 -> {
assertTrue(closeFrameReceived.get());
complete();
});
}));
});
}));
await();
}
use of io.vertx.core.net.impl.NetSocketInternal in project vert.x by eclipse.
the class Http1xClientConnection method toNetSocket.
/**
* @return a raw {@code NetSocket} - for internal use
*/
public NetSocketInternal toNetSocket() {
removeChannelHandlers();
NetSocketImpl socket = new NetSocketImpl(context, chctx, null, metrics());
socket.metric(metric());
evictionHandler.handle(null);
chctx.pipeline().replace("handler", "handler", VertxHandler.create(ctx -> socket));
return socket;
}
use of io.vertx.core.net.impl.NetSocketInternal in project vertx-proton by vert-x3.
the class ProtonTransport method flush.
void flush() {
boolean done = false;
while (!done) {
ByteBuffer outputBuffer = transport.getOutputBuffer();
if (outputBuffer != null && outputBuffer.hasRemaining()) {
final NetSocketInternal internal = (NetSocketInternal) socket;
final ByteBuf bb = internal.channelHandlerContext().alloc().directBuffer(outputBuffer.remaining());
bb.writeBytes(outputBuffer);
internal.writeMessage(bb);
transport.outputConsumed();
} else {
done = true;
}
}
}
use of io.vertx.core.net.impl.NetSocketInternal in project hono by eclipse.
the class CommandAndControlMqttIT method injectMqttClientPubAckBlocker.
private Future<Void> injectMqttClientPubAckBlocker(final AtomicBoolean outboundPubAckBlocked) {
// Therefore the underlying NetSocket pipeline is used here to filter out the outbound PubAck messages.
try {
final Method connectionMethod = MqttClientImpl.class.getDeclaredMethod("connection");
connectionMethod.setAccessible(true);
final NetSocketInternal connection = (NetSocketInternal) connectionMethod.invoke(mqttClient);
connection.channelHandlerContext().pipeline().addBefore("handler", "OutboundPubAckBlocker", new ChannelOutboundHandlerAdapter() {
@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
if (outboundPubAckBlocked.get() && msg instanceof io.netty.handler.codec.mqtt.MqttPubAckMessage) {
LOGGER.debug("suppressing PubAck, message id: {}", ((MqttPubAckMessage) msg).variableHeader().messageId());
} else {
super.write(ctx, msg, promise);
}
}
});
return Future.succeededFuture();
} catch (final Exception e) {
LOGGER.error("failed to inject PubAck blocking handler");
return Future.failedFuture(new Exception("failed to inject PubAck blocking handler", e));
}
}
use of io.vertx.core.net.impl.NetSocketInternal in project vert.x by eclipse.
the class NetTest method testNetSocketInternalEvent.
@Test
public void testNetSocketInternalEvent() throws Exception {
server.connectHandler(so -> {
NetSocketInternal soi = (NetSocketInternal) so;
Object expectedEvent = new Object();
soi.eventHandler(event -> {
assertSame(expectedEvent, event);
soi.close();
});
ChannelPipeline pipeline = soi.channelHandlerContext().pipeline();
pipeline.addFirst(new ChannelHandlerAdapter() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
super.handlerAdded(ctx);
ctx.executor().schedule(() -> {
ctx.fireUserEventTriggered(expectedEvent);
}, 10, TimeUnit.MILLISECONDS);
}
});
});
startServer();
client.connect(testAddress, onSuccess(so -> {
so.closeHandler(v -> testComplete());
}));
await();
}
Aggregations