static void remove_pending_message_by_index(MESSAGE_SENDER_INSTANCE* message_sender_instance, size_t index) { MESSAGE_WITH_CALLBACK** new_messages; if (message_sender_instance->messages[index]->message != NULL) { message_destroy(message_sender_instance->messages[index]->message); message_sender_instance->messages[index]->message = NULL; } amqpalloc_free(message_sender_instance->messages[index]); if (message_sender_instance->message_count - index > 1) { (void)memmove(&message_sender_instance->messages[index], &message_sender_instance->messages[index + 1], sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count - index - 1)); } message_sender_instance->message_count--; if (message_sender_instance->message_count > 0) { new_messages = (MESSAGE_WITH_CALLBACK**)amqpalloc_realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count)); if (new_messages != NULL) { message_sender_instance->messages = new_messages; } } else { amqpalloc_free(message_sender_instance->messages); message_sender_instance->messages = NULL; } }
void session_destroy_link_endpoint(LINK_ENDPOINT_HANDLE link_endpoint) { /* Codes_SRS_SESSION_01_050: [If link_endpoint is NULL, session_destroy_link_endpoint shall do nothing.] */ if (link_endpoint != NULL) { LINK_ENDPOINT_INSTANCE* endpoint_instance = (LINK_ENDPOINT_INSTANCE*)link_endpoint; SESSION_INSTANCE* session_instance = endpoint_instance->session; uint64_t i; /* Codes_SRS_SESSION_01_049: [session_destroy_link_endpoint shall free all resources associated with the endpoint.] */ for (i = 0; i < session_instance->link_endpoint_count; i++) { if (session_instance->link_endpoints[i] == link_endpoint) { break; } } if (i < session_instance->link_endpoint_count) { LINK_ENDPOINT_INSTANCE** new_endpoints; (void)memmove(&session_instance->link_endpoints[i], &session_instance->link_endpoints[i + 1], (session_instance->link_endpoint_count - (uint32_t)i - 1) * sizeof(LINK_ENDPOINT_INSTANCE*)); session_instance->link_endpoint_count--; if (session_instance->link_endpoint_count == 0) { amqpalloc_free(session_instance->link_endpoints); session_instance->link_endpoints = NULL; } else { new_endpoints = (LINK_ENDPOINT_INSTANCE**)amqpalloc_realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * session_instance->link_endpoint_count); if (new_endpoints != NULL) { session_instance->link_endpoints = new_endpoints; } } } if (endpoint_instance->name != NULL) { amqpalloc_free(endpoint_instance->name); } amqpalloc_free(endpoint_instance); } }
LINK_ENDPOINT_HANDLE session_create_link_endpoint(SESSION_HANDLE session, const char* name) { LINK_ENDPOINT_INSTANCE* result; /* Codes_SRS_SESSION_01_044: [If session, name or frame_received_callback is NULL, session_create_link_endpoint shall fail and return NULL.] */ if ((session == NULL) || (name == NULL)) { result = NULL; } else { /* Codes_SRS_SESSION_01_043: [session_create_link_endpoint shall create a link endpoint associated with a given session and return a non-NULL handle to it.] */ SESSION_INSTANCE* session_instance = (SESSION_INSTANCE*)session; result = (LINK_ENDPOINT_INSTANCE*)amqpalloc_malloc(sizeof(LINK_ENDPOINT_INSTANCE)); /* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */ if (result != NULL) { /* Codes_SRS_SESSION_01_046: [An unused handle shall be assigned to the link endpoint.] */ handle selected_handle = 0; size_t i; for (i = 0; i < session_instance->link_endpoint_count; i++) { if (session_instance->link_endpoints[i]->output_handle > selected_handle) { break; } selected_handle++; } result->on_session_state_changed = NULL; result->on_session_flow_on = NULL; result->frame_received_callback = NULL; result->callback_context = NULL; result->output_handle = selected_handle; result->name = amqpalloc_malloc(strlen(name) + 1); if (result->name == NULL) { /* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */ amqpalloc_free(result); result = NULL; } else { LINK_ENDPOINT_INSTANCE** new_link_endpoints; strcpy(result->name, name); result->session = session; new_link_endpoints = amqpalloc_realloc(session_instance->link_endpoints, sizeof(LINK_ENDPOINT_INSTANCE*) * (session_instance->link_endpoint_count + 1)); if (new_link_endpoints == NULL) { /* Codes_SRS_SESSION_01_045: [If allocating memory for the link endpoint fails, session_create_link_endpoint shall fail and return NULL.] */ amqpalloc_free(result); result = NULL; } else { session_instance->link_endpoints = new_link_endpoints; if (session_instance->link_endpoint_count - selected_handle > 0) { (void)memmove(&session_instance->link_endpoints[selected_handle], &session_instance->link_endpoints[selected_handle + 1], (session_instance->link_endpoint_count - selected_handle) * sizeof(LINK_ENDPOINT_INSTANCE*)); } session_instance->link_endpoints[selected_handle] = result; session_instance->link_endpoint_count++; } } } } return result; }
int messagesender_send(MESSAGE_SENDER_HANDLE message_sender, MESSAGE_HANDLE message, ON_MESSAGE_SEND_COMPLETE on_message_send_complete, void* callback_context) { int result; if ((message_sender == NULL) || (message == NULL)) { result = __LINE__; } else { MESSAGE_SENDER_INSTANCE* message_sender_instance = (MESSAGE_SENDER_INSTANCE*)message_sender; if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_ERROR) { result = __LINE__; } else { MESSAGE_WITH_CALLBACK* message_with_callback = (MESSAGE_WITH_CALLBACK*)amqpalloc_malloc(sizeof(MESSAGE_WITH_CALLBACK)); if (message_with_callback == NULL) { result = __LINE__; } else { MESSAGE_WITH_CALLBACK** new_messages = (MESSAGE_WITH_CALLBACK**)amqpalloc_realloc(message_sender_instance->messages, sizeof(MESSAGE_WITH_CALLBACK*) * (message_sender_instance->message_count + 1)); if (new_messages == NULL) { amqpalloc_free(message_with_callback); result = __LINE__; } else { result = 0; message_sender_instance->messages = new_messages; if (message_sender_instance->message_sender_state != MESSAGE_SENDER_STATE_OPEN) { message_with_callback->message = message_clone(message); if (message_with_callback->message == NULL) { amqpalloc_free(message_with_callback); result = __LINE__; } message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT; } else { message_with_callback->message = NULL; message_with_callback->message_send_state = MESSAGE_SEND_STATE_PENDING; } if (result == 0) { message_with_callback->on_message_send_complete = on_message_send_complete; message_with_callback->context = callback_context; message_with_callback->message_sender = message_sender_instance; message_sender_instance->messages[message_sender_instance->message_count] = message_with_callback; message_sender_instance->message_count++; if (message_sender_instance->message_sender_state == MESSAGE_SENDER_STATE_OPEN) { switch (send_one_message(message_sender_instance, message_with_callback, message)) { default: case SEND_ONE_MESSAGE_ERROR: remove_pending_message_by_index(message_sender_instance, message_sender_instance->message_count - 1); result = __LINE__; break; case SEND_ONE_MESSAGE_BUSY: message_with_callback->message = message_clone(message); if (message_with_callback->message == NULL) { amqpalloc_free(message_with_callback); result = __LINE__; } else { message_with_callback->message_send_state = MESSAGE_SEND_STATE_NOT_SENT; result = 0; } break; case SEND_ONE_MESSAGE_OK: result = 0; break; } } } } } } } return result; }
static void remove_operation_message_by_index(AMQP_MANAGEMENT_INSTANCE* amqp_management_instance, size_t index) { message_destroy(amqp_management_instance->operation_messages[index]->message); amqpalloc_free(amqp_management_instance->operation_messages[index]); if (amqp_management_instance->operation_message_count - index > 1) { memmove(&amqp_management_instance->operation_messages[index], &amqp_management_instance->operation_messages[index + 1], sizeof(OPERATION_MESSAGE_INSTANCE*)); } if (amqp_management_instance->operation_message_count == 1) { amqpalloc_free(amqp_management_instance->operation_messages); amqp_management_instance->operation_messages = NULL; } else { OPERATION_MESSAGE_INSTANCE** new_operation_messages = (OPERATION_MESSAGE_INSTANCE**)amqpalloc_realloc(amqp_management_instance->operation_messages, sizeof(OPERATION_MESSAGE_INSTANCE*) * (amqp_management_instance->operation_message_count - 1)); if (new_operation_messages != NULL) { amqp_management_instance->operation_messages = new_operation_messages; } } amqp_management_instance->operation_message_count--; }
int amqpmanagement_start_operation(AMQP_MANAGEMENT_HANDLE amqp_management, const char* operation, const char* type, const char* locales, MESSAGE_HANDLE message, ON_OPERATION_COMPLETE on_operation_complete, void* context) { int result; if ((amqp_management == NULL) || (operation == NULL)) { result = __LINE__; } else { AMQP_VALUE application_properties; if (message_get_application_properties(message, &application_properties) != 0) { result = __LINE__; } else { if ((add_string_key_value_pair_to_map(application_properties, "operation", operation) != 0) || (add_string_key_value_pair_to_map(application_properties, "type", type) != 0) || ((locales != NULL) && (add_string_key_value_pair_to_map(application_properties, "locales", locales) != 0))) { result = __LINE__; } else { if ((message_set_application_properties(message, application_properties) != 0) || (set_message_id(message, amqp_management->next_message_id) != 0)) { result = __LINE__; } else { OPERATION_MESSAGE_INSTANCE* pending_operation_message = amqpalloc_malloc(sizeof(OPERATION_MESSAGE_INSTANCE)); if (pending_operation_message == NULL) { result = __LINE__; } else { pending_operation_message->message = message_clone(message); pending_operation_message->callback_context = context; pending_operation_message->on_operation_complete = on_operation_complete; pending_operation_message->operation_state = OPERATION_STATE_NOT_SENT; pending_operation_message->message_id = amqp_management->next_message_id; amqp_management->next_message_id++; OPERATION_MESSAGE_INSTANCE** new_operation_messages = amqpalloc_realloc(amqp_management->operation_messages, (amqp_management->operation_message_count + 1) * sizeof(OPERATION_MESSAGE_INSTANCE*)); if (new_operation_messages == NULL) { message_destroy(message); amqpalloc_free(pending_operation_message); result = __LINE__; } else { amqp_management->operation_messages = new_operation_messages; amqp_management->operation_messages[amqp_management->operation_message_count] = pending_operation_message; amqp_management->operation_message_count++; if (send_operation_messages(amqp_management) != 0) { if (on_operation_complete != NULL) { on_operation_complete(context, OPERATION_RESULT_CBS_ERROR, 0, NULL); } result = __LINE__; } else { result = 0; } } } } } amqpvalue_destroy(application_properties); } } return result; }