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.ChannelOption;
20  import io.netty.channel.MessageSizeEstimator;
21  import io.netty.channel.RecvByteBufAllocator;
22  import io.netty.util.NetUtil;
23  
24  import java.util.Map;
25  
26  import static io.netty.channel.ChannelOption.SO_BACKLOG;
27  import static io.netty.channel.ChannelOption.SO_RCVBUF;
28  import static io.netty.channel.ChannelOption.SO_REUSEADDR;
29  
30  public class EpollServerChannelConfig extends EpollChannelConfig {
31      protected final AbstractEpollChannel channel;
32      private volatile int backlog = NetUtil.SOMAXCONN;
33  
34      EpollServerChannelConfig(AbstractEpollChannel channel) {
35          super(channel);
36          this.channel = channel;
37      }
38  
39      @Override
40      public Map<ChannelOption<?>, Object> getOptions() {
41          return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG);
42      }
43  
44      @SuppressWarnings("unchecked")
45      @Override
46      public <T> T getOption(ChannelOption<T> option) {
47          if (option == SO_RCVBUF) {
48              return (T) Integer.valueOf(getReceiveBufferSize());
49          }
50          if (option == SO_REUSEADDR) {
51              return (T) Boolean.valueOf(isReuseAddress());
52          }
53          if (option == SO_BACKLOG) {
54              return (T) Integer.valueOf(getBacklog());
55          }
56          return super.getOption(option);
57      }
58  
59      @Override
60      public <T> boolean setOption(ChannelOption<T> option, T value) {
61          validate(option, value);
62  
63          if (option == SO_RCVBUF) {
64              setReceiveBufferSize((Integer) value);
65          } else if (option == SO_REUSEADDR) {
66              setReuseAddress((Boolean) value);
67          } else if (option == SO_BACKLOG) {
68              setBacklog((Integer) value);
69          } else {
70              return super.setOption(option, value);
71          }
72  
73          return true;
74      }
75  
76      public boolean isReuseAddress() {
77          return Native.isReuseAddress(channel.fd().intValue()) == 1;
78      }
79  
80      public EpollServerChannelConfig setReuseAddress(boolean reuseAddress) {
81          Native.setReuseAddress(channel.fd().intValue(), reuseAddress ? 1 : 0);
82          return this;
83      }
84  
85      public int getReceiveBufferSize() {
86          return Native.getReceiveBufferSize(channel.fd().intValue());
87      }
88  
89      public EpollServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
90          Native.setReceiveBufferSize(channel.fd().intValue(), receiveBufferSize);
91          return this;
92      }
93  
94      public int getBacklog() {
95          return backlog;
96      }
97  
98      public EpollServerChannelConfig setBacklog(int backlog) {
99          if (backlog < 0) {
100             throw new IllegalArgumentException("backlog: " + backlog);
101         }
102         this.backlog = backlog;
103         return this;
104     }
105 
106     @Override
107     public EpollServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
108         super.setConnectTimeoutMillis(connectTimeoutMillis);
109         return this;
110     }
111 
112     @Override
113     public EpollServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
114         super.setMaxMessagesPerRead(maxMessagesPerRead);
115         return this;
116     }
117 
118     @Override
119     public EpollServerChannelConfig setWriteSpinCount(int writeSpinCount) {
120         super.setWriteSpinCount(writeSpinCount);
121         return this;
122     }
123 
124     @Override
125     public EpollServerChannelConfig setAllocator(ByteBufAllocator allocator) {
126         super.setAllocator(allocator);
127         return this;
128     }
129 
130     @Override
131     public EpollServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
132         super.setRecvByteBufAllocator(allocator);
133         return this;
134     }
135 
136     @Override
137     public EpollServerChannelConfig setAutoRead(boolean autoRead) {
138         super.setAutoRead(autoRead);
139         return this;
140     }
141 
142     @Override
143     public EpollServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
144         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
145         return this;
146     }
147 
148     @Override
149     public EpollServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
150         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
151         return this;
152     }
153 
154     @Override
155     public EpollServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
156         super.setMessageSizeEstimator(estimator);
157         return this;
158     }
159 
160     @Override
161     public EpollServerChannelConfig setEpollMode(EpollMode mode) {
162         super.setEpollMode(mode);
163         return this;
164     }
165 }