-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathUDT.java
411 lines (325 loc) · 10.6 KB
/
UDT.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) 2014 Tom Zhou<[email protected]>
package com.iwebpp.node.net;
import com.iwebpp.SimpleDebug;
import com.iwebpp.libuvpp.Address;
import com.iwebpp.libuvpp.cb.StreamConnectionCallback;
import com.iwebpp.libuvpp.handles.LoopHandle;
import com.iwebpp.libuvpp.handles.StreamHandle;
import com.iwebpp.libuvpp.handles.UDTHandle;
import com.iwebpp.node.NodeContext;
import com.iwebpp.node.Util;
public final class UDT extends SimpleDebug {
private final static String TAG = "UDT";
public static final class Socket
extends AbstractSocket {
@SuppressWarnings("unused")
private final static String TAG = "UDT:Socket";
protected AbstractServer server;
public Socket(NodeContext context, Options options) throws Exception {
super(context, options);
// TODO Auto-generated constructor stub
}
private Socket() throws Exception{super(null, null);};
@Override
protected int _bind(String address, int port) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.bind(address, port);
} else
return -1;
}
@Override
protected int _bind6(String address, int port) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.bind6(address, port);
} else
return -1;
}
@Override
protected int _connect(String address, int port) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.connect(address, port);
} else
return -1;
}
@Override
protected int _connect6(String address, int port) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.connect6(address, port);
} else
return -1;
}
@Override
protected Address _getSocketName() {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.getSocketName();
} else
return null;
}
@Override
protected Address _getPeerName() {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.getPeerName();
} else
return null;
}
@Override
public int setNoDelay(final boolean enable) {
// backwards compatibility: assume true when `enable` is omitted
if (this._handle /*&& this._handle.setNoDelay*/ != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.setNoDelay(enable);
} else
return -1;
}
@Override
public int setKeepAlive(final boolean enable, final int delay) {
// backwards compatibility: assume true when `enable` is omitted
if (this._handle /*&& this._handle.setNoDelay*/ != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.setKeepAlive(enable, delay);
} else
return -1;
}
@Override
protected StreamHandle _createHandle(final LoopHandle loop) {
return new UDTHandle(loop);
}
}
// /* [ options, ] listener */
public static class Server
extends AbstractServer {
private final static String TAG = "UDT:Server";
public Server(NodeContext context, Options options,
ConnectionListener listener) throws Exception {
super(context, options, listener);
// TODO Auto-generated constructor stub
}
private Server() throws Exception{super(null, null, null);};
@Override
protected int _bind(String address, int port) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.bind(address, port);
} else
return -1;
}
@Override
protected int _bind6(String address, int port) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.bind6(address, port);
} else
return -1;
}
@Override
protected int _listen(int backlog) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.listen(backlog);
} else
return -1;
}
@Override
protected int _accept(StreamHandle client) {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.accept(client);
} else
return -1;
}
@Override
protected Address _getSocketName() {
if (this._handle != null) {
UDTHandle udt = (UDTHandle)this._handle;
return udt.getSocketName();
} else
return null;
}
@Override
protected StreamHandle _createHandle(final LoopHandle loop) {
return new UDTHandle(loop);
}
@Override
protected StreamHandle _createServerHandle(String address, int port, int addressType, int fd) {
UDTHandle handle = (UDTHandle) _createHandle(context.getLoop());
int err = 0;
debug(TAG, "bind to " + (address /*|| 'anycast'*/)+":"+port);
if (Util.zeroString(address)) {
// Try binding to ipv6 first
err = handle.bind6("::", port);
if (err!=0) {
handle.close();
// Fallback to ipv4
return _createServerHandle("0.0.0.0", port, 4, -1);
}
} else if (addressType == 6) {
err = handle.bind6(address, port);
} else {
err = handle.bind(address, port);
}
if (err!=0) {
debug(TAG, "bind err "+err);
handle.close();
return null;
}
return handle;
}
@Override
protected void _listen2(String address, int port, int addressType,
int backlog, int fd) throws Exception {
debug(TAG, "listen2 "+address+":"+port+":"+addressType+":"+backlog);
// check if address is ip
if (!Util.isIP(address)) throw new Exception("Invalid IP address: "+address);
final AbstractServer self = this;
boolean alreadyListening = false;
// If there is not yet a handle, we need to create one and bind.
// In the case of a server sent via IPC, we don't need to do this.
if (null==self._handle) {
debug(TAG, "_listen2: create a handle");
/*var rval = createServerHandle(address, port, addressType, fd);
if (util.isNumber(rval)) {
var error = errnoException(rval, 'listen');
process.nextTick(function() {
self.emit('error', error);
});
return;
}*/
StreamHandle rval = _createServerHandle(address, port, addressType, fd);
if (rval == null) {
final String error = "err listen";
///process.nextTick(function() {
context.nextTick(new NodeContext.nextTickListener() {
@Override
public void onNextTick() throws Exception {
self.emit("error", error);
}
});
return;
}
///alreadyListening = (process.platform === 'win32');
self._handle = rval;
} else {
///debug('_listen2: have a handle already');
debug(TAG, "_listen2: have a handle already");
}
///self._handle.onconnection = onconnection;
// onConnection callback
StreamConnectionCallback onconnection = new StreamConnectionCallback(){
@Override
public void onConnection(int status, Exception error)
throws Exception {
///var handle = this;
///var self = handle.owner;
StreamHandle handle = self._handle;
StreamHandle clientHandle = _createHandle(context.getLoop());
int err = handle.accept(clientHandle);
debug(TAG, "onconnection");
if (err!=0) {
///self.emit('error', errnoException(err, 'accept'));
self.emit("error", "err accept "+err);
return;
}
if (/*self.maxConnections &&*/ self.get_connections() >= self.getMaxConnections()) {
debug(TAG, "exceed maxim connections");
clientHandle.close();
return;
}
/*AbstractSocket socket = new AbstractSocket({
handle: clientHandle,
allowHalfOpen: self.allowHalfOpen
});*/
Socket socket = new Socket(
context,
new Socket.Options(clientHandle, true, true, self.isAllowHalfOpen()));
socket.readable(true); socket.writable(true);
self.set_connections(self.get_connections() + 1);
socket.server = self;
///DTRACE_NET_SERVER_CONNECTION(socket);
///COUNTER_NET_SERVER_CONNECTION(socket);
self.emit("connection", socket);
}
};
self._handle.setConnectionCallback(onconnection);
///self._handle.owner = self;
int err = 0;
if (!alreadyListening)
err = _listen(self._handle, backlog);
if (0!=err) {
///var ex = errnoException(err, "listen");
final String ex = "err listen";
self._handle.close();
self._handle = null;
///process.nextTick(function() {
context.nextTick(new NodeContext.nextTickListener() {
@Override
public void onNextTick() throws Exception {
self.emit("error", ex);
}
});
debug(TAG, ex);
return;
}
///process.nextTick(function() {
context.nextTick(new NodeContext.nextTickListener() {
@Override
public void onNextTick() throws Exception {
// ensure handle hasn't closed
if (self._handle != null)
self.emit("listening");
}
});
}
}
public static Server createServer(
final NodeContext context,
final AbstractServer.ConnectionListener listener) throws Exception {
return new Server(context, new Server.Options(false), listener);
}
public static Server listen(
final NodeContext context,
final String host,
final int port,
final AbstractServer.ConnectionListener listener) throws Exception {
Server srv = createServer(context, listener);
srv.listen(host, port, null);
return srv;
}
// Target API:
//
// var s = net.connect({port: 80, host: 'google.com'}, function() {
// ...
// });
//
// There are various forms:
//
// connect(options, [cb])
// connect(port, [host], [cb])
// connect(path, [cb]);
//
public static Socket createConnection(
NodeContext ctx,
String address, int port,
String localAddress, int localPort,
final AbstractSocket.ConnectListener cb) throws Exception {
debug(TAG, "createConnection " + address + ":" + port + "@"+localAddress+":"+localPort);
Socket s = new Socket(ctx, new Socket.Options(null, false, false, true));
s.connect(address, port, localAddress, localPort, cb);
return s;
}
public static Socket connect(
NodeContext ctx,
String address, int port,
String localAddress, int localPort,
final AbstractSocket.ConnectListener cb) throws Exception {
debug(TAG, "connect " + address + ":" + port + "@"+localAddress+":"+localPort);
Socket s = new Socket(ctx, new Socket.Options(null, false, false, true));
s.connect(address, port, localAddress, localPort, cb);
return s;
}
}