# 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
        Encode word in API format.

        :param word: Word to encode.
        :returns: Encoded word.
        """
        #pylint: disable=no-member
        encoded_word = word.encode(encoding=self.encoding, errors='strict') # type: ignore
        return Encoder.encodeLength(len(word)) + encoded_word

    @staticmethod
    def encodeLength(length: int) -> bytes:
        """
        Encode given length in mikrotik format.

        :param length: Integer < 268435456.
        :returns: Encoded length.
        """
        if length < 128:
            ored_length = length
            offset = -1
        elif length < 16384:
            ored_length = length | 0x8000
            offset = -2
        elif length < 2097152:
            ored_length = length | 0xC00000
            offset = -3
        elif length < 268435456:
            ored_length = length | 0xE0000000
            offset = -4
        else:

# 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

        Encode word in API format.

        :param word: Word to encode.
        :returns: Encoded word.
        """
        #pylint: disable=no-member
        encoded_word = word.encode(encoding=self.encoding, errors='strict') # type: ignore
        return Encoder.encodeLength(len(word)) + encoded_word

    @staticmethod
    def encodeLength(length: int) -> bytes:
        """
        Encode given length in mikrotik format.

        :param length: Integer < 268435456.
        :returns: Encoded length.
        """
        if length < 128:
            ored_length = length
            offset = -1
        elif length < 16384:
            ored_length = length | 0x8000
            offset = -2
        elif length < 2097152:
            ored_length = length | 0xC00000
            offset = -3
        elif length < 268435456:
            ored_length = length | 0xE0000000
            offset = -4
        else:

            raise ProtocolError(f'Unable to encode length of {length}')