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.unix.DomainSocketAddress;
20 import io.netty.channel.unix.ServerDomainSocketChannel;
21 import io.netty.channel.unix.Socket;
22 import io.netty.util.internal.logging.InternalLogger;
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24
25 import java.io.File;
26 import java.net.SocketAddress;
27
28 import static io.netty.channel.epoll.LinuxSocket.newSocketDomain;
29
30 public final class EpollServerDomainSocketChannel extends AbstractEpollServerChannel
31 implements ServerDomainSocketChannel {
32 private static final InternalLogger logger = InternalLoggerFactory.getInstance(
33 EpollServerDomainSocketChannel.class);
34
35 private final EpollServerChannelConfig config = new EpollServerChannelConfig(this);
36 private volatile DomainSocketAddress local;
37
38 public EpollServerDomainSocketChannel() {
39 super(newSocketDomain(), false);
40 }
41
42 public EpollServerDomainSocketChannel(int fd) {
43 super(fd);
44 }
45
46 public EpollServerDomainSocketChannel(int fd, boolean active) {
47 super(new LinuxSocket(fd), active);
48 }
49
50 EpollServerDomainSocketChannel(LinuxSocket fd) {
51 super(fd);
52 }
53
54 EpollServerDomainSocketChannel(LinuxSocket fd, boolean active) {
55 super(fd, active);
56 }
57
58 @Override
59 protected Channel newChildChannel(int fd, byte[] addr, int offset, int len) throws Exception {
60 return new EpollDomainSocketChannel(this, new Socket(fd));
61 }
62
63 @Override
64 protected DomainSocketAddress localAddress0() {
65 return local;
66 }
67
68 @Override
69 protected void doBind(SocketAddress localAddress) throws Exception {
70 socket.bind(localAddress);
71 socket.listen(config.getBacklog());
72 local = (DomainSocketAddress) localAddress;
73 active = true;
74
75 submitCurrentOps();
76 }
77
78 @Override
79 protected void doClose() throws Exception {
80 try {
81 super.doClose();
82 } finally {
83 DomainSocketAddress local = this.local;
84 if (local != null) {
85
86 File socketFile = new File(local.path());
87 boolean success = socketFile.delete();
88 if (!success && logger.isDebugEnabled()) {
89 logger.debug("Failed to delete a domain socket file: {}", local.path());
90 }
91 }
92 }
93 }
94
95 @Override
96 public EpollServerChannelConfig config() {
97 return config;
98 }
99
100 @Override
101 public DomainSocketAddress remoteAddress() {
102 return (DomainSocketAddress) super.remoteAddress();
103 }
104
105 @Override
106 public DomainSocketAddress localAddress() {
107 return (DomainSocketAddress) super.localAddress();
108 }
109 }