1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.sctp;
17
18 import com.sun.nio.sctp.SctpChannel;
19 import com.sun.nio.sctp.SctpStandardSocketOptions;
20 import io.netty.buffer.ByteBufAllocator;
21 import io.netty.channel.ChannelException;
22 import io.netty.channel.ChannelOption;
23 import io.netty.channel.DefaultChannelConfig;
24 import io.netty.channel.MessageSizeEstimator;
25 import io.netty.channel.RecvByteBufAllocator;
26 import io.netty.channel.WriteBufferWaterMark;
27 import io.netty.util.internal.ObjectUtil;
28 import io.netty.util.internal.PlatformDependent;
29
30 import java.io.IOException;
31 import java.util.Map;
32
33 import static io.netty.channel.ChannelOption.SO_RCVBUF;
34 import static io.netty.channel.ChannelOption.SO_SNDBUF;
35 import static io.netty.channel.sctp.SctpChannelOption.SCTP_INIT_MAXSTREAMS;
36 import static io.netty.channel.sctp.SctpChannelOption.SCTP_NODELAY;
37
38
39
40
41 public class DefaultSctpChannelConfig extends DefaultChannelConfig implements SctpChannelConfig {
42
43 private final SctpChannel javaChannel;
44
45 public DefaultSctpChannelConfig(io.netty.channel.sctp.SctpChannel channel, SctpChannel javaChannel) {
46 super(channel);
47 this.javaChannel = ObjectUtil.checkNotNull(javaChannel, "javaChannel");
48
49
50 if (PlatformDependent.canEnableTcpNoDelayByDefault()) {
51 try {
52 setSctpNoDelay(true);
53 } catch (Exception e) {
54
55 }
56 }
57 }
58
59 @Override
60 public Map<ChannelOption<?>, Object> getOptions() {
61 return getOptions(
62 super.getOptions(),
63 SO_RCVBUF, SO_SNDBUF, SCTP_NODELAY, SCTP_INIT_MAXSTREAMS);
64 }
65
66 @SuppressWarnings("unchecked")
67 @Override
68 public <T> T getOption(ChannelOption<T> option) {
69 if (option == SO_RCVBUF) {
70 return (T) Integer.valueOf(getReceiveBufferSize());
71 }
72 if (option == SO_SNDBUF) {
73 return (T) Integer.valueOf(getSendBufferSize());
74 }
75 if (option == SCTP_NODELAY) {
76 return (T) Boolean.valueOf(isSctpNoDelay());
77 }
78 if (option == SCTP_INIT_MAXSTREAMS) {
79 return (T) getInitMaxStreams();
80 }
81 return super.getOption(option);
82 }
83
84 @Override
85 public <T> boolean setOption(ChannelOption<T> option, T value) {
86 validate(option, value);
87
88 if (option == SO_RCVBUF) {
89 setReceiveBufferSize((Integer) value);
90 } else if (option == SO_SNDBUF) {
91 setSendBufferSize((Integer) value);
92 } else if (option == SCTP_NODELAY) {
93 setSctpNoDelay((Boolean) value);
94 } else if (option == SCTP_INIT_MAXSTREAMS) {
95 setInitMaxStreams((SctpStandardSocketOptions.InitMaxStreams) value);
96 } else {
97 return super.setOption(option, value);
98 }
99
100 return true;
101 }
102
103 @Override
104 public boolean isSctpNoDelay() {
105 try {
106 return javaChannel.getOption(SctpStandardSocketOptions.SCTP_NODELAY);
107 } catch (IOException e) {
108 throw new ChannelException(e);
109 }
110 }
111
112 @Override
113 public SctpChannelConfig setSctpNoDelay(boolean sctpNoDelay) {
114 try {
115 javaChannel.setOption(SctpStandardSocketOptions.SCTP_NODELAY, sctpNoDelay);
116 } catch (IOException e) {
117 throw new ChannelException(e);
118 }
119 return this;
120 }
121
122 @Override
123 public int getSendBufferSize() {
124 try {
125 return javaChannel.getOption(SctpStandardSocketOptions.SO_SNDBUF);
126 } catch (IOException e) {
127 throw new ChannelException(e);
128 }
129 }
130
131 @Override
132 public SctpChannelConfig setSendBufferSize(int sendBufferSize) {
133 try {
134 javaChannel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
135 } catch (IOException e) {
136 throw new ChannelException(e);
137 }
138 return this;
139 }
140
141 @Override
142 public int getReceiveBufferSize() {
143 try {
144 return javaChannel.getOption(SctpStandardSocketOptions.SO_RCVBUF);
145 } catch (IOException e) {
146 throw new ChannelException(e);
147 }
148 }
149
150 @Override
151 public SctpChannelConfig setReceiveBufferSize(int receiveBufferSize) {
152 try {
153 javaChannel.setOption(SctpStandardSocketOptions.SO_RCVBUF, receiveBufferSize);
154 } catch (IOException e) {
155 throw new ChannelException(e);
156 }
157 return this;
158 }
159
160 @Override
161 public SctpStandardSocketOptions.InitMaxStreams getInitMaxStreams() {
162 try {
163 return javaChannel.getOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
164 } catch (IOException e) {
165 throw new ChannelException(e);
166 }
167 }
168
169 @Override
170 public SctpChannelConfig setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
171 try {
172 javaChannel.setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, initMaxStreams);
173 } catch (IOException e) {
174 throw new ChannelException(e);
175 }
176 return this;
177 }
178
179 @Override
180 public SctpChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
181 super.setConnectTimeoutMillis(connectTimeoutMillis);
182 return this;
183 }
184
185 @Override
186 @Deprecated
187 public SctpChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
188 super.setMaxMessagesPerRead(maxMessagesPerRead);
189 return this;
190 }
191
192 @Override
193 public SctpChannelConfig setWriteSpinCount(int writeSpinCount) {
194 super.setWriteSpinCount(writeSpinCount);
195 return this;
196 }
197
198 @Override
199 public SctpChannelConfig setAllocator(ByteBufAllocator allocator) {
200 super.setAllocator(allocator);
201 return this;
202 }
203
204 @Override
205 public SctpChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
206 super.setRecvByteBufAllocator(allocator);
207 return this;
208 }
209
210 @Override
211 public SctpChannelConfig setAutoRead(boolean autoRead) {
212 super.setAutoRead(autoRead);
213 return this;
214 }
215
216 @Override
217 public SctpChannelConfig setAutoClose(boolean autoClose) {
218 super.setAutoClose(autoClose);
219 return this;
220 }
221
222 @Override
223 public SctpChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
224 super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
225 return this;
226 }
227
228 @Override
229 public SctpChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
230 super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
231 return this;
232 }
233
234 @Override
235 public SctpChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
236 super.setWriteBufferWaterMark(writeBufferWaterMark);
237 return this;
238 }
239
240 @Override
241 public SctpChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
242 super.setMessageSizeEstimator(estimator);
243 return this;
244 }
245 }