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.ByteBufAllocator;
19  import io.netty.channel.ChannelException;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.DefaultChannelConfig;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  
25  import java.io.IOException;
26  import java.util.Map;
27  
28  public class EpollChannelConfig extends DefaultChannelConfig {
29      final AbstractEpollChannel channel;
30  
31      EpollChannelConfig(AbstractEpollChannel channel) {
32          super(channel);
33          this.channel = channel;
34      }
35  
36      @Override
37      public Map<ChannelOption<?>, Object> getOptions() {
38          return getOptions(super.getOptions(), EpollChannelOption.EPOLL_MODE);
39      }
40  
41      @SuppressWarnings("unchecked")
42      @Override
43      public <T> T getOption(ChannelOption<T> option) {
44          if (option == EpollChannelOption.EPOLL_MODE) {
45              return (T) getEpollMode();
46          }
47          return super.getOption(option);
48      }
49  
50      @Override
51      public <T> boolean setOption(ChannelOption<T> option, T value) {
52          validate(option, value);
53          if (option == EpollChannelOption.EPOLL_MODE) {
54              setEpollMode((EpollMode) value);
55          } else {
56              return super.setOption(option, value);
57          }
58          return true;
59      }
60  
61      @Override
62      public EpollChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
63          super.setConnectTimeoutMillis(connectTimeoutMillis);
64          return this;
65      }
66  
67      @Override
68      public EpollChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
69          super.setMaxMessagesPerRead(maxMessagesPerRead);
70          return this;
71      }
72  
73      @Override
74      public EpollChannelConfig setWriteSpinCount(int writeSpinCount) {
75          super.setWriteSpinCount(writeSpinCount);
76          return this;
77      }
78  
79      @Override
80      public EpollChannelConfig setAllocator(ByteBufAllocator allocator) {
81          super.setAllocator(allocator);
82          return this;
83      }
84  
85      @Override
86      public EpollChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
87          super.setRecvByteBufAllocator(allocator);
88          return this;
89      }
90  
91      @Override
92      public EpollChannelConfig setAutoRead(boolean autoRead) {
93          super.setAutoRead(autoRead);
94          return this;
95      }
96  
97      @Override
98      public EpollChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
99          super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
100         return this;
101     }
102 
103     @Override
104     public EpollChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
105         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
106         return this;
107     }
108 
109     @Override
110     public EpollChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
111         super.setMessageSizeEstimator(estimator);
112         return this;
113     }
114 
115     /**
116      * Return the {@link EpollMode} used. Default is
117      * {@link EpollMode#EDGE_TRIGGERED}. If you want to use {@link #isAutoRead()} {@code false} or
118      * {@link #getMaxMessagesPerRead()} and have an accurate behaviour you should use
119      * {@link EpollMode#LEVEL_TRIGGERED}.
120      */
121     public EpollMode getEpollMode() {
122         return channel.isFlagSet(Native.EPOLLET)
123                 ? EpollMode.EDGE_TRIGGERED : EpollMode.LEVEL_TRIGGERED;
124     }
125 
126     /**
127      * Set the {@link EpollMode} used. Default is
128      * {@link EpollMode#EDGE_TRIGGERED}. If you want to use {@link #isAutoRead()} {@code false} or
129      * {@link #getMaxMessagesPerRead()} and have an accurate behaviour you should use
130      * {@link EpollMode#LEVEL_TRIGGERED}.
131      *
132      * <strong>Be aware this config setting can only be adjusted before the channel was registered.</strong>
133      */
134     public EpollChannelConfig setEpollMode(EpollMode mode) {
135         if (mode == null) {
136             throw new NullPointerException("mode");
137         }
138         try {
139             switch (mode) {
140             case EDGE_TRIGGERED:
141                 checkChannelNotRegistered();
142                 channel.setFlag(Native.EPOLLET);
143                 break;
144             case LEVEL_TRIGGERED:
145                 checkChannelNotRegistered();
146                 channel.clearFlag(Native.EPOLLET);
147                 break;
148             default:
149                 throw new Error();
150             }
151         } catch (IOException e) {
152             throw new ChannelException(e);
153         }
154         return this;
155     }
156 
157     private void checkChannelNotRegistered() {
158         if (channel.isRegistered()) {
159             throw new IllegalStateException("EpollMode can only be changed before channel is registered");
160         }
161     }
162 
163     @Override
164     protected final void autoReadCleared() {
165         channel.clearEpollIn();
166     }
167 }