View Javadoc
1   /*
2    * Copyright 2025 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.uring;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.MessageSizeEstimator;
21  import io.netty.channel.RecvByteBufAllocator;
22  import io.netty.channel.WriteBufferWaterMark;
23  import io.netty.channel.socket.DuplexChannelConfig;
24  import io.netty.channel.unix.DomainSocketChannelConfig;
25  import io.netty.channel.unix.DomainSocketReadMode;
26  import io.netty.util.internal.ObjectUtil;
27  
28  import java.io.IOException;
29  import java.util.Map;
30  import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
31  
32  import static io.netty.channel.ChannelOption.ALLOW_HALF_CLOSURE;
33  import static io.netty.channel.ChannelOption.SO_RCVBUF;
34  import static io.netty.channel.ChannelOption.SO_SNDBUF;
35  import static io.netty.channel.unix.UnixChannelOption.DOMAIN_SOCKET_READ_MODE;
36  
37  final class IoUringDomainSocketChannelConfig extends IoUringStreamChannelConfig
38          implements DomainSocketChannelConfig, DuplexChannelConfig {
39      @SuppressWarnings("checkstyle:LineLength") //Avoid Checkstyle thinking this is too long
40      private static final AtomicReferenceFieldUpdater<IoUringDomainSocketChannelConfig, DomainSocketReadMode> MODE_UPDATER =
41              AtomicReferenceFieldUpdater.newUpdater(IoUringDomainSocketChannelConfig.class, DomainSocketReadMode.class, "mode");
42  
43      private volatile DomainSocketReadMode mode = DomainSocketReadMode.BYTES;
44      private volatile boolean allowHalfClosure;
45  
46      IoUringDomainSocketChannelConfig(IoUringDomainSocketChannel channel) {
47          super(channel);
48      }
49  
50      @Override
51      public Map<ChannelOption<?>, Object> getOptions() {
52          return getOptions(super.getOptions(), DOMAIN_SOCKET_READ_MODE, ALLOW_HALF_CLOSURE, SO_SNDBUF, SO_RCVBUF);
53      }
54  
55      @Override
56      public <T> T getOption(ChannelOption<T> option) {
57          if (option == DOMAIN_SOCKET_READ_MODE) {
58              return (T) getReadMode();
59          }
60          if (option == ALLOW_HALF_CLOSURE) {
61              return (T) Boolean.valueOf(isAllowHalfClosure());
62          }
63          if (option == SO_SNDBUF) {
64              return (T) Integer.valueOf(getSendBufferSize());
65          }
66          if (option == SO_RCVBUF) {
67              return (T) Integer.valueOf(getReceiveBufferSize());
68          }
69          return super.getOption(option);
70      }
71  
72      @Override
73      public <T> boolean setOption(ChannelOption<T> option, T value) {
74          validate(option, value);
75  
76          if (option == DOMAIN_SOCKET_READ_MODE) {
77              setReadMode((DomainSocketReadMode) value);
78          } else if (option == ALLOW_HALF_CLOSURE) {
79              setAllowHalfClosure((Boolean) value);
80          } else if (option == SO_SNDBUF) {
81              setSendBufferSize((Integer) value);
82          } else if (option == SO_RCVBUF) {
83              setReceiveBufferSize((Integer) value);
84          } else {
85              return super.setOption(option, value);
86          }
87  
88          return true;
89      }
90  
91      public int getSendBufferSize() {
92          try {
93              return ((IoUringDomainSocketChannel) channel).socket.getSendBufferSize();
94          } catch (IOException e) {
95              throw new RuntimeException(e);
96          }
97      }
98  
99      public IoUringDomainSocketChannelConfig setSendBufferSize(int sendBufferSize) {
100         try {
101             ((IoUringDomainSocketChannel) channel).socket.setSendBufferSize(sendBufferSize);
102             return this;
103         } catch (IOException e) {
104             throw new RuntimeException(e);
105         }
106     }
107 
108     public int getReceiveBufferSize() {
109         try {
110             return ((IoUringDomainSocketChannel) channel).socket.getReceiveBufferSize();
111         } catch (IOException e) {
112             throw new RuntimeException(e);
113         }
114     }
115 
116     public IoUringDomainSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
117         try {
118             ((IoUringDomainSocketChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
119             return this;
120         } catch (IOException e) {
121             throw new RuntimeException(e);
122         }
123     }
124 
125     @Override
126     public IoUringDomainSocketChannelConfig setReadMode(DomainSocketReadMode mode) {
127         ObjectUtil.checkNotNull(mode, "mode");
128         DomainSocketReadMode expectedMode = mode == DomainSocketReadMode.BYTES ?
129                 DomainSocketReadMode.FILE_DESCRIPTORS : DomainSocketReadMode.BYTES;
130         boolean change = MODE_UPDATER.compareAndSet(this, expectedMode, mode);
131         if (change) {
132             if (channel.isRegistered()) {
133                 // cancel current Read
134                 ((IoUringDomainSocketChannel) channel).autoReadCleared();
135             }
136         }
137         return this;
138     }
139 
140     @Override
141     public DomainSocketReadMode getReadMode() {
142         return mode;
143     }
144 
145     @Override
146     public boolean isAllowHalfClosure() {
147         return allowHalfClosure;
148     }
149 
150     @Override
151     public DuplexChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
152         this.allowHalfClosure = allowHalfClosure;
153         return this;
154     }
155 
156     @Override
157     public IoUringDomainSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
158         super.setMaxMessagesPerRead(maxMessagesPerRead);
159         return this;
160     }
161 
162     @Override
163     public IoUringDomainSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
164         super.setConnectTimeoutMillis(connectTimeoutMillis);
165         return this;
166     }
167 
168     @Override
169     public IoUringDomainSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
170         super.setWriteSpinCount(writeSpinCount);
171         return this;
172     }
173 
174     @Override
175     public IoUringDomainSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
176         super.setAllocator(allocator);
177         return this;
178     }
179 
180     @Override
181     public IoUringDomainSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
182         super.setRecvByteBufAllocator(allocator);
183         return this;
184     }
185 
186     @Override
187     public IoUringDomainSocketChannelConfig setAutoRead(boolean autoRead) {
188         super.setAutoRead(autoRead);
189         return this;
190     }
191 
192     @Override
193     public IoUringDomainSocketChannelConfig setAutoClose(boolean autoClose) {
194         super.setAutoClose(autoClose);
195         return this;
196     }
197 
198     @Override
199     public IoUringDomainSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
200         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
201         return this;
202     }
203 
204     @Override
205     public IoUringDomainSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
206         super.setMessageSizeEstimator(estimator);
207         return this;
208     }
209 
210     @Override
211     public IoUringDomainSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
212         super.setWriteBufferWaterMark(writeBufferWaterMark);
213         return this;
214     }
215 
216     @Override
217     public IoUringDomainSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
218         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
219         return this;
220     }
221 }