-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathTCPClient.java
More file actions
166 lines (155 loc) · 4.54 KB
/
TCPClient.java
File metadata and controls
166 lines (155 loc) · 4.54 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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2005 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.EOFException;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
@SuppressWarnings("resource")
class TCPClient implements AutoCloseable {
private final long startTime;
private final Duration timeout;
private final SelectionKey key;
TCPClient(Duration timeout) throws IOException {
this.timeout = timeout;
startTime = System.nanoTime();
boolean done = false;
Selector selector = null;
SocketChannel channel = SocketChannel.open();
try {
selector = Selector.open();
channel.configureBlocking(false);
key = channel.register(selector, SelectionKey.OP_READ);
done = true;
} finally {
if (!done && selector != null) {
selector.close();
}
if (!done) {
channel.close();
}
}
}
void bind(SocketAddress addr) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
channel.socket().bind(addr);
}
void connect(SocketAddress addr) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
if (channel.connect(addr)) {
return;
}
key.interestOps(SelectionKey.OP_CONNECT);
try {
while (!channel.finishConnect()) {
if (!key.isConnectable()) {
blockUntil(key);
}
}
} finally {
if (key.isValid()) {
key.interestOps(0);
}
}
}
void send(byte[] data) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
NioClient.verboseLog(
"TCP write",
channel.socket().getLocalSocketAddress(),
channel.socket().getRemoteSocketAddress(),
data);
byte[] lengthArray = new byte[2];
lengthArray[0] = (byte) (data.length >>> 8);
lengthArray[1] = (byte) (data.length & 0xFF);
ByteBuffer[] buffers = new ByteBuffer[2];
buffers[0] = ByteBuffer.wrap(lengthArray);
buffers[1] = ByteBuffer.wrap(data);
int nsent = 0;
key.interestOps(SelectionKey.OP_WRITE);
try {
while (nsent < data.length + 2) {
if (key.isWritable()) {
long n = channel.write(buffers);
if (n < 0) {
throw new EOFException();
}
nsent += (int) n;
if (nsent < data.length + 2 && System.nanoTime() - startTime >= timeout.toNanos()) {
throw new SocketTimeoutException();
}
} else {
blockUntil(key);
}
}
} finally {
if (key.isValid()) {
key.interestOps(0);
}
}
}
private byte[] recv(int length) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
int nrecvd = 0;
byte[] data = new byte[length];
ByteBuffer buffer = ByteBuffer.wrap(data);
key.interestOps(SelectionKey.OP_READ);
try {
while (nrecvd < length) {
if (key.isReadable()) {
long n = channel.read(buffer);
if (n < 0) {
throw new EOFException();
}
nrecvd += (int) n;
if (nrecvd < length && System.nanoTime() - startTime >= timeout.toNanos()) {
throw new SocketTimeoutException();
}
} else {
blockUntil(key);
}
}
} finally {
if (key.isValid()) {
key.interestOps(0);
}
}
return data;
}
private void blockUntil(SelectionKey key) throws IOException {
long remainingTimeout =
timeout.minus(System.nanoTime() - startTime, ChronoUnit.NANOS).toMillis();
int nkeys = 0;
if (remainingTimeout > 0) {
nkeys = key.selector().select(remainingTimeout);
} else if (remainingTimeout == 0) {
nkeys = key.selector().selectNow();
}
if (nkeys == 0) {
throw new SocketTimeoutException();
}
}
@Override
public void close() throws IOException {
key.selector().close();
key.channel().close();
}
byte[] recv() throws IOException {
byte[] buf = recv(2);
int length = ((buf[0] & 0xFF) << 8) + (buf[1] & 0xFF);
byte[] data = recv(length);
SocketChannel channel = (SocketChannel) key.channel();
NioClient.verboseLog(
"TCP read",
channel.socket().getLocalSocketAddress(),
channel.socket().getRemoteSocketAddress(),
data);
return data;
}
}