Search in sources :

Example 21 with ByteBuf

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

the class ReadCompletionHandler method completed.

@Override
public void completed(Integer result, AioSocketChannel channel) {
    try {
        if (result < 1) {
            if (result == 0) {
                channel.read(channel.getReadCache());
                return;
            }
            CloseUtil.close(channel);
            return;
        }
        channel.active();
        ByteBuf buf = channel.getReadCache();
        buf.reverse();
        buf.flip();
        byteBufReader.accept(channel, buf);
    } catch (Exception e) {
        failed(e, channel);
    }
    channel.read(channel.getReadCache());
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException)

Example 22 with ByteBuf

use of com.generallycloud.baseio.buffer.ByteBuf 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 23 with ByteBuf

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

the class Http2PrefaceFuture method read.

@Override
public boolean read(SocketChannel channel, ByteBuf buffer) throws IOException {
    ByteBuf buf = this.buf;
    if (!isComplete) {
        buf.read(buffer);
        if (buf.hasRemaining()) {
            return false;
        }
        this.isComplete = true;
        doComplete(channel, buf.flip());
    }
    return true;
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 24 with ByteBuf

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

the class SslFutureImpl method copy.

@Override
public SslFuture copy(SocketChannel channel) {
    ByteBuf src = getByteBuf();
    ByteBuf buf = allocate(channel, src.limit());
    buf.read(src.flip());
    SslFutureImpl copy = new SslFutureImpl(channel.getContext(), buf, 1024 * 64);
    copy.header_complete = this.header_complete;
    return copy;
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 25 with ByteBuf

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

the class TestSimplyByteBufAllocator method main.

public static void main(String[] args) throws Exception {
    int capacity = 100;
    int unit = 1;
    int max = unit * capacity / 5;
    allocator = new SimplyByteBufAllocator(capacity, unit, false);
    // allocator = new SimpleByteBufAllocator(capacity, unit, false);
    allocator.start();
    Runnable r = () -> {
        for (; ; ) {
            int random = new Random().nextInt(max);
            if (random == 0) {
                continue;
            }
            ByteBuf buf = allocator.allocate(random);
            if (buf == null) {
                System.out.println(buf + Thread.currentThread().getName());
            }
            ThreadUtil.sleep(new Random().nextInt(20));
            ReleaseUtil.release(buf);
            // String des = allocator.toString();
            // 
            // if (des.indexOf("free=100") == -1) {
            // System.out.println();
            // }
            // ThreadUtil.sleep(10);
            debug();
        }
    };
    ThreadUtil.execute(r);
    ThreadUtil.execute(r);
// ThreadUtil.execute(r);
// 
// ThreadUtil.execute(r);
}
Also used : SimplyByteBufAllocator(com.generallycloud.baseio.buffer.SimplyByteBufAllocator) Random(java.util.Random) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Aggregations

ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)31 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)9 IOException (java.io.IOException)9 EmptyByteBuf (com.generallycloud.baseio.buffer.EmptyByteBuf)4 UnpooledByteBufAllocator (com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)3 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)2 SslHandler (com.generallycloud.baseio.component.ssl.SslHandler)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 FixedUnpooledByteBuf (com.generallycloud.baseio.buffer.FixedUnpooledByteBuf)1 PooledByteBufAllocatorManager (com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager)1 SimplyByteBufAllocator (com.generallycloud.baseio.buffer.SimplyByteBufAllocator)1 CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)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