View Javadoc
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    *   http://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.ChannelConfig;
21  import io.netty.channel.RecvByteBufAllocator;
22  import io.netty.util.UncheckedBooleanSupplier;
23  import io.netty.util.internal.ObjectUtil;
24  
25  class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle {
26      private final RecvByteBufAllocator.ExtendedHandle delegate;
27      private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = new UncheckedBooleanSupplier() {
28          @Override
29          public boolean get() {
30              return maybeMoreDataToRead();
31          }
32      };
33      private boolean isEdgeTriggered;
34      private boolean receivedRdHup;
35  
36      EpollRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) {
37          this.delegate = ObjectUtil.checkNotNull(handle, "handle");
38      }
39  
40      final void receivedRdHup() {
41          receivedRdHup = true;
42      }
43  
44      final boolean isReceivedRdHup() {
45          return receivedRdHup;
46      }
47  
48      boolean maybeMoreDataToRead() {
49          /**
50           * EPOLL ET requires that we read until we get an EAGAIN
51           * (see Q9 in <a href="http://man7.org/linux/man-pages/man7/epoll.7.html">epoll man</a>). However in order to
52           * respect auto read we supporting reading to stop if auto read is off. It is expected that the
53           * {@link #EpollSocketChannel} implementations will track if we are in edgeTriggered mode and all data was not
54           * read, and will force a EPOLLIN ready event.
55           *
56           * It is assumed RDHUP is handled externally by checking {@link #isReceivedRdHup()}.
57           */
58          return (isEdgeTriggered && lastBytesRead() > 0) ||
59                 (!isEdgeTriggered && lastBytesRead() == attemptedBytesRead());
60      }
61  
62      final void edgeTriggered(boolean edgeTriggered) {
63          isEdgeTriggered = edgeTriggered;
64      }
65  
66      final boolean isEdgeTriggered() {
67          return isEdgeTriggered;
68      }
69  
70      @Override
71      public final ByteBuf allocate(ByteBufAllocator alloc) {
72          return delegate.allocate(alloc);
73      }
74  
75      @Override
76      public final int guess() {
77          return delegate.guess();
78      }
79  
80      @Override
81      public final void reset(ChannelConfig config) {
82          delegate.reset(config);
83      }
84  
85      @Override
86      public final void incMessagesRead(int numMessages) {
87          delegate.incMessagesRead(numMessages);
88      }
89  
90      @Override
91      public final void lastBytesRead(int bytes) {
92          delegate.lastBytesRead(bytes);
93      }
94  
95      @Override
96      public final int lastBytesRead() {
97          return delegate.lastBytesRead();
98      }
99  
100     @Override
101     public final int attemptedBytesRead() {
102         return delegate.attemptedBytesRead();
103     }
104 
105     @Override
106     public final void attemptedBytesRead(int bytes) {
107         delegate.attemptedBytesRead(bytes);
108     }
109 
110     @Override
111     public final void readComplete() {
112         delegate.readComplete();
113     }
114 
115     @Override
116     public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
117         return delegate.continueReading(maybeMoreDataSupplier);
118     }
119 
120     @Override
121     public final boolean continueReading() {
122         // We must override the supplier which determines if there maybe more data to read.
123         return delegate.continueReading(defaultMaybeMoreDataSupplier);
124     }
125 }