# Path: server/websockets/exceptions.py
# class PayloadTooBig(Exception):
#     """
#     Exception raised when a frame's payload exceeds the maximum size.
# 
#     """
# 
# class WebSocketProtocolError(Exception):
#     """
#     Internal exception raised when the remote side breaks the protocol.
# 
#     """


import asyncio
import collections
import io
import random
import struct
from .exceptions import PayloadTooBig, WebSocketProtocolError
"""


@asyncio.coroutine
def read_frame(reader, mask, *, max_size=None):
    """
    Read a WebSocket frame and return a :class:`Frame` object.

    ``reader`` is a coroutine taking an integer argument and reading exactly
    this number of bytes, unless the end of file is reached.

    ``mask`` is a :class:`bool` telling whether the frame should be masked
    i.e. whether the read happens on the server side.

    If ``max_size`` is set and the payload exceeds this size in bytes,
    :exc:`~websockets.exceptions.PayloadTooBig` is raised.

    This function validates the frame before returning it and raises
    :exc:`~websockets.exceptions.WebSocketProtocolError` if it contains
    incorrect values.

    """
    # Read the header
    data = yield from reader(2)
    head1, head2 = struct.unpack('!BB', data)
    fin = bool(head1 & 0b10000000)
    if head1 & 0b01110000:
        raise WebSocketProtocolError("Reserved bits must be 0")
    opcode = head1 & 0b00001111
    if bool(head2 & 0b10000000) != mask:

# Path: server/websockets/exceptions.py
# class PayloadTooBig(Exception):
#     """
#     Exception raised when a frame's payload exceeds the maximum size.
# 
#     """
# 
# class WebSocketProtocolError(Exception):
#     """
#     Internal exception raised when the remote side breaks the protocol.
# 
#     """


# Path: server/websockets/framing.py
import asyncio
import collections
import io
import random
import struct
from .exceptions import PayloadTooBig, WebSocketProtocolError

"""


@asyncio.coroutine
def read_frame(reader, mask, *, max_size=None):
    """
    Read a WebSocket frame and return a :class:`Frame` object.

    ``reader`` is a coroutine taking an integer argument and reading exactly
    this number of bytes, unless the end of file is reached.

    ``mask`` is a :class:`bool` telling whether the frame should be masked
    i.e. whether the read happens on the server side.

    If ``max_size`` is set and the payload exceeds this size in bytes,
    :exc:`~websockets.exceptions.PayloadTooBig` is raised.

    This function validates the frame before returning it and raises
    :exc:`~websockets.exceptions.WebSocketProtocolError` if it contains
    incorrect values.

    """
    # Read the header
    data = yield from reader(2)
    head1, head2 = struct.unpack('!BB', data)
    fin = bool(head1 & 0b10000000)
    if head1 & 0b01110000:
        raise WebSocketProtocolError("Reserved bits must be 0")
    opcode = head1 & 0b00001111
    if bool(head2 & 0b10000000) != mask:

        raise WebSocketProtocolError("Incorrect masking")