(self)
Generator to parse bytes into a frame. Yields until
        enough bytes have been read or an error is met.
Generator to parse bytes into a frame. Yields until
        enough bytes have been read or an error is met.
Generator to parse bytes into a frame . Yields until enough bytes have been read or an error is met .
def _parsing(self):
        """
        Generator to parse bytes into a frame. Yields until
        enough bytes have been read or an error is met.
        """
        buf = enc('')
        bytes = enc('')

        # yield until we get the first header's byte
        while not bytes:
            bytes = (yield 1)

        first_byte = ord(bytes[0])
        self.fin = (first_byte >> 7) & 1
        self.rsv1 = (first_byte >> 6) & 1
        self.rsv2 = (first_byte >> 5) & 1
        self.rsv3 = (first_byte >> 4) & 1
        self.opcode = first_byte & 0xf

        # frame-fin = %x0 ; more frames of this message follow
        #           / %x1 ; final frame of this message
        if self.fin not in [0, 1]:
            raise ProtocolException()

        # frame-rsv1 = %x0 ; 1 bit, MUST be 0 unless negotiated otherwise
        # frame-rsv2 = %x0 ; 1 bit, MUST be 0 unless negotiated otherwise
        # frame-rsv3 = %x0 ; 1 bit, MUST be 0 unless negotiated otherwise
        if self.rsv1 or self.rsv2 or self.rsv3:
            raise ProtocolException()

        # control frames between 3 and 7 as well as above 0xA are currently reserved
        if 2 < self.opcode < 8 or self.opcode > 0xA:
            raise ProtocolException()

        # control frames cannot be fragmented
        if self.opcode > 0x7 and self.fin == 0:
            raise ProtocolException()

        # do we already have enough bytes to continue?
        bytes = bytes[1:] if bytes and len(bytes) > 1 else enc('')

        # Yield until we get the second header's byte
        while not bytes:
            bytes = (yield 1)

        second_byte = ord(bytes[0])
        mask = (second_byte >> 7) & 1
        self.payload_length = second_byte & 0x7f

        # All control frames MUST have a payload length of 125 bytes or less
        if self.opcode > 0x7 and self.payload_length > 125:
            raise FrameTooLargeException()

        if bytes and len(bytes) > 1:
            buf = bytes[1:]
            bytes = buf
        else:
            buf = enc('')
            bytes = enc('')

        if self.payload_length == 127:
            if len(buf) < 8:
                nxt_buf_size = 8 - len(buf)
                bytes = (yield nxt_buf_size)
                bytes = buf + (bytes or enc(''))
                while len(bytes) < 8:
                    b = (yield 8 - len(bytes))
                    if b is not None:
                        bytes = bytes + b
                if len(bytes) > 8:
                    buf = bytes[8:]
            else:
                bytes = buf[:8]
                buf = buf[8:]
            extended_payload_length = bytes
            self.payload_length = unpack(
                '!Q', extended_payload_length)[0]
            if self.payload_length > 0x7FFFFFFFFFFFFFFF:
                raise FrameTooLargeException()
        elif self.payload_length == 126:
            if len(buf) < 2:
                nxt_buf_size = 2 - len(buf)
                bytes = (yield nxt_buf_size)
                bytes = buf + (bytes or enc(''))
                while len(bytes) < 2:
                    b = (yield 2 - len(bytes))
                    if b is not None:
                        bytes = bytes + b
                if len(bytes) > 2:
                    buf = bytes[2:]
            else:
                bytes = buf[:2]
                buf = buf[2:]
            extended_payload_length = bytes
            self.payload_length = unpack(
                '!H', extended_payload_length)[0]

        if mask:
            if len(buf) < 4:
                nxt_buf_size = 4 - len(buf)
                bytes = (yield nxt_buf_size)
                bytes = buf + (bytes or enc(''))
                while not bytes or len(bytes) < 4:
                    b = (yield 4 - len(bytes))
                    if b is not None:
                        bytes = bytes + b
                if len(bytes) > 4:
                    buf = bytes[4:]
            else:
                bytes = buf[:4]
                buf = buf[4:]
            self.masking_key = bytes

        if len(buf) < self.payload_length:
            nxt_buf_size = self.payload_length - len(buf)
            bytes = (yield nxt_buf_size)
            bytes = buf + (bytes or enc(''))
            while len(bytes) < self.payload_length:
                l = self.payload_length - len(bytes)
                b = (yield l)
                if b is not None:
                    bytes = bytes + b
        else:
            if self.payload_length == len(buf):
                bytes = buf
            else:
                bytes = buf[:self.payload_length]

        self.body = bytes
        yield
