Commit 05c0e693 authored by ibuler's avatar ibuler

remove select from interacitve

parent 6a623a6d
...@@ -41,6 +41,7 @@ class Coco: ...@@ -41,6 +41,7 @@ class Coco:
self.ws = None self.ws = None
self.root_path = root_path self.root_path = root_path
self.name = name self.name = name
self.lock = threading.Lock()
if name is None: if name is None:
self.name = self.config['NAME'] self.name = self.config['NAME']
...@@ -97,14 +98,20 @@ class Coco: ...@@ -97,14 +98,20 @@ class Coco:
self.sshd.shutdown() self.sshd.shutdown()
def add_client(self, client): def add_client(self, client):
self.clients.append(client) with self.lock:
print("%s add client, now %d s" % (self.name, len(self.clients))) self.clients.append(client)
print("%s add client, now %d s" % (self.name, len(self.clients)))
def remove_client(self, client): def remove_client(self, client):
self.clients.remove(client) with self.lock:
client.send("Closed by server") self.clients.remove(client)
client.close() print("%s remove client, now %d s" % (self.name, len(self.clients)))
print("%s remove client, now %d s" % (self.name, len(self.clients)))
try:
client.send("Closed by server")
client.close()
except:
pass
def monitor_session(self): def monitor_session(self):
pass pass
......
...@@ -44,45 +44,43 @@ class InteractiveServer: ...@@ -44,45 +44,43 @@ class InteractiveServer:
self.request.meta.get("height", 24)) self.request.meta.get("height", 24))
self.client.send(wr(prompt, before=1, after=0)) self.client.send(wr(prompt, before=1, after=0))
while True: while True:
r, w, x = select.select([self.client], [], []) data = self.client.recv(10)
if self.client in r: if len(data) == 0:
data = self.client.recv(10) self.app.remove_client(self.client)
if len(data) == 0: break
self.app.remove_client(self.client) # Client input backspace
# Client input backspace if data in char.BACKSPACE_CHAR:
if data in char.BACKSPACE_CHAR: # If input words less than 0, should send 'BELL'
# If input words less than 0, should send 'BELL' if len(input_data) > 0:
if len(input_data) > 0: data = char.BACKSPACE_CHAR[data]
data = char.BACKSPACE_CHAR[data] input_data.pop()
input_data.pop()
else:
data = char.BELL_CHAR
self.client.send(data)
continue
# Todo: Move x1b to char
if data.startswith(b'\x1b') or data in char.UNSUPPORTED_CHAR:
self.client.send('')
continue
# handle shell expect
multi_char_with_enter = False
if len(data) > 1 and data[-1] in char.ENTER_CHAR:
self.client.send(data)
input_data.append(data[:-1])
multi_char_with_enter = True
# If user type ENTER we should get user input
if data in char.ENTER_CHAR or multi_char_with_enter:
self.client.send(wr('', after=2))
option = parser.parse_input(b''.join(input_data))
return option.strip()
else: else:
self.client.send(data) data = char.BELL_CHAR
input_data.append(data) self.client.send(data)
continue
# Todo: Move x1b to char
if data.startswith(b'\x1b') or data in char.UNSUPPORTED_CHAR:
self.client.send('')
continue
# handle shell expect
multi_char_with_enter = False
if len(data) > 1 and data[-1] in char.ENTER_CHAR:
self.client.send(data)
input_data.append(data[:-1])
multi_char_with_enter = True
# If user type ENTER we should get user input
if data in char.ENTER_CHAR or multi_char_with_enter:
self.client.send(wr('', after=2))
option = parser.parse_input(b''.join(input_data))
return option.strip()
else:
self.client.send(data)
input_data.append(data)
def dispatch(self, opt): def dispatch(self, opt):
print(opt)
if opt in ['q', 'Q']: if opt in ['q', 'Q']:
self.app.remove_client(self.client) self.app.remove_client(self.client)
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment