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