View Javadoc
1   /*
2    * Copyright 2015 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.epoll;
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 EpollDomainSocketChannelConfig extends EpollChannelConfig
37          implements DomainSocketChannelConfig, DuplexChannelConfig {
38      private volatile DomainSocketReadMode mode = DomainSocketReadMode.BYTES;
39      private volatile boolean allowHalfClosure;
40  
41      EpollDomainSocketChannelConfig(AbstractEpollChannel 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      @Deprecated
89      public EpollDomainSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
90          super.setMaxMessagesPerRead(maxMessagesPerRead);
91          return this;
92      }
93  
94      @Override
95      public EpollDomainSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
96          super.setConnectTimeoutMillis(connectTimeoutMillis);
97          return this;
98      }
99  
100     @Override
101     public EpollDomainSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
102         super.setWriteSpinCount(writeSpinCount);
103         return this;
104     }
105 
106     @Override
107     public EpollDomainSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
108         super.setRecvByteBufAllocator(allocator);
109         return this;
110     }
111 
112     @Override
113     public EpollDomainSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
114         super.setAllocator(allocator);
115         return this;
116     }
117 
118     @Override
119     public EpollDomainSocketChannelConfig setAutoClose(boolean autoClose) {
120         super.setAutoClose(autoClose);
121         return this;
122     }
123 
124     @Override
125     public EpollDomainSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
126         super.setMessageSizeEstimator(estimator);
127         return this;
128     }
129 
130     @Override
131     @Deprecated
132     public EpollDomainSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
133         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
134         return this;
135     }
136 
137     @Override
138     @Deprecated
139     public EpollDomainSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
140         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
141         return this;
142     }
143 
144     @Override
145     public EpollDomainSocketChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
146         super.setWriteBufferWaterMark(writeBufferWaterMark);
147         return this;
148     }
149 
150     @Override
151     public EpollDomainSocketChannelConfig setAutoRead(boolean autoRead) {
152         super.setAutoRead(autoRead);
153         return this;
154     }
155 
156     @Override
157     public EpollDomainSocketChannelConfig setEpollMode(EpollMode mode) {
158         super.setEpollMode(mode);
159         return this;
160     }
161 
162     @Override
163     public EpollDomainSocketChannelConfig 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     @Override
174     public boolean isAllowHalfClosure() {
175         return allowHalfClosure;
176     }
177 
178     @Override
179     public EpollDomainSocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
180         this.allowHalfClosure = allowHalfClosure;
181         return this;
182     }
183 
184     public int getSendBufferSize() {
185         try {
186             return ((EpollDomainSocketChannel) channel).socket.getSendBufferSize();
187         } catch (IOException e) {
188             throw new RuntimeException(e);
189         }
190     }
191 
192     public EpollDomainSocketChannelConfig setSendBufferSize(int sendBufferSize) {
193         try {
194             ((EpollDomainSocketChannel) channel).socket.setSendBufferSize(sendBufferSize);
195             return this;
196         } catch (IOException e) {
197             throw new RuntimeException(e);
198         }
199     }
200 
201     public int getReceiveBufferSize() {
202         try {
203             return ((EpollDomainSocketChannel) channel).socket.getReceiveBufferSize();
204         } catch (IOException e) {
205             throw new RuntimeException(e);
206         }
207     }
208 
209     public EpollDomainSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
210         try {
211             ((EpollDomainSocketChannel) channel).socket.setReceiveBufferSize(receiveBufferSize);
212             return this;
213         } catch (IOException e) {
214             throw new RuntimeException(e);
215         }
216     }
217 }