-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (74 loc) · 1.97 KB
/
app.js
File metadata and controls
95 lines (74 loc) · 1.97 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
const net = require('net')
const ConnectionManager = require('./lib/connection_manager')
const InputHandler = require('./lib/input_handler')
const config = require('./config')
process.on('uncaughtException', err => {
console.log(err.stack)
})
process.on('unhandledRejection', err => {
console.log(err.stack)
})
start()
async function start() {
try {
validateConfig()
startServer()
console.log('DoorNode listening on port ' + config.port)
if (config.debugPort) {
startDebugServer()
console.log('Debug interface listening on port ' + config.debugPort)
}
} catch (err) {
console.log(err.stack)
process.exit()
}
}
function validateConfig() {
if (!config.port) {
throw new Error('config.port not configured')
}
if (!config.dosbox.dosboxPath) {
throw new Error('config.dosbox.dosboxPath not configured')
}
if (!config.dosbox.configPath) {
throw new Error('config.dosbox.configPath not configured')
}
if (!config.dosbox.drivePath) {
throw new Error('config.dosbox.drivePath not configured')
}
if (!config.dosbox.startPort) {
throw new Error('config.dosbox.startPort not specified')
}
}
function startServer() {
let server = net.createServer(conn => {
conn.setEncoding('binary')
let wrapper = ConnectionManager.init(conn)
conn.on('end', () => {
ConnectionManager.close(wrapper)
})
conn.on('data', async str => {
await InputHandler.onData(wrapper, str)
})
})
server.listen(config.port)
}
function startDebugServer() {
let server = net.createServer(conn => {
conn.setEncoding('binary')
let wrapper = ConnectionManager.init(conn)
wrapper.user = {
name: 'DebugUser',
module: 'Debug',
terminal: 'ansi'
}
wrapper.setDebugModule()
conn.on('end', () => {
ConnectionManager.close(wrapper)
})
conn.on('data', async str => {
await InputHandler.onData(wrapper, str)
})
})
server.listen(config.debugPort)
}