1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
100
101 return bytesSent > 0;
102 } finally {
103 iov.release();
104 }
105 }
106 }
107 }
108 return super.doConnect0(remoteAddress, localAddress);
109 }
110 }