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.DefaultChannelConfig;
22 import io.netty.channel.MessageSizeEstimator;
23 import io.netty.channel.RecvByteBufAllocator;
24 import io.netty.channel.WriteBufferWaterMark;
25 import io.netty.channel.unix.IntegerUnixChannelOption;
26 import io.netty.channel.unix.RawUnixChannelOption;
27
28 import java.io.IOException;
29 import java.nio.ByteBuffer;
30 import java.util.Map;
31
32 import static io.netty.channel.kqueue.KQueueChannelOption.RCV_ALLOC_TRANSPORT_PROVIDES_GUESS;
33 import static io.netty.channel.unix.Limits.SSIZE_MAX;
34 import static java.lang.Math.min;
35
36 public class KQueueChannelConfig extends DefaultChannelConfig {
37 private volatile boolean transportProvidesGuess;
38 private volatile long maxBytesPerGatheringWrite = SSIZE_MAX;
39
40 KQueueChannelConfig(AbstractKQueueChannel channel) {
41 super(channel);
42 }
43
44 KQueueChannelConfig(AbstractKQueueChannel channel, RecvByteBufAllocator recvByteBufAllocator) {
45 super(channel, recvByteBufAllocator);
46 }
47
48 @Override
49 @SuppressWarnings("deprecation")
50 public Map<ChannelOption<?>, Object> getOptions() {
51 return getOptions(super.getOptions(), RCV_ALLOC_TRANSPORT_PROVIDES_GUESS);
52 }
53
54 @SuppressWarnings("unchecked")
55 @Override
56 public <T> T getOption(ChannelOption<T> option) {
57 if (option == RCV_ALLOC_TRANSPORT_PROVIDES_GUESS) {
58 return (T) Boolean.valueOf(getRcvAllocTransportProvidesGuess());
59 }
60 try {
61 if (option instanceof IntegerUnixChannelOption) {
62 IntegerUnixChannelOption opt = (IntegerUnixChannelOption) option;
63 return (T) Integer.valueOf(((AbstractKQueueChannel) channel).socket.getIntOpt(
64 opt.level(), opt.optname()));
65 }
66 if (option instanceof RawUnixChannelOption) {
67 RawUnixChannelOption opt = (RawUnixChannelOption) option;
68 ByteBuffer out = ByteBuffer.allocate(opt.length());
69 ((AbstractKQueueChannel) channel).socket.getRawOpt(opt.level(), opt.optname(), out);
70 return (T) out.flip();
71 }
72 } catch (IOException e) {
73 throw new ChannelException(e);
74 }
75 return super.getOption(option);
76 }
77
78 @Override
79 public <T> boolean setOption(ChannelOption<T> option, T value) {
80 validate(option, value);
81
82 if (option == RCV_ALLOC_TRANSPORT_PROVIDES_GUESS) {
83 setRcvAllocTransportProvidesGuess((Boolean) value);
84 } else {
85 try {
86 if (option instanceof IntegerUnixChannelOption) {
87 IntegerUnixChannelOption opt = (IntegerUnixChannelOption) option;
88 ((AbstractKQueueChannel) channel).socket.setIntOpt(opt.level(), opt.optname(), (Integer) value);
89 return true;
90 } else if (option instanceof RawUnixChannelOption) {
91 RawUnixChannelOption opt = (RawUnixChannelOption) option;
92 ((AbstractKQueueChannel) channel).socket.setRawOpt(opt.level(), opt.optname(), (ByteBuffer) value);
93 return true;
94 }
95 } catch (IOException e) {
96 throw new ChannelException(e);
97 }
98 return super.setOption(option, value);
99 }
100
101 return true;
102 }
103
104
105
106
107
108
109
110 @Deprecated
111 public KQueueChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
112 this.transportProvidesGuess = transportProvidesGuess;
113 return this;
114 }
115
116
117
118
119
120
121
122 @Deprecated
123 public boolean getRcvAllocTransportProvidesGuess() {
124 return transportProvidesGuess;
125 }
126
127 @Override
128 public KQueueChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
129 super.setConnectTimeoutMillis(connectTimeoutMillis);
130 return this;
131 }
132
133 @Override
134 @Deprecated
135 public KQueueChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
136 super.setMaxMessagesPerRead(maxMessagesPerRead);
137 return this;
138 }
139
140 @Override
141 public KQueueChannelConfig setWriteSpinCount(int writeSpinCount) {
142 super.setWriteSpinCount(writeSpinCount);
143 return this;
144 }
145
146 @Override
147 public KQueueChannelConfig setAllocator(ByteBufAllocator allocator) {
148 super.setAllocator(allocator);
149 return this;
150 }
151
152 @Override
153 public KQueueChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
154 if (!(allocator.newHandle() instanceof RecvByteBufAllocator.ExtendedHandle)) {
155 throw new IllegalArgumentException("allocator.newHandle() must return an object of type: " +
156 RecvByteBufAllocator.ExtendedHandle.class);
157 }
158 super.setRecvByteBufAllocator(allocator);
159 return this;
160 }
161
162 @Override
163 public KQueueChannelConfig setAutoRead(boolean autoRead) {
164 super.setAutoRead(autoRead);
165 return this;
166 }
167
168 @Override
169 @Deprecated
170 public KQueueChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
171 super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
172 return this;
173 }
174
175 @Override
176 @Deprecated
177 public KQueueChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
178 super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
179 return this;
180 }
181
182 @Override
183 public KQueueChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
184 super.setWriteBufferWaterMark(writeBufferWaterMark);
185 return this;
186 }
187
188 @Override
189 public KQueueChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
190 super.setMessageSizeEstimator(estimator);
191 return this;
192 }
193
194 @Override
195 protected final void autoReadCleared() {
196 ((AbstractKQueueChannel) channel).clearReadFilter();
197 }
198
199 final void setMaxBytesPerGatheringWrite(long maxBytesPerGatheringWrite) {
200 this.maxBytesPerGatheringWrite = min(SSIZE_MAX, maxBytesPerGatheringWrite);
201 }
202
203 final long getMaxBytesPerGatheringWrite() {
204 return maxBytesPerGatheringWrite;
205 }
206 }