Java Code Examples for io.netty.channel.ChannelHandlerContext.write()

The following are Jave code examples for showing how to use write() of the io.netty.channel.ChannelHandlerContext class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: wechat-mall   File: OAuth2ServerHandler.java   View Source Code Vote up 6 votes
@Override
public void processMessageReceived(ChannelHandlerContext ctx, RemotingCommand msg) {
    HashMap<String, String> map = msg.getExtFields();
    String key = map.get("action");
    switch (key) {
        case "authorize":
            auth(ctx, msg);
            break;

        case "token":
            token(ctx, msg);
            break;

        case "access":
            access(ctx, msg);
            break;

        default:
            break;
    }
    ctx.write(msg);
}
 
Example 2
Project: CustomWorldGen   File: NetworkDispatcher.java   View Source Code Vote up 6 votes
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
{
    if (msg instanceof FMLProxyPacket)
    {
        if (side == Side.CLIENT)
        {
            //Client to server large packets are not supported to prevent client being bad.
            ctx.write(((FMLProxyPacket) msg).toC17Packet(), promise);
        }
        else
        {
            List<Packet<INetHandlerPlayClient>> parts = ((FMLProxyPacket)msg).toS3FPackets();
            for (Packet<INetHandlerPlayClient> pkt : parts)
            {
                ctx.write(pkt, promise);
            }
        }
    }
    else
    {
        ctx.write(msg, promise);
    }
}
 
Example 3
Project: neoscada   File: MessageChannel.java   View Source Code Vote up 6 votes
private void writeMessageToChannel ( final ChannelHandlerContext ctx, final ByteBuf data, final ChannelPromise promise )
{
    final int seq = this.ackBuffer.addMessage ( data );

    if ( promise == null )
    {
        ctx.write ( new InformationTransfer ( seq, this.receiveCounter, data ) );
    }
    else
    {
        ctx.write ( new InformationTransfer ( seq, this.receiveCounter, data ), promise );
    }

    logger.trace ( "Enqueued message as {} : {}", seq, data );

    // we can stop timer #2 here ... will be restarted by receive
    this.timer2.stop ();
}
 
Example 4
Project: neoscada   File: FrameProcessor.java   View Source Code Vote up 6 votes
@Override
public void write ( final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise ) throws Exception
{
    if ( msg instanceof OpenResponse )
    {
        writeOpenResponse ( ctx, (OpenResponse)msg, promise );
    }
    else if ( msg instanceof SyslogResponse )
    {
        writeGenericResponse ( ctx, (AbstractGenericResponse)msg, promise );
    }
    else if ( msg instanceof ServerCloseMessage )
    {
        ctx.write ( new Frame ( 0, "serverclose", (ByteBuf)null ) );
    }
    else
    {
        super.write ( ctx, msg, promise );
    }
}
 
Example 5
Project: tools   File: HandshakePhase.java   View Source Code Vote up 5 votes
private void sendHandshakeOptionHagglingReply(ChannelHandlerContext ctx, int option, int error, ByteBuf data) throws IOException {
    ByteBuf optionReply = ctx.alloc().buffer(18);
    optionReply.writeLong(Protocol.HANDSHAKE_REPLY_MAGIC);
    optionReply.writeInt(option);
    optionReply.writeInt(error); // aka. reply type
    int len = 0;
    if (data != null)
        len = data.readableBytes();
    optionReply.writeInt(len);
    ctx.write(optionReply);
    if (data != null)
        ctx.write(data);
}
 
Example 6
Project: neoscada   File: MessageChannel.java   View Source Code Vote up 5 votes
@Override
public void write ( final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise ) throws Exception
{
    logger.trace ( "Write {}", msg );
    synchronized ( this )
    {
        if ( msg instanceof DataTransmissionMessage )
        {
            switch ( (DataTransmissionMessage)msg )
            {
                case REQUEST_START:
                    ctx.write ( new UnnumberedControl ( Function.STARTDT_ACT ), promise );
                    break;
                case CONFIRM_START:
                    ctx.write ( new UnnumberedControl ( Function.STARTDT_CONFIRM ), promise );
                    break;
                case REQUEST_STOP:
                    ctx.write ( new UnnumberedControl ( Function.STOPDT_ACT ), promise );
                    break;
                case CONFIRM_STOP:
                    ctx.write ( new UnnumberedControl ( Function.STOPDT_CONFIRM ), promise );
                    break;
                default:
                    throw new EncoderException ( String.format ( "Unknown data transmission message: %s", msg ) );
            }
        }
        else if ( msg == MessageSource.NOTIFY_TOKEN )
        {
            handleMessageSourceUpdates ( ctx );
        }
        else
        {
            handleMessageWrite ( ctx, msg, promise );
        }
    }
}
 
