Example usage for io.netty.channel ChannelHandlerContext alloc

List of usage examples for io.netty.channel ChannelHandlerContext alloc

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext alloc.

Prototype

ByteBufAllocator alloc();

Source Link

Document

Return the assigned ByteBufAllocator which will be used to allocate ByteBuf s.

Usage

From source file:org.dcache.xrootd.protocol.messages.AbstractXrootdResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    int dlen = getDataLength();
    ByteBuf buffer = ctx.alloc().buffer(8 + dlen);
    try {//from  w w  w. java 2s .  c  o  m
        buffer.writeShort(request.getStreamId());
        buffer.writeShort(stat);
        buffer.writeInt(dlen);
        getBytes(buffer);
    } catch (Error | RuntimeException t) {
        promise.setFailure(t);
        buffer.release();
        return;
    } finally {
        ReferenceCountUtil.release(this);
    }
    ctx.write(buffer, promise);
}

From source file:org.dcache.xrootd.protocol.messages.AsyncResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, final ChannelPromise promise) {
    try {/* ww  w. ja  va  2 s. com*/
        int dlen = getDataLength();
        ByteBuf header = ctx.alloc().buffer(8 + dlen);
        try {
            header.writeShort(0);
            header.writeShort(kXR_attn);
            header.writeInt(dlen);
            header.writeInt(kXR_asynresp);
            header.writeInt(0);
        } catch (Error | RuntimeException t) {
            promise.setFailure(t);
            header.release();
            return;
        }
        ctx.write(header).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    promise.tryFailure(future.cause());
                }
            }
        });

        ChannelPromise channelPromise = ctx.newPromise();
        channelPromise.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    promise.trySuccess();
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        });
        ReferenceCountUtil.retain(response).writeTo(ctx, channelPromise);
    } finally {
        release();
    }
}

From source file:org.dcache.xrootd.protocol.messages.ReadResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    checkState(refCnt() > 0);// w w  w  .j  av  a 2s .c o  m

    ByteBuf header = ctx.alloc().buffer(8);
    header.writeShort(request.getStreamId());
    header.writeShort(stat);
    header.writeInt(data.readableBytes());

    ctx.write(ctx.alloc().compositeBuffer(2).addComponents(header, data).writerIndex(8 + data.readableBytes()),
            promise);
}

From source file:org.dcache.xrootd.protocol.messages.ReadVResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    checkState(refCnt() > 0);/*from   ww w.j a  v a2s .c om*/

    CompositeByteBuf buffer = ctx.alloc().compositeBuffer(2 * length + 1);

    ByteBuf header = ctx.alloc().buffer(8);
    header.writeShort(request.getStreamId());
    header.writeShort(stat);
    header.writeInt(getDataLength());
    buffer.addComponent(header);

    for (int i = 0; i < length; i++) {
        header = ctx.alloc().buffer(READ_LIST_HEADER_SIZE);
        header.writeInt(requests[index + i].getFileHandle());
        header.writeInt(data[index + i].readableBytes());
        header.writeLong(requests[index + i].getOffset());
        buffer.addComponent(header);
        buffer.addComponent(data[index + i].retain());
    }

    buffer.writerIndex(buffer.capacity());
    ctx.write(buffer, promise);

    release();
}

From source file:org.dcache.xrootd.protocol.messages.ZeroCopyReadResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, final ChannelPromise promise) {
    ByteBuf header = ctx.alloc().buffer(8);
    header.writeShort(request.getStreamId());
    header.writeShort(kXR_ok);// www  . j ava  2s.  c  o m
    header.writeInt(count);
    ctx.write(header).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                promise.tryFailure(future.cause());
            }
        }
    });
    ctx.write(new DefaultFileRegion(file, request.getReadOffset(), count))
            .addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        promise.trySuccess();
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            });
}

From source file:org.dcache.xrootd.stream.ChunkedResponseWriteHandler.java

License:Open Source License

private boolean doFlush(final ChannelHandlerContext ctx) throws Exception {
    final Channel channel = ctx.channel();
    if (!channel.isActive()) {
        discard(null);/*w  w w.  java  2  s  .  c o  m*/
        return false;
    }
    boolean flushed = false;
    while (channel.isWritable()) {
        if (currentWrite == null) {
            currentWrite = queue.poll();
        }

        if (currentWrite == null) {
            break;
        }
        final PendingWrite currentWrite = this.currentWrite;
        final ChunkedResponse pendingMessage = currentWrite.msg;

        boolean endOfInput;
        Object message = null;
        try {
            message = pendingMessage.nextChunk(ctx.alloc());
            endOfInput = pendingMessage.isEndOfInput();
        } catch (final Throwable t) {
            this.currentWrite = null;

            if (message != null) {
                ReferenceCountUtil.release(message);
            }

            currentWrite.fail(t);
            break;
        }

        if (message == null) {
            // If message is null write an empty ByteBuf.
            // See https://github.com/netty/netty/issues/1671
            message = Unpooled.EMPTY_BUFFER;
        }

        final int amount = amount(message);
        ChannelFuture f = ctx.write(message);
        if (endOfInput) {
            this.currentWrite = null;

            // Register a listener which will close the input once the write is complete.
            // This is needed because the Chunk may have some resource bound that can not
            // be closed before its not written.
            //
            // See https://github.com/netty/netty/issues/303
            f.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        currentWrite.fail(future.cause());
                    } else {
                        currentWrite.progress(amount);
                        currentWrite.success();
                    }
                }
            });
        } else {
            f.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        currentWrite.fail(future.cause());
                    } else {
                        currentWrite.progress(amount);
                    }
                }
            });
        }

        // Always need to flush
        ctx.flush();
        flushed = true;

        if (!channel.isActive()) {
            discard(new ClosedChannelException());
            break;
        }
    }
    return flushed;

}

From source file:org.dcache.xrootd.tpc.protocol.messages.AbstractXrootdOutboundRequest.java

License:Open Source License

public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    ByteBuf buffer = ctx.alloc().buffer(4 + getParamsLen());
    try {//from  w ww  .  java  2  s  .  c o m
        writeToBuffer(buffer);
    } catch (Error | RuntimeException t) {
        promise.setFailure(t);
        buffer.release();
        return;
    }
    ctx.write(buffer, promise);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundHandshakeRequest.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    ByteBuf buffer = ctx.alloc().buffer(XrootdProtocol.CLIENT_HANDSHAKE_LEN);
    try {//ww w  . j  av  a2s .c  o  m
        buffer.writeBytes(XrootdProtocol.HANDSHAKE_REQUEST);
    } catch (Error | RuntimeException t) {
        promise.setFailure(t);
        buffer.release();
        return;
    }

    ctx.write(buffer, promise);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundProtocolRequest.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    ByteBuf buffer = ctx.alloc().buffer(24);
    try {/*from www  .j a v a  2 s.c o  m*/
        buffer.writeShort(streamId);
        buffer.writeShort(kXR_protocol);
        buffer.writeInt(version);
        buffer.writeBytes(RESERVED);
        buffer.writeByte(kXR_secreqs);
        buffer.writeInt(0);
    } catch (Error | RuntimeException t) {
        promise.setFailure(t);
        buffer.release();
        return;
    }

    ctx.write(buffer, promise);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.OutboundSigverRequest.java

License:Open Source License

/**
 *  A signature consists of a SHA-256 hash of
 *    1. an unsigned 64-bit sequence number,
 *    2. the request header, and//from  w w  w. j a  v  a2 s.c om
 *    3. the request payload,
 *  in that exact order.
 */
private byte[] getSignature(AbstractXrootdOutboundRequest request, ChannelHandlerContext ctx)
        throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    ByteBuf buffer = ctx.alloc().buffer(12 + request.getParamsLen());
    try {
        buffer.writeLong(seqno);
        request.writeToBuffer(buffer);
        byte[] contents = new byte[buffer.readableBytes()];
        buffer.getBytes(0, contents);
        return digest.digest(contents);
    } finally {
        buffer.release();
    }
}