1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.MessageSizeEstimator;
22 import io.netty.channel.RecvByteBufAllocator;
23 import io.netty.channel.socket.ServerSocketChannelConfig;
24
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.util.Map;
28
29 public final class EpollServerSocketChannelConfig extends EpollServerChannelConfig
30 implements ServerSocketChannelConfig {
31
32 EpollServerSocketChannelConfig(EpollServerSocketChannel channel) {
33 super(channel);
34
35
36
37
38 setReuseAddress(true);
39 }
40
41 @Override
42 public Map<ChannelOption<?>, Object> getOptions() {
43 return getOptions(super.getOptions(), EpollChannelOption.SO_REUSEPORT, EpollChannelOption.IP_FREEBIND,
44 EpollChannelOption.TCP_DEFER_ACCEPT);
45 }
46
47 @SuppressWarnings("unchecked")
48 @Override
49 public <T> T getOption(ChannelOption<T> option) {
50 if (option == EpollChannelOption.SO_REUSEPORT) {
51 return (T) Boolean.valueOf(isReusePort());
52 }
53 if (option == EpollChannelOption.IP_FREEBIND) {
54 return (T) Boolean.valueOf(isFreeBind());
55 }
56 if (option == EpollChannelOption.TCP_DEFER_ACCEPT) {
57 return (T) Integer.valueOf(getTcpDeferAccept());
58 }
59 return super.getOption(option);
60 }
61
62 @Override
63 public <T> boolean setOption(ChannelOption<T> option, T value) {
64 validate(option, value);
65
66 if (option == EpollChannelOption.SO_REUSEPORT) {
67 setReusePort((Boolean) value);
68 } else if (option == EpollChannelOption.IP_FREEBIND) {
69 setFreeBind((Boolean) value);
70 } else if (option == EpollChannelOption.TCP_MD5SIG) {
71 @SuppressWarnings("unchecked")
72 final Map<InetAddress, byte[]> m = (Map<InetAddress, byte[]>) value;
73 setTcpMd5Sig(m);
74 } else if (option == EpollChannelOption.TCP_DEFER_ACCEPT) {
75 setTcpDeferAccept((Integer) value);
76 } else {
77 return super.setOption(option, value);
78 }
79
80 return true;
81 }
82
83 @Override
84 public EpollServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
85 super.setReuseAddress(reuseAddress);
86 return this;
87 }
88
89 @Override
90 public EpollServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
91 super.setReceiveBufferSize(receiveBufferSize);
92 return this;
93 }
94
95 @Override
96 public EpollServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
97 return this;
98 }
99
100 @Override
101 public EpollServerSocketChannelConfig setBacklog(int backlog) {
102 super.setBacklog(backlog);
103 return this;
104 }
105
106 @Override
107 public EpollServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
108 super.setConnectTimeoutMillis(connectTimeoutMillis);
109 return this;
110 }
111
112 @Override
113 public EpollServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
114 super.setMaxMessagesPerRead(maxMessagesPerRead);
115 return this;
116 }
117
118 @Override
119 public EpollServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
120 super.setWriteSpinCount(writeSpinCount);
121 return this;
122 }
123
124 @Override
125 public EpollServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
126 super.setAllocator(allocator);
127 return this;
128 }
129
130 @Override
131 public EpollServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
132 super.setRecvByteBufAllocator(allocator);
133 return this;
134 }
135
136 @Override
137 public EpollServerSocketChannelConfig setAutoRead(boolean autoRead) {
138 super.setAutoRead(autoRead);
139 return this;
140 }
141
142 @Override
143 public EpollServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
144 super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
145 return this;
146 }
147
148 @Override
149 public EpollServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
150 super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
151 return this;
152 }
153
154 @Override
155 public EpollServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
156 super.setMessageSizeEstimator(estimator);
157 return this;
158 }
159
160
161
162
163
164
165 public EpollServerSocketChannelConfig setTcpMd5Sig(Map<InetAddress, byte[]> keys) {
166 try {
167 ((EpollServerSocketChannel) channel).setTcpMd5Sig(keys);
168 return this;
169 } catch (IOException e) {
170 throw new ChannelException(e);
171 }
172 }
173
174
175
176
177 public boolean isReusePort() {
178 try {
179 return Native.isReusePort(channel.fd().intValue()) == 1;
180 } catch (IOException e) {
181 throw new ChannelException(e);
182 }
183 }
184
185
186
187
188
189
190
191
192 public EpollServerSocketChannelConfig setReusePort(boolean reusePort) {
193 try {
194 Native.setReusePort(channel.fd().intValue(), reusePort ? 1 : 0);
195 return this;
196 } catch (IOException e) {
197 throw new ChannelException(e);
198 }
199 }
200
201
202
203
204
205 public boolean isFreeBind() {
206 try {
207 return Native.isIpFreeBind(channel.fd().intValue()) != 0;
208 } catch (IOException e) {
209 throw new ChannelException(e);
210 }
211 }
212
213
214
215
216
217 public EpollServerSocketChannelConfig setFreeBind(boolean freeBind) {
218 try {
219 Native.setIpFreeBind(channel.fd().intValue(), freeBind ? 1 : 0);
220 return this;
221 } catch (IOException e) {
222 throw new ChannelException(e);
223 }
224 }
225
226
227
228
229 public EpollServerSocketChannelConfig setTcpDeferAccept(int deferAccept) {
230 try {
231 channel.fd().setTcpDeferAccept(deferAccept);
232 return this;
233 } catch (IOException e) {
234 throw new ChannelException(e);
235 }
236 }
237
238
239
240
241 public int getTcpDeferAccept() {
242 try {
243 return channel.fd().getTcpDeferAccept();
244 } catch (IOException e) {
245 throw new ChannelException(e);
246 }
247 }
248 }