Debian Codegraph @

src/vendor/azure-uamqp-c/samples/local_server_tls_sample/main.c

raw
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/xio.h"
10 #include "azure_uamqp_c/uamqp.h"
11 #include "tls_server_io.h"
12
13 static unsigned int sent_messages = 0;
14 static const size_t msg_count = 1;
15 static CONNECTION_HANDLE connection;
16 static SESSION_HANDLE session;
17 static LINK_HANDLE link;
18 static MESSAGE_RECEIVER_HANDLE message_receiver;
19 static size_t count_received;
20 static unsigned char* cert_buffer;
21 static size_t cert_size;
22
23 static void on_message_receiver_state_changed(const void* context, MESSAGE_RECEIVER_STATE new_state, MESSAGE_RECEIVER_STATE previous_state)
24 {
25 (void)context;
26 (void)new_state;
27 (void)previous_state;
28 }
29
30 static AMQP_VALUE on_message_received(const void* context, MESSAGE_HANDLE message)
31 {
32 (void)context;
33 (void)message;
34
35 if ((count_received % 1000) == 0)
36 {
37 printf("Messages received : %u.\r\n", (unsigned int)count_received);
38 }
39 count_received++;
40
41 return messaging_delivery_accepted();
42 }
43
44 static bool on_new_link_attached(void* context, LINK_ENDPOINT_HANDLE new_link_endpoint, const char* name, role role, AMQP_VALUE source, AMQP_VALUE target, fields properties)
45 {
46 (void)context;
47 (void)properties;
48 link = link_create_from_endpoint(session, new_link_endpoint, name, role, source, target);
49 link_set_rcv_settle_mode(link, receiver_settle_mode_first);
50 message_receiver = messagereceiver_create(link, on_message_receiver_state_changed, NULL);
51 messagereceiver_open(message_receiver, on_message_received, NULL);
52 return true;
53 }
54
55 static bool on_new_session_endpoint(void* context, ENDPOINT_HANDLE new_endpoint)
56 {
57 (void)context;
58 session = session_create_from_endpoint(connection, new_endpoint, on_new_link_attached, NULL);
59 session_set_incoming_window(session, 10000);
60 session_begin(session);
61 return true;
62 }
63
64 static void on_socket_accepted(void* context, const IO_INTERFACE_DESCRIPTION* interface_description, void* io_parameters)
65 {
66 HEADER_DETECT_IO_CONFIG header_detect_io_config;
67 TLS_SERVER_IO_CONFIG tls_server_io_config;
68 XIO_HANDLE underlying_io;
69 XIO_HANDLE header_detect_io;
70
71 (void)context;
72
73 tls_server_io_config.certificate = cert_buffer;
74 tls_server_io_config.certificate_size = cert_size;
75 tls_server_io_config.underlying_io_interface = interface_description;
76 tls_server_io_config.underlying_io_parameters = io_parameters;
77
78 underlying_io = xio_create(tls_server_io_get_interface_description(), &tls_server_io_config);
79
80 header_detect_io_config.underlying_io = underlying_io;
81 header_detect_io = xio_create(header_detect_io_get_interface_description(), &header_detect_io_config);
82 connection = connection_create(header_detect_io, NULL, "1", on_new_session_endpoint, NULL);
83 connection_listen(connection);
84 }
85
86 int main(int argc, char** argv)
87 {
88 int result;
89
90 (void)argc;
91 (void)argv;
92
93 if (platform_init() != 0)
94 {
95 (void)printf("Could not initialize platform\r\n");
96 result = -1;
97 }
98 else
99 {
100 size_t last_memory_used = 0;
101 SOCKET_LISTENER_HANDLE socket_listener;
102
103 gballoc_init();
104
105 socket_listener = socketlistener_create(5671);
106 if (socketlistener_start(socket_listener, on_socket_accepted, NULL) != 0)
107 {
108 (void)printf("Could not start socket listener\r\n");
109 result = -1;
110 }
111 else
112 {
113 bool keep_running = true;
114 while (keep_running)
115 {
116 size_t current_memory_used;
117 size_t maximum_memory_used;
118 socketlistener_dowork(socket_listener);
119
120 current_memory_used = gballoc_getCurrentMemoryUsed();
121 maximum_memory_used = gballoc_getMaximumMemoryUsed();
122
123 if (current_memory_used != last_memory_used)
124 {
125 (void)printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used);
126 last_memory_used = current_memory_used;
127 }
128
129 if (sent_messages == msg_count)
130 {
131 break;
132 }
133
134 if (connection != NULL)
135 {
136 connection_dowork(connection);
137 }
138 }
139
140 result = 0;
141 }
142
143 socketlistener_destroy(socket_listener);
144 platform_deinit();
145
146 (void)printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed());
147 (void)printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed());
148
149 gballoc_deinit();
150 }
151
152 return result;
153 }