-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessor.java
More file actions
211 lines (188 loc) · 7.87 KB
/
Processor.java
File metadata and controls
211 lines (188 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.janekey.httpserver.net;
import com.janekey.httpserver.util.Millisecond100Clock;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* User: janekey
* Date: 14-11-14
* Time: 下午5:21
*/
public class Processor implements Runnable {
protected static final int WRITE_SPIN_COUNT = 32;
protected static final int SELECT_TIMEOUT = 100;
protected static final int IO_TIMEOUT_CHECK_INTERVAL = 5000;
protected Selector selector;
private Filter filter;
private Handler handler;
protected ConcurrentLinkedQueue<Ternary<Integer, SocketChannel, Integer>> toRegister;
protected ConcurrentLinkedQueue<SelectionKey> toCancel;
protected ConcurrentLinkedQueue<SelectionKey> toWrite;
protected long lastIoTimeoutCheckTime;
public Processor(String name, Filter filter, Handler handler, int num) {
try {
this.filter = filter;
this.handler = handler;
selector = Selector.open();
toRegister = new ConcurrentLinkedQueue<Ternary<Integer, SocketChannel, Integer>>();
toCancel = new ConcurrentLinkedQueue<SelectionKey>();
toWrite = new ConcurrentLinkedQueue<SelectionKey>();
new Thread(this, name + "-Processor-" + num).start();
} catch (Throwable th) {
Logger.log(th, "Processor Construct Error");
throw new RuntimeException(th);
}
}
@Override
public void run() {
try {
lastIoTimeoutCheckTime = Millisecond100Clock.currentTimeMillis();
while (true) {
int n = selector.select(SELECT_TIMEOUT);
long now = Millisecond100Clock.currentTimeMillis();
Ternary<Integer, SocketChannel, Integer> register;
while ((register = toRegister.poll()) != null) register(register, now);
if (n > 0) {
Iterator<SelectionKey> readyKeys = selector.selectedKeys().iterator();
now = Millisecond100Clock.currentTimeMillis();
SelectionKey key;
while (readyKeys.hasNext()) {
key = readyKeys.next();
if (key.isReadable()) read(key, now);
if (key.isWritable()) ((Session) key.attachment()).scheduleWrite(true);
readyKeys.remove();
}
}
now = Millisecond100Clock.currentTimeMillis();
SelectionKey writableKey;
while ((writableKey = toWrite.poll()) != null) write(writableKey, now);
SelectionKey cancelableKey;
while ((cancelableKey = toCancel.poll()) != null) cancel(cancelableKey);
now = Millisecond100Clock.currentTimeMillis();
if (now - lastIoTimeoutCheckTime > IO_TIMEOUT_CHECK_INTERVAL) {
lastIoTimeoutCheckTime = now;
for (SelectionKey everyKey : selector.keys()) checkIoTimeout(everyKey, now);
}
}
} catch (Throwable th) {
Logger.log(th, "Processor run error");
}
}
private void checkIoTimeout(SelectionKey key, long now) {
if (!key.isValid()) return;
Session session = (Session) key.attachment();
int ioTimeoutMillis = session.getIoTimeoutMillis();
if (ioTimeoutMillis < 0) return;
long lastIoTime = Math.max(session.getOpenTime(), Math.max(session.getLastReadTime(), session.getLastWriteTime()));
if (now - lastIoTime > ioTimeoutMillis) {
Logger.log("connection io timeout", ioTimeoutMillis);
session.close(true);
}
}
public void scheduleRegister(SocketChannel socketChannel, int sessionId, int ioTimeoutMillis) {
toRegister.offer(new Ternary<Integer, SocketChannel, Integer>(sessionId, socketChannel, ioTimeoutMillis));
selector.wakeup();
}
protected void register(Ternary<Integer, SocketChannel, Integer> register, long now) {
try {
int sessionId = register.first;
SocketChannel socketChannel = register.second;
int ioTimeoutMillis = register.third;
socketChannel.configureBlocking(false);
SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ);
Session session = new Session(sessionId, this, key, ioTimeoutMillis);
session.setOpenTime(now);
key.attach(session);
} catch (Throwable th) {
Logger.log(th, "register");
}
}
private void read(SelectionKey key, long now) {
Session session = (Session) key.attachment();
try {
SocketChannel channel = (SocketChannel) key.channel();
int size = session.getReadBufferSize();
ByteBuffer buffer = ByteBuffer.allocate(size);
int read = 0;
int n;
while ((n = channel.read(buffer)) > 0) read += n;
if (read > 0) {
buffer.flip();
filter.decode(session, buffer, handler);
if (read == size) session.increaseReadBufferSize();
else if (read < size >>> 1) session.decreaseReadBufferSize();
session.setLastReadTime(now);
session.increaseReadBytes(read);
} else {
session.decreaseReadBufferSize();
}
if (n < 0) {
session.close(false);
}
} catch (Throwable th) {
session.close(true);
th.printStackTrace();
}
}
public void scheduleCancel(SelectionKey selectionKey) {
toCancel.offer(selectionKey);
}
protected void cancel(SelectionKey selectionKey) {
try {
if (!selectionKey.isValid()) return;
// Session session=(Session)selectionKey.attachment();
selectionKey.cancel();
selectionKey.attach(null);
selectionKey.channel().close();
} catch (Throwable th) {
Logger.log(th, "processor cancel error");
}
}
public void scheduleWrite(SelectionKey key, boolean isWakeUp) {
boolean isEmpty = toWrite.isEmpty();
toWrite.offer(key);
if (isWakeUp && isEmpty) {
selector.wakeup();
}
}
protected void write(SelectionKey key, long now) {
if (!key.isValid()) return;
Session session = (Session) key.attachment();
try {
// session.scheduleWrite(false);
setInterestedInWrite(key, false);
SocketChannel channel = (SocketChannel) key.channel();
int write = 0;
ConcurrentLinkedQueue<ByteBuffer> queue = session.getWriteQueue();
ByteBuffer buff = null;
while ((buff = queue.peek()) != null) {
if (buff == Session.CLOSE_FLAG) {
queue.clear();
scheduleCancel(key);
buff = null;
break;
}
int n = 0;
for (int i = 0; i < WRITE_SPIN_COUNT; i++) if ((n = channel.write(buff)) > 0) break;
if (n == 0) break;
write += n;
if (!buff.hasRemaining()) queue.poll();
}
if (write > 0) {
session.setLastWriteTime(now);
session.increaseWriteBytes(write);
}
if (buff != null) {
setInterestedInWrite(key, true);
}
} catch (Throwable th) {
session.close(true);
}
}
protected void setInterestedInWrite(SelectionKey key, boolean interestedInWrite) {
key.interestOps(interestedInWrite ? (SelectionKey.OP_READ | SelectionKey.OP_WRITE) : SelectionKey.OP_READ);
}
}