-
Notifications
You must be signed in to change notification settings - Fork 0
/
netProxyEmul.js
125 lines (120 loc) · 3.4 KB
/
netProxyEmul.js
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
const uuidv4 = require('uuid/v4')
const Emitter = require('events')
const debug = require('debug')
const {
NetClientProxy,
NetServerProxy
} = require('@lug/netproxy')
const netProxyEmul = new Emitter()
class NetClientProxyEmitter extends NetClientProxy {
constructor() {
super()
this._server = null
this._address = uuidv4()
this._timeout = 1000
this._onData = this._onData.bind(this)
}
get state() {
return this._server ? 'connected' : 'disconnected'
}
_initEvents() {
super._initEvents()
}
get socket() {
return this._server ? `${this._server}.${this._address}` : null
}
connect(ip, port) {
let socket = `net.${port}.${this._address}`
let timeout = setTimeout(() => {
this.ev.emit('error', `Server not found ${ip}:${port}`)
}, this._timeout)
netProxyEmul.once(`${socket}.connected`, () => {
clearTimeout(timeout)
this._server = `net.${port}`
this.ev.emit('connected', this.socket)
netProxyEmul.on(this.socket, this._onData)
})
netProxyEmul.emit(`net.${port}.connect`, socket)
}
_onData(data) {
this.ev.emit('data', data)
}
disconnect() {
netProxyEmul.emit(`${this._server}.disconnect`, this.socket)
this.ev.emit('disconnect')
netProxyEmul.off(this.socket, this._onData)
this._server = null
this.ev.emit('destroy')
}
send(data) {
netProxyEmul.emit(`${this._server}.data`, this.socket, data)
}
}
class NetServerProxyEmitter extends NetServerProxy {
constructor() {
super()
this.port = null
this.sockets = []
this.waiter = null
this._onConnect = this._onConnect.bind(this)
this._onDisonnect = this._onDisonnect.bind(this)
this._onData = this._onData.bind(this)
}
_initEvents() {
super._initEvents()
}
get state() {
return this.port ? 'on' : 'off'
}
listen(port) {
this.port = port
netProxyEmul.on(`net.${port}.connect`, this._onConnect)
netProxyEmul.on(`net.${port}.disconnect`, this._onDisonnect)
netProxyEmul.on(`net.${port}.data`, this._onData)
this.ev.emit('start')
this.waiter = setInterval(() => {}, 1000)
}
_onConnect(socket) {
let sock = this.sockets.find(s => s === socket)
if (sock === undefined) {
this.sockets.push(socket)
this.ev.emit('socket.connect', socket)
} else this.ev.emit('error', `Socket already connected : ${socket}`)
netProxyEmul.emit(`${socket}.connected`)
}
_onDisonnect(socket) {
let index = this.sockets.indexOf(socket)
if (index !== -1) {
this.sockets.splice(index, 1)
this.ev.emit('socket.disconnect', socket)
} else this.ev.emit('error', `Socket already disconnected : ${socket}`)
}
_onData(socket, data) {
let sock = this.sockets.find(s => s === socket)
if (sock !== undefined) {
this.ev.emit('socket.data', socket, data)
} else
this.ev.emit(
'error',
`Socket try to send data but is not known : ${socket} ; ${data}`
)
}
stop() {
let port = this.port
this.port = null
this.sockets = []
netProxyEmul.off(`net.${port}.connect`, this._onConnect)
netProxyEmul.off(`net.${port}.disconnect`, this._onDisonnect)
netProxyEmul.off(`net.${port}.data`, this._onData)
this.ev.emit('stop')
clearInterval(this.waiter)
}
send(socket, data) {
netProxyEmul.emit(socket, data)
}
}
module.exports = {
netEmulEmitter: netProxyEmul,
NetClientProxyEmitter,
NetServerProxyEmitter
}