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