# Path: gnsq/errors.py
# class NSQSocketError(socket.error, NSQException):
#     pass
# 
# Path: gnsq/stream/defalte.py
# class DefalteSocket(CompressionSocket):
#     def __init__(self, socket, level):
#         wbits = -zlib.MAX_WBITS
#         self._decompressor = zlib.decompressobj(wbits)
#         self._compressor = zlib.compressobj(level, zlib.DEFLATED, wbits)
#         super(DefalteSocket, self).__init__(socket)
# 
#     def compress(self, data):
#         data = self._compressor.compress(data)
#         return data + self._compressor.flush(zlib.Z_SYNC_FLUSH)
# 
#     def decompress(self, data):
#         return self._decompressor.decompress(data)
# 
#     def close(self):
#         self._socket.sendall(self._compressor.flush(zlib.Z_FINISH))
#         self._socket.close()


from mmap import PAGESIZE
from errno import ENOTCONN, EDEADLK, EAGAIN, EWOULDBLOCK
from gevent import socket
from gevent.lock import Semaphore
from gevent.ssl import SSLSocket, PROTOCOL_TLSv1_2, CERT_NONE
from gnsq.errors import NSQSocketError
    from .snappy import SnappySocket
from .defalte import DefalteSocket
import six
import gevent




try:
except ImportError:
    SnappySocket = None  # pyflakes.ignore



class Stream(object):
    def __init__(self, address, port, timeout, buffer_size=PAGESIZE,
                 lock_class=Semaphore):
        self.address = address
        self.port = port
        self.timeout = timeout

        self.buffer = b''
        self.buffer_size = buffer_size

        self.socket = None
        self.lock = lock_class()

    @property
    def is_connected(self):
        return self.socket is not None

    def ensure_connection(self):
        if self.is_connected:
            return

# Path: gnsq/errors.py
# class NSQSocketError(socket.error, NSQException):
#     pass
# 
# Path: gnsq/stream/defalte.py
# class DefalteSocket(CompressionSocket):
#     def __init__(self, socket, level):
#         wbits = -zlib.MAX_WBITS
#         self._decompressor = zlib.decompressobj(wbits)
#         self._compressor = zlib.compressobj(level, zlib.DEFLATED, wbits)
#         super(DefalteSocket, self).__init__(socket)
# 
#     def compress(self, data):
#         data = self._compressor.compress(data)
#         return data + self._compressor.flush(zlib.Z_SYNC_FLUSH)
# 
#     def decompress(self, data):
#         return self._decompressor.decompress(data)
# 
#     def close(self):
#         self._socket.sendall(self._compressor.flush(zlib.Z_FINISH))
#         self._socket.close()


# Path: gnsq/stream/stream.py
from mmap import PAGESIZE
from errno import ENOTCONN, EDEADLK, EAGAIN, EWOULDBLOCK
from gevent import socket
from gevent.lock import Semaphore
from gevent.ssl import SSLSocket, PROTOCOL_TLSv1_2, CERT_NONE
from gnsq.errors import NSQSocketError
    from .snappy import SnappySocket
from .defalte import DefalteSocket
import six
import gevent





try:
except ImportError:
    SnappySocket = None  # pyflakes.ignore



class Stream(object):
    def __init__(self, address, port, timeout, buffer_size=PAGESIZE,
                 lock_class=Semaphore):
        self.address = address
        self.port = port
        self.timeout = timeout

        self.buffer = b''
        self.buffer_size = buffer_size

        self.socket = None
        self.lock = lock_class()

    @property
    def is_connected(self):
        return self.socket is not None

    def ensure_connection(self):
        if self.is_connected:
            return

        raise NSQSocketError(ENOTCONN, 'Socket is not connected')