Debian Codegraph @

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