コード例 #1
0
ファイル: websocketproxy.py プロジェクト: ppeereb1/kinetic
    def do_websocket_proxy(self, target):
        """Proxy websocket link

        Proxy client WebSocket to normal target socket.
        """
        self.cqueue = []
        self.tqueue = []
        self.c_pend = 0
        rlist = [self.request, target]

        if self.server.heartbeat:
            now = time.time()
            self.heartbeat = now + self.server.heartbeat
        else:
            self.heartbeat = None

        while True:
            wlist = []

            if self.heartbeat is not None:
                now = time.time()
                if now > self.heartbeat:
                    self.heartbeat = now + self.server.heartbeat
                    self.send_ping()

            if self.tqueue:
                wlist.append(target)
            if self.cqueue or self.c_pend:
                wlist.append(self.request)
            try:
                ins, outs, excepts = select.select(rlist, wlist, [], 1)
            except (select.error, OSError):
                exc = sys.exc_info()[1]
                if hasattr(exc, 'errno'):
                    err = exc.errno
                else:
                    err = exc[0]
                if err != errno.EINTR:
                    raise
                else:
                    continue

            if excepts:
                raise exception.SocketException()

            self._handle_ins_outs(target, ins, outs)
コード例 #2
0
ファイル: websocketproxy.py プロジェクト: ppeereb1/kinetic
    def _send_buffer(self, buff, target, send_all=False):
        size = len(buff)
        tosend = size
        already_sent = 0

        while tosend > 0:
            try:
                # i should be able to send a bytearray
                sent = target.send(buff[already_sent:])
                if sent == 0:
                    raise RuntimeError('socket connection broken')

                already_sent += sent
                tosend -= sent

            except socket.error as e:
                # if full buffers then wait for them to drain and try again
                if e.errno in [errno.EAGAIN, errno.EWOULDBLOCK]:
                    if send_all:
                        continue
                    return buff[already_sent:]
                else:
                    raise exception.SocketException(str(e))
        return None