1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.oio;
17
18 import io.netty.channel.AbstractChannel;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelPromise;
21 import io.netty.channel.EventLoop;
22 import io.netty.channel.ThreadPerChannelEventLoop;
23
24 import java.net.SocketAddress;
25
26
27
28
29
30
31 @Deprecated
32 public abstract class AbstractOioChannel extends AbstractChannel {
33
34 protected static final int SO_TIMEOUT = 1000;
35
36 boolean readPending;
37 boolean readWhenInactive;
38 final Runnable readTask = new Runnable() {
39 @Override
40 public void run() {
41 doRead();
42 }
43 };
44 private final Runnable clearReadPendingRunnable = new Runnable() {
45 @Override
46 public void run() {
47 readPending = false;
48 }
49 };
50
51
52
53
54 protected AbstractOioChannel(Channel parent) {
55 super(parent);
56 }
57
58 @Override
59 protected AbstractUnsafe newUnsafe() {
60 return new DefaultOioUnsafe();
61 }
62
63 private final class DefaultOioUnsafe extends AbstractUnsafe {
64 @Override
65 public void connect(
66 final SocketAddress remoteAddress,
67 final SocketAddress localAddress, final ChannelPromise promise) {
68 if (!promise.setUncancellable() || !ensureOpen(promise)) {
69 return;
70 }
71
72 try {
73 boolean wasActive = isActive();
74 doConnect(remoteAddress, localAddress);
75
76
77
78 boolean active = isActive();
79
80 safeSetSuccess(promise);
81 if (!wasActive && active) {
82 pipeline().fireChannelActive();
83 }
84 } catch (Throwable t) {
85 safeSetFailure(promise, annotateConnectException(t, remoteAddress));
86 closeIfClosed();
87 }
88 }
89 }
90
91 @Override
92 protected boolean isCompatible(EventLoop loop) {
93 return loop instanceof ThreadPerChannelEventLoop;
94 }
95
96
97
98
99 protected abstract void doConnect(
100 SocketAddress remoteAddress, SocketAddress localAddress) throws Exception;
101
102 @Override
103 protected void doBeginRead() throws Exception {
104 if (readPending) {
105 return;
106 }
107 if (!isActive()) {
108 readWhenInactive = true;
109 return;
110 }
111
112 readPending = true;
113 eventLoop().execute(readTask);
114 }
115
116 protected abstract void doRead();
117
118
119
120
121
122 @Deprecated
123 protected boolean isReadPending() {
124 return readPending;
125 }
126
127
128
129
130
131 @Deprecated
132 protected void setReadPending(final boolean readPending) {
133 if (isRegistered()) {
134 EventLoop eventLoop = eventLoop();
135 if (eventLoop.inEventLoop()) {
136 this.readPending = readPending;
137 } else {
138 eventLoop.execute(new Runnable() {
139 @Override
140 public void run() {
141 AbstractOioChannel.this.readPending = readPending;
142 }
143 });
144 }
145 } else {
146 this.readPending = readPending;
147 }
148 }
149
150
151
152
153 protected final void clearReadPending() {
154 if (isRegistered()) {
155 EventLoop eventLoop = eventLoop();
156 if (eventLoop.inEventLoop()) {
157 readPending = false;
158 } else {
159 eventLoop.execute(clearReadPendingRunnable);
160 }
161 } else {
162
163 readPending = false;
164 }
165 }
166 }