void socketlistener_dowork(SOCKET_LISTENER_HANDLE socket_listener) { if (socket_listener != NULL) { SOCKET_LISTENER_INSTANCE* socket_listener_instance = (SOCKET_LISTENER_INSTANCE*)socket_listener; SOCKET accepted_socket = accept(socket_listener_instance->socket, NULL, NULL); if (accepted_socket != INVALID_SOCKET) { if (socket_listener_instance->on_socket_accepted != NULL) { SOCKETIO_CONFIG socketio_config = { NULL, socket_listener_instance->port, &accepted_socket }; XIO_HANDLE io = xio_create(socketio_get_interface_description(), &socketio_config, NULL); if (io == NULL) { (void)closesocket(accepted_socket); } else { socket_listener_instance->on_socket_accepted(socket_listener_instance->callback_context, io); } } else { (void)closesocket(accepted_socket); } } } }
void mqtt_client_sample_run() { if (platform_init() != 0) { PrintLogFunction(LOG_LINE, "platform_init failed"); } else { MQTT_CLIENT_HANDLE mqttHandle = mqtt_client_init(OnRecvCallback, OnOperationComplete, NULL, PrintLogFunction); if (mqttHandle == NULL) { PrintLogFunction(LOG_LINE, "mqtt_client_init failed"); } else { MQTT_CLIENT_OPTIONS options = { 0 }; options.clientId = "azureiotclient"; options.willMessage = NULL; options.username = NULL; options.password = NULL; options.keepAliveInterval = 10; options.useCleanSession = true; options.qualityOfServiceValue = DELIVER_AT_MOST_ONCE; SOCKETIO_CONFIG config = {"test.mosquitto.org", PORT_NUM_UNENCRYPTED, NULL}; XIO_HANDLE xio = xio_create(socketio_get_interface_description(), &config, PrintLogFunction); if (xio == NULL) { PrintLogFunction(LOG_LINE, "xio_create failed"); } else { if (mqtt_client_connect(mqttHandle, xio, &options) != 0) { PrintLogFunction(LOG_LINE, "mqtt_client_connect failed"); } else { do { mqtt_client_dowork(mqttHandle); } while (g_continue); } xio_close(xio, OnCloseComplete, NULL); } mqtt_client_deinit(mqttHandle); } platform_deinit(); } #ifdef _CRT_DBG_MAP_ALLOC _CrtDumpMemoryLeaks(); #endif }
CONCRETE_IO_HANDLE tlsio_schannel_create(void* io_create_parameters, LOGGER_LOG logger_log) { TLSIO_CONFIG* tls_io_config = io_create_parameters; TLS_IO_INSTANCE* result; if (tls_io_config == NULL) { result = NULL; } else { result = malloc(sizeof(TLS_IO_INSTANCE)); if (result != NULL) { SOCKETIO_CONFIG socketio_config; socketio_config.hostname = tls_io_config->hostname; socketio_config.port = tls_io_config->port; socketio_config.accepted_socket = NULL; result->on_bytes_received = NULL; result->on_io_open_complete = NULL; result->on_io_close_complete = NULL; result->on_io_error = NULL; result->logger_log = logger_log; result->on_io_open_complete_context = NULL; result->on_io_close_complete_context = NULL; result->on_bytes_received_context = NULL; result->on_io_error_context = NULL; result->credential_handle_allocated = false; result->host_name = (SEC_CHAR*)malloc(sizeof(SEC_CHAR) * (1 + strlen(tls_io_config->hostname))); if (result->host_name == NULL) { free(result); result = NULL; } else { (void)strcpy(result->host_name, tls_io_config->hostname); const IO_INTERFACE_DESCRIPTION* socket_io_interface = socketio_get_interface_description(); if (socket_io_interface == NULL) { free(result->host_name); free(result); result = NULL; } else { result->socket_io = xio_create(socket_io_interface, &socketio_config, logger_log); if (result->socket_io == NULL) { free(result->host_name); free(result); result = NULL; } else { result->received_bytes = NULL; result->received_byte_count = 0; result->buffer_size = 0; result->tlsio_state = TLSIO_STATE_NOT_OPEN; } } } } } return result; }
CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters) { TLSIO_CONFIG* tls_io_config = io_create_parameters; TLS_IO_INSTANCE* result; if (tls_io_config == NULL) { result = NULL; } else { result = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE)); if (result != NULL) { memset(result, 0, sizeof(TLS_IO_INSTANCE)); mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname); result->port = tls_io_config->port; result->socket_io_read_bytes = 0; result->socket_io_read_byte_count = 0; result->socket_io = NULL; result->ssl = NULL; result->ssl_context = NULL; result->certificate = NULL; result->on_bytes_received = NULL; result->on_bytes_received_context = NULL; result->on_io_open_complete = NULL; result->on_io_open_complete_context = NULL; result->on_io_close_complete = NULL; result->on_io_close_complete_context = NULL; result->on_io_error = NULL; result->on_io_error_context = NULL; result->tlsio_state = TLSIO_STATE_NOT_OPEN; result->ssl_context = wolfSSL_CTX_new(wolfTLSv1_client_method()); if (result->ssl_context == NULL) { free(result); result = NULL; } else { const IO_INTERFACE_DESCRIPTION* socket_io_interface = socketio_get_interface_description(); if (socket_io_interface == NULL) { wolfSSL_CTX_free(result->ssl_context); free(result); result = NULL; } else { SOCKETIO_CONFIG socketio_config; socketio_config.hostname = result->hostname; socketio_config.port = result->port; socketio_config.accepted_socket = NULL; result->socket_io = xio_create(socket_io_interface, &socketio_config); if (result->socket_io == NULL) { LogError("Failure connecting to underlying socket_io"); wolfSSL_CTX_free(result->ssl_context); free(result); result = NULL; } } } } } return result; }
int main(int argc, char** argv) { int result; (void)argc, argv; amqpalloc_set_memory_tracing_enabled(true); if (platform_init() != 0) { result = -1; } else { CONNECTION_HANDLE connection; SESSION_HANDLE session; LINK_HANDLE link; MESSAGE_SENDER_HANDLE message_sender; MESSAGE_HANDLE message; size_t last_memory_used = 0; /* create socket IO */ XIO_HANDLE socket_io; SOCKETIO_CONFIG socketio_config = { "localhost", 5672, NULL }; socket_io = xio_create(socketio_get_interface_description(), &socketio_config); /* create the connection, session and link */ connection = connection_create(socket_io, "localhost", "some", NULL, NULL); session = session_create(connection, NULL, NULL); session_set_incoming_window(session, 2147483647); session_set_outgoing_window(session, 65536); AMQP_VALUE source = messaging_create_source("ingress"); AMQP_VALUE target = messaging_create_target("localhost/ingress"); link = link_create(session, "sender-link", role_sender, source, target); link_set_snd_settle_mode(link, sender_settle_mode_settled); (void)link_set_max_message_size(link, 65536); amqpvalue_destroy(source); amqpvalue_destroy(target); message = message_create(); unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; BINARY_DATA binary_data; binary_data.bytes = hello; binary_data.length = sizeof(hello); message_add_body_amqp_data(message, binary_data); /* create a message sender */ message_sender = messagesender_create(link, NULL, NULL); if (messagesender_open(message_sender) == 0) { uint32_t i; #if _WIN32 unsigned long startTime = (unsigned long)GetTickCount64(); #endif for (i = 0; i < msg_count; i++) { (void)messagesender_send(message_sender, message, on_message_send_complete, message); } message_destroy(message); while (true) { size_t current_memory_used; size_t maximum_memory_used; connection_dowork(connection); current_memory_used = amqpalloc_get_current_memory_used(); maximum_memory_used = amqpalloc_get_maximum_memory_used(); if (current_memory_used != last_memory_used) { (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); last_memory_used = current_memory_used; } if (sent_messages == msg_count) { break; } } #if _WIN32 unsigned long endTime = (unsigned long)GetTickCount64(); (void)printf("Send %zu messages in %lu ms: %.02f msgs/sec\r\n", msg_count, (endTime - startTime), (float)msg_count / ((float)(endTime - startTime) / 1000)); #endif } messagesender_destroy(message_sender); link_destroy(link); session_destroy(session); connection_destroy(connection); xio_destroy(socket_io); platform_deinit(); (void)printf("Max memory usage:%lu\r\n", (unsigned long)amqpalloc_get_maximum_memory_used()); (void)printf("Current memory usage:%lu\r\n", (unsigned long)amqpalloc_get_current_memory_used()); result = 0; } #ifdef _CRTDBG_MAP_ALLOC _CrtDumpMemoryLeaks(); #endif return result; }