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    *   http://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.channel.Channel;
19  import io.netty.channel.ChannelConfig;
20  import io.netty.channel.ChannelOutboundBuffer;
21  import io.netty.channel.ChannelPipeline;
22  import io.netty.channel.unix.DomainSocketAddress;
23  import io.netty.channel.unix.DomainSocketChannel;
24  import io.netty.channel.unix.FileDescriptor;
25  import io.netty.channel.unix.PeerCredentials;
26  import io.netty.util.internal.UnstableApi;
27  
28  import java.io.IOException;
29  import java.net.SocketAddress;
30  
31  import static io.netty.channel.epoll.LinuxSocket.newSocketDomain;
32  
33  public final class EpollDomainSocketChannel extends AbstractEpollStreamChannel implements DomainSocketChannel {
34      private final EpollDomainSocketChannelConfig config = new EpollDomainSocketChannelConfig(this);
35  
36      private volatile DomainSocketAddress local;
37      private volatile DomainSocketAddress remote;
38  
39      public EpollDomainSocketChannel() {
40          super(newSocketDomain(), false);
41      }
42  
43      EpollDomainSocketChannel(Channel parent, FileDescriptor fd) {
44          super(parent, new LinuxSocket(fd.intValue()));
45      }
46  
47      public EpollDomainSocketChannel(int fd) {
48          super(fd);
49      }
50  
51      public EpollDomainSocketChannel(Channel parent, LinuxSocket fd) {
52          super(parent, fd);
53      }
54  
55      public EpollDomainSocketChannel(int fd, boolean active) {
56          super(new LinuxSocket(fd), active);
57      }
58  
59      @Override
60      protected AbstractEpollUnsafe newUnsafe() {
61          return new EpollDomainUnsafe();
62      }
63  
64      @Override
65      protected DomainSocketAddress localAddress0() {
66          return local;
67      }
68  
69      @Override
70      protected DomainSocketAddress remoteAddress0() {
71          return remote;
72      }
73  
74      @Override
75      protected void doBind(SocketAddress localAddress) throws Exception {
76          socket.bind(localAddress);
77          local = (DomainSocketAddress) localAddress;
78      }
79  
80      @Override
81      public EpollDomainSocketChannelConfig config() {
82          return config;
83      }
84  
85      @Override
86      protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
87          if (super.doConnect(remoteAddress, localAddress)) {
88              local = (DomainSocketAddress) localAddress;
89              remote = (DomainSocketAddress) remoteAddress;
90              return true;
91          }
92          return false;
93      }
94  
95      @Override
96      public DomainSocketAddress remoteAddress() {
97          return (DomainSocketAddress) super.remoteAddress();
98      }
99  
100     @Override
101     public DomainSocketAddress localAddress() {
102         return (DomainSocketAddress) super.localAddress();
103     }
104 
105     @Override
106     protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception {
107         Object msg = in.current();
108         if (msg instanceof FileDescriptor && socket.sendFd(((FileDescriptor) msg).intValue()) > 0) {
109             // File descriptor was written, so remove it.
110             in.remove();
111             return 1;
112         }
113         return super.doWriteSingle(in);
114     }
115 
116     @Override
117     protected Object filterOutboundMessage(Object msg) {
118         if (msg instanceof FileDescriptor) {
119             return msg;
120         }
121         return super.filterOutboundMessage(msg);
122     }
123 
124     /**
125      * Returns the unix credentials (uid, gid, pid) of the peer
126      * <a href=http://man7.org/linux/man-pages/man7/socket.7.html>SO_PEERCRED</a>
127      */
128     @UnstableApi
129     public PeerCredentials peerCredentials() throws IOException {
130         return socket.getPeerCredentials();
131     }
132 
133     private final class EpollDomainUnsafe extends EpollStreamUnsafe {
134         @Override
135         void epollInReady() {
136             switch (config().getReadMode()) {
137                 case BYTES:
138                     super.epollInReady();
139                     break;
140                 case FILE_DESCRIPTORS:
141                     epollInReadFd();
142                     break;
143                 default:
144                     throw new Error();
145             }
146         }
147 
148         private void epollInReadFd() {
149             if (socket.isInputShutdown()) {
150                 clearEpollIn0();
151                 return;
152             }
153             final ChannelConfig config = config();
154             final EpollRecvByteAllocatorHandle allocHandle = recvBufAllocHandle();
155             allocHandle.edgeTriggered(isFlagSet(Native.EPOLLET));
156 
157             final ChannelPipeline pipeline = pipeline();
158             allocHandle.reset(config);
159             epollInBefore();
160 
161             try {
162                 readLoop: do {
163                     // lastBytesRead represents the fd. We use lastBytesRead because it must be set so that the
164                     // EpollRecvByteAllocatorHandle knows if it should try to read again or not when autoRead is
165                     // enabled.
166                     allocHandle.lastBytesRead(socket.recvFd());
167                     switch(allocHandle.lastBytesRead()) {
168                     case 0:
169                         break readLoop;
170                     case -1:
171                         close(voidPromise());
172                         return;
173                     default:
174                         allocHandle.incMessagesRead(1);
175                         readPending = false;
176                         pipeline.fireChannelRead(new FileDescriptor(allocHandle.lastBytesRead()));
177                         break;
178                     }
179                 } while (allocHandle.continueReading());
180 
181                 allocHandle.readComplete();
182                 pipeline.fireChannelReadComplete();
183             } catch (Throwable t) {
184                 allocHandle.readComplete();
185                 pipeline.fireChannelReadComplete();
186                 pipeline.fireExceptionCaught(t);
187             } finally {
188                 epollInFinally(config);
189             }
190         }
191     }
192 }