# Path: server/websockets/compatibility.py

# 
# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidState(Exception):
#     """
#     Exception raised when an operation is forbidden in the current state.
# 
#     """
# 
# 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 asyncio.queues
import codecs
import collections
import logging
import random
import struct
from .compatibility import asyncio_ensure_future
from .exceptions import (ConnectionClosed, InvalidState, PayloadTooBig,
                         WebSocketProtocolError)
from .framing import *
from .handshake import *
        transport = self.writer._transport
        if transport is not None:                           # pragma: no cover
            # PR 291 added the is_closing method to transports shortly after
            # PR 280 fixed the bug we're trying to work around in this block.
            if not hasattr(transport, 'is_closing'):
                # This emulates what is_closing would return if it existed.
                try:
                    is_closing = transport._closing
                except AttributeError:
                    is_closing = transport._closed
                if is_closing:
                    yield

        try:
            # Handle flow control automatically.
            yield from self.writer.drain()
        except ConnectionError:
            # Terminate the connection if the socket died.
            yield from self.fail_connection(1006)
            # And raise an exception, since the frame couldn't be sent.
            raise ConnectionClosed(self.close_code, self.close_reason)

    @asyncio.coroutine
    def close_connection(self, force=False):
        # 7.1.1. Close the WebSocket Connection
        if self.state == CLOSED:
            return

        # Defensive assertion for protocol compliance.
        if self.state != CLOSING and not force:             # pragma: no cover

# Path: server/websockets/compatibility.py

# 
# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidState(Exception):
#     """
#     Exception raised when an operation is forbidden in the current state.
# 
#     """
# 
# 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/protocol.py
import asyncio
import asyncio.queues
import codecs
import collections
import logging
import random
import struct
from .compatibility import asyncio_ensure_future
from .exceptions import (ConnectionClosed, InvalidState, PayloadTooBig,
                         WebSocketProtocolError)
from .framing import *
from .handshake import *

        transport = self.writer._transport
        if transport is not None:                           # pragma: no cover
            # PR 291 added the is_closing method to transports shortly after
            # PR 280 fixed the bug we're trying to work around in this block.
            if not hasattr(transport, 'is_closing'):
                # This emulates what is_closing would return if it existed.
                try:
                    is_closing = transport._closing
                except AttributeError:
                    is_closing = transport._closed
                if is_closing:
                    yield

        try:
            # Handle flow control automatically.
            yield from self.writer.drain()
        except ConnectionError:
            # Terminate the connection if the socket died.
            yield from self.fail_connection(1006)
            # And raise an exception, since the frame couldn't be sent.
            raise ConnectionClosed(self.close_code, self.close_reason)

    @asyncio.coroutine
    def close_connection(self, force=False):
        # 7.1.1. Close the WebSocket Connection
        if self.state == CLOSED:
            return

        # Defensive assertion for protocol compliance.
        if self.state != CLOSING and not force:             # pragma: no cover

            raise InvalidState("Cannot close a WebSocket connection "