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