CONCRETE_IO_HANDLE saslclientio_create()

in src/saslclientio.c [960:1031]


CONCRETE_IO_HANDLE saslclientio_create(void* io_create_parameters)
{
    SASLCLIENTIO_CONFIG* sasl_client_io_config = (SASLCLIENTIO_CONFIG*)io_create_parameters;
    SASL_CLIENT_IO_INSTANCE* result;

    /* Codes_SRS_SASLCLIENTIO_01_005: [If `io_create_parameters` is NULL, `saslclientio_create` shall fail and return NULL.] */
    if (sasl_client_io_config == NULL)
    {
        LogError("NULL io_create_parameters");
        result = NULL;
    }
    /* Codes_SRS_SASLCLIENTIO_01_092: [If any of the `sasl_mechanism` or `underlying_io` members of the configuration structure are NULL, `saslclientio_create` shall fail and return NULL.] */
    else if ((sasl_client_io_config->underlying_io == NULL) ||
        (sasl_client_io_config->sasl_mechanism == NULL))
    {
        LogError("Bad parameters: underlying_io = %p, sasl_mechanism = %p",
            sasl_client_io_config->underlying_io, sasl_client_io_config->sasl_mechanism);
        result = NULL;
    }
    else
    {
        result = (SASL_CLIENT_IO_INSTANCE*)calloc(1, sizeof(SASL_CLIENT_IO_INSTANCE));
        if (result == NULL)
        {
            /* Codes_SRS_SASLCLIENTIO_01_006: [If memory cannot be allocated for the new instance, `saslclientio_create` shall fail and return NULL.] */
            LogError("Cannot allocate sasl client IO instance");
        }
        else
        {
            result->underlying_io = sasl_client_io_config->underlying_io;
            /* Codes_SRS_SASLCLIENTIO_01_089: [`saslclientio_create` shall create a frame codec to be used for encoding/decoding frames by calling `frame_codec_create` and passing `on_frame_codec_error` and a context as arguments.] */
            result->frame_codec = frame_codec_create(on_frame_codec_error, result);
            if (result->frame_codec == NULL)
            {
                /* Codes_SRS_SASLCLIENTIO_01_090: [If `frame_codec_create` fails, then `saslclientio_create` shall fail and return NULL.] */
                LogError("frame_codec_create failed");
                free(result);
                result = NULL;
            }
            else
            {
                /* Codes_SRS_SASLCLIENTIO_01_084: [`saslclientio_create` shall create a SASL frame codec to be used for SASL frame encoding/decoding by calling `sasl_frame_codec_create` and passing the just created frame codec as argument.] */
                result->sasl_frame_codec = sasl_frame_codec_create(result->frame_codec, on_sasl_frame_received_callback, on_sasl_frame_codec_error, result);
                if (result->sasl_frame_codec == NULL)
                {
                    /* Codes_SRS_SASLCLIENTIO_01_085: [If `sasl_frame_codec_create` fails, then `saslclientio_create` shall fail and return NULL.] */
                    LogError("sasl_frame_codec_create failed");
                    frame_codec_destroy(result->frame_codec);
                    free(result);
                    result = NULL;
                }
                else
                {
                    /* Codes_SRS_SASLCLIENTIO_01_004: [`saslclientio_create` shall return on success a non-NULL handle to a new SASL client IO instance.] */
                    result->on_bytes_received = NULL;
                    result->on_io_open_complete = NULL;
                    result->on_io_error = NULL;
                    result->on_io_close_complete = NULL;
                    result->on_bytes_received_context = NULL;
                    result->on_io_open_complete_context = NULL;
                    result->on_io_close_complete_context = NULL;
                    result->on_io_error_context = NULL;
                    result->sasl_mechanism = sasl_client_io_config->sasl_mechanism;

                    result->io_state = IO_STATE_NOT_OPEN;
                }
            }
        }
    }

    return result;
}