forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketServer.ts
More file actions
55 lines (46 loc) · 1.5 KB
/
socketServer.ts
File metadata and controls
55 lines (46 loc) · 1.5 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
"use strict";
import * as net from "net";
import { EventEmitter } from 'events';
import { createDeferred } from '../helpers';
export class SocketServer extends EventEmitter {
private socketServer: net.Server = null;
constructor() {
super();
}
public Stop() {
if (this.socketServer === null) { return; }
try {
this.socketServer.close();
}
catch (ex) { }
this.socketServer = null;
}
public Start(): Promise<number> {
const def = createDeferred<number>();
this.socketServer = net.createServer(this.connectionListener.bind(this));
this.socketServer.listen(0, function (this: SocketServer) {
def.resolve(this.socketServer.address().port);
}.bind(this));
this.socketServer.on("error", ex => {
console.error('Error in Socket Server', ex);
if (def.completed) {
// Ooops
debugger;
}
const msg = `Failed to start the socket server. (Error: ${ex.message})`;
def.reject(msg);
});
return def.promise;
}
private connectionListener(client: net.Socket) {
client.on("close", function () {
this.emit('close', client);
}.bind(this));
client.on("data", function (data: Buffer) {
this.emit('data', client, data);
}.bind(this));
client.on("timeout", d => {
// let msg = "Debugger client timedout, " + d;
});
}
}