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