Example 7
Project: NSS   File: SocksServerHandler.java   View Source Code Vote up 5 votes
@Override
public void channelRead0(ChannelHandlerContext ctx,
		SocksRequest socksRequest) throws Exception {
	switch (socksRequest.requestType()) {
	case INIT: {
		logger.info("localserver init");
		ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
		ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH));
		break;
	}
	case AUTH:
		ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
		ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS));
		break;
	case CMD:
		SocksCmdRequest req = (SocksCmdRequest) socksRequest;
		if (req.cmdType() == SocksCmdType.CONNECT) {
			logger.info("localserver connect");
			ctx.pipeline().addLast(new SocksServerConnectHandler(config));
			ctx.pipeline().remove(this);
			ctx.fireChannelRead(socksRequest);
		} else {
			ctx.close();
		}
		break;
	case UNKNOWN:
		ctx.close();
		break;
	}
}
 
Example 8
Project: message-broker   File: AmqpMessageWriter.java   View Source Code Vote up 5 votes
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof AmqpDeliverMessage) {
        ((AmqpDeliverMessage) msg).write(ctx);
    } else  {
        ctx.write(msg, promise);
    }
}
 
Example 9
Project: NettyStudy   File: EchoServerHandler.java   View Source Code Vote up 5 votes
@Override
public void channelRead(ChannelHandlerContext ctx,
    Object msg) {
    ByteBuf in = (ByteBuf) msg;
    System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8));
    ctx.write(in);
}
 
Example 10
Project: drift   File: ThriftClientHandler.java   View Source Code Vote up 5 votes
@Override
public void write(ChannelHandlerContext ctx, Object message, ChannelPromise promise)
        throws Exception
{
    if (message instanceof ThriftRequest) {
        ThriftRequest thriftRequest = (ThriftRequest) message;
        sendMessage(ctx, thriftRequest, promise);
    }
    else {
        ctx.write(message, promise);
    }
}
 
Example 11
Project: Quavo   File: HandshakeListener.java   View Source Code Vote up 5 votes
@Override
public void handleMessage(ChannelHandlerContext ctx, HandshakeRequest msg) {
	ClientMessage message = ClientMessage.SUCCESSFUL_CONNECTION;
	if (msg.getVersion() != Quavo.VERSION) {
		message = ClientMessage.OUT_OF_DATE;
	}

	ctx.write(new HandshakeResponse(message));
}
 
Example 12
Project: Ink   File: HttpRequestHandler.java   View Source Code Vote up 5 votes
private void handleException(ChannelHandlerContext ctx, Exception e) {
    if (e instanceof UnauthorizedException) {
        ctx.write(Response.buildDefaultFullHttpResponse(HttpResponseStatus.UNAUTHORIZED));
    }
    else if (e instanceof ForbiddenException) {
        ctx.write(Response.buildDefaultFullHttpResponse(HttpResponseStatus.FORBIDDEN));
    }
    else {
        ctx.write(Response.buildDefaultFullHttpResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR));
    }
}
 
Example 13
Project: hekate   File: NettyClientIdleStateHandler.java   View Source Code Vote up 5 votes
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof Heartbeat) {
        ctx.write(msg, promise);
    } else {
        super.write(ctx, msg, promise);
    }
}
 
Example 14
Project: CustomWorldGen   File: FMLOutboundHandler.java   View Source Code Vote up 4 votes
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
{
    if (!(msg instanceof FMLProxyPacket))
    {
        return;
    }
    FMLProxyPacket pkt = (FMLProxyPacket) msg;
    OutboundTarget outboundTarget;
    Object args = null;
    NetworkDispatcher dispatcher = ctx.channel().attr(NetworkDispatcher.FML_DISPATCHER).get();
    // INTERNAL message callback - let it pass out
    if (dispatcher != null)
    {
        ctx.write(msg, promise);
        return;
    }

    outboundTarget = ctx.channel().attr(FML_MESSAGETARGET).get();
    Side channelSide = ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get();
    if (outboundTarget != null && outboundTarget.allowed.contains(channelSide))
    {
        args = ctx.channel().attr(FML_MESSAGETARGETARGS).get();
        outboundTarget.validateArgs(args);
    }
    else if (channelSide == Side.CLIENT)
    {
        outboundTarget = OutboundTarget.TOSERVER;
    }
    else
    {
        throw new FMLNetworkException("Packet arrived at the outbound handler without a valid target!");
    }

    List<NetworkDispatcher> dispatchers = outboundTarget.selectNetworks(args, ctx, pkt);

    // This will drop the messages into the output queue at the embedded channel
    if (dispatchers == null)
    {
        ctx.write(msg, promise);
        return;
    }
    for (NetworkDispatcher targetDispatcher : dispatchers)
    {
        targetDispatcher.sendProxy((FMLProxyPacket) msg);
    }
}
 
