-
Notifications
You must be signed in to change notification settings - Fork 50
/
NonBlockingServer.java
105 lines (91 loc) · 4.57 KB
/
NonBlockingServer.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
package tlschannel.example;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Iterator;
import javax.net.ssl.SSLContext;
import tlschannel.NeedsReadException;
import tlschannel.NeedsWriteException;
import tlschannel.ServerTlsChannel;
import tlschannel.TlsChannel;
/**
* Server non-blocking example. Accepts any number of connections and echos bytes sent by the
* clients into standard output.
*
* <p>To test, use: <code> openssl s_client -connect localhost:10000 </code>
*
* <p>This example is similar to the canonical selector loop, except for:
*
* <ul>
* <li>When a connection arrives, the newly-created channel is wrapped in a TlsChannel: all IO is
* done using the TlsChannel, but the raw channel is registered in the selector.
* <li>IO operations are surrounded in a try-catch block that traps {@link NeedsWriteException}
* and {@link NeedsReadException} and enables the appropriate operation for the corresponding
* key.
* </ul>
*/
public class NonBlockingServer {
public static void main(String[] args) throws IOException, GeneralSecurityException {
// initialize the SSLContext, a configuration holder, reusable object
SSLContext sslContext = ContextFactory.authenticatedContext("TLSv1.2");
// connect server socket channel and register it in the selector
try (ServerSocketChannel serverSocket = ServerSocketChannel.open()) {
serverSocket.socket().bind(new InetSocketAddress(10000));
serverSocket.configureBlocking(false);
Selector selector = Selector.open();
serverSocket.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
// loop blocks here
selector.select();
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if (key.isAcceptable()) {
// we have a new connection
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
// accept new connection
SocketChannel rawChannel = serverChannel.accept();
rawChannel.configureBlocking(false);
// wrap raw channel in TlsChannel
TlsChannel tlsChannel = ServerTlsChannel.newBuilder(rawChannel, sslContext)
.build();
// Note that the raw channel is registered in the selector (and now the wrapped ont),
// the TlsChannel is put as an attachment. Additionally, the channel is registered for
// reading, because TLS connections are initiated by clients.
SelectionKey newKey = rawChannel.register(selector, SelectionKey.OP_READ);
newKey.attach(tlsChannel);
} else if (key.isReadable() || key.isWritable()) {
// we have data (or buffer space) in existing connections
ByteBuffer buffer = ByteBuffer.allocate(10000);
// recover the TlsChannel from the attachment
TlsChannel tlsChannel = (TlsChannel) key.attachment();
try {
// write received bytes in stdout
int c = tlsChannel.read(buffer);
if (c > 0) {
buffer.flip();
System.out.print(StandardCharsets.UTF_8.decode(buffer));
}
if (c < 0) {
tlsChannel.close();
}
} catch (NeedsReadException e) {
key.interestOps(SelectionKey.OP_READ); // overwrites previous value
} catch (NeedsWriteException e) {
key.interestOps(SelectionKey.OP_WRITE); // overwrites previous value
}
} else {
throw new IllegalStateException();
}
}
}
}
}
}