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.ByteBuf;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelOutboundBuffer;
21  import io.netty.channel.socket.InternetProtocolFamily;
22  import io.netty.channel.socket.ServerSocketChannel;
23  import io.netty.channel.socket.SocketChannel;
24  import io.netty.channel.socket.SocketProtocolFamily;
25  import io.netty.channel.unix.IovArray;
26  
27  import java.net.InetSocketAddress;
28  import java.net.SocketAddress;
29  
30  public final class KQueueSocketChannel extends AbstractKQueueStreamChannel implements SocketChannel {
31      private final KQueueSocketChannelConfig config;
32  
33      public KQueueSocketChannel() {
34          super(null, BsdSocket.newSocketStream(), false);
35          config = new KQueueSocketChannelConfig(this);
36      }
37  
38      /**
39       * @deprecated use {@link KQueueDatagramChannel(SocketProtocolFamily)}
40       */
41      @Deprecated
42      public KQueueSocketChannel(InternetProtocolFamily protocol) {
43          super(null, BsdSocket.newSocketStream(protocol), false);
44          config = new KQueueSocketChannelConfig(this);
45      }
46  
47      public KQueueSocketChannel(SocketProtocolFamily protocol) {
48          super(null, BsdSocket.newSocketStream(protocol), false);
49          config = new KQueueSocketChannelConfig(this);
50      }
51  
52      public KQueueSocketChannel(int fd) {
53          super(new BsdSocket(fd));
54          config = new KQueueSocketChannelConfig(this);
55      }
56  
57      KQueueSocketChannel(Channel parent, BsdSocket fd, InetSocketAddress remoteAddress) {
58          super(parent, fd, remoteAddress);
59          config = new KQueueSocketChannelConfig(this);
60      }
61  
62      @Override
63      public InetSocketAddress remoteAddress() {
64          return (InetSocketAddress) super.remoteAddress();
65      }
66  
67      @Override
68      public InetSocketAddress localAddress() {
69          return (InetSocketAddress) super.localAddress();
70      }
71  
72      @Override
73      public KQueueSocketChannelConfig config() {
74          return config;
75      }
76  
77      @Override
78      public ServerSocketChannel parent() {
79          return (ServerSocketChannel) super.parent();
80      }
81  
82      @Override
83      protected boolean doConnect0(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
84          if (config.isTcpFastOpenConnect()) {
85              ChannelOutboundBuffer outbound = unsafe().outboundBuffer();
86              outbound.addFlush();
87              Object curr;
88              if ((curr = outbound.current()) instanceof ByteBuf) {
89                  ByteBuf initialData = (ByteBuf) curr;
90                  // Don't bother with TCP FastOpen if we don't have any initial data to send anyway.
91                  if (initialData.isReadable()) {
92                      IovArray iov = new IovArray(config.getAllocator().directBuffer());
93                      try {
94                          iov.add(initialData, initialData.readerIndex(), initialData.readableBytes());
95                          int bytesSent = socket.connectx(
96                                  (InetSocketAddress) localAddress, (InetSocketAddress) remoteAddress, iov, true);
97                          writeFilter(true);
98                          outbound.removeBytes(Math.abs(bytesSent));
99                          // The `connectx` method returns a negative number if connection is in-progress.
100                         // So we should return `true` to indicate that connection was established, if it's positive.
101                         return bytesSent > 0;
102                     } finally {
103                         iov.release();
104                     }
105                 }
106             }
107         }
108         return super.doConnect0(remoteAddress, localAddress);
109     }
110 }