View Javadoc
1   /*
2    * Copyright 2020 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.handler.codec.quic;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.DefaultChannelConfig;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.WriteBufferWaterMark;
25  import org.jetbrains.annotations.Nullable;
26  
27  import java.util.Map;
28  
29  /**
30   * Default {@link QuicChannelConfig} implementation.
31   */
32  final class QuicheQuicChannelConfig extends DefaultChannelConfig implements QuicChannelConfig {
33  
34      private volatile QLogConfiguration qLogConfiguration;
35      private volatile SegmentedDatagramPacketAllocator segmentedDatagramPacketAllocator =
36              SegmentedDatagramPacketAllocator.NONE;
37  
38      QuicheQuicChannelConfig(Channel channel) {
39          super(channel);
40      }
41  
42      @Override
43      public Map<ChannelOption<?>, Object> getOptions() {
44          return getOptions(super.getOptions(),
45                  QuicChannelOption.QLOG, QuicChannelOption.SEGMENTED_DATAGRAM_PACKET_ALLOCATOR);
46      }
47  
48      @SuppressWarnings("unchecked")
49      @Override
50      public <T> T getOption(ChannelOption<T> option) {
51          if (option == QuicChannelOption.QLOG) {
52              return (T) getQLogConfiguration();
53          }
54          if (option == QuicChannelOption.SEGMENTED_DATAGRAM_PACKET_ALLOCATOR) {
55              return (T) getSegmentedDatagramPacketAllocator();
56          }
57          return super.getOption(option);
58      }
59  
60      @Override
61      public <T> boolean setOption(ChannelOption<T> option, T value) {
62          if (option == QuicChannelOption.QLOG) {
63              setQLogConfiguration((QLogConfiguration) value);
64              return true;
65          }
66          if (option == QuicChannelOption.SEGMENTED_DATAGRAM_PACKET_ALLOCATOR) {
67              setSegmentedDatagramPacketAllocator((SegmentedDatagramPacketAllocator) value);
68              return true;
69          }
70          return super.setOption(option, value);
71      }
72  
73      @Override
74      public QuicChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
75          super.setConnectTimeoutMillis(connectTimeoutMillis);
76          return this;
77      }
78  
79      @Override
80      @Deprecated
81      public QuicChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
82          super.setMaxMessagesPerRead(maxMessagesPerRead);
83          return this;
84      }
85  
86      @Override
87      public QuicChannelConfig setWriteSpinCount(int writeSpinCount) {
88          super.setWriteSpinCount(writeSpinCount);
89          return this;
90      }
91  
92      @Override
93      public QuicChannelConfig setAllocator(ByteBufAllocator allocator) {
94          super.setAllocator(allocator);
95          return this;
96      }
97  
98      @Override
99      public QuicChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
100         super.setRecvByteBufAllocator(allocator);
101         return this;
102     }
103 
104     @Override
105     public QuicChannelConfig setAutoRead(boolean autoRead) {
106         super.setAutoRead(autoRead);
107         return this;
108     }
109 
110     @Override
111     public QuicChannelConfig setAutoClose(boolean autoClose) {
112         super.setAutoClose(autoClose);
113         return this;
114     }
115 
116     @Override
117     public QuicChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
118         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
119         return this;
120     }
121 
122     @Override
123     public QuicChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
124         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
125         return this;
126     }
127 
128     @Override
129     public QuicChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
130         super.setWriteBufferWaterMark(writeBufferWaterMark);
131         return this;
132     }
133 
134     @Override
135     public QuicChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
136         super.setMessageSizeEstimator(estimator);
137         return this;
138     }
139 
140     @Nullable
141     QLogConfiguration getQLogConfiguration() {
142         return qLogConfiguration;
143     }
144 
145     private void setQLogConfiguration(QLogConfiguration qLogConfiguration) {
146         if (channel.isRegistered()) {
147             throw new IllegalStateException("QLOG can only be enabled before the Channel was registered");
148         }
149         this.qLogConfiguration = qLogConfiguration;
150     }
151 
152     SegmentedDatagramPacketAllocator getSegmentedDatagramPacketAllocator() {
153         return segmentedDatagramPacketAllocator;
154     }
155 
156     private void setSegmentedDatagramPacketAllocator(
157             SegmentedDatagramPacketAllocator segmentedDatagramPacketAllocator) {
158         this.segmentedDatagramPacketAllocator = segmentedDatagramPacketAllocator;
159     }
160 }