@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { if (!acceptForeignIp) { if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) { ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted." + QosConstants.BR_STR).getBytes()); ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE); } } } }
/** * Sends the specified message to the proxy server. Use this method to send a response to the proxy server in * {@link #handleResponse(ChannelHandlerContext, Object)}. */ protected final void sendToProxyServer(Object msg) { ctx.writeAndFlush(msg).addListener(writeListener); }
/** * async Send message to tx Manager. * * @param heartBeat {@linkplain HeartBeat } */ public void asyncSendTxManagerMessage(final HeartBeat heartBeat) { if (ctx != null && ctx.channel() != null && ctx.channel().isActive()) { ctx.writeAndFlush(heartBeat); } }
@Override public void channelActive(final ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(shutdownKey + "\r\n").addListener(ChannelFutureListener.CLOSE); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { if (!acceptForeignIp) { if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) { ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted." + QosConstants.BR_STR).getBytes()); ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE); } } } }
@Override @SuppressWarnings("unchecked") public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { final Channel channel = ctx.channel(); final String request = (String)msg; try { ctx.writeAndFlush(request, ctx.voidPromise()); LOGGER.info("client:" + msg); } catch (Exception e) { LOGGER.error("when try flush error", e); } }
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) .addListener(ChannelFutureListener.CLOSE); }
@Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (StringUtils.isBlank(msg)) { ctx.writeAndFlush(QosProcessHandler.prompt); } else { CommandContext commandContext = TelnetCommandDecoder.decode(msg); commandContext.setRemote(ctx.channel()); try { String result = commandExecutor.execute(commandContext); if (StringUtils.isEquals(QosConstants.CLOSE, result)) { ctx.writeAndFlush(getByeLabel()).addListener(ChannelFutureListener.CLOSE); } else { ctx.writeAndFlush(result + QosConstants.BR_STR + QosProcessHandler.prompt); } } catch (NoSuchCommandException ex) { ctx.writeAndFlush(msg + " :no such command"); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("can not found command " + commandContext, ex); } catch (Exception ex) { ctx.writeAndFlush(msg + " :fail to execute commandContext by " + ex.getMessage()); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("execute commandContext got exception " + commandContext, ex); } } }
@Override final protected void handlePing(CommandPing ping) { // Immediately reply success to ping requests if (log.isDebugEnabled()) { log.debug("[{}] Replying back to ping message", ctx.channel()); } ctx.writeAndFlush(Commands.newPong()); }
public final void sendErrorAndClose(ChannelHandlerContext ctx, int statusCode, String reasonText) { final Object mesg = serverClosingConnectionMessage(statusCode, reasonText); ctx.writeAndFlush(mesg).addListener(ChannelFutureListener.CLOSE); }
@Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (StringUtils.isBlank(msg)) { ctx.writeAndFlush(QosProcessHandler.prompt); } else { CommandContext commandContext = TelnetCommandDecoder.decode(msg); commandContext.setRemote(ctx.channel()); try { String result = commandExecutor.execute(commandContext); if (StringUtils.isEquals(QosConstants.CLOSE, result)) { ctx.writeAndFlush(getByeLabel()).addListener(ChannelFutureListener.CLOSE); } else { ctx.writeAndFlush(result + QosConstants.BR_STR + QosProcessHandler.prompt); } } catch (NoSuchCommandException ex) { ctx.writeAndFlush(msg + " :no such command"); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("can not found command " + commandContext, ex); } catch (Exception ex) { ctx.writeAndFlush(msg + " :fail to execute commandContext by " + ex.getMessage()); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("execute commandContext got exception " + commandContext, ex); } } }
private void sendRedirectResponse(ChannelHandlerContext ctx, String token, String server, int msgId) { MessageBase response; if (server == null || server.equals(holder.props.host)) { log.debug("HardwareLogic token is invalid. Token '{}', '{}'", token, ctx.channel().remoteAddress()); response = invalidToken(msgId); } else { log.debug("Redirecting token '{}' to {}", token, server); response = makeASCIIStringMessage(CONNECT_REDIRECT, msgId, server + BODY_SEPARATOR + listenPort); } ctx.writeAndFlush(response, ctx.voidPromise()); }
public final void sendErrorAndClose(ChannelHandlerContext ctx, int statusCode, String reasonText) { ctx.writeAndFlush(serverClosingConnectionMessage(statusCode, reasonText)) .addListener(ChannelFutureListener.CLOSE); }
private void protocolViolation(ChannelHandlerContext ctx, CorruptedFrameException ex) { state = State.CORRUPT; if (ctx.channel().isActive()) { Object closeMessage; if (receivedClosingHandshake) { closeMessage = Unpooled.EMPTY_BUFFER; } else { closeMessage = new CloseWebSocketFrame(1002, null); } ctx.writeAndFlush(closeMessage).addListener(ChannelFutureListener.CLOSE); } throw ex; }
@Override protected void channelRead0(ChannelHandlerContext ctx, MessageBase msg) { log.debug("User not logged. {}. Closing.", ctx.channel().remoteAddress()); if (msg instanceof RegisterMessage) { if (ctx.channel().isWritable()) { ctx.writeAndFlush(notAllowed(msg.id), ctx.voidPromise()); } } ctx.close(); }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { var response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
public CompletableFuture<MessageIdData> sendGetLastMessageId(ByteBuf request, long requestId) { CompletableFuture<MessageIdData> future = new CompletableFuture<>(); pendingGetLastMessageIdRequests.put(requestId, future); ctx.writeAndFlush(request).addListener(writeFuture -> { if (!writeFuture.isSuccess()) { log.warn("{} Failed to send GetLastMessageId request to broker: {}", ctx.channel(), writeFuture.cause().getMessage()); pendingGetLastMessageIdRequests.remove(requestId); future.completeExceptionally(writeFuture.cause()); } }); return future; }
public void sendHelloMessage(ChannelHandlerContext ctx, FrameCodec frameCodec, String nodeId) throws IOException, InterruptedException { final HelloMessage helloMessage = staticMessages.createHelloMessage(nodeId); ByteBuf byteBufMsg = ctx.alloc().buffer(); frameCodec.writeFrame(new FrameCodec.Frame(helloMessage.getCode(), helloMessage.getEncoded()), byteBufMsg); ctx.writeAndFlush(byteBufMsg).sync(); if (logger.isDebugEnabled()) logger.debug("To: {} Send: {}", ctx.channel().remoteAddress(), helloMessage); getNodeStatistics().rlpxOutHello.add(); }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { var response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
public CompletableFuture<List<String>> newGetTopicsOfNamespace(ByteBuf request, long requestId) { CompletableFuture<List<String>> future = new CompletableFuture<>(); pendingGetTopicsRequests.put(requestId, future); ctx.writeAndFlush(request).addListener(writeFuture -> { if (!writeFuture.isSuccess()) { log.warn("{} Failed to send request {} to broker: {}", ctx.channel(), requestId, writeFuture.cause().getMessage()); pendingGetTopicsRequests.remove(requestId); future.completeExceptionally(writeFuture.cause()); } }); return future; }