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.DefaultChannelConfig;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.WriteBufferWaterMark;
25  import io.netty.channel.unix.IntegerUnixChannelOption;
26  import io.netty.channel.unix.RawUnixChannelOption;
27  
28  import java.io.IOException;
29  import java.nio.ByteBuffer;
30  
31  abstract class IoUringChannelConfig extends DefaultChannelConfig {
32      IoUringChannelConfig(AbstractIoUringChannel channel) {
33          super(channel);
34      }
35  
36      IoUringChannelConfig(AbstractIoUringChannel channel, RecvByteBufAllocator allocator) {
37          super(channel, allocator);
38      }
39  
40      @SuppressWarnings("unchecked")
41      @Override
42      public <T> T getOption(ChannelOption<T> option) {
43          try {
44              if (option instanceof IntegerUnixChannelOption) {
45                  IntegerUnixChannelOption opt = (IntegerUnixChannelOption) option;
46                  return (T) Integer.valueOf(((AbstractIoUringChannel) channel).socket.getIntOpt(
47                          opt.level(), opt.optname()));
48              }
49              if (option instanceof RawUnixChannelOption) {
50                  RawUnixChannelOption opt = (RawUnixChannelOption) option;
51                  ByteBuffer out = ByteBuffer.allocate(opt.length());
52                  ((AbstractIoUringChannel) channel).socket.getRawOpt(opt.level(), opt.optname(), out);
53                  return (T) out.flip();
54              }
55          } catch (IOException e) {
56              throw new ChannelException(e);
57          }
58          return super.getOption(option);
59      }
60  
61      @Override
62      public <T> boolean setOption(ChannelOption<T> option, T value) {
63          validate(option, value);
64          try {
65              if (option instanceof IntegerUnixChannelOption) {
66                  IntegerUnixChannelOption opt = (IntegerUnixChannelOption) option;
67                  ((AbstractIoUringChannel) channel).socket.setIntOpt(opt.level(), opt.optname(), (Integer) value);
68                  return true;
69              } else if (option instanceof RawUnixChannelOption) {
70                  RawUnixChannelOption opt = (RawUnixChannelOption) option;
71                  ((AbstractIoUringChannel) channel).socket.setRawOpt(opt.level(), opt.optname(), (ByteBuffer) value);
72                  return true;
73              }
74          } catch (IOException e) {
75              throw new ChannelException(e);
76          }
77          return super.setOption(option, value);
78      }
79  
80      @Override
81      public IoUringChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
82          super.setMaxMessagesPerRead(maxMessagesPerRead);
83          return this;
84      }
85  
86      @Override
87      public IoUringChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
88          super.setConnectTimeoutMillis(connectTimeoutMillis);
89          return this;
90      }
91  
92      @Override
93      public IoUringChannelConfig setMaxMessagesPerWrite(int maxMessagesPerWrite) {
94          super.setMaxMessagesPerWrite(maxMessagesPerWrite);
95          return this;
96      }
97  
98      @Override
99      public IoUringChannelConfig setWriteSpinCount(int writeSpinCount) {
100         super.setWriteSpinCount(writeSpinCount);
101         return this;
102     }
103 
104     @Override
105     public IoUringChannelConfig setAllocator(ByteBufAllocator allocator) {
106         super.setAllocator(allocator);
107         return this;
108     }
109 
110     @Override
111     public IoUringChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
112         super.setRecvByteBufAllocator(allocator);
113         return this;
114     }
115 
116     @Override
117     public IoUringChannelConfig setAutoRead(boolean autoRead) {
118         super.setAutoRead(autoRead);
119         return this;
120     }
121 
122     @Override
123     public IoUringChannelConfig setAutoClose(boolean autoClose) {
124         super.setAutoClose(autoClose);
125         return this;
126     }
127 
128     @Override
129     public IoUringChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
130         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
131         return this;
132     }
133 
134     @Override
135     public IoUringChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
136         super.setMessageSizeEstimator(estimator);
137         return this;
138     }
139 
140     @Override
141     public IoUringChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
142         super.setWriteBufferWaterMark(writeBufferWaterMark);
143         return this;
144     }
145 
146     @Override
147     public IoUringChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
148         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
149         return this;
150     }
151 
152     @Override
153     protected void autoReadCleared() {
154         ((AbstractIoUringChannel) channel).autoReadCleared();
155     }
156 }