1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.kqueue;
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.ServerChannelRecvByteBufAllocator;
24 import io.netty.channel.WriteBufferWaterMark;
25 import io.netty.channel.socket.ServerSocketChannelConfig;
26 import io.netty.util.NetUtil;
27 import io.netty.util.internal.UnstableApi;
28
29 import java.io.IOException;
30 import java.util.Map;
31
32 import static io.netty.channel.ChannelOption.SO_BACKLOG;
33 import static io.netty.channel.ChannelOption.SO_RCVBUF;
34 import static io.netty.channel.ChannelOption.SO_REUSEADDR;
35 import static io.netty.channel.ChannelOption.TCP_FASTOPEN;
36 import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
37
38 @UnstableApi
39 public class KQueueServerChannelConfig extends KQueueChannelConfig implements ServerSocketChannelConfig {
40 private volatile int backlog = NetUtil.SOMAXCONN;
41 private volatile boolean enableTcpFastOpen;
42
43 KQueueServerChannelConfig(AbstractKQueueChannel channel) {
44 super(channel, new ServerChannelRecvByteBufAllocator());
45 }
46
47 @Override
48 public Map<ChannelOption<?>, Object> getOptions() {
49 return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG, TCP_FASTOPEN);
50 }
51
52 @SuppressWarnings("unchecked")
53 @Override
54 public <T> T getOption(ChannelOption<T> option) {
55 if (option == SO_RCVBUF) {
56 return (T) Integer.valueOf(getReceiveBufferSize());
57 }
58 if (option == SO_REUSEADDR) {
59 return (T) Boolean.valueOf(isReuseAddress());
60 }
61 if (option == SO_BACKLOG) {
62 return (T) Integer.valueOf(getBacklog());
63 }
64 if (option == TCP_FASTOPEN) {
65 return (T) (isTcpFastOpen() ? Integer.valueOf(1) : Integer.valueOf(0));
66 }
67 return super.getOption(option);
68 }
69
70 @Override
71 public <T> boolean setOption(ChannelOption<T> option, T value) {
72 validate(option, value);
73
74 if (option == SO_RCVBUF) {
75 setReceiveBufferSize((Integer) value);
76 } else if (option == SO_REUSEADDR) {
77 setReuseAddress((Boolean) value);
78 } else if (option == SO_BACKLOG) {
79 setBacklog((Integer) value);
80 } else if (option == TCP_FASTOPEN) {
81 setTcpFastOpen((Integer) value > 0);
82 } else {
83 return super.setOption(option, value);
84 }
85
86 return true;
87 }
88
89 @Override
90 public boolean isReuseAddress() {
91 try {
92 return ((AbstractKQueueChannel) channel).socket.isReuseAddress();
93 } catch (IOException e) {
94 throw new ChannelException(e);
95 }
96 }
97
98 @Override
99 public KQueueServerChannelConfig setReuseAddress(boolean reuseAddress) {
100 try {
101 ((AbstractKQueueChannel) channel).socket.setReuseAddress(reuseAddress);
102 return this;
103 } catch (IOException e) {
104 throw new ChannelException(e);
105 }
106 }
107
108 @Override
109 public int getReceiveBufferSize() {
110 try {
111 return ((AbstractKQueueChannel) channel).socket.getReceiveBufferSize();
112 } catch (IOException e) {
113 throw new ChannelException(e);
114 }
115 }
116
117 @Override
118 public KQueueServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
119 try {
120 ((AbstractKQueueChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
121 return this;
122 } catch (IOException e) {
123 throw new ChannelException(e);
124 }
125 }
126
127 @Override
128 public int getBacklog() {
129 return backlog;
130 }
131
132 @Override
133 public KQueueServerChannelConfig setBacklog(int backlog) {
134 checkPositiveOrZero(backlog, "backlog");
135 this.backlog = backlog;
136 return this;
137 }
138
139
140
141
142
143
144 public boolean isTcpFastOpen() {
145 return enableTcpFastOpen;
146 }
147
148
149
150
151
152
153
154
155
156 public KQueueServerChannelConfig setTcpFastOpen(boolean enableTcpFastOpen) {
157 this.enableTcpFastOpen = enableTcpFastOpen;
158 return this;
159 }
160
161 @Override
162 public KQueueServerChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
163 super.setRcvAllocTransportProvidesGuess(transportProvidesGuess);
164 return this;
165 }
166
167 @Override
168 public KQueueServerChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
169 return this;
170 }
171
172 @Override
173 public KQueueServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
174 super.setConnectTimeoutMillis(connectTimeoutMillis);
175 return this;
176 }
177
178 @Override
179 @Deprecated
180 public KQueueServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
181 super.setMaxMessagesPerRead(maxMessagesPerRead);
182 return this;
183 }
184
185 @Override
186 public KQueueServerChannelConfig setWriteSpinCount(int writeSpinCount) {
187 super.setWriteSpinCount(writeSpinCount);
188 return this;
189 }
190
191 @Override
192 public KQueueServerChannelConfig setAllocator(ByteBufAllocator allocator) {
193 super.setAllocator(allocator);
194 return this;
195 }
196
197 @Override
198 public KQueueServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
199 super.setRecvByteBufAllocator(allocator);
200 return this;
201 }
202
203 @Override
204 public KQueueServerChannelConfig setAutoRead(boolean autoRead) {
205 super.setAutoRead(autoRead);
206 return this;
207 }
208
209 @Override
210 @Deprecated
211 public KQueueServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
212 super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
213 return this;
214 }
215
216 @Override
217 @Deprecated
218 public KQueueServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
219 super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
220 return this;
221 }
222
223 @Override
224 public KQueueServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
225 super.setWriteBufferWaterMark(writeBufferWaterMark);
226 return this;
227 }
228
229 @Override
230 public KQueueServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
231 super.setMessageSizeEstimator(estimator);
232 return this;
233 }
234 }