1 Unstar Star 0 Fork 32

cntoby / opensshPythonApache-2.0

forked from Jack / openssh 
Clone or download
ioloop.py
Copy Edit Web IDE Raw Normal History
Jack   第一提交 2016-12-30
__author__ = 'JackSong'import socketimport errnofrom tornado.ioloop import IOLoop
Jack   增加异步代码 2017-02-03
from tornado.gen import coroutine
Jack   第一提交 2016-12-30
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)class IOloop(object):    def __init__(self, ioloop):        self.ioloop = ioloop        self.bridges = {}    @staticmethod    def instance():        if not hasattr(IOloop, "_instance"):            IOloop._instance = IOloop(IOLoop.instance())        return IOloop._instance    def register(self, bridge):
Jack   增加异步代码 2017-02-03
        self.bridges[bridge.fd] = bridge        self.add_handler(bridge.fd)
Jack   第一提交 2016-12-30
    def add_handler(self, fd):
Jack   增加异步代码 2017-02-03
        self.ioloop.add_handler(fd, self.read_data_callback, self.ioloop.READ | self.ioloop.ERROR)
Jack   第一提交 2016-12-30
    def update_handler(self, fd, events):        self.ioloop.update_handler(fd, events)    def remove_handler(self, fd):        self.ioloop.remove_handler(fd)        bridge = self.bridges.pop(fd, None)        if bridge: del bridge
Jack   增加异步代码 2017-02-03
    @coroutine
Jack   第一提交 2016-12-30
    def read_data_callback(self, fd, event):        if event & IOLoop.READ:            bridge = self.bridges.get(fd, None)            if bridge:
Jack   增加异步代码 2017-02-03
                data = ''
Jack   第一提交 2016-12-30
                while True:                    try:
Jack   增加异步代码 2017-02-03
                        recv = yield bridge.executor(bridge.shell.recv, *(1024,))
Jack   第一提交 2016-12-30
                        data = ''.join((data, recv,))
Jack   增加异步代码 2017-02-03
                        if len(recv) == 0:                            break
Jack   第一提交 2016-12-30
                    except socket.error as e:                        if e.errno == errno.EAGAIN:                            self.update_handler(fd, IOLoop.WRITE)                        elif isinstance(e, socket.timeout):
Jack   增加异步代码 2017-02-03
                            break
Jack   第一提交 2016-12-30
                        else:                            self.remove_handler(fd)
Jack   增加异步代码 2017-02-03
                if data:                    rest = yield bridge.executor(bridge.trans_back, *(data,))                    if not rest:                        self.remove_handler(fd)
Jack   第一提交 2016-12-30
            else:                return        if event & IOLoop.ERROR:            self.remove_handler(fd)
Python
1
https://gitee.com/cntoby/openssh.git
git@gitee.com:cntoby/openssh.git
cntoby
openssh
openssh
master

Search

132457 8cb2edc1 1899542 131848 70c8d3a4 1899542