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