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
75 private final byte[] acceptedAddress = new byte[26];
76
77 @Override
78 public void connect(SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
79
80 channelPromise.setFailure(new UnsupportedOperationException());
81 }
82
83 @Override
84 void epollInReady() {
85 assert eventLoop().inEventLoop();
86 final ChannelConfig config = config();
87 if (shouldBreakEpollInReady(config)) {
88 clearEpollIn0();
89 return;
90 }
91 final EpollRecvByteAllocatorHandle allocHandle = recvBufAllocHandle();
92 final ChannelPipeline pipeline = pipeline();
93 allocHandle.reset(config);
94 allocHandle.attemptedBytesRead(1);
95
96 Throwable exception = null;
97 try {
98 try {
99 do {
100
101
102
103 allocHandle.lastBytesRead(socket.accept(acceptedAddress));
104 if (allocHandle.lastBytesRead() == -1) {
105
106 break;
107 }
108 allocHandle.incMessagesRead(1);
109
110 readPending = false;
111 pipeline.fireChannelRead(newChildChannel(allocHandle.lastBytesRead(), acceptedAddress, 1,
112 acceptedAddress[0]));
113 } while (allocHandle.continueReading());
114 } catch (Throwable t) {
115 exception = t;
116 }
117 allocHandle.readComplete();
118 pipeline.fireChannelReadComplete();
119
120 if (exception != null) {
121 pipeline.fireExceptionCaught(exception);
122 }
123 } finally {
124 if (shouldStopReading(config)) {
125 clearEpollIn();
126 }
127 }
128 }
129 }
130
131 @Override
132 protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
133 throw new UnsupportedOperationException();
134 }
135 }