# Path: librouteros/exceptions.py
# class ProtocolError(LibRouterosError):
#     """Raised when e.g. encoding/decoding fails."""
# 
# class FatalError(ProtocolError):
#     """Exception raised when !fatal is received."""
# 
# Path: librouteros/connections.py
# class SocketTransport:
# 
#     def __init__(self, sock: socket):
#         self.sock = sock
# 
#     def write(self, data: bytes) -> None:
#         """
#         Write given bytes to socket. Loop as long as every byte in
#         string is written unless exception is raised.
#         """
#         self.sock.sendall(data)
# 
#     def read(self, length: int) -> bytes:
#         """
#         Read as many bytes from socket as specified in length.
#         Loop as long as every byte is read unless exception is raised.
#         """
#         data = bytearray()
#         while len(data) != length:
#             data += self.sock.recv((length - len(data)))
#             if not data:
#                 raise ConnectionClosed('Connection unexpectedly closed.')
#         return data
# 
#     def close(self) -> None:
#         self.sock.close()


import typing
from struct import pack, unpack #pylint: disable=no-name-in-module
from logging import getLogger, NullHandler
from librouteros.exceptions import (
    ProtocolError,
    FatalError,
)
from librouteros.connections import SocketTransport
            offset = -4
        else:
            raise ProtocolError(f'Unable to encode length of {length}')

        return pack('!I', ored_length)[offset:]


class Decoder:

    @staticmethod
    def determineLength(length: bytes) -> int:
        """
        Given first read byte, determine how many more bytes
        needs to be known in order to get fully encoded length.

        :param length: First read byte.
        :return: How many bytes to read.
        """
        integer = ord(length)

        #pylint: disable=no-else-return
        if integer < 128:
            return 0
        elif integer < 192:
            return 1
        elif integer < 224:
            return 2
        elif integer < 240:
            return 3


# Path: librouteros/exceptions.py
# class ProtocolError(LibRouterosError):
#     """Raised when e.g. encoding/decoding fails."""
# 
# class FatalError(ProtocolError):
#     """Exception raised when !fatal is received."""
# 
# Path: librouteros/connections.py
# class SocketTransport:
# 
#     def __init__(self, sock: socket):
#         self.sock = sock
# 
#     def write(self, data: bytes) -> None:
#         """
#         Write given bytes to socket. Loop as long as every byte in
#         string is written unless exception is raised.
#         """
#         self.sock.sendall(data)
# 
#     def read(self, length: int) -> bytes:
#         """
#         Read as many bytes from socket as specified in length.
#         Loop as long as every byte is read unless exception is raised.
#         """
#         data = bytearray()
#         while len(data) != length:
#             data += self.sock.recv((length - len(data)))
#             if not data:
#                 raise ConnectionClosed('Connection unexpectedly closed.')
#         return data
# 
#     def close(self) -> None:
#         self.sock.close()


# Path: librouteros/protocol.py
import typing
from struct import pack, unpack #pylint: disable=no-name-in-module
from logging import getLogger, NullHandler
from librouteros.exceptions import (
    ProtocolError,
    FatalError,
)
from librouteros.connections import SocketTransport

            offset = -4
        else:
            raise ProtocolError(f'Unable to encode length of {length}')

        return pack('!I', ored_length)[offset:]


class Decoder:

    @staticmethod
    def determineLength(length: bytes) -> int:
        """
        Given first read byte, determine how many more bytes
        needs to be known in order to get fully encoded length.

        :param length: First read byte.
        :return: How many bytes to read.
        """
        integer = ord(length)

        #pylint: disable=no-else-return
        if integer < 128:
            return 0
        elif integer < 192:
            return 1
        elif integer < 224:
            return 2
        elif integer < 240:
            return 3


        raise ProtocolError(f'Unknown controll byte {length!r}')