Пример #1
0
static int add_pending_io(SOCKET_IO_INSTANCE* socket_io_instance, const unsigned char* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
{
    int result;
    PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)malloc(sizeof(PENDING_SOCKET_IO));
    if (pending_socket_io == NULL)
    {
        result = __LINE__;
    }
    else
    {
        pending_socket_io->bytes = (unsigned char*)malloc(size);
        if (pending_socket_io->bytes == NULL)
        {
            LogError("Allocation Failure: Unable to allocate pending list.");
            free(pending_socket_io);
            result = __LINE__;
        }
        else
        {
            pending_socket_io->size = size;
            pending_socket_io->on_send_complete = on_send_complete;
            pending_socket_io->callback_context = callback_context;
            pending_socket_io->pending_io_list = socket_io_instance->pending_io_list;
            (void)memcpy(pending_socket_io->bytes, buffer, size);

            if (singlylinkedlist_add(socket_io_instance->pending_io_list, pending_socket_io) == NULL)
            {
                LogError("Failure: Unable to add socket to pending list.");
                free(pending_socket_io->bytes);
                free(pending_socket_io);
                result = __LINE__;
            }
            else
            {
                result = 0;
            }
        }
    }

    return result;
}
Пример #2
0
BROKER_RESULT Broker_AddModule(BROKER_HANDLE broker, const MODULE* module)
{
    BROKER_RESULT result;

    /*Codes_SRS_BROKER_99_013: [If `broker` or `module` is NULL the function shall return BROKER_INVALIDARG.]*/
    if (broker == NULL || module == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("invalid parameter (NULL).");
    }
    /*Codes_SRS_BROKER_99_014: [If `module_handle` or `module_apis` are `NULL` the function shall return `BROKER_INVALIDARG`.]*/
    else if (module->module_apis == NULL || module->module_handle == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("invalid parameter (NULL).");
    }
    else
    {
        BROKER_MODULEINFO* module_info = (BROKER_MODULEINFO*)malloc(sizeof(BROKER_MODULEINFO));
        if (module_info == NULL)
        {
            LogError("Allocate module info failed");
            result = BROKER_ERROR;
        }
        else
        {
            if (init_module(module_info, module) != BROKER_OK)
            {
                /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                LogError("start_module failed");
                free(module_info->module);
                free(module_info);
                result = BROKER_ERROR;
            }
            else
            {
                /*Codes_SRS_BROKER_13_039: [This function shall acquire the lock on BROKER_HANDLE_DATA::modules_lock.]*/
                BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker;
                if (Lock(broker_data->modules_lock) != LOCK_OK)
                {
                    /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                    LogError("Lock on broker_data->modules_lock failed");
                    deinit_module(module_info);
                    free(module_info);
                    result = BROKER_ERROR;
                }
                else
                {
                    /*Codes_SRS_BROKER_13_045: [Broker_AddModule shall append the new instance of BROKER_MODULEINFO to BROKER_HANDLE_DATA::modules.]*/
                    LIST_ITEM_HANDLE moduleListItem = singlylinkedlist_add(broker_data->modules, module_info);
                    if (moduleListItem == NULL)
                    {
                        /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                        LogError("singlylinkedlist_add failed");
                        deinit_module(module_info);
                        free(module_info);
                        result = BROKER_ERROR;
                    }
                    else
                    {
                        if (start_module(module_info, broker_data->url) != BROKER_OK)
                        {
                            LogError("start_module failed");
                            deinit_module(module_info);
                            singlylinkedlist_remove(broker_data->modules, moduleListItem);
                            free(module_info);
                            result = BROKER_ERROR;
                        }
                        else
                        {
                            /*Codes_SRS_BROKER_13_047: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                            result = BROKER_OK;
                        }
                    }

                    /*Codes_SRS_BROKER_13_046: [This function shall release the lock on BROKER_HANDLE_DATA::modules_lock.]*/
                    Unlock(broker_data->modules_lock);
                }
            }

        }
    }

    return result;
}