-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNIOServer.java
151 lines (120 loc) · 4.15 KB
/
NIOServer.java
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
/*
Non blocking (NIO) tcp/http server implementation.
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.net.StandardSocketOptions;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
public class NIOServer {
private static final int BUFFER_SIZE = 4096;
private int port;
private InetAddress hostAddress = null;
private Selector selector;
private ByteBuffer readBuffer = ByteBuffer.allocate(BUFFER_SIZE);
public NIOServer(int port) throws IOException {
this.port = port;
selector = SelectorProvider.provider().openSelector();
//
// http://docs.oracle.com/javase/7/docs/api/java/nio/channels/SocketChannel.html
//
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.socket().bind(new InetSocketAddress(hostAddress, port));
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
nioLoop();
}
private void nioLoop() {
while (true) {
try{
selector.select();
Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
SelectionKey key = selectedKeys.next();
selectedKeys.remove();
if (!key.isValid()) {
continue;
}
if (key.isAcceptable()) {
accept(key);
} else if (key.isReadable()) {
read(key);
} else if (key.isWritable()) {
write(key);
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
private void accept(SelectionKey key) throws IOException {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, true);
socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
socketChannel.register(selector, SelectionKey.OP_READ);
}
//
// http://docs.oracle.com/javase/7/docs/api/java/nio/channels/SelectionKey.html
//
// Object attach(Object ob)
// Attaches the given object to this key.
// Object attachment()
// Retrieves the current attachment.
//
private void read(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
readBuffer.clear();
int nRead;
try {
nRead = socketChannel.read(readBuffer);
if (nRead == -1) {
socketChannel.close();
return;
}
} catch (IOException e) {
socketChannel.close();
return;
}
//System.out.println("nRead="+nRead);
//System.out.println("readBuffer="+new String(readBuffer.array(), "UTF-8"));
socketChannel.register(selector, SelectionKey.OP_WRITE);
}
//
// http://tutorials.jenkov.com/java-nio/scatter-gather.html
// https://forums.bukkit.org/threads/ultra-fast-java-nio-webserver-less-than-350-lines.101080/
//
private void write(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer responseBuffer = ByteBuffer.wrap("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 7\r\n\r\nHello\r\n".getBytes("UTF-8"));
socketChannel.write(responseBuffer);
// if (responseBuffer.remaining() > 0) {
// socketChannel.close();
// return;
// }
// key.interestOps(SelectionKey.OP_READ);
key.channel().close();
key.cancel();
}
public final class NIOSession {
private final SocketChannel channel;
public NIOSession(SocketChannel channel) {
this.channel = channel;
}
}
public static void main(String[] args) throws IOException {
int port = 9090;
System.out.println("Starting server "+port);
new NIOServer(port);
}
}