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.ChannelConfig;
20 import io.netty.channel.ChannelMetadata;
21 import io.netty.channel.ChannelOutboundBuffer;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.ChannelPromise;
24 import io.netty.channel.EventLoop;
25 import io.netty.channel.ServerChannel;
26
27 import java.net.InetSocketAddress;
28 import java.net.SocketAddress;
29
30 public abstract class AbstractEpollServerChannel extends AbstractEpollChannel implements ServerChannel {
31 private static final ChannelMetadata METADATA = new ChannelMetadata(false, 16);
32
33 protected AbstractEpollServerChannel(int fd) {
34 this(new LinuxSocket(fd), false);
35 }
36
37 protected AbstractEpollServerChannel(LinuxSocket fd) {
38 this(fd, isSoErrorZero(fd));
39 }
40
41 protected AbstractEpollServerChannel(LinuxSocket fd, boolean active) {
42 super(null, fd, active);
43 }
44
45 @Override
46 public ChannelMetadata metadata() {
47 return METADATA;
48 }
49
50 @Override
51 protected boolean isCompatible(EventLoop loop) {
52 return loop instanceof EpollEventLoop;
53 }
54
55 @Override
56 protected InetSocketAddress remoteAddress0() {
57 return null;
58 }
59
60 @Override
61 protected AbstractEpollUnsafe newUnsafe() {
62 return new EpollServerSocketUnsafe();
63 }
64
65 @Override
66 protected void doWrite(ChannelOutboundBuffer in) throws Exception {
67 throw new UnsupportedOperationException();
68 }
69
70 @Override
71 protected Object filterOutboundMessage(Object msg) throws Exception {
72 throw new UnsupportedOperationException();
73 }
74
75 protected abstract Channel newChildChannel(int fd, byte[] remote, int offset, int len) throws Exception;
76
77 final class EpollServerSocketUnsafe extends AbstractEpollUnsafe {
78
79
80
81 private final byte[] acceptedAddress = new byte[26];
82
83 @Override
84 public void connect(SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
85
86 channelPromise.setFailure(new UnsupportedOperationException());
87 }
88
89 @Override
90 void epollInReady() {
91 assert eventLoop().inEventLoop();
92 final ChannelConfig config = config();
93 if (shouldBreakEpollInReady(config)) {
94 clearEpollIn0();
95 return;
96 }
97 final EpollRecvByteAllocatorHandle allocHandle = recvBufAllocHandle();
98 allocHandle.edgeTriggered(isFlagSet(Native.EPOLLET));
99
100 final ChannelPipeline pipeline = pipeline();
101 allocHandle.reset(config);
102 allocHandle.attemptedBytesRead(1);
103 epollInBefore();
104
105 Throwable exception = null;
106 try {
107 try {
108 do {
109
110
111
112 allocHandle.lastBytesRead(socket.accept(acceptedAddress));
113 if (allocHandle.lastBytesRead() == -1) {
114
115 break;
116 }
117 allocHandle.incMessagesRead(1);
118
119 readPending = false;
120 pipeline.fireChannelRead(newChildChannel(allocHandle.lastBytesRead(), acceptedAddress, 1,
121 acceptedAddress[0]));
122 } while (allocHandle.continueReading());
123 } catch (Throwable t) {
124 exception = t;
125 }
126 allocHandle.readComplete();
127 pipeline.fireChannelReadComplete();
128
129 if (exception != null) {
130 pipeline.fireExceptionCaught(exception);
131 }
132 } finally {
133 epollInFinally(config);
134 }
135 }
136 }
137
138 @Override
139 protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
140 throw new UnsupportedOperationException();
141 }
142 }