| 1 |
#------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for |
| 4 |
# license information. |
| 5 |
#-------------------------------------------------------------------------- |
| 6 |
|
| 7 |
# Python imports |
| 8 |
import logging |
| 9 |
|
| 10 |
# C imports |
| 11 |
cimport c_xio |
| 12 |
cimport c_wsio |
| 13 |
cimport c_sasl_mechanism |
| 14 |
|
| 15 |
|
| 16 |
_logger = logging.getLogger(__name__) |
| 17 |
|
| 18 |
|
| 19 |
cpdef xio_from_wsioconfig(WSIOConfig io_config): |
| 20 |
cdef const c_xio.IO_INTERFACE_DESCRIPTION* ws_io_interface |
| 21 |
ws_io_interface = c_wsio.wsio_get_interface_description() |
| 22 |
xio = XIO() |
| 23 |
xio.create(ws_io_interface, io_config, &io_config._c_value) |
| 24 |
return xio |
| 25 |
|
| 26 |
|
| 27 |
cpdef xio_from_tlsioconfig(IOInterfaceDescription io_desc, TLSIOConfig io_config): |
| 28 |
xio = XIO() |
| 29 |
xio.create(io_desc._c_value, io_config, &io_config._c_value) |
| 30 |
return xio |
| 31 |
|
| 32 |
|
| 33 |
cpdef xio_from_saslioconfig(SASLClientIOConfig io_config): |
| 34 |
cdef const c_xio.IO_INTERFACE_DESCRIPTION* interface |
| 35 |
interface = c_sasl_mechanism.saslclientio_get_interface_description() |
| 36 |
if <void*>interface == NULL: |
| 37 |
raise ValueError("Failed to create SASL Client IO Interface description") |
| 38 |
xio = XIO() |
| 39 |
xio.create(interface, io_config, &io_config._c_value) |
| 40 |
return xio |
| 41 |
|
| 42 |
|
| 43 |
cdef class XIO(StructBase): |
| 44 |
|
| 45 |
cdef c_xio.XIO_HANDLE _c_value |
| 46 |
cdef object _io_config |
| 47 |
|
| 48 |
def __cinit__(self): |
| 49 |
pass |
| 50 |
|
| 51 |
def __dealloc__(self): |
| 52 |
_logger.debug("Deallocating XIO") |
| 53 |
self.destroy() |
| 54 |
|
| 55 |
cdef _create(self): |
| 56 |
if <void*>self._c_value is NULL: |
| 57 |
self._memory_error() |
| 58 |
|
| 59 |
cpdef destroy(self): |
| 60 |
if <void*>self._c_value is not NULL: |
| 61 |
_logger.debug("Destroying XIO") |
| 62 |
c_xio.xio_destroy(self._c_value) |
| 63 |
self._c_value = <c_xio.XIO_HANDLE>NULL |
| 64 |
self._io_config = None |
| 65 |
|
| 66 |
cdef wrap(self, XIO value): |
| 67 |
self.destroy() |
| 68 |
self._io_config = value._io_config |
| 69 |
self._c_value = value._c_value |
| 70 |
self._create() |
| 71 |
|
| 72 |
cdef create(self, c_xio.IO_INTERFACE_DESCRIPTION* io_desc, object io_config, void *io_params): |
| 73 |
self.destroy() |
| 74 |
self._io_config = io_config |
| 75 |
self._c_value = c_xio.xio_create(io_desc, io_params) |
| 76 |
self._create() |
| 77 |
|
| 78 |
cpdef set_option(self, const char* option_name, value): |
| 79 |
cdef const void* option_value |
| 80 |
option_value = <const void*>value |
| 81 |
if c_xio.xio_setoption(self._c_value, option_name, option_value) != 0: |
| 82 |
raise self._value_error("Failed to set option {}".format(option_name)) |
| 83 |
|
| 84 |
cpdef set_certificates(self, bytes value): |
| 85 |
cdef char *certificate = value |
| 86 |
if c_xio.xio_setoption(self._c_value, b'TrustedCerts', <void*>certificate) != 0: |
| 87 |
raise self._value_error("Failed to set certificates") |
| 88 |
|
| 89 |
|
| 90 |
cdef class IOInterfaceDescription(object): |
| 91 |
|
| 92 |
cdef c_xio.IO_INTERFACE_DESCRIPTION* _c_value |
| 93 |
|
| 94 |
def __cinit__(self): |
| 95 |
pass |
| 96 |
|
| 97 |
cdef wrap(self, c_xio.IO_INTERFACE_DESCRIPTION* value): |
| 98 |
self._c_value = value |