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.epoll;
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 EpollDomainDatagramChannelConfig extends EpollChannelConfig implements DomainDatagramChannelConfig {
36  
37      private boolean activeOnOpen;
38  
39      EpollDomainDatagramChannelConfig(EpollDomainDatagramChannel channel) {
40          super(channel, new FixedRecvByteBufAllocator(2048));
41      }
42  
43      @Override
44      @SuppressWarnings("deprecation")
45      public Map<ChannelOption<?>, Object> getOptions() {
46          return getOptions(
47                  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 EpollDomainDatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
92          super.setAllocator(allocator);
93          return this;
94      }
95  
96      @Override
97      public EpollDomainDatagramChannelConfig setAutoClose(boolean autoClose) {
98          super.setAutoClose(autoClose);
99          return this;
100     }
101 
102     @Override
103     public EpollDomainDatagramChannelConfig setAutoRead(boolean autoRead) {
104         super.setAutoRead(autoRead);
105         return this;
106     }
107 
108     @Override
109     public EpollDomainDatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
110         super.setConnectTimeoutMillis(connectTimeoutMillis);
111         return this;
112     }
113 
114     @Override
115     public EpollDomainDatagramChannelConfig setEpollMode(EpollMode mode) {
116         super.setEpollMode(mode);
117         return this;
118     }
119 
120     @Override
121     @Deprecated
122     public EpollDomainDatagramChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
123         super.setMaxMessagesPerRead(maxMessagesPerRead);
124         return this;
125     }
126 
127     @Override
128     public EpollDomainDatagramChannelConfig setMaxMessagesPerWrite(int maxMessagesPerWrite) {
129         super.setMaxMessagesPerWrite(maxMessagesPerWrite);
130         return this;
131     }
132 
133     @Override
134     public EpollDomainDatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
135         super.setMessageSizeEstimator(estimator);
136         return this;
137     }
138 
139     @Override
140     public EpollDomainDatagramChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
141         super.setRecvByteBufAllocator(allocator);
142         return this;
143     }
144 
145     @Override
146     public EpollDomainDatagramChannelConfig setSendBufferSize(int sendBufferSize) {
147         try {
148             ((EpollDomainDatagramChannel) 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 ((EpollDomainDatagramChannel) channel).socket.getSendBufferSize();
159         } catch (IOException e) {
160             throw new ChannelException(e);
161         }
162     }
163 
164     @Override
165     public EpollDomainDatagramChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
166         super.setWriteBufferWaterMark(writeBufferWaterMark);
167         return this;
168     }
169 
170     @Override
171     public EpollDomainDatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
172         super.setWriteSpinCount(writeSpinCount);
173         return this;
174     }
175 }