View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.uring;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.channel.ChannelConfig;
21  import io.netty.channel.RecvByteBufAllocator;
22  import io.netty.channel.unix.PreferredDirectByteBufAllocator;
23  import io.netty.util.UncheckedBooleanSupplier;
24  
25  final class IoUringRecvByteAllocatorHandle extends RecvByteBufAllocator.DelegatingHandle
26          implements RecvByteBufAllocator.ExtendedHandle {
27      private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
28              new PreferredDirectByteBufAllocator();
29  
30      IoUringRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) {
31          super(handle);
32      }
33  
34      private boolean rdHupReceived;
35      private boolean readComplete;
36  
37      @Override
38      public void reset(ChannelConfig config) {
39          super.reset(config);
40          readComplete = false;
41      }
42  
43      void rdHupReceived() {
44          this.rdHupReceived = true;
45      }
46  
47      @Override
48      public ByteBuf allocate(ByteBufAllocator alloc) {
49          // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
50          preferredDirectByteBufAllocator.updateAllocator(alloc);
51          return delegate().allocate(preferredDirectByteBufAllocator);
52      }
53  
54      @Override
55      public boolean continueReading() {
56          // If we received an POLLRDHUP we need to continue draining the input until there is nothing left.
57          return super.continueReading() || rdHupReceived;
58      }
59  
60      @Override
61      public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
62          // If we received an POLLRDHUP we need to continue draining the input until there is nothing left.
63          return ((RecvByteBufAllocator.ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier)
64                  || rdHupReceived;
65      }
66  
67      @Override
68      public void readComplete() {
69          super.readComplete();
70          readComplete = true;
71      }
72  
73      boolean isReadComplete() {
74          return readComplete;
75      }
76  }