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, SctpChannelOption.SCTP_INIT_MAXSTREAMS);
58      }
59  
60      @SuppressWarnings("unchecked")
61      @Override
62      public <T> T getOption(ChannelOption<T> option) {
63          if (option == ChannelOption.SO_RCVBUF) {
64              return (T) Integer.valueOf(getReceiveBufferSize());
65          }
66          if (option == ChannelOption.SO_SNDBUF) {
67              return (T) Integer.valueOf(getSendBufferSize());
68          }
69          if (option == SctpChannelOption.SCTP_INIT_MAXSTREAMS) {
70              return (T) getInitMaxStreams();
71          }
72          return super.getOption(option);
73      }
74  
75      @Override
76      public <T> boolean setOption(ChannelOption<T> option, T value) {
77          validate(option, value);
78  
79          if (option == ChannelOption.SO_RCVBUF) {
80              setReceiveBufferSize((Integer) value);
81          } else if (option == ChannelOption.SO_SNDBUF) {
82              setSendBufferSize((Integer) value);
83          } else if (option == SctpChannelOption.SCTP_INIT_MAXSTREAMS) {
84              setInitMaxStreams((SctpStandardSocketOptions.InitMaxStreams) value);
85          } else {
86              return super.setOption(option, value);
87          }
88  
89          return true;
90      }
91  
92      @Override
93      public int getSendBufferSize() {
94          try {
95              return javaChannel.getOption(SctpStandardSocketOptions.SO_SNDBUF);
96          } catch (IOException e) {
97              throw new ChannelException(e);
98          }
99      }
100 
101     @Override
102     public SctpServerChannelConfig setSendBufferSize(int sendBufferSize) {
103         try {
104             javaChannel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
105         } catch (IOException e) {
106             throw new ChannelException(e);
107         }
108         return this;
109     }
110 
111     @Override
112     public int getReceiveBufferSize() {
113         try {
114             return javaChannel.getOption(SctpStandardSocketOptions.SO_RCVBUF);
115         } catch (IOException e) {
116             throw new ChannelException(e);
117         }
118     }
119 
120     @Override
121     public SctpServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
122         try {
123             javaChannel.setOption(SctpStandardSocketOptions.SO_RCVBUF, receiveBufferSize);
124         } catch (IOException e) {
125             throw new ChannelException(e);
126         }
127         return this;
128     }
129 
130     @Override
131     public SctpStandardSocketOptions.InitMaxStreams getInitMaxStreams() {
132         try {
133             return javaChannel.getOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
134         } catch (IOException e) {
135             throw new ChannelException(e);
136         }
137     }
138 
139     @Override
140     public SctpServerChannelConfig setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
141         try {
142             javaChannel.setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, initMaxStreams);
143         } catch (IOException e) {
144             throw new ChannelException(e);
145         }
146         return this;
147     }
148 
149     @Override
150     public int getBacklog() {
151         return backlog;
152     }
153 
154     @Override
155     public SctpServerChannelConfig setBacklog(int backlog) {
156         checkPositiveOrZero(backlog, "backlog");
157         this.backlog = backlog;
158         return this;
159     }
160 
161     @Override
162     @Deprecated
163     public SctpServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
164         super.setMaxMessagesPerRead(maxMessagesPerRead);
165         return this;
166     }
167 
168     @Override
169     public SctpServerChannelConfig setWriteSpinCount(int writeSpinCount) {
170         super.setWriteSpinCount(writeSpinCount);
171         return this;
172     }
173 
174     @Override
175     public SctpServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
176         super.setConnectTimeoutMillis(connectTimeoutMillis);
177         return this;
178     }
179 
180     @Override
181     public SctpServerChannelConfig setAllocator(ByteBufAllocator allocator) {
182         super.setAllocator(allocator);
183         return this;
184     }
185 
186     @Override
187     public SctpServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
188         super.setRecvByteBufAllocator(allocator);
189         return this;
190     }
191 
192     @Override
193     public SctpServerChannelConfig setAutoRead(boolean autoRead) {
194         super.setAutoRead(autoRead);
195         return this;
196     }
197 
198     @Override
199     public SctpServerChannelConfig setAutoClose(boolean autoClose) {
200         super.setAutoClose(autoClose);
201         return this;
202     }
203 
204     @Override
205     public SctpServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
206         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
207         return this;
208     }
209 
210     @Override
211     public SctpServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
212         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
213         return this;
214     }
215 
216     @Override
217     public SctpServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
218         super.setWriteBufferWaterMark(writeBufferWaterMark);
219         return this;
220     }
221 
222     @Override
223     public SctpServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
224         super.setMessageSizeEstimator(estimator);
225         return this;
226     }
227 }