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