查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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.udt;
17  
18  import com.barchart.udt.OptionUDT;
19  import com.barchart.udt.SocketUDT;
20  import com.barchart.udt.nio.ChannelUDT;
21  import io.netty.buffer.ByteBufAllocator;
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  
28  import java.io.IOException;
29  import java.util.Map;
30  
31  import static io.netty.channel.ChannelOption.SO_LINGER;
32  import static io.netty.channel.ChannelOption.SO_RCVBUF;
33  import static io.netty.channel.ChannelOption.SO_REUSEADDR;
34  import static io.netty.channel.ChannelOption.SO_SNDBUF;
35  import static io.netty.channel.udt.UdtChannelOption.PROTOCOL_RECEIVE_BUFFER_SIZE;
36  import static io.netty.channel.udt.UdtChannelOption.PROTOCOL_SEND_BUFFER_SIZE;
37  import static io.netty.channel.udt.UdtChannelOption.SYSTEM_RECEIVE_BUFFER_SIZE;
38  import static io.netty.channel.udt.UdtChannelOption.SYSTEM_SEND_BUFFER_SIZE;
39  
40  /**
41   * The default {@link UdtChannelConfig} implementation.
42   *
43   * @deprecated The UDT transport is no longer maintained and will be removed.
44   */
45  @Deprecated
46  public class DefaultUdtChannelConfig extends DefaultChannelConfig implements
47          UdtChannelConfig {
48  
49      private static final int K = 1024;
50      private static final int M = K * K;
51  
52      private volatile int protocolReceiveBufferSize = 10 * M;
53      private volatile int protocolSendBufferSize = 10 * M;
54  
55      private volatile int systemReceiveBufferSize = M;
56      private volatile int systemSendBufferSize = M;
57  
58      private volatile int allocatorReceiveBufferSize = 128 * K;
59      private volatile int allocatorSendBufferSize = 128 * K;
60  
61      private volatile int soLinger;
62  
63      private volatile boolean reuseAddress = true;
64  
65      public DefaultUdtChannelConfig(final UdtChannel channel,
66              final ChannelUDT channelUDT, final boolean apply)
67              throws IOException {
68          super(channel);
69          if (apply) {
70              apply(channelUDT);
71          }
72      }
73  
74      protected void apply(final ChannelUDT channelUDT) throws IOException {
75          final SocketUDT socketUDT = channelUDT.socketUDT();
76          socketUDT.setReuseAddress(isReuseAddress());
77          socketUDT.setSendBufferSize(getSendBufferSize());
78          if (getSoLinger() <= 0) {
79              socketUDT.setSoLinger(false, 0);
80          } else {
81              socketUDT.setSoLinger(true, getSoLinger());
82          }
83          socketUDT.setOption(OptionUDT.Protocol_Receive_Buffer_Size,
84                  getProtocolReceiveBufferSize());
85          socketUDT.setOption(OptionUDT.Protocol_Send_Buffer_Size,
86                  getProtocolSendBufferSize());
87          socketUDT.setOption(OptionUDT.System_Receive_Buffer_Size,
88                  getSystemReceiveBufferSize());
89          socketUDT.setOption(OptionUDT.System_Send_Buffer_Size,
90                  getSystemSendBufferSize());
91      }
92  
93      @Override
94      public int getProtocolReceiveBufferSize() {
95          return protocolReceiveBufferSize;
96      }
97  
98      @SuppressWarnings("unchecked")
99      @Override
100     public <T> T getOption(final ChannelOption<T> option) {
101         if (option == PROTOCOL_RECEIVE_BUFFER_SIZE) {
102             return (T) Integer.valueOf(getProtocolReceiveBufferSize());
103         }
104         if (option == PROTOCOL_SEND_BUFFER_SIZE) {
105             return (T) Integer.valueOf(getProtocolSendBufferSize());
106         }
107         if (option == SYSTEM_RECEIVE_BUFFER_SIZE) {
108             return (T) Integer.valueOf(getSystemReceiveBufferSize());
109         }
110         if (option == SYSTEM_SEND_BUFFER_SIZE) {
111             return (T) Integer.valueOf(getSystemSendBufferSize());
112         }
113         if (option == SO_RCVBUF) {
114             return (T) Integer.valueOf(getReceiveBufferSize());
115         }
116         if (option == SO_SNDBUF) {
117             return (T) Integer.valueOf(getSendBufferSize());
118         }
119         if (option == SO_REUSEADDR) {
120             return (T) Boolean.valueOf(isReuseAddress());
121         }
122         if (option == SO_LINGER) {
123             return (T) Integer.valueOf(getSoLinger());
124         }
125         return super.getOption(option);
126     }
127 
128     @Override
129     public Map<ChannelOption<?>, Object> getOptions() {
130         return getOptions(super.getOptions(), PROTOCOL_RECEIVE_BUFFER_SIZE,
131                 PROTOCOL_SEND_BUFFER_SIZE, SYSTEM_RECEIVE_BUFFER_SIZE,
132                 SYSTEM_SEND_BUFFER_SIZE, SO_RCVBUF, SO_SNDBUF, SO_REUSEADDR,
133                 SO_LINGER);
134     }
135 
136     @Override
137     public int getReceiveBufferSize() {
138         return allocatorReceiveBufferSize;
139     }
140 
141     @Override
142     public int getSendBufferSize() {
143         return allocatorSendBufferSize;
144     }
145 
146     @Override
147     public int getSoLinger() {
148         return soLinger;
149     }
150 
151     @Override
152     public boolean isReuseAddress() {
153         return reuseAddress;
154     }
155 
156     @Override
157     public UdtChannelConfig setProtocolReceiveBufferSize(final int protocolReceiveBufferSize) {
158         this.protocolReceiveBufferSize = protocolReceiveBufferSize;
159         return this;
160     }
161 
162     @Override
163     public <T> boolean setOption(final ChannelOption<T> option, final T value) {
164         validate(option, value);
165         if (option == PROTOCOL_RECEIVE_BUFFER_SIZE) {
166             setProtocolReceiveBufferSize((Integer) value);
167         } else if (option == PROTOCOL_SEND_BUFFER_SIZE) {
168             setProtocolSendBufferSize((Integer) value);
169         } else if (option == SYSTEM_RECEIVE_BUFFER_SIZE) {
170             setSystemReceiveBufferSize((Integer) value);
171         } else if (option == SYSTEM_SEND_BUFFER_SIZE) {
172             setSystemSendBufferSize((Integer) value);
173         } else if (option == SO_RCVBUF) {
174             setReceiveBufferSize((Integer) value);
175         } else if (option == SO_SNDBUF) {
176             setSendBufferSize((Integer) value);
177         } else if (option == SO_REUSEADDR) {
178             setReuseAddress((Boolean) value);
179         } else if (option == SO_LINGER) {
180             setSoLinger((Integer) value);
181         } else {
182             return super.setOption(option, value);
183         }
184         return true;
185     }
186 
187     @Override
188     public UdtChannelConfig setReceiveBufferSize(final int receiveBufferSize) {
189         allocatorReceiveBufferSize = receiveBufferSize;
190         return this;
191     }
192 
193     @Override
194     public UdtChannelConfig setReuseAddress(final boolean reuseAddress) {
195         this.reuseAddress = reuseAddress;
196         return this;
197     }
198 
199     @Override
200     public UdtChannelConfig setSendBufferSize(final int sendBufferSize) {
201         allocatorSendBufferSize = sendBufferSize;
202         return this;
203     }
204 
205     @Override
206     public UdtChannelConfig setSoLinger(final int soLinger) {
207         this.soLinger = soLinger;
208         return this;
209     }
210 
211     @Override
212     public int getSystemReceiveBufferSize() {
213         return systemReceiveBufferSize;
214     }
215 
216     @Override
217     public UdtChannelConfig setSystemReceiveBufferSize(
218             final int systemReceiveBufferSize) {
219         this.systemReceiveBufferSize = systemReceiveBufferSize;
220         return this;
221     }
222 
223     @Override
224     public int getProtocolSendBufferSize() {
225         return protocolSendBufferSize;
226     }
227 
228     @Override
229     public UdtChannelConfig setProtocolSendBufferSize(
230             final int protocolSendBufferSize) {
231         this.protocolSendBufferSize = protocolSendBufferSize;
232         return this;
233     }
234 
235     @Override
236     public UdtChannelConfig setSystemSendBufferSize(
237             final int systemSendBufferSize) {
238         this.systemSendBufferSize = systemSendBufferSize;
239         return this;
240     }
241 
242     @Override
243     public int getSystemSendBufferSize() {
244         return systemSendBufferSize;
245     }
246 
247     @Override
248     public UdtChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
249         super.setConnectTimeoutMillis(connectTimeoutMillis);
250         return this;
251     }
252 
253     @Override
254     @Deprecated
255     public UdtChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
256         super.setMaxMessagesPerRead(maxMessagesPerRead);
257         return this;
258     }
259 
260     @Override
261     public UdtChannelConfig setWriteSpinCount(int writeSpinCount) {
262         super.setWriteSpinCount(writeSpinCount);
263         return this;
264     }
265 
266     @Override
267     public UdtChannelConfig setAllocator(ByteBufAllocator allocator) {
268         super.setAllocator(allocator);
269         return this;
270     }
271 
272     @Override
273     public UdtChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
274         super.setRecvByteBufAllocator(allocator);
275         return this;
276     }
277 
278     @Override
279     public UdtChannelConfig setAutoRead(boolean autoRead) {
280         super.setAutoRead(autoRead);
281         return this;
282     }
283 
284     @Override
285     public UdtChannelConfig setAutoClose(boolean autoClose) {
286         super.setAutoClose(autoClose);
287         return this;
288     }
289 
290     @Override
291     public UdtChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
292         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
293         return this;
294     }
295 
296     @Override
297     public UdtChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
298         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
299         return this;
300     }
301 
302     @Override
303     public UdtChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
304         super.setWriteBufferWaterMark(writeBufferWaterMark);
305         return this;
306     }
307 
308     @Override
309     public UdtChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
310         super.setMessageSizeEstimator(estimator);
311         return this;
312     }
313 }