server.connectHandler(so -> { NetSocketInternal internal = (NetSocketInternal) so; assertEquals(expectSSL, internal.isSsl()); ChannelHandlerContext chctx = internal.channelHandlerContext(); ChannelPipeline pipeline = chctx.pipeline(); pipeline.addBefore("handler", "http", new HttpServerCodec()); internal.handler(buff -> fail()); internal.messageHandler(obj -> { if (obj instanceof LastHttpContent) { DefaultFullHttpResponse response = new DefaultFullHttpResponse( Unpooled.copiedBuffer("Hello World", StandardCharsets.UTF_8)); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, "11"); internal.writeMessage(response, onSuccess(v -> complete()));
handshake(res -> { NetSocketInternal so = (NetSocketInternal) res; so.channelHandlerContext().pipeline().addBefore("handler", "encoder", new WebSocket13FrameEncoder(true)); so.channelHandlerContext().pipeline().addBefore("handler", "decoder", new WebSocket13FrameDecoder(false, false, 1000)); int status = 4000 + TestUtils.randomPositiveInt() % 100; String reason = TestUtils.randomAlphaString(10); so.writeMessage(new CloseWebSocketFrame(status, reason)); Deque<Object> received = new ArrayDeque<>(); so.messageHandler(received::add); so.closeHandler(v2 -> { assertEquals(1, received.size()); Object msg = received.getFirst();
server.connectHandler(so -> { NetSocketInternal soi = (NetSocketInternal) so; soi.messageHandler(msg -> fail("Unexpected")); soi.handler(msg -> { ByteBuf byteBuf = msg.getByteBuf(); assertFalse(byteBuf.isDirect()); assertFalse(byteBuf.release()); assertEquals(1, byteBuf.refCnt()); soi.write(msg); }); }); client.connect(testAddress, onSuccess(so -> { NetSocketInternal soi = (NetSocketInternal) so; soi.write(Buffer.buffer("Hello World")); soi.messageHandler(msg -> fail("Unexpected")); soi.handler(msg -> { ByteBuf byteBuf = msg.getByteBuf(); assertFalse(byteBuf.isDirect());
void initializeCodec() { decoder = new MessageDecoder(inflight, socket.channelHandlerContext().alloc()); encoder = new MessageEncoder(socket.channelHandlerContext()); ChannelPipeline pipeline = socket.channelHandlerContext().pipeline(); pipeline.addBefore("handler", "decoder", decoder); socket.closeHandler(this::handleClosed); socket.exceptionHandler(this::handleException); socket.messageHandler(msg -> { try { handleMessage(msg); } catch (Exception e) { handleException(e); } }); }
/** * Initialize the client, this will configure Netty's pipeline an set an handler to process the * decoded messages. */ private void init() { // Get the pipeline ChannelPipeline pipeline = so.channelHandlerContext().pipeline(); // Add the memcached message aggregator pipeline.addFirst("aggregator", new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE)); // Add the memcached decoder pipeline.addFirst("memcached", new BinaryMemcacheClientCodec()); // Set the message handler to process memcached message so.messageHandler(this::processResponse); }
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; } } }
public MqttServerConnection(NetSocketInternal so, MqttServerOptions options) { this.so = so; this.chctx = so.channelHandlerContext(); this.options = options; }
private void writeRequest(BinaryMemcacheRequest request, Handler<AsyncResult<FullBinaryMemcacheResponse>> completionHandler) { // Write the message, the memcached codec will encode the request // to a buffer and it will be sent so.writeMessage(request, ar -> { if (ar.succeeded()) { // The message has been encoded succesfully and sent // we add the handler to the inflight queue inflight.add(completionHandler); } else { // The message could not be encoded or sent // we signal an error completionHandler.handle(Future.failedFuture(ar.cause())); } }); }
this.so.exceptionHandler(t -> { this.endpoint.handleException(t); }); this.so.closeHandler(v -> this.endpoint.handleClosed());
public boolean isSsl() { return socket.isSsl(); }
void sendCancelRequestMessage(int processId, int secretKey, Handler<AsyncResult<Void>> handler) { Buffer buffer = Buffer.buffer(16); buffer.appendInt(16); // cancel request code buffer.appendInt(80877102); buffer.appendInt(processId); buffer.appendInt(secretKey); socket.write(buffer, ar -> { if (ar.succeeded()) { // directly close this connection if (status == Status.CONNECTED) { status = Status.CLOSING; socket.close(); } handler.handle(Future.succeededFuture()); } else { handler.handle(Future.failedFuture(ar.cause())); } }); }
/** * Check if the endpoint was created and is connected * * @return status of the endpoint (connected or not) */ private boolean checkConnected() { synchronized (this.so) { if ((this.endpoint != null) && (this.endpoint.isConnected())) { return true; } else { so.close(); throw new IllegalStateException("Received an MQTT packet from a not connected client (CONNECT not sent yet)"); } } } }
void initializeCodec() { decoder = new MessageDecoder(inflight, socket.channelHandlerContext().alloc()); encoder = new MessageEncoder(socket.channelHandlerContext()); ChannelPipeline pipeline = socket.channelHandlerContext().pipeline(); pipeline.addBefore("handler", "decoder", decoder); socket.closeHandler(this::handleClosed); socket.exceptionHandler(this::handleException); socket.messageHandler(msg -> { try { handleMessage(msg); } catch (Exception e) { handleException(e); } }); }
@Override public MqttServer listen(int port, String host, Handler<AsyncResult<MqttServer>> listenHandler) { Handler<MqttEndpoint> h1 = endpointHandler; Handler<Throwable> h2 = exceptionHandler; server.connectHandler(so -> { NetSocketInternal soi = (NetSocketInternal) so; ChannelPipeline pipeline = soi.channelHandlerContext().pipeline(); initChannel(pipeline); MqttServerConnection conn = new MqttServerConnection(soi, options); soi.messageHandler(msg -> { synchronized (conn) { conn.handleMessage(msg); } }); conn.init(h1, h2); }); server.listen(port, host, ar -> listenHandler.handle(ar.map(this))); return this; }
public MqttServerConnection(NetSocketInternal so, MqttServerOptions options) { this.so = so; this.chctx = so.channelHandlerContext(); this.options = options; }
void write(io.netty.handler.codec.mqtt.MqttMessage mqttMessage) { log.debug(String.format("Sending packet %s", mqttMessage)); this.connection().writeMessage(mqttMessage); }
this.so.exceptionHandler(t -> { this.endpoint.handleException(t); }); this.so.closeHandler(v -> this.endpoint.handleClosed());
public boolean isSsl() { return socket.isSsl(); }
void sendCancelRequestMessage(int processId, int secretKey, Handler<AsyncResult<Void>> handler) { Buffer buffer = Buffer.buffer(16); buffer.appendInt(16); // cancel request code buffer.appendInt(80877102); buffer.appendInt(processId); buffer.appendInt(secretKey); socket.write(buffer, ar -> { if (ar.succeeded()) { // directly close this connection if (status == Status.CONNECTED) { status = Status.CLOSING; socket.close(); } handler.handle(Future.succeededFuture()); } else { handler.handle(Future.failedFuture(ar.cause())); } }); }
/** * Check if the endpoint was created and is connected * * @return status of the endpoint (connected or not) */ private boolean checkConnected() { synchronized (this.so) { if ((this.endpoint != null) && (this.endpoint.isConnected())) { return true; } else { so.close(); throw new IllegalStateException("Received an MQTT packet from a not connected client (CONNECT not sent yet)"); } } } }