-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetProxy.js
More file actions
137 lines (130 loc) · 3.33 KB
/
Copy pathnetProxy.js
File metadata and controls
137 lines (130 loc) · 3.33 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
const uuidv4 = require('uuid/v4')
const ipc = require('node-ipc')
const {
NetClientProxy,
NetServerProxy
} = require('@lug/netproxy')
const dataEvent = '_message_'
ipc.config.id = '_net_proxy_'
ipc.config.silent = true
class NetClientProxyNodeIpc extends NetClientProxy {
constructor() {
super()
this._ipcId = uuidv4()
this._ipc = ipc
this._emitter = null
this._state = null
this._dataEvent = dataEvent
}
get state() {
return this._state
}
_initEvents() {
super._initEvents()
}
get socket() {
return null
}
connect(ip, port) {
if (this.state !== null)
return this.ev.emit('error', Error('Cannot connect two times the same client without disconnect'))
ipc.connectToNet(this._ipcId, ip, port, () => {
this._emitter = ipc.of[this._ipcId]
this._initIpcEvents()
this.ev.emit('connect')
})
}
_initIpcEvents() {
this._emitter.on('error', err => this.ev.emit('error', err))
this._emitter.on('connect', () => {
this._state = 'connected'
this.ev.emit('connected')
})
this._emitter.on('disconnect', () => {
this._state = 'disconnected'
this.ev.emit('disconnect')
})
this._emitter.on('destroy', () => {
this._state = null
this._emitter = null
this.ev.emit('destroy')
})
this._emitter.on(this._dataEvent, this._onData.bind(this))
}
_onData(data) {
this.ev.emit('data', data)
}
disconnect() {
ipc.disconnect(this._ipcId)
}
send(data) {
this._emitter.emit(this._dataEvent, data)
}
}
class NetServerProxyNodeIpc extends NetServerProxy {
constructor() {
super()
this._port = null
this._emitter = null
this._state = null
this._onConnect = this._onConnect.bind(this)
this._onDisonnect = this._onDisonnect.bind(this)
this._onData = this._onData.bind(this)
this._dataEvent = dataEvent
this._ipc = ipc
this._sockets = []
}
_initEvents() {
super._initEvents()
}
get state() {
return this._state
}
listen(port) {
this._port = port
ipc.serveNet('0.0.0.0', port, () => {
this._state = 'listening'
this._emitter = ipc.server
this._initIpcEvents()
this.ev.emit('start')
})
ipc.server.start()
}
_initIpcEvents() {
this._emitter.on('connect', this._onConnect)
this._emitter.on('socket.disconnected', this._onDisonnect)
this._emitter.on(this._dataEvent, this._onData)
}
_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', Error(`Socket already in list : ${socket}`))
}
_onDisonnect(socket) {
let index = this._sockets.findIndex(s => s === socket)
if (index !== -1) {
this._sockets.splice(index, 1)
this.ev.emit('socket.disconnect', socket)
} else this.ev.emit('error', Error(`Socket not in list but fire a disconnect event: ${socket}`))
}
_onData(data, socket) {
this.ev.emit('socket.data', socket, data)
}
stop() {
this._emitter.stop()
this._emitter = null
this._port = null
this._sockets = []
this._state = null
this.ev.emit('stop')
}
send(socket, data) {
ipc.server.emit(socket, this._dataEvent, data)
}
}
module.exports = {
NetClientProxyNodeIpc,
NetServerProxyNodeIpc
}