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