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