def _parsing ( self ) : buf = enc ( '' ) bytes = enc ( '' ) # yield until we get the first header's byte while not bytes : bytes = ( yield 1 ) first_byte = ord ( bytes [ 0 ] ) self . fin = ( first_byte >> 7 ) & 1 self . rsv1 = ( first_byte >> 6 ) & 1 self . rsv2 = ( first_byte >> 5 ) & 1 self . rsv3 = ( first_byte >> 4 ) & 1 self . opcode = first_byte & 0xf # frame-fin = %x0 ; more frames of this message follow #           / %x1 ; final frame of this message if self . fin not in [ 0 , 1 ] : raise ProtocolException ( ) # frame-rsv1 = %x0 ; 1 bit, MUST be 0 unless negotiated otherwise # frame-rsv2 = %x0 ; 1 bit, MUST be 0 unless negotiated otherwise # frame-rsv3 = %x0 ; 1 bit, MUST be 0 unless negotiated otherwise if self . rsv1 or self . rsv2 or self . rsv3 : raise ProtocolException ( ) # control frames between 3 and 7 as well as above 0xA are currently reserved if 2 < self . opcode < 8 or self . opcode > 0xA : raise ProtocolException ( ) # control frames cannot be fragmented if self . opcode > 0x7 and self . fin == 0 : raise ProtocolException ( ) # do we already have enough bytes to continue? bytes = bytes [ 1 : ] if bytes and len ( bytes ) > 1 else enc ( '' ) # Yield until we get the second header's byte while not bytes : bytes = ( yield 1 ) second_byte = ord ( bytes [ 0 ] ) mask = ( second_byte >> 7 ) & 1 self . payload_length = second_byte & 0x7f # All control frames MUST have a payload length of 125 bytes or less if self . opcode > 0x7 and self . payload_length > 125 : raise FrameTooLargeException ( ) if bytes and len ( bytes ) > 1 : buf = bytes [ 1 : ] bytes = buf else : buf = enc ( '' ) bytes = enc ( '' ) if self . payload_length == 127 : if len ( buf ) < 8 : nxt_buf_size = 8 - len ( buf ) bytes = ( yield nxt_buf_size ) bytes = buf + ( bytes or enc ( '' ) ) while len ( bytes ) < 8 : b = ( yield 8 - len ( bytes ) ) if b is not None : bytes = bytes + b if len ( bytes ) > 8 : buf = bytes [ 8 : ] else : bytes = buf [ : 8 ] buf = buf [ 8 : ] extended_payload_length = bytes self . payload_length = unpack ( '!Q' , extended_payload_length ) [ 0 ] if self . payload_length > 0x7FFFFFFFFFFFFFFF : raise FrameTooLargeException ( ) elif self . payload_length == 126 : if len ( buf ) < 2 : nxt_buf_size = 2 - len ( buf ) bytes = ( yield nxt_buf_size ) bytes = buf + ( bytes or enc ( '' ) ) while len ( bytes ) < 2 : b = ( yield 2 - len ( bytes ) ) if b is not None : bytes = bytes + b if len ( bytes ) > 2 : buf = bytes [ 2 : ] else : bytes = buf [ : 2 ] buf = buf [ 2 : ] extended_payload_length = bytes self . payload_length = unpack ( '!H' , extended_payload_length ) [ 0 ] if mask : if len ( buf ) < 4 : nxt_buf_size = 4 - len ( buf ) bytes = ( yield nxt_buf_size ) bytes = buf + ( bytes or enc ( '' ) ) while not bytes or len ( bytes ) < 4 : b = ( yield 4 - len ( bytes ) ) if b is not None : bytes = bytes + b if len ( bytes ) > 4 : buf = bytes [ 4 : ] else : bytes = buf [ : 4 ] buf = buf [ 4 : ] self . masking_key = bytes if len ( buf ) < self . payload_length : nxt_buf_size = self . payload_length - len ( buf ) bytes = ( yield nxt_buf_size ) bytes = buf + ( bytes or enc ( '' ) ) while len ( bytes ) < self . payload_length : l = self . payload_length - len ( bytes ) b = ( yield l ) if b is not None : bytes = bytes + b else : if self . payload_length == len ( buf ) : bytes = buf else : bytes = buf [ : self . payload_length ] self . body = bytes yield