| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdbool.h> |
| 7 |
#include "azure_c_shared_utility/gballoc.h" |
| 8 |
#include "azure_c_shared_utility/platform.h" |
| 9 |
#include "azure_c_shared_utility/tlsio.h" |
| 10 |
#include "azure_c_shared_utility/tickcounter.h" |
| 11 |
#include "azure_c_shared_utility/socketio.h" |
| 12 |
#include "azure_uamqp_c/uamqp.h" |
| 13 |
|
| 14 |
#if _WIN32 |
| 15 |
#include "windows.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
/* This sample connects to an Event Hub, authenticates using SASL PLAIN (key name/key) and then it sends 1000 messages */ |
| 19 |
/* Replace the below settings with your own.*/ |
| 20 |
|
| 21 |
#define EH_HOST "<<<Replace with your own EH host (like myeventhub.servicebus.windows.net)>>>" |
| 22 |
#define EH_KEY_NAME "<<<Replace with your own key name>>>" |
| 23 |
#define EH_KEY "<<<Replace with your own key>>>" |
| 24 |
#define EH_NAME "<<<Replace with your own EH name (like ingress_eh)>>>" |
| 25 |
|
| 26 |
static const size_t msg_count = 1000; |
| 27 |
static unsigned int sent_messages = 0; |
| 28 |
|
| 29 |
static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result, AMQP_VALUE delivery_state) |
| 30 |
{ |
| 31 |
(void)send_result; |
| 32 |
(void)context; |
| 33 |
(void)delivery_state; |
| 34 |
|
| 35 |
sent_messages++; |
| 36 |
} |
| 37 |
|
| 38 |
int main(int argc, char** argv) |
| 39 |
{ |
| 40 |
int result; |
| 41 |
|
| 42 |
(void)argc; |
| 43 |
(void)argv; |
| 44 |
|
| 45 |
if (platform_init() != 0) |
| 46 |
{ |
| 47 |
result = -1; |
| 48 |
} |
| 49 |
else |
| 50 |
{ |
| 51 |
XIO_HANDLE sasl_io; |
| 52 |
CONNECTION_HANDLE connection; |
| 53 |
SESSION_HANDLE session; |
| 54 |
LINK_HANDLE link; |
| 55 |
MESSAGE_SENDER_HANDLE message_sender; |
| 56 |
MESSAGE_HANDLE message; |
| 57 |
SASL_PLAIN_CONFIG sasl_plain_config = { EH_KEY_NAME, EH_KEY, NULL }; |
| 58 |
TLSIO_CONFIG tls_io_config = { EH_HOST, 5671 }; |
| 59 |
const IO_INTERFACE_DESCRIPTION* tlsio_interface; |
| 60 |
SASLCLIENTIO_CONFIG sasl_io_config; |
| 61 |
AMQP_VALUE source; |
| 62 |
AMQP_VALUE target; |
| 63 |
|
| 64 |
size_t last_memory_used = 0; |
| 65 |
SASL_MECHANISM_HANDLE sasl_mechanism_handle; |
| 66 |
XIO_HANDLE tls_io; |
| 67 |
unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' }; |
| 68 |
BINARY_DATA binary_data; |
| 69 |
|
| 70 |
gballoc_init(); |
| 71 |
|
| 72 |
/* create SASL PLAIN handler */ |
| 73 |
sasl_mechanism_handle = saslmechanism_create(saslplain_get_interface(), &sasl_plain_config); |
| 74 |
|
| 75 |
/* create the TLS IO */ |
| 76 |
tlsio_interface = platform_get_default_tlsio(); |
| 77 |
tls_io = xio_create(tlsio_interface, &tls_io_config); |
| 78 |
|
| 79 |
/* create the SASL client IO using the TLS IO */ |
| 80 |
sasl_io_config.underlying_io = tls_io; |
| 81 |
sasl_io_config.sasl_mechanism = sasl_mechanism_handle; |
| 82 |
sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config); |
| 83 |
|
| 84 |
/* create the connection, session and link */ |
| 85 |
connection = connection_create(sasl_io, EH_HOST, "some", NULL, NULL); |
| 86 |
connection_set_trace(connection, true); |
| 87 |
session = session_create(connection, NULL, NULL); |
| 88 |
session_set_incoming_window(session, 2147483647); |
| 89 |
session_set_outgoing_window(session, 65536); |
| 90 |
|
| 91 |
source = messaging_create_source("ingress"); |
| 92 |
target = messaging_create_target("amqps://" EH_HOST "/" EH_NAME); |
| 93 |
link = link_create(session, "sender-link", role_sender, source, target); |
| 94 |
link_set_snd_settle_mode(link, sender_settle_mode_unsettled); |
| 95 |
(void)link_set_max_message_size(link, 65536); |
| 96 |
|
| 97 |
amqpvalue_destroy(source); |
| 98 |
amqpvalue_destroy(target); |
| 99 |
|
| 100 |
message = message_create(); |
| 101 |
|
| 102 |
binary_data.bytes = hello; |
| 103 |
binary_data.length = sizeof(hello); |
| 104 |
message_add_body_amqp_data(message, binary_data); |
| 105 |
|
| 106 |
/* create a message sender */ |
| 107 |
message_sender = messagesender_create(link, NULL, NULL); |
| 108 |
if (messagesender_open(message_sender) == 0) |
| 109 |
{ |
| 110 |
uint32_t i; |
| 111 |
bool keep_running = true; |
| 112 |
tickcounter_ms_t start_time; |
| 113 |
TICK_COUNTER_HANDLE tick_counter = tickcounter_create(); |
| 114 |
|
| 115 |
if (tickcounter_get_current_ms(tick_counter, &start_time) != 0) |
| 116 |
{ |
| 117 |
(void)printf("Error getting start time\r\n"); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
for (i = 0; i < msg_count; i++) |
| 122 |
{ |
| 123 |
/* timeout if it takes longer than 10s */ |
| 124 |
(void)messagesender_send_async(message_sender, message, on_message_send_complete, message, 10000); |
| 125 |
} |
| 126 |
|
| 127 |
message_destroy(message); |
| 128 |
|
| 129 |
while (keep_running) |
| 130 |
{ |
| 131 |
size_t current_memory_used; |
| 132 |
size_t maximum_memory_used; |
| 133 |
connection_dowork(connection); |
| 134 |
|
| 135 |
current_memory_used = gballoc_getCurrentMemoryUsed(); |
| 136 |
maximum_memory_used = gballoc_getMaximumMemoryUsed(); |
| 137 |
|
| 138 |
if (current_memory_used != last_memory_used) |
| 139 |
{ |
| 140 |
(void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used); |
| 141 |
last_memory_used = current_memory_used; |
| 142 |
} |
| 143 |
|
| 144 |
if (sent_messages == msg_count) |
| 145 |
{ |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
{ |
| 151 |
tickcounter_ms_t end_time; |
| 152 |
if (tickcounter_get_current_ms(tick_counter, &end_time) != 0) |
| 153 |
{ |
| 154 |
(void)printf("Error getting end time\r\n"); |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
(void)printf("Send %u messages in %lu ms: %.02f msgs/sec\r\n", (unsigned int)msg_count, (unsigned long)(end_time - start_time), (float)msg_count / ((float)(end_time - start_time) / 1000)); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
message_destroy(message); |
| 166 |
} |
| 167 |
|
| 168 |
messagesender_destroy(message_sender); |
| 169 |
link_destroy(link); |
| 170 |
session_destroy(session); |
| 171 |
connection_destroy(connection); |
| 172 |
xio_destroy(sasl_io); |
| 173 |
xio_destroy(tls_io); |
| 174 |
saslmechanism_destroy(sasl_mechanism_handle); |
| 175 |
platform_deinit(); |
| 176 |
|
| 177 |
(void)printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed()); |
| 178 |
(void)printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed()); |
| 179 |
|
| 180 |
gballoc_deinit(); |
| 181 |
|
| 182 |
result = 0; |
| 183 |
} |
| 184 |
|
| 185 |
return result; |
| 186 |
} |