Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
LyqydNet-Programs/nshd
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
76 lines (72 sloc)
2.63 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local connections = {} | |
| local function newSession() | |
| local sessionThread = coroutine.create(function() shell.run("/rom/programs/shell") end) | |
| return sessionThread | |
| end | |
| nshdExit = false | |
| mon = peripheral.wrap("top") | |
| --initialization | |
| while not nshdExit do | |
| conn, packetType, message = connection.listen(23) | |
| if conn == 0 then | |
| --daemon command logic | |
| else | |
| if connections[conn] and connections[conn].status == "open" then | |
| if packetType == "event" or string.sub(packetType, 1, 4) == "text" then | |
| local eventTable = {} | |
| if packetType == "event" then | |
| eventTable = textutils.unserialize(message) | |
| else | |
| table.insert(eventTable, "socket_inbound") | |
| table.insert(eventTable, connection.connectionTable[conn].localport) | |
| table.insert(eventTable, net.CIDFromRoute(connection.connectionTable[conn].route)) | |
| table.insert(eventTable, connection.pTypeLookup[packetType]..":"..connection.connectionTable[conn].localport..","..connection.connectionTable[conn].foreignport..";"..message) | |
| end | |
| term.redirect(connections[conn].target) | |
| --term.redirect(mon) | |
| coroutine.resume(connections[conn].thread, unpack(eventTable)) | |
| if coroutine.status(connections[conn].thread) == "dead" then | |
| if not daemonMode then print("Client disconnected: "..connection.name(conn)) end | |
| connection.close(conn) | |
| table.remove(connections, conn) | |
| end | |
| term.restore() | |
| elseif packetType == "query" then | |
| --reset connection | |
| connections[conn].status = "open" | |
| connections[conn].mode = nil | |
| connections[conn].target = connection.text(conn) | |
| connections[conn].thread = newSession() | |
| connection.send(conn, "response", "/") | |
| term.redirect(connections[conn].target) | |
| --term.redirect(mon) | |
| coroutine.resume(connections[conn].thread) | |
| term.restore() | |
| if not daemonMode then print("Client reconnected: "..connection.name(conn)) end | |
| elseif packetType == "close" then | |
| table.remove(connections, conn) | |
| if not daemonMode then print("Client disconnected: "..connection.name(conn)) end | |
| --close connection | |
| else | |
| --error | |
| end | |
| elseif packetType ~= "query" then | |
| connection.close(conn) | |
| --client thinks connection is open, but server thinks it isn't. | |
| else | |
| --open new connection | |
| local connInfo = {} | |
| connInfo.status = "open" | |
| connInfo.mode = nil | |
| connInfo.target = connection.text(conn) | |
| connInfo.thread = newSession() | |
| connection.send(conn, "response", "/") | |
| table.insert(connections, conn, connInfo) | |
| term.redirect(connInfo.target) | |
| --term.redirect(mon) | |
| coroutine.resume(connInfo.thread) | |
| term.restore() | |
| if not daemonMode then print("Client connected: "..connection.name(conn)) end | |
| end | |
| end | |
| end |