Example 15
Project: Quavo   File: XOREncryptionListener.java   View Source Code Vote up 4 votes
@Override
public void handleMessage(ChannelHandlerContext ctx, XOREncryptionRequest msg) {
	ctx.write(new XOREncryptionResponse(msg.getKey()));
}
 
Example 16
Project: ss-java   File: ShadowsocksMessageCodec.java   View Source Code Vote up 4 votes
@Override
public void write(ChannelHandlerContext ctx, Object msg,
        ChannelPromise promise) throws Exception {
    ctx.write(crypto.encrypt((ByteBuf) msg), promise);
}
 
Example 17
Project: xrpc   File: MessageLogger.java   View Source Code Vote up 4 votes
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
    throws Exception {
  this.logMessageDebug(ctx, "WRITE", msg);
  ctx.write(msg, promise);
}
 
Example 18
Project: Ink   File: HttpRequestHandler.java   View Source Code Vote up 4 votes
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {


    if (fullHttpRequest == null) {
        return;
    }

    log.info("Request [{}]", fullHttpRequest.uri());


    Request request = new Request(channelHandlerContext.channel(), fullHttpRequest);
    WebContext.setCurrentRequest(request);


    Response preparedResponse = new Response(request);
    WebContext.setCurrentResponse(preparedResponse);


    Route route = null;
    try {
        route = RouteFinder.findRoute(request);
        if (route == null) {
            HttpResponse exceptionResponse = Response.buildDefaultFullHttpResponse(HttpResponseStatus.NOT_FOUND);
            channelHandlerContext.write(exceptionResponse);
            return;
        }
        RouteSetter.routeSetter(route, fullHttpRequest);
    } catch (Exception e) {
        handleException(channelHandlerContext, e);
        return;
    }

    Boolean continueProcess = true;
    //before aop
    if (route.beforeProxyChain().size() != 0) {
        continueProcess = route.beforeProxyChain().doChain(request, preparedResponse, route);
    }

    if (continueProcess) {
        Object o = route.getMethod().invoke(route.getObject(), route.getParamters());
        //after aop
        if (route.afterProxyChain().size() != 0) {
            route.afterProxyChain().doChain(request, preparedResponse, route);
        }

        if (o instanceof Response) {
            preparedResponse = Response.mergeResponse(preparedResponse, (Response) o);
            WebContext.setCurrentResponse(preparedResponse);
        }
        else if (route.view()) {
            preparedResponse.setFile(resolver.resolve(o.toString(), preparedResponse.getModel()));
            preparedResponse.setResponseStatus(HttpResponseStatus.OK);
            preparedResponse.header(HttpHeader.CONTENT_TYPE, "text/html");
        }
        else  {
            preparedResponse.setBody(o);
            preparedResponse.setResponseStatus(HttpResponseStatus.OK);
        }
        log.info("Response {{}}", preparedResponse.body());
    }

    HttpResponse response = Response.buildDefaultFullHttpResponse0();
    channelHandlerContext.write(response);
    if (WebContext.currentResponse().file() != null) {
        channelHandlerContext.write(new HttpChunkedInput(new ChunkedFile(WebContext.currentResponse().file())));
    }
}
 
Example 19
Project: spark_deep   File: EchoServerHandler.java   View Source Code Vote up 4 votes
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ctx.write(msg);
}
 
Example 20
Project: neoscada   File: FrameProcessor.java   View Source Code Vote up 4 votes
protected void writeGenericResponse ( final ChannelHandlerContext ctx, final AbstractGenericResponse msg, final ChannelPromise promise )
{
    ctx.write ( createCommonResponse ( msg.getTransactionId (), msg.getCode (), msg.getMessage (), null ) );
}