Search in sources :

Example 6 with ByteBufAllocator

use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class ParamedProtobaseProtocolDecoder method decode.

@Override
public ChannelFuture decode(SocketChannel channel, ByteBuf buffer) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    ByteBuf buf = allocator.allocate(2);
    return new ParamedProtobaseFutureImpl(channel, buf);
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ParamedProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFutureImpl) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 7 with ByteBufAllocator

use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class SslHandler method getTempDst.

private ByteBuf getTempDst(SSLEngine engine) {
    if (tempDst == null) {
        synchronized (this) {
            if (tempDst != null) {
                return tempDst;
            }
            ByteBufAllocator allocator = UnpooledByteBufAllocator.getHeap();
            int packetBufferSize = engine.getSession().getPacketBufferSize();
            tempDst = allocator.allocate(packetBufferSize);
        }
    }
    return tempDst;
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) UnpooledByteBufAllocator(com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)

Example 8 with ByteBufAllocator

use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class ProtobaseProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    if (future.isHeartbeat()) {
        ByteBuf buf = future.isPING() ? PING.duplicate() : PONG.duplicate();
        future.setByteBuf(buf);
        return;
    }
    ProtobaseFuture f = (ProtobaseFuture) future;
    String futureName = f.getFutureName();
    if (StringUtil.isNullOrBlank(futureName)) {
        throw new ProtocolException("future name is empty");
    }
    byte[] futureNameBytes = futureName.getBytes(channel.getEncoding());
    if (futureNameBytes.length > Byte.MAX_VALUE) {
        throw new ProtocolException("future name max length 127");
    }
    byte futureNameLength = (byte) futureNameBytes.length;
    int allLen = 6 + futureNameLength;
    int textWriteSize = f.getWriteSize();
    int binaryWriteSize = f.getWriteBinarySize();
    byte h1 = 0b01000000;
    if (f.isBroadcast()) {
        h1 |= 0b00100000;
    }
    if (f.getFutureId() > 0) {
        h1 |= 0b00010000;
        allLen += 4;
    }
    if (f.getSessionId() > 0) {
        h1 |= 0b00001000;
        allLen += 4;
    }
    if (f.getHashCode() > 0) {
        h1 |= 0b00000100;
        allLen += 4;
    }
    if (f.getWriteBinarySize() > 0) {
        h1 |= 0b00000010;
        allLen += 4;
        allLen += f.getWriteBinarySize();
    }
    if (textWriteSize > 0) {
        allLen += textWriteSize;
    }
    ByteBuf buf = allocator.allocate(allLen);
    buf.putByte(h1);
    buf.putByte(futureNameLength);
    buf.putInt(textWriteSize);
    if (f.getFutureId() > 0) {
        buf.putInt(f.getFutureId());
    }
    if (f.getSessionId() > 0) {
        buf.putInt(f.getSessionId());
    }
    if (f.getHashCode() > 0) {
        buf.putInt(f.getHashCode());
    }
    if (binaryWriteSize > 0) {
        buf.putInt(binaryWriteSize);
    }
    buf.put(futureNameBytes);
    if (textWriteSize > 0) {
        buf.put(f.getWriteBuffer(), 0, textWriteSize);
    }
    if (binaryWriteSize > 0) {
        buf.put(f.getWriteBinary(), 0, binaryWriteSize);
    }
    future.setByteBuf(buf.flip());
}
Also used : UnpooledByteBufAllocator(com.generallycloud.baseio.buffer.UnpooledByteBufAllocator) ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ProtocolException(com.generallycloud.baseio.protocol.ProtocolException) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 9 with ByteBufAllocator

use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class ServerHTTPProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture readFuture) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    ServerHttpFuture f = (ServerHttpFuture) readFuture;
    if (f.isUpdateWebSocketProtocol()) {
        channel.setProtocolDecoder(WebSocketProtocolFactory.WS_PROTOCOL_DECODER);
        channel.setProtocolEncoder(WebSocketProtocolFactory.WS_PROTOCOL_ENCODER);
        channel.setProtocolFactory(WebSocketProtocolFactory.WS_PROTOCOL_FACTORY);
        channel.getSession().setAttribute(WebSocketFuture.SESSION_KEY_SERVICE_NAME, f.getFutureName());
    }
    f.setResponseHeader("Date", HttpHeaderDateFormat.getFormat().format(System.currentTimeMillis()));
    ByteArrayBuffer os = f.getBinaryBuffer();
    if (os != null) {
        encode(allocator, f, os.size(), os.array());
        return;
    }
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        encode(allocator, f, 0, null);
        return;
    }
    encode(allocator, f, writeSize, f.getWriteBuffer());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ServerHttpFuture(com.generallycloud.baseio.codec.http11.future.ServerHttpFuture) ByteArrayBuffer(com.generallycloud.baseio.component.ByteArrayBuffer)

Example 10 with ByteBufAllocator

use of com.generallycloud.baseio.buffer.ByteBufAllocator in project baseio by generallycloud.

the class WebSocketProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    WebSocketFuture f = (WebSocketFuture) future;
    byte[] header;
    byte[] data = f.getWriteBuffer();
    int size = f.getWriteSize();
    byte header0 = (byte) (0x8f & (f.getType() | 0xf0));
    if (size < 126) {
        header = new byte[2];
        header[0] = header0;
        header[1] = (byte) size;
    } else if (size <= MAX_UNSIGNED_SHORT) {
        header = new byte[4];
        header[0] = header0;
        header[1] = 126;
        MathUtil.unsignedShort2Byte(header, size, 2);
    } else {
        header = new byte[6];
        header[0] = header0;
        header[1] = 127;
        MathUtil.int2Byte(header, size, 2);
    }
    ByteBuf buf = allocator.allocate(header.length + size);
    buf.put(header);
    buf.put(data, 0, size);
    future.setByteBuf(buf.flip());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Aggregations

ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)11 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)9 UnpooledByteBufAllocator (com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)3 IOException (java.io.IOException)2 PooledByteBufAllocatorManager (com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager)1 CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)1 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)1 ServerHttpFuture (com.generallycloud.baseio.codec.http11.future.ServerHttpFuture)1 WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)1 Http2Frame (com.generallycloud.baseio.codec.http2.future.Http2Frame)1 Http2FrameType (com.generallycloud.baseio.codec.http2.future.Http2FrameType)1 Http2HeadersFrame (com.generallycloud.baseio.codec.http2.future.Http2HeadersFrame)1 Http2SettingsFrame (com.generallycloud.baseio.codec.http2.future.Http2SettingsFrame)1 ParamedProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFutureImpl)1 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)1 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)1 ByteArrayBuffer (com.generallycloud.baseio.component.ByteArrayBuffer)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1