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