Search in sources :

Example 11 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project neo4j by neo4j.

the class BoltProtocolV1Test method newChannelMock.

private static Channel newChannelMock() {
    Channel channel = mock(Channel.class);
    ByteBufAllocator allocator = mock(ByteBufAllocator.class, RETURNS_MOCKS);
    when(channel.alloc()).thenReturn(allocator);
    return channel;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Channel(io.netty.channel.Channel)

Example 12 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method beforeMethod.

@Before
public void beforeMethod() {
    socketChannelMock = mock(SocketChannel.class);
    channelPipelineMock = mock(ChannelPipeline.class);
    ByteBufAllocator byteBufAllocatorMock = mock(ByteBufAllocator.class);
    doReturn(channelPipelineMock).when(socketChannelMock).pipeline();
    doReturn(byteBufAllocatorMock).when(socketChannelMock).alloc();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ChannelPipeline(io.netty.channel.ChannelPipeline) Before(org.junit.Before)

Example 13 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project Glowstone by GlowstoneMC.

the class QueryTest method testChannelRead.

private void testChannelRead(QueryHandler handler, byte[] recv, byte[] send) throws Exception {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ByteBufAllocator alloc = mock(ByteBufAllocator.class);
    when(ctx.alloc()).thenReturn(alloc);
    when(alloc.buffer()).thenReturn(Unpooled.buffer());
    DatagramPacket packet = new DatagramPacket(Unpooled.wrappedBuffer(recv), null, address);
    handler.channelRead(ctx, packet);
    verify(ctx).write(argThat(new DatagramPacketMatcher(send)));
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) DatagramPacket(io.netty.channel.socket.DatagramPacket) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 14 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project spring-framework by spring-projects.

the class RxNettyWebSocketClient method executeInternal.

@SuppressWarnings("cast")
private Observable<Void> executeInternal(URI url, HttpHeaders headers, WebSocketHandler handler) {
    String[] protocols = beforeHandshake(url, headers, handler);
    return createRequest(url, headers, protocols).flatMap(response -> {
        Observable<WebSocketConnection> conn = response.getWebSocketConnection();
        return (Observable<Tuple2<WebSocketResponse<ByteBuf>, WebSocketConnection>>) Observable.zip(Observable.just(response), conn, Tuples::of);
    }).flatMap(tuple -> {
        WebSocketResponse<ByteBuf> response = tuple.getT1();
        WebSocketConnection conn = tuple.getT2();
        HandshakeInfo info = afterHandshake(url, toHttpHeaders(response));
        ByteBufAllocator allocator = response.unsafeNettyChannel().alloc();
        NettyDataBufferFactory factory = new NettyDataBufferFactory(allocator);
        RxNettyWebSocketSession session = new RxNettyWebSocketSession(conn, info, factory);
        session.aggregateFrames(response.unsafeNettyChannel(), WsClientDecoder.getName());
        return RxReactiveStreams.toObservable(handler.handle(session));
    });
}
Also used : NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Observable(rx.Observable) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) RxEventLoopProvider(io.reactivex.netty.threads.RxEventLoopProvider) WebSocketResponse(io.reactivex.netty.protocol.http.ws.client.WebSocketResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) HttpHeaders(org.springframework.http.HttpHeaders) ObjectUtils(org.springframework.util.ObjectUtils) RxNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession) WebSocketConnection(io.reactivex.netty.protocol.http.ws.WebSocketConnection) Mono(reactor.core.publisher.Mono) WebSocketRequest(io.reactivex.netty.protocol.http.ws.client.WebSocketRequest) List(java.util.List) RxReactiveStreams(rx.RxReactiveStreams) WsClientDecoder(io.reactivex.netty.protocol.http.HttpHandlerNames.WsClientDecoder) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) WebSocketConnection(io.reactivex.netty.protocol.http.ws.WebSocketConnection) RxNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession) ByteBuf(io.netty.buffer.ByteBuf) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) Observable(rx.Observable) Tuple2(reactor.util.function.Tuple2) Tuples(reactor.util.function.Tuples) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 15 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project netty by netty.

the class SslHandler method wrap.

// This method will not call setHandshakeFailure(...) !
private void wrap(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLException {
    ByteBuf out = null;
    ChannelPromise promise = null;
    ByteBufAllocator alloc = ctx.alloc();
    boolean needUnwrap = false;
    try {
        // See https://github.com/netty/netty/issues/5860
        while (!ctx.isRemoved()) {
            Object msg = pendingUnencryptedWrites.current();
            if (msg == null) {
                break;
            }
            ByteBuf buf = (ByteBuf) msg;
            if (out == null) {
                out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.nioBufferCount());
            }
            SSLEngineResult result = wrap(alloc, engine, buf, out);
            if (result.getStatus() == Status.CLOSED) {
                // SSLEngine has been closed already.
                // Any further write attempts should be denied.
                pendingUnencryptedWrites.removeAndFailAll(SSLENGINE_CLOSED);
                return;
            } else {
                if (!buf.isReadable()) {
                    promise = pendingUnencryptedWrites.remove();
                } else {
                    promise = null;
                }
                switch(result.getHandshakeStatus()) {
                    case NEED_TASK:
                        runDelegatedTasks();
                        break;
                    case FINISHED:
                        setHandshakeSuccess();
                    // deliberate fall-through
                    case NOT_HANDSHAKING:
                        setHandshakeSuccessIfStillHandshaking();
                    // deliberate fall-through
                    case NEED_WRAP:
                        finishWrap(ctx, out, promise, inUnwrap, false);
                        promise = null;
                        out = null;
                        break;
                    case NEED_UNWRAP:
                        needUnwrap = true;
                        return;
                    default:
                        throw new IllegalStateException("Unknown handshake status: " + result.getHandshakeStatus());
                }
            }
        }
    } finally {
        finishWrap(ctx, out, promise, inUnwrap, needUnwrap);
    }
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) SSLEngineResult(javax.net.ssl.SSLEngineResult) ChannelPromise(io.netty.channel.ChannelPromise) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)27 ByteBuf (io.netty.buffer.ByteBuf)12 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 URI (java.net.URI)3 Test (org.junit.Test)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Publisher (org.reactivestreams.Publisher)2 Context (ratpack.handling.Context)2 Streams (ratpack.stream.Streams)2 MetricFilter (com.codahale.metrics.MetricFilter)1