查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2015 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.epoll;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
21  import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
22  import io.netty.channel.unix.PreferredDirectByteBufAllocator;
23  import io.netty.util.UncheckedBooleanSupplier;
24  
25  class EpollRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
26      private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
27              new PreferredDirectByteBufAllocator();
28      private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = new UncheckedBooleanSupplier() {
29          @Override
30          public boolean get() {
31              return maybeMoreDataToRead();
32          }
33      };
34      private boolean isEdgeTriggered;
35      private boolean receivedRdHup;
36  
37      EpollRecvByteAllocatorHandle(ExtendedHandle handle) {
38          super(handle);
39      }
40  
41      final void receivedRdHup() {
42          receivedRdHup = true;
43      }
44  
45      final boolean isReceivedRdHup() {
46          return receivedRdHup;
47      }
48  
49      boolean maybeMoreDataToRead() {
50          /**
51           * EPOLL ET requires that we read until we get an EAGAIN
52           * (see Q9 in <a href="https://man7.org/linux/man-pages/man7/epoll.7.html">epoll man</a>). However in order to
53           * respect auto read we supporting reading to stop if auto read is off. It is expected that the
54           * {@link #EpollSocketChannel} implementations will track if we are in edgeTriggered mode and all data was not
55           * read, and will force a EPOLLIN ready event.
56           *
57           * It is assumed RDHUP is handled externally by checking {@link #isReceivedRdHup()}.
58           */
59          return (isEdgeTriggered && lastBytesRead() > 0) ||
60                 (!isEdgeTriggered && lastBytesRead() == attemptedBytesRead());
61      }
62  
63      final void edgeTriggered(boolean edgeTriggered) {
64          isEdgeTriggered = edgeTriggered;
65      }
66  
67      final boolean isEdgeTriggered() {
68          return isEdgeTriggered;
69      }
70  
71      @Override
72      public final ByteBuf allocate(ByteBufAllocator alloc) {
73          // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
74          preferredDirectByteBufAllocator.updateAllocator(alloc);
75          return delegate().allocate(preferredDirectByteBufAllocator);
76      }
77  
78      @Override
79      public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
80          return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
81      }
82  
83      @Override
84      public final boolean continueReading() {
85          // We must override the supplier which determines if there maybe more data to read.
86          return continueReading(defaultMaybeMoreDataSupplier);
87      }
88  }