Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve terminals #413

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion plugins/terminals/fps_terminals/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ async def create_terminal(
self,
user: User,
):
name = str(len(TERMINALS) + 1)
name_int = 1
while True:
if str(name_int) not in TERMINALS:
break
name_int += 1
name = str(name_int)
terminal = Terminal(
name=name,
last_activity=datetime.utcnow().isoformat() + "Z",
Expand Down
58 changes: 38 additions & 20 deletions plugins/terminals/fps_terminals/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ def __init__(self):
async def serve(self, websocket, permissions):
self.websocket = websocket
self.websockets.append(websocket)
self.event = asyncio.Event()
self.data_from_terminal = asyncio.Queue()
self.loop = asyncio.get_event_loop()

task = asyncio.create_task(self.send_data())
task = asyncio.create_task(self.send_data()) # noqa: F841

def on_output():
try:
self.data_or_disconnect = self.p_out.read(65536).decode()
self.event.set()
except Exception:
os.close(self.fd)
self.loop.remove_reader(self.p_out)
self.data_or_disconnect = None
self.event.set()
data = self.p_out.read(65536).decode()
except OSError:
try:
self.loop.remove_reader(self.p_out)
except Exception:
pass
try:
os.close(self.fd)
except OSError:
pass
self.data_from_terminal.put_nowait(None)
self.websockets.clear()
self.quit()
else:
self.data_from_terminal.put_nowait(data)

self.loop.add_reader(self.p_out, on_output)
await websocket.send_json(["setup", {}])
Expand All @@ -58,21 +66,31 @@ def on_output():
winsize = struct.pack("HH", msg[1], msg[2])
fcntl.ioctl(self.fd, termios.TIOCSWINSZ, winsize)
except WebSocketDisconnect:
task.cancel()
self.quit(websocket)

async def send_data(self):
while True:
await self.event.wait()
self.event.clear()
if self.data_or_disconnect is None:
await self.websocket.send_json(["disconnect", 1])
else:
for websocket in self.websockets:
await websocket.send_json(["stdout", self.data_or_disconnect])
data = await self.data_from_terminal.get()
if data is None:
try:
await self.websocket.send_json(["disconnect", 1])
except Exception:
pass
return

for websocket in self.websockets:
await websocket.send_json(["stdout", data])

def quit(self, websocket):
def quit(self, websocket=None):
if websocket in self.websockets:
self.websockets.remove(websocket)
if not self.websockets:
os.close(self.fd)
self.loop.remove_reader(self.p_out)
try:
self.loop.remove_reader(self.p_out)
except Exception:
pass
try:
os.close(self.fd)
except OSError:
pass
self.data_from_terminal.put_nowait(None)
Loading