Commit 05c0e693 authored by ibuler's avatar ibuler

remove select from interacitve

parent 6a623a6d
......@@ -41,6 +41,7 @@ class Coco:
self.ws = None
self.root_path = root_path
self.name = name
self.lock = threading.Lock()
if name is None:
self.name = self.config['NAME']
......@@ -97,14 +98,20 @@ class Coco:
self.sshd.shutdown()
def add_client(self, client):
self.clients.append(client)
print("%s add client, now %d s" % (self.name, len(self.clients)))
with self.lock:
self.clients.append(client)
print("%s add client, now %d s" % (self.name, len(self.clients)))
def remove_client(self, client):
self.clients.remove(client)
client.send("Closed by server")
client.close()
print("%s remove client, now %d s" % (self.name, len(self.clients)))
with self.lock:
self.clients.remove(client)
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):
pass
......
......@@ -44,45 +44,43 @@ class InteractiveServer:
self.request.meta.get("height", 24))
self.client.send(wr(prompt, before=1, after=0))
while True:
r, w, x = select.select([self.client], [], [])
if self.client in r:
data = self.client.recv(10)
if len(data) == 0:
self.app.remove_client(self.client)
# Client input backspace
if data in char.BACKSPACE_CHAR:
# If input words less than 0, should send 'BELL'
if len(input_data) > 0:
data = char.BACKSPACE_CHAR[data]
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()
data = self.client.recv(10)
if len(data) == 0:
self.app.remove_client(self.client)
break
# Client input backspace
if data in char.BACKSPACE_CHAR:
# If input words less than 0, should send 'BELL'
if len(input_data) > 0:
data = char.BACKSPACE_CHAR[data]
input_data.pop()
else:
self.client.send(data)
input_data.append(data)
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:
self.client.send(data)
input_data.append(data)
def dispatch(self, opt):
print(opt)
if opt in ['q', 'Q']:
self.app.remove_client(self.client)
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