View Javadoc
1   /*
2    * Copyright 2021 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.kqueue;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelException;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.FixedRecvByteBufAllocator;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.WriteBufferWaterMark;
25  import io.netty.channel.unix.DomainDatagramChannelConfig;
26  import io.netty.util.internal.UnstableApi;
27  
28  import java.io.IOException;
29  import java.util.Map;
30  
31  import static io.netty.channel.ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION;
32  import static io.netty.channel.ChannelOption.SO_SNDBUF;
33  
34  @UnstableApi
35  public final class KQueueDomainDatagramChannelConfig
36          extends KQueueChannelConfig implements DomainDatagramChannelConfig {
37  
38      private boolean activeOnOpen;
39  
40      KQueueDomainDatagramChannelConfig(KQueueDomainDatagramChannel channel) {
41          super(channel, new FixedRecvByteBufAllocator(2048));
42      }
43  
44      @Override
45      @SuppressWarnings("deprecation")
46      public Map<ChannelOption<?>, Object> getOptions() {
47          return getOptions(super.getOptions(),
48                  DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, SO_SNDBUF);
49      }
50  
51      @Override
52      @SuppressWarnings({"unchecked", "deprecation"})
53      public <T> T getOption(ChannelOption<T> option) {
54          if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
55              return (T) Boolean.valueOf(activeOnOpen);
56          }
57          if (option == SO_SNDBUF) {
58              return (T) Integer.valueOf(getSendBufferSize());
59          }
60          return super.getOption(option);
61      }
62  
63      @Override
64      @SuppressWarnings("deprecation")
65      public <T> boolean setOption(ChannelOption<T> option, T value) {
66          validate(option, value);
67  
68          if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
69              setActiveOnOpen((Boolean) value);
70          } else if (option == SO_SNDBUF) {
71              setSendBufferSize((Integer) value);
72          } else {
73              return super.setOption(option, value);
74          }
75  
76          return true;
77      }
78  
79      private void setActiveOnOpen(boolean activeOnOpen) {
80          if (channel.isRegistered()) {
81              throw new IllegalStateException("Can only changed before channel was registered");
82          }
83          this.activeOnOpen = activeOnOpen;
84      }
85  
86      boolean getActiveOnOpen() {
87          return activeOnOpen;
88      }
89  
90      @Override
91      public KQueueDomainDatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
92          super.setAllocator(allocator);
93          return this;
94      }
95  
96      @Override
97      public KQueueDomainDatagramChannelConfig setAutoClose(boolean autoClose) {
98          super.setAutoClose(autoClose);
99          return this;
100     }
101 
102     @Override
103     public KQueueDomainDatagramChannelConfig setAutoRead(boolean autoRead) {
104         super.setAutoRead(autoRead);
105         return this;
106     }
107 
108     @Override
109     public KQueueDomainDatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
110         super.setConnectTimeoutMillis(connectTimeoutMillis);
111         return this;
112     }
113 
114     @Override
115     @Deprecated
116     public KQueueDomainDatagramChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
117         super.setMaxMessagesPerRead(maxMessagesPerRead);
118         return this;
119     }
120 
121     @Override
122     public KQueueDomainDatagramChannelConfig setMaxMessagesPerWrite(int maxMessagesPerWrite) {
123         super.setMaxMessagesPerWrite(maxMessagesPerWrite);
124         return this;
125     }
126 
127     @Override
128     public KQueueDomainDatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
129         super.setMessageSizeEstimator(estimator);
130         return this;
131     }
132 
133     @Override
134     public KQueueDomainDatagramChannelConfig setRcvAllocTransportProvidesGuess(boolean transportProvidesGuess) {
135         super.setRcvAllocTransportProvidesGuess(transportProvidesGuess);
136         return this;
137     }
138 
139     @Override
140     public KQueueDomainDatagramChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
141         super.setRecvByteBufAllocator(allocator);
142         return this;
143     }
144 
145     @Override
146     public KQueueDomainDatagramChannelConfig setSendBufferSize(int sendBufferSize) {
147         try {
148             ((KQueueDomainDatagramChannel) channel).socket.setSendBufferSize(sendBufferSize);
149             return this;
150         } catch (IOException e) {
151             throw new ChannelException(e);
152         }
153     }
154 
155     @Override
156     public int getSendBufferSize() {
157         try {
158             return ((KQueueDomainDatagramChannel) channel).socket.getSendBufferSize();
159         } catch (IOException e) {
160             throw new ChannelException(e);
161         }
162     }
163 
164     @Override
165     public KQueueDomainDatagramChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
166         super.setWriteBufferWaterMark(writeBufferWaterMark);
167         return this;
168     }
169 
170     @Override
171     public KQueueDomainDatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
172         super.setWriteSpinCount(writeSpinCount);
173         return this;
174     }
175 }