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