View Javadoc
1   /*
2    * Copyright 2016 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.kqueue;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.DefaultChannelConfig;
21  import io.netty.channel.MessageSizeEstimator;
22  import io.netty.channel.RecvByteBufAllocator;
23  import io.netty.channel.WriteBufferWaterMark;
24  import io.netty.util.internal.UnstableApi;
25  
26  import java.util.Map;
27  
28  import static io.netty.channel.kqueue.KQueueChannelOption.RCV_ALLOC_TRANSPORT_PROVIDES_GUESS;
29  import static io.netty.channel.unix.Limits.SSIZE_MAX;
30  import static java.lang.Math.min;
31  
32  @UnstableApi
33  public class KQueueChannelConfig extends DefaultChannelConfig {
34      final AbstractKQueueChannel channel;
35      private volatile boolean transportProvidesGuess;
36      private volatile long maxBytesPerGatheringWrite = SSIZE_MAX;
37  
38      KQueueChannelConfig(AbstractKQueueChannel channel) {
39          super(channel);
40          this.channel = channel;
41      }
42  
43      @Override
44      @SuppressWarnings("deprecation")
45      public Map<ChannelOption<?>, Object> getOptions() {
46          return getOptions(super.getOptions(), RCV_ALLOC_TRANSPORT_PROVIDES_GUESS);
47      }
48  
49      @SuppressWarnings("unchecked")
50      @Override
51      public <T> T getOption(ChannelOption<T> option) {
52          if (option == RCV_ALLOC_TRANSPORT_PROVIDES_GUESS) {
53              return (T) Boolean.valueOf(getRcvAllocTransportProvidesGuess());
54          }
55          return super.getOption(option);
56      }
57  
58      @Override
59      public <T> boolean setOption(ChannelOption<T> option, T value) {
60          validate(option, value);
61  
62          if (option == RCV_ALLOC_TRANSPORT_PROVIDES_GUESS) {
63              setRcvAllocTransportProvidesGuess((Boolean) value);
64          } else {
65              return super.setOption(option, value);
66          }
67  
68          return true;
69      }
70  
71      /**
72       * If this is {@code true} then the {@link RecvByteBufAllocator.Handle#guess()} will be overriden to always attempt
73       * to read as many bytes as kqueue says are available.
74       */
75      public KQueueChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
76          this.transportProvidesGuess = transportProvidesGuess;
77          return this;
78      }
79  
80      /**
81       * If this is {@code true} then the {@link RecvByteBufAllocator.Handle#guess()} will be overriden to always attempt
82       * to read as many bytes as kqueue says are available.
83       */
84      public boolean getRcvAllocTransportProvidesGuess() {
85          return transportProvidesGuess;
86      }
87  
88      @Override
89      public KQueueChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
90          super.setConnectTimeoutMillis(connectTimeoutMillis);
91          return this;
92      }
93  
94      @Override
95      @Deprecated
96      public KQueueChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
97          super.setMaxMessagesPerRead(maxMessagesPerRead);
98          return this;
99      }
100 
101     @Override
102     public KQueueChannelConfig setWriteSpinCount(int writeSpinCount) {
103         super.setWriteSpinCount(writeSpinCount);
104         return this;
105     }
106 
107     @Override
108     public KQueueChannelConfig setAllocator(ByteBufAllocator allocator) {
109         super.setAllocator(allocator);
110         return this;
111     }
112 
113     @Override
114     public KQueueChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
115         if (!(allocator.newHandle() instanceof RecvByteBufAllocator.ExtendedHandle)) {
116             throw new IllegalArgumentException("allocator.newHandle() must return an object of type: " +
117                     RecvByteBufAllocator.ExtendedHandle.class);
118         }
119         super.setRecvByteBufAllocator(allocator);
120         return this;
121     }
122 
123     @Override
124     public KQueueChannelConfig setAutoRead(boolean autoRead) {
125         super.setAutoRead(autoRead);
126         return this;
127     }
128 
129     @Override
130     @Deprecated
131     public KQueueChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
132         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
133         return this;
134     }
135 
136     @Override
137     @Deprecated
138     public KQueueChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
139         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
140         return this;
141     }
142 
143     @Override
144     public KQueueChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
145         super.setWriteBufferWaterMark(writeBufferWaterMark);
146         return this;
147     }
148 
149     @Override
150     public KQueueChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
151         super.setMessageSizeEstimator(estimator);
152         return this;
153     }
154 
155     @Override
156     protected final void autoReadCleared() {
157         channel.clearReadFilter();
158     }
159 
160     final void setMaxBytesPerGatheringWrite(long maxBytesPerGatheringWrite) {
161         this.maxBytesPerGatheringWrite = min(SSIZE_MAX, maxBytesPerGatheringWrite);
162     }
163 
164     final long getMaxBytesPerGatheringWrite() {
165         return maxBytesPerGatheringWrite;
166     }
167 }