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.beaconmc.network.socket.pipeline.PacketCompression.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects)
        throws Exception {

    int index = byteBuf.readerIndex();

    PacketSerializer inPacketSerializer = new PacketSerializer(byteBuf);

    int uncompressedSize = inPacketSerializer.readVarInt();

    if (uncompressedSize == 0) {
        int length = byteBuf.readableBytes();
        if (length >= threshold) {
            throw new IllegalArgumentException("Received uncompressed packet");
        }/* www .j a va2  s .  c o m*/

        ByteBuf outByteBuf = channelHandlerContext.alloc().buffer(length);
        byteBuf.readBytes(outByteBuf, length);
        objects.add(outByteBuf);
    } else {
        byte[] source = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(source);
        this.inflater.setInput(source);

        byte[] output = new byte[uncompressedSize];
        int resultLength = this.inflater.inflate(output);
        this.inflater.reset();

        if (resultLength == 0) {
            byteBuf.readerIndex(index);
            byteBuf.retain();
            objects.add(byteBuf);
        } else if (resultLength != uncompressedSize) {
            throw new IllegalStateException("Received compressed packet with invalid length");
        } else {
            objects.add(Unpooled.wrappedBuffer(output));
        }

    }

}

From source file:org.beaconmc.network.socket.pipeline.PacketEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, PacketOut in, List<Object> out) throws Exception {
    Integer id = protocol.getId(in.getClass(), ProtocolDirection.OUT);

    if (id == null) {
        ErrorStream.handle(/*from  w w w  .  ja  v a2s. c om*/
                new IOException("Tried to serialize unregistered packet " + in.getClass().getSimpleName()));
    } else {
        //DebugStream.handle("Packet id "+id);
        ByteBuf buf = ctx.alloc().buffer();
        PacketSerializer packetSerializer = new PacketSerializer(buf);
        packetSerializer.writeVarInt(id);
        in.write(packetSerializer, this.protocolVersion);
        out.add(packetSerializer.getByteBuf());
    }
}

From source file:org.beaconmc.network.socket.pipeline.PacketFraming.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    in.markReaderIndex();/* ww  w  .  j  a v  a 2 s .  c om*/
    if (!readableVarInt(in)) {
        return;
    }
    int length = readVarInt(in);
    if (in.readableBytes() < length) {
        in.resetReaderIndex();
        return;
    }
    ByteBuf buf = ctx.alloc().buffer(length);
    in.readBytes(buf, length);
    out.add(buf);
}

From source file:org.bermuda.cartilage.core.ConnectionController.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    final ByteBuf message = ctx.alloc().buffer(4);
    message.writeInt(12345);/*from  ww w.j a  v  a2  s. c o m*/
    final ChannelFuture future = ctx.writeAndFlush(message);
    future.addListener(f -> {
        if (f.isSuccess()) {
            session = new UserSession(ctx.channel());
            logger.info("Message sent");
        }
    });
}

From source file:org.bermuda.cartilage.core.PacketEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    ByteBuf buf = ctx.alloc().buffer(2);
    byte[] data = new BigInteger((String) msg).modPow(Stream.getRSAExponent(), Stream.getRSAModulus())
            .toByteArray();//from   ww  w.  j ava2 s .  c o m
    buf.writeBytes(data);
    ctx.write(buf, promise);
}

From source file:org.bridje.http.impl.HttpServerChannelHandler.java

License:Apache License

private void handleRequest(ChannelHandlerContext ctx) throws IOException {
    if (resp == null) {
        resp = new HttpBridletResponseImpl(ctx.alloc().buffer());
    }//  w w w .  j  a va  2  s . c o m
    RootHttpBridlet rootHandler = Ioc.context().find(RootHttpBridlet.class);
    context.set(HttpBridletRequest.class, req);
    context.set(HttpBridletResponse.class, resp);
    rootHandler.handle(context);
    sendResponse(ctx);
}

From source file:org.ch24.networking.game.client.DiscardClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    this.ctx = ctx;
    this.cnt = 1;
    this.exponent = 1;

    // Initialize the message.
    //        content = ctx.alloc().directBuffer(DiscardClient.SIZE).writeZero(DiscardClient.SIZE).writeBytes("alma".getBytes());
    content = ctx.alloc().buffer();

    // Send the initial messages.
    generateTraffic();//from  w w  w.  j  a v  a 2s . com
    //sendMsg();
}

From source file:org.clitherproject.clither.server.net.PacketEncoder.java

License:Open Source License

@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
@Override/*from w  w  w . ja v  a2  s .com*/
protected void encode(ChannelHandlerContext ctx, Packet packet, List out) throws Exception {
    ByteBuf buf = ctx.alloc().buffer().order(ByteOrder.BIG_ENDIAN);
    int packetId = PacketRegistry.CLIENTBOUND.getPacketId(packet.getClass());
    if (packetId == -1) {
        throw new IllegalArgumentException("Provided packet is not registered as a clientbound packet!");
    }

    buf.writeByte(packetId);
    packet.writeData(buf);
    out.add(new BinaryWebSocketFrame(buf));

    ClitherServer.log.finest("Sent packet " + " (" + packet.getClass().getSimpleName() + ") to "
            + ctx.channel().remoteAddress());
}

From source file:org.codice.alliance.video.stream.mpegts.netty.RawUdpDataToMTSPacketDecoder.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    byteBuf = ctx.alloc().buffer(BUFFER_SIZE);
}

From source file:org.dcache.xrootd.core.XrootdSigverDecoder.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//ww w.ja va 2  s.com
 *    3. the request payload,
 *  in that exact order.
 *
 *  In this case, 2 + 3 are given in order by the frame buffer, which
 *  contains the raw bytes of the request.
 */
private byte[] generateHash(long seqno, byte[] payload, ChannelHandlerContext ctx) throws XrootdException {
    ByteBuf buffer = ctx.alloc().buffer(8 + payload.length);
    try {
        buffer.writeLong(seqno);
        buffer.writeBytes(payload);
        byte[] contents = new byte[buffer.readableBytes()];
        buffer.getBytes(0, contents);

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(contents);
    } catch (NoSuchAlgorithmException e) {
        throw new XrootdException(kXR_error, e.toString());
    } finally {
        buffer.release();
    }
}