Search in sources :

Example 11 with ByteBuf

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

the class Http2WindowUpdateFrameImpl method read.

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

Example 12 with ByteBuf

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

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

the class SslHandler method wrap.

public ByteBuf wrap(SocketChannel channel, ByteBuf src) throws IOException {
    SSLEngine engine = channel.getSSLEngine();
    ByteBuf dst = getTempDst(engine);
    ByteBuf out = null;
    try {
        for (; ; ) {
            dst.clear();
            SSLEngineResult result = engine.wrap(src.nioBuffer(), dst.nioBuffer());
            Status status = result.getStatus();
            HandshakeStatus handshakeStatus = result.getHandshakeStatus();
            synchByteBuf(result, src, dst);
            if (status == Status.CLOSED) {
                return gc(channel, dst.flip());
            }
            if (handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) {
                if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
                    if (out != null) {
                        out.read(dst.flip());
                        return out.flip();
                    }
                    return gc(channel, dst.flip());
                } else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
                    if (out == null) {
                        out = allocate(channel, 256);
                    }
                    out.read(dst.flip());
                    continue;
                } else if (handshakeStatus == HandshakeStatus.FINISHED) {
                    channel.finishHandshake(null);
                    out.read(dst.flip());
                    return out.flip();
                } else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
                    runDelegatedTasks(engine);
                    continue;
                }
            }
            if (src.hasRemaining()) {
                if (out == null) {
                    int outLength = ((src.limit() / src.position()) + 1) * (dst.position() - src.position()) + src.limit();
                    out = allocate(channel, outLength);
                }
                out.read(dst.flip());
                continue;
            }
            if (out != null) {
                out.read(dst.flip());
                return out.flip();
            }
            return gc(channel, dst.flip());
        }
    } catch (Throwable e) {
        ReleaseUtil.release(out);
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new IOException(e);
    }
}
Also used : HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus) Status(javax.net.ssl.SSLEngineResult.Status) SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLEngine(javax.net.ssl.SSLEngine) IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) EmptyByteBuf(com.generallycloud.baseio.buffer.EmptyByteBuf) HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus)

Example 14 with ByteBuf

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

the class SslHandler method unwrap.

public ByteBuf unwrap(SocketChannel channel, ByteBuf src) throws IOException {
    SSLEngine sslEngine = channel.getSSLEngine();
    ByteBuf dst = getTempDst(sslEngine);
    for (; ; ) {
        dst.clear();
        SSLEngineResult result = sslEngine.unwrap(src.nioBuffer(), dst.nioBuffer());
        HandshakeStatus handshakeStatus = result.getHandshakeStatus();
        synchByteBuf(result, src, dst);
        if (handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) {
            if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
                channel.doFlush(forgeFuture.duplicate());
                return null;
            } else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
                runDelegatedTasks(sslEngine);
                continue;
            } else if (handshakeStatus == HandshakeStatus.FINISHED) {
                channel.finishHandshake(null);
                return null;
            } else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
                return null;
            }
        }
        return dst.flip();
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLEngine(javax.net.ssl.SSLEngine) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) EmptyByteBuf(com.generallycloud.baseio.buffer.EmptyByteBuf) HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus)

Example 15 with ByteBuf

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

the class SocketSelectorEventLoop method accept.

private void accept(NioSocketChannel channel) {
    try {
        ByteBuf buf = this.buf;
        buf.clear();
        buf.nioBuffer();
        int length = channel.read(buf);
        if (length < 1) {
            if (length == -1) {
                CloseUtil.close(channel);
            }
            return;
        }
        channel.active();
        byteBufReader.accept(channel, buf.flip());
    } catch (Throwable e) {
        if (e instanceof SSLHandshakeException) {
            // failed connect , the session should be null
            getSelector().finishConnect(null, e);
        }
        closeSocketChannel(channel, e);
    }
}
Also used : ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

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