1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.epoll;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.EventLoop;
20 import io.netty.channel.socket.InternetProtocolFamily;
21 import io.netty.channel.socket.ServerSocketChannel;
22
23 import java.io.IOException;
24 import java.net.InetAddress;
25 import java.net.InetSocketAddress;
26 import java.net.SocketAddress;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Map;
30
31 import static io.netty.channel.epoll.LinuxSocket.newSocketStream;
32 import static io.netty.channel.epoll.Native.IS_SUPPORTING_TCP_FASTOPEN_SERVER;
33 import static io.netty.channel.unix.NativeInetAddress.address;
34
35
36
37
38
39 public final class EpollServerSocketChannel extends AbstractEpollServerChannel implements ServerSocketChannel {
40
41 private final EpollServerSocketChannelConfig config;
42 private volatile Collection<InetAddress> tcpMd5SigAddresses = Collections.emptyList();
43
44 public EpollServerSocketChannel() {
45 this((InternetProtocolFamily) null);
46 }
47
48 public EpollServerSocketChannel(InternetProtocolFamily protocol) {
49 super(newSocketStream(protocol), false);
50 config = new EpollServerSocketChannelConfig(this);
51 }
52
53 public EpollServerSocketChannel(int fd) {
54
55
56 this(new LinuxSocket(fd));
57 }
58
59 EpollServerSocketChannel(LinuxSocket fd) {
60 super(fd);
61 config = new EpollServerSocketChannelConfig(this);
62 }
63
64 EpollServerSocketChannel(LinuxSocket fd, boolean active) {
65 super(fd, active);
66 config = new EpollServerSocketChannelConfig(this);
67 }
68
69 @Override
70 protected boolean isCompatible(EventLoop loop) {
71 return loop instanceof EpollEventLoop;
72 }
73
74 @Override
75 protected void doBind(SocketAddress localAddress) throws Exception {
76 super.doBind(localAddress);
77 final int tcpFastopen;
78 if (IS_SUPPORTING_TCP_FASTOPEN_SERVER && (tcpFastopen = config.getTcpFastopen()) > 0) {
79 socket.setTcpFastOpen(tcpFastopen);
80 }
81 socket.listen(config.getBacklog());
82 active = true;
83 }
84
85 @Override
86 public InetSocketAddress remoteAddress() {
87 return (InetSocketAddress) super.remoteAddress();
88 }
89
90 @Override
91 public InetSocketAddress localAddress() {
92 return (InetSocketAddress) super.localAddress();
93 }
94
95 @Override
96 public EpollServerSocketChannelConfig config() {
97 return config;
98 }
99
100 @Override
101 protected Channel newChildChannel(int fd, byte[] address, int offset, int len) throws Exception {
102 return new EpollSocketChannel(this, new LinuxSocket(fd), address(address, offset, len));
103 }
104
105 Collection<InetAddress> tcpMd5SigAddresses() {
106 return tcpMd5SigAddresses;
107 }
108
109 void setTcpMd5Sig(Map<InetAddress, byte[]> keys) throws IOException {
110
111 synchronized (this) {
112 tcpMd5SigAddresses = TcpMd5Util.newTcpMd5Sigs(this, tcpMd5SigAddresses, keys);
113 }
114 }
115 }