static void broker_decrement_ref(BROKER_HANDLE broker) { /*Codes_SRS_BROKER_13_058: [If `broker` is NULL the function shall do nothing.]*/ if (broker != NULL) { /*Codes_SRS_BROKER_13_111: [Otherwise, Broker_Destroy shall decrement the internal ref count of the message.]*/ /*Codes_SRS_BROKER_13_112: [If the ref count is zero then the allocated resources are freed.]*/ if (DEC_REF(BROKER_HANDLE_DATA, broker) == DEC_RETURN_ZERO) { BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker; if (singlylinkedlist_get_head_item(broker_data->modules) != NULL) { LogError("WARNING: There are still active modules attached to the broker and the broker is being destroyed."); } /* May want to do nn_shutdown first for cleanliness. */ nn_close(broker_data->publish_socket); STRING_delete(broker_data->url); singlylinkedlist_destroy(broker_data->modules); Lock_Deinit(broker_data->modules_lock); free(broker_data); } } else { LogError("broker handle is NULL"); } }
static void feedbackReceivedCallback(void* context, IOTHUB_SERVICE_FEEDBACK_BATCH* feedbackBatch) { printf("Feedback received, context: %s\n", (char*)context); if (feedbackBatch != NULL) { (void)printf("Batch userId : %s\r\n", feedbackBatch->userId); (void)printf("Batch lockToken : %s\r\n", feedbackBatch->lockToken); if (feedbackBatch->feedbackRecordList != NULL) { LIST_ITEM_HANDLE feedbackRecord = singlylinkedlist_get_head_item(feedbackBatch->feedbackRecordList); while (feedbackRecord != NULL) { IOTHUB_SERVICE_FEEDBACK_RECORD* feedback = (IOTHUB_SERVICE_FEEDBACK_RECORD*)singlylinkedlist_item_get_value(feedbackRecord); if (feedback != NULL) { (void)printf("Feedback\r\n"); (void)printf(" deviceId : %s\r\n", feedback->deviceId); (void)printf(" generationId : %s\r\n", feedback->generationId); (void)printf(" correlationId : %s\r\n", feedback->correlationId); (void)printf(" description : %s\r\n", feedback->description); (void)printf(" enqueuedTimeUtc : %s\r\n", feedback->enqueuedTimeUtc); feedbackRecord = singlylinkedlist_get_next_item(feedbackRecord); } } } } }
void socketio_destroy(CONCRETE_IO_HANDLE socket_io) { LIST_ITEM_HANDLE first_pending_io; if (socket_io != NULL) { SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; /* we cannot do much if the close fails, so just ignore the result */ (void)closesocket(socket_io_instance->socket); /* clear allpending IOs */ while ((first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list)) != NULL) { PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); if (pending_socket_io != NULL) { free(pending_socket_io->bytes); free(pending_socket_io); } singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); } singlylinkedlist_destroy(socket_io_instance->pending_io_list); if (socket_io_instance->hostname != NULL) { free(socket_io_instance->hostname); } free(socket_io); } }
void socketio_dowork(CONCRETE_IO_HANDLE socket_io) { int send_result; if (socket_io != NULL) { SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; if (socket_io_instance->io_state == IO_STATE_OPEN) { int received = 1; LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); while (first_pending_io != NULL) { PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); if (pending_socket_io == NULL) { LogError("Failure: retrieving socket from list"); indicate_error(socket_io_instance); break; } /* TODO: we need to do more than a cast here to be 100% clean The following bug was filed: [WarnL4] socketio_win32 does not account for already sent bytes and there is a truncation of size from size_t to int */ send_result = send(socket_io_instance->socket, (const char*)pending_socket_io->bytes, (int)pending_socket_io->size, 0); if (send_result != (int)pending_socket_io->size) { int last_error = WSAGetLastError(); if (last_error != WSAEWOULDBLOCK) { free(pending_socket_io->bytes); free(pending_socket_io); (void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); } else { /* try again */ } } else { if (pending_socket_io->on_send_complete != NULL) { pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK); } free(pending_socket_io->bytes); free(pending_socket_io); if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0) { LogError("Failure: removing socket from list"); indicate_error(socket_io_instance); } } first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); } while (received > 0) { unsigned char* recv_bytes = malloc(RECEIVE_BYTES_VALUE); if (recv_bytes == NULL) { LogError("Socketio_Failure: NULL allocating input buffer."); indicate_error(socket_io_instance); } else { received = recv(socket_io_instance->socket, (char*)recv_bytes, RECEIVE_BYTES_VALUE, 0); if (received > 0) { if (socket_io_instance->on_bytes_received != NULL) { /* explictly ignoring here the result of the callback */ (void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, recv_bytes, received); } } else { int last_error = WSAGetLastError(); if (last_error != WSAEWOULDBLOCK && last_error != ERROR_SUCCESS) { LogError("Socketio_Failure: Recieving data from endpoint: %d.", last_error); indicate_error(socket_io_instance); } } free(recv_bytes); } } } } }
int socketio_send(CONCRETE_IO_HANDLE socket_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) { int result; if ((socket_io == NULL) || (buffer == NULL) || (size == 0)) { /* Invalid arguments */ LogError("Invalid argument: send given invalid parameter"); result = __LINE__; } else { SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; if (socket_io_instance->io_state != IO_STATE_OPEN) { LogError("Failure: socket state is not opened."); result = __LINE__; } else { LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); if (first_pending_io != NULL) { if (add_pending_io(socket_io_instance, buffer, size, on_send_complete, callback_context) != 0) { LogError("Failure: add_pending_io failed."); result = __LINE__; } else { result = 0; } } else { /* TODO: we need to do more than a cast here to be 100% clean The following bug was filed: [WarnL4] socketio_win32 does not account for already sent bytes and there is a truncation of size from size_t to int */ int send_result = send(socket_io_instance->socket, buffer, (int)size, 0); if (send_result != (int)size) { int last_error = WSAGetLastError(); if (last_error != WSAEWOULDBLOCK) { indicate_error(socket_io_instance); LogError("Failure: sending socket failed %d.", last_error); result = __LINE__; } else { /* queue data */ if (add_pending_io(socket_io_instance, buffer, size, on_send_complete, callback_context) != 0) { LogError("Failure: add_pending_io failed."); result = __LINE__; } else { result = 0; } } } else { if (on_send_complete != NULL) { on_send_complete(callback_context, IO_SEND_OK); } result = 0; } } } } return result; }
void tls_server_io_schannel_destroy(CONCRETE_IO_HANDLE tls_io) { if (tls_io != NULL) { LIST_ITEM_HANDLE first_pending_io; TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; if (tls_io_instance->credential_handle_allocated) { (void)FreeCredentialHandle(&tls_io_instance->credential_handle); tls_io_instance->credential_handle_allocated = false; } if (tls_io_instance->received_bytes != NULL) { free(tls_io_instance->received_bytes); } if (tls_io_instance->x509_schannel_handle != NULL) { x509_schannel_destroy(tls_io_instance->x509_schannel_handle); } if (tls_io_instance->x509certificate != NULL) { free(tls_io_instance->x509certificate); } if (tls_io_instance->x509privatekey != NULL) { free(tls_io_instance->x509privatekey); } xio_destroy(tls_io_instance->socket_io); free(tls_io_instance->host_name); first_pending_io = singlylinkedlist_get_head_item(tls_io_instance->pending_io_list); while (first_pending_io != NULL) { PENDING_SEND* pending_send = (PENDING_SEND*)singlylinkedlist_item_get_value(first_pending_io); if (pending_send == NULL) { LogError("Failure: retrieving socket from list"); indicate_error(tls_io_instance); break; } else { if (pending_send->on_send_complete != NULL) { pending_send->on_send_complete(pending_send->on_send_complete_context, IO_SEND_CANCELLED); } if (singlylinkedlist_remove(tls_io_instance->pending_io_list, first_pending_io) != 0) { LogError("Failure: removing pending IO from list"); } } } if (tls_io_instance->cert_context != NULL) { if (!CertFreeCertificateContext(tls_io_instance->cert_context)) { LogError("Failure freeing certificate context"); } } singlylinkedlist_destroy(tls_io_instance->pending_io_list); free(tls_io); } }