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.jfxvnc.net.rfb.codec.encoder.ClientCutTextEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, ClientCutText msg, List<Object> out) throws Exception {
    byte[] text = msg.getText().getBytes(StandardCharsets.ISO_8859_1);
    ByteBuf buf = ctx.alloc().buffer(8 + text.length);
    buf.writeByte(ClientEventType.CLIENT_CUT_TEXT);
    buf.writeZero(3);//from   www  .  j av  a2 s .  c  om
    buf.writeInt(text.length);
    buf.writeBytes(text);

    out.add(buf);
}

From source file:org.jfxvnc.net.rfb.codec.encoder.KeyButtonEventEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, KeyButtonEvent msg, ByteBuf out) throws Exception {
    ByteBuf buf = ctx.alloc().buffer(8);
    try {/*from w  ww  .j a va 2s .  c o  m*/
        buf.writeByte(ClientEventType.KEY_EVENT);
        buf.writeBoolean(msg.isDown());
        buf.writeZero(2);
        buf.writeInt(msg.getKey());
        out.writeBytes(buf);
    } finally {
        buf.release();
    }
}

From source file:org.jfxvnc.net.rfb.codec.encoder.PointerEventEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, PointerEvent msg, ByteBuf out) throws Exception {
    ByteBuf buf = ctx.alloc().buffer(6);
    try {//from  www .j a va 2s . c om
        buf.writeByte(ClientEventType.POINTER_EVENT);
        buf.writeByte(msg.getButtonMask());
        buf.writeShort(msg.getxPos());
        buf.writeShort(msg.getyPos());
        out.writeBytes(buf);
    } finally {
        buf.release();
    }
}

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandler.java

License:Apache License

public void sendFramebufferUpdateRequest(ChannelHandlerContext ctx, boolean incremental, int x, int y, int w,
        int h) {/*from   w ww  .java2s .c  om*/
    ByteBuf buf = ctx.alloc().buffer(10);
    buf.writeByte(ClientEventType.FRAMEBUFFER_UPDATE_REQUEST);
    buf.writeByte(incremental ? 1 : 0);

    buf.writeShort(x);
    buf.writeShort(y);
    buf.writeShort(w);
    buf.writeShort(h);

    ctx.writeAndFlush(buf);
}

From source file:org.jmqtt.core.codec.ConnectEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, ConnectPacket message, ByteBuf out) {
    ByteBuf staticHeaderBuff = chc.alloc().buffer(12);
    ByteBuf buff = chc.alloc().buffer();
    ByteBuf variableHeaderBuff = chc.alloc().buffer(12);
    try {/*from   w  ww.  j  av a 2s. com*/
        staticHeaderBuff.writeBytes(MqttUtils.encodeString("MQIsdp"));

        //version 
        staticHeaderBuff.writeByte(0x03);

        //connection flags and Strings
        byte connectionFlags = 0;
        if (message.isCleanSession()) {
            connectionFlags |= 0x02;
        }
        if (message.isWillFlag()) {
            connectionFlags |= 0x04;
        }
        connectionFlags |= ((message.getWillQos() & 0x03) << 3);
        if (message.isWillRetain()) {
            connectionFlags |= 0x020;
        }
        if (message.isPasswordFlag()) {
            connectionFlags |= 0x040;
        }
        if (message.isUserFlag()) {
            connectionFlags |= 0x080;
        }
        staticHeaderBuff.writeByte(connectionFlags);

        //Keep alive timer
        staticHeaderBuff.writeShort(message.getKeepAlive());

        //Variable part
        if (message.getClientId() != null) {
            variableHeaderBuff.writeBytes(MqttUtils.encodeString(message.getClientId()));
            if (message.isWillFlag()) {
                variableHeaderBuff.writeBytes(MqttUtils.encodeString(message.getWillTopic()));
                variableHeaderBuff.writeBytes(MqttUtils.encodeFixedLengthContent(message.getWillMessage()));
            }
            if (message.isUserFlag() && message.getUsername() != null) {
                variableHeaderBuff.writeBytes(MqttUtils.encodeString(message.getUsername()));
                if (message.isPasswordFlag() && message.getPassword() != null) {
                    variableHeaderBuff.writeBytes(MqttUtils.encodeFixedLengthContent(message.getPassword()));
                }
            }
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        buff.writeByte(AbstractPacket.CONNECT << 4);
        buff.writeBytes(MqttUtils.encodeRemainingLength(12 + variableHeaderSize));
        buff.writeBytes(staticHeaderBuff).writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        staticHeaderBuff.release();
        buff.release();
        variableHeaderBuff.release();
    }
}

From source file:org.jmqtt.core.codec.PubAckEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, PubAckPacket msg, ByteBuf out) {
    ByteBuf buff = chc.alloc().buffer(4);
    try {/*from ww w  .  j  a va 2s  .  c  o  m*/
        buff.writeByte(AbstractPacket.PUBACK << 4);
        buff.writeBytes(MqttUtils.encodeRemainingLength(2));
        buff.writeShort(msg.getPacketId());
        out.writeBytes(buff);
    } finally {
        buff.release();
    }
}

From source file:org.jmqtt.core.codec.PublishEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, PublishPacket message, ByteBuf out) {
    if (message.getQos() == QosType.RESERVED) {
        throw new IllegalArgumentException("Found a message with RESERVED Qos");
    }/*from  w  w  w  . jav  a  2 s.c o  m*/
    if (message.getTopicName() == null || message.getTopicName().isEmpty()) {
        throw new IllegalArgumentException("Found a message with empty or null topic name");
    }

    ByteBuf variableHeaderBuff = ctx.alloc().buffer(2);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeBytes(MqttUtils.encodeString(message.getTopicName()));
        if (message.getQos() == QosType.LEAST_ONE || message.getQos() == QosType.EXACTLY_ONCE) {
            if (message.getPacketId() == null) {
                throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted");
            }
            variableHeaderBuff.writeShort(message.getPacketId());
        }
        variableHeaderBuff.writeBytes(message.getPayload());
        int variableHeaderSize = variableHeaderBuff.readableBytes();

        byte flags = MqttUtils.encodeFlags(message);

        buff = ctx.alloc().buffer(2 + variableHeaderSize);
        buff.writeByte(AbstractPacket.PUBLISH << 4 | flags);
        buff.writeBytes(MqttUtils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);
        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        if (buff != null) {
            buff.release();
        }
    }
}

From source file:org.jmqtt.core.codec.SubAckEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, SubAckPacket message, ByteBuf out) {
    if (message.types().isEmpty()) {
        throw new IllegalArgumentException("Found a suback message with empty topics");
    }//  www  .  j  ava  2  s .  co  m

    int variableHeaderSize = 2 + message.types().size();
    ByteBuf buff = chc.alloc().buffer(6 + variableHeaderSize);
    try {
        buff.writeByte(AbstractPacket.SUBACK << 4);
        buff.writeBytes(MqttUtils.encodeRemainingLength(variableHeaderSize));
        buff.writeShort(message.getPacketId());
        for (QosType c : message.types()) {
            buff.writeByte(c.byteValue());
        }

        out.writeBytes(buff);
    } finally {
        buff.release();
    }
}

From source file:org.jmqtt.core.codec.SubscribeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, SubscribePacket message, ByteBuf out) {
    if (message.subscriptions().isEmpty()) {
        throw new IllegalArgumentException("Found a subscribe message with empty topics");
    }//from ww  w.ja va2s. c om

    if (message.getQos() != QosType.LEAST_ONE) {
        throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos());
    }

    ByteBuf variableHeaderBuff = chc.alloc().buffer(4);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeShort(message.getPacketId());
        for (SubscribePacket.Couple c : message.subscriptions()) {
            variableHeaderBuff.writeBytes(MqttUtils.encodeString(c.topicFilter));
            variableHeaderBuff.writeByte(c.qos);
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        byte flags = MqttUtils.encodeFlags(message);
        buff = chc.alloc().buffer(2 + variableHeaderSize);

        buff.writeByte(AbstractPacket.SUBSCRIBE << 4 | flags);
        buff.writeBytes(MqttUtils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        buff.release();
    }
}

From source file:org.jmqtt.core.codec.UnsubscribeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, UnsubscribePacket message, ByteBuf out) {
    if (message.topicFilters().isEmpty()) {
        throw new IllegalArgumentException("Found an unsubscribe message with empty topics");
    }/*w  w  w .j a  v a2s .  co m*/

    if (message.getQos() != QosType.LEAST_ONE) {
        throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos());
    }

    ByteBuf variableHeaderBuff = chc.alloc().buffer(4);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeShort(message.getPacketId());
        for (String topic : message.topicFilters()) {
            variableHeaderBuff.writeBytes(MqttUtils.encodeString(topic));
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        byte flags = MqttUtils.encodeFlags(message);
        buff = chc.alloc().buffer(2 + variableHeaderSize);

        buff.writeByte(AbstractPacket.UNSUBSCRIBE << 4 | flags);
        buff.writeBytes(MqttUtils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        buff.release();
    }
}