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