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          return (isEdgeTriggered && lastBytesRead() > 0) ||
57                 (!isEdgeTriggered && lastBytesRead() == attemptedBytesRead()) ||
58                  receivedRdHup;
59      }
60  
61      final void edgeTriggered(boolean edgeTriggered) {
62          isEdgeTriggered = edgeTriggered;
63      }
64  
65      final boolean isEdgeTriggered() {
66          return isEdgeTriggered;
67      }
68  
69      @Override
70      public final ByteBuf allocate(ByteBufAllocator alloc) {
71          return delegate.allocate(alloc);
72      }
73  
74      @Override
75      public final int guess() {
76          return delegate.guess();
77      }
78  
79      @Override
80      public final void reset(ChannelConfig config) {
81          delegate.reset(config);
82      }
83  
84      @Override
85      public final void incMessagesRead(int numMessages) {
86          delegate.incMessagesRead(numMessages);
87      }
88  
89      @Override
90      public final void lastBytesRead(int bytes) {
91          delegate.lastBytesRead(bytes);
92      }
93  
94      @Override
95      public final int lastBytesRead() {
96          return delegate.lastBytesRead();
97      }
98  
99      @Override
100     public final int attemptedBytesRead() {
101         return delegate.attemptedBytesRead();
102     }
103 
104     @Override
105     public final void attemptedBytesRead(int bytes) {
106         delegate.attemptedBytesRead(bytes);
107     }
108 
109     @Override
110     public final void readComplete() {
111         delegate.readComplete();
112     }
113 
114     @Override
115     public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
116         return delegate.continueReading(maybeMoreDataSupplier);
117     }
118 
119     @Override
120     public final boolean continueReading() {
121         // We must override the supplier which determines if there maybe more data to read.
122         return delegate.continueReading(defaultMaybeMoreDataSupplier);
123     }
124 }