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  
19  import com.sun.nio.sctp.SctpChannel;
20  import com.sun.nio.sctp.SctpStandardSocketOptions;
21  import io.netty.buffer.ByteBufAllocator;
22  import io.netty.channel.ChannelException;
23  import io.netty.channel.ChannelOption;
24  import io.netty.channel.DefaultChannelConfig;
25  import io.netty.channel.MessageSizeEstimator;
26  import io.netty.channel.RecvByteBufAllocator;
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.*;
33  import static io.netty.channel.sctp.SctpChannelOption.*;
34  
35  /**
36   * The default {@link SctpChannelConfig} implementation for SCTP.
37   */
38  public class DefaultSctpChannelConfig extends DefaultChannelConfig implements SctpChannelConfig {
39  
40      private final SctpChannel javaChannel;
41  
42      public DefaultSctpChannelConfig(io.netty.channel.sctp.SctpChannel channel, SctpChannel javaChannel) {
43          super(channel);
44          if (javaChannel == null) {
45              throw new NullPointerException("javaChannel");
46          }
47          this.javaChannel = javaChannel;
48  
49          // Enable TCP_NODELAY by default if possible.
50          if (PlatformDependent.canEnableTcpNoDelayByDefault()) {
51              try {
52                  setSctpNoDelay(true);
53              } catch (Exception e) {
54                  // Ignore.
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          return super.getOption(option);
79      }
80  
81      @Override
82      public <T> boolean setOption(ChannelOption<T> option, T value) {
83          validate(option, value);
84  
85          if (option == SO_RCVBUF) {
86              setReceiveBufferSize((Integer) value);
87          } else if (option == SO_SNDBUF) {
88              setSendBufferSize((Integer) value);
89          } else if (option == SCTP_NODELAY) {
90              setSctpNoDelay((Boolean) value);
91          } else if (option == SCTP_INIT_MAXSTREAMS) {
92              setInitMaxStreams((SctpStandardSocketOptions.InitMaxStreams) value);
93          } else {
94              return super.setOption(option, value);
95          }
96  
97          return true;
98      }
99  
100     @Override
101     public boolean isSctpNoDelay() {
102         try {
103             return javaChannel.getOption(SctpStandardSocketOptions.SCTP_NODELAY);
104         } catch (IOException e) {
105             throw new ChannelException(e);
106         }
107     }
108 
109     @Override
110     public SctpChannelConfig setSctpNoDelay(boolean sctpNoDelay) {
111         try {
112             javaChannel.setOption(SctpStandardSocketOptions.SCTP_NODELAY, sctpNoDelay);
113         } catch (IOException e) {
114             throw new ChannelException(e);
115         }
116         return this;
117     }
118 
119     @Override
120     public int getSendBufferSize() {
121         try {
122             return javaChannel.getOption(SctpStandardSocketOptions.SO_SNDBUF);
123         } catch (IOException e) {
124             throw new ChannelException(e);
125         }
126     }
127 
128     @Override
129     public SctpChannelConfig setSendBufferSize(int sendBufferSize) {
130         try {
131             javaChannel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
132         } catch (IOException e) {
133             throw new ChannelException(e);
134         }
135         return this;
136     }
137 
138     @Override
139     public int getReceiveBufferSize() {
140         try {
141             return javaChannel.getOption(SctpStandardSocketOptions.SO_RCVBUF);
142         } catch (IOException e) {
143             throw new ChannelException(e);
144         }
145     }
146 
147     @Override
148     public SctpChannelConfig setReceiveBufferSize(int receiveBufferSize) {
149         try {
150             javaChannel.setOption(SctpStandardSocketOptions.SO_RCVBUF, receiveBufferSize);
151         } catch (IOException e) {
152             throw new ChannelException(e);
153         }
154         return this;
155     }
156 
157     @Override
158     public SctpStandardSocketOptions.InitMaxStreams getInitMaxStreams() {
159         try {
160             return javaChannel.getOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
161         } catch (IOException e) {
162             throw new ChannelException(e);
163         }
164     }
165 
166     @Override
167     public SctpChannelConfig setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
168         try {
169             javaChannel.setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, initMaxStreams);
170         } catch (IOException e) {
171             throw new ChannelException(e);
172         }
173         return this;
174     }
175 
176     @Override
177     public SctpChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
178         super.setConnectTimeoutMillis(connectTimeoutMillis);
179         return this;
180     }
181 
182     @Override
183     public SctpChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
184         super.setMaxMessagesPerRead(maxMessagesPerRead);
185         return this;
186     }
187 
188     @Override
189     public SctpChannelConfig setWriteSpinCount(int writeSpinCount) {
190         super.setWriteSpinCount(writeSpinCount);
191         return this;
192     }
193 
194     @Override
195     public SctpChannelConfig setAllocator(ByteBufAllocator allocator) {
196         super.setAllocator(allocator);
197         return this;
198     }
199 
200     @Override
201     public SctpChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
202         super.setRecvByteBufAllocator(allocator);
203         return this;
204     }
205 
206     @Override
207     public SctpChannelConfig setAutoRead(boolean autoRead) {
208         super.setAutoRead(autoRead);
209         return this;
210     }
211 
212     @Override
213     public SctpChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
214         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
215         return this;
216     }
217 
218     @Override
219     public SctpChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
220         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
221         return this;
222     }
223 
224     @Override
225     public SctpChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
226         super.setMessageSizeEstimator(estimator);
227         return this;
228     }
229 }