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