# Path: couchbaseConstants.py
# AUDIT_PKT_FMT = ">I"
# 
# INCRDECR_RES_FMT = ">Q"
# 
# MIN_RECV_PACKET = struct.calcsize(REQ_PKT_FMT)
# 
# REQ_MAGIC_BYTE = 0x80
# 
# REQ_PKT_FMT = ">BBHBBHIIQ"
# 
# RES_MAGIC_BYTE = 0x81
# 
# RES_PKT_FMT = ">BBHBBHIIQ"
# 
# SET_PKT_FMT = ">II"


import array
import random
import socket
import ssl
import struct
import couchbaseConstants
from typing import Iterable, List, Optional, Tuple, Union
from couchbaseConstants import (AUDIT_PKT_FMT, INCRDECR_RES_FMT, MIN_RECV_PACKET, REQ_MAGIC_BYTE, REQ_PKT_FMT,
                                RES_MAGIC_BYTE, RES_PKT_FMT, SET_PKT_FMT)
    from cb_version import VERSION  # pylint: disable=import-error
                sock_error = e
                # If we get here socket objects will be close()d via
                # garbage collection.
                pass
        else:
            # Didn't break from the loop, re-raise the last error
            raise sock_error

        self.r = random.Random()

    def close(self):
        self.s.close()

    def __del__(self):
        self.close()

    def _send_cmd(self, cmd: int, key: bytes, val: bytes, opaque: int, extra_header: bytes = b'', cas: int = 0,
                  extra_meta: bytes = b''):
        self._send_msg(cmd, key, val, opaque, extra_header=extra_header, cas=cas,
                       vbucket_id=self.vbucket_id, extra_meta=extra_meta)

    def _send_msg(self, cmd: int, key: bytes, val: bytes, opaque: int, extra_header: bytes = b'', cas: int = 0,
                  dtype: int = 0, vbucket_id: int = 0, extra_meta: bytes = b'',
                  fmt: str = REQ_PKT_FMT, magic: int = REQ_MAGIC_BYTE):
        msg: bytes = struct.pack(fmt, magic, cmd, len(key), len(extra_header), dtype, vbucket_id,
                                 len(key) + len(extra_header) + len(val) + len(extra_meta), opaque, cas)
        self.s.sendall(msg + extra_header + key + val + extra_meta)

    def _recv_msg(self) -> Tuple[int, int, int, int, int, int, bytes]:
        response = b''

# Path: couchbaseConstants.py
# AUDIT_PKT_FMT = ">I"
# 
# INCRDECR_RES_FMT = ">Q"
# 
# MIN_RECV_PACKET = struct.calcsize(REQ_PKT_FMT)
# 
# REQ_MAGIC_BYTE = 0x80
# 
# REQ_PKT_FMT = ">BBHBBHIIQ"
# 
# RES_MAGIC_BYTE = 0x81
# 
# RES_PKT_FMT = ">BBHBBHIIQ"
# 
# SET_PKT_FMT = ">II"


# Path: cb_bin_client.py
import array
import random
import socket
import ssl
import struct
import couchbaseConstants
from typing import Iterable, List, Optional, Tuple, Union
from couchbaseConstants import (AUDIT_PKT_FMT, INCRDECR_RES_FMT, MIN_RECV_PACKET, REQ_MAGIC_BYTE, REQ_PKT_FMT,
                                RES_MAGIC_BYTE, RES_PKT_FMT, SET_PKT_FMT)
    from cb_version import VERSION  # pylint: disable=import-error

                sock_error = e
                # If we get here socket objects will be close()d via
                # garbage collection.
                pass
        else:
            # Didn't break from the loop, re-raise the last error
            raise sock_error

        self.r = random.Random()

    def close(self):
        self.s.close()

    def __del__(self):
        self.close()

    def _send_cmd(self, cmd: int, key: bytes, val: bytes, opaque: int, extra_header: bytes = b'', cas: int = 0,
                  extra_meta: bytes = b''):
        self._send_msg(cmd, key, val, opaque, extra_header=extra_header, cas=cas,
                       vbucket_id=self.vbucket_id, extra_meta=extra_meta)

    def _send_msg(self, cmd: int, key: bytes, val: bytes, opaque: int, extra_header: bytes = b'', cas: int = 0,
                  dtype: int = 0, vbucket_id: int = 0, extra_meta: bytes = b'',
                  fmt: str = REQ_PKT_FMT, magic: int = REQ_MAGIC_BYTE):
        msg: bytes = struct.pack(fmt, magic, cmd, len(key), len(extra_header), dtype, vbucket_id,
                                 len(key) + len(extra_header) + len(val) + len(extra_meta), opaque, cas)
        self.s.sendall(msg + extra_header + key + val + extra_meta)

    def _recv_msg(self) -> Tuple[int, int, int, int, int, int, bytes]:
        response = b''

        while len(response) < MIN_RECV_PACKET: