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