Debian Codegraph @

examples/amqp_connect_timeout.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <rabbitmq-c/amqp.h>
10 #include <rabbitmq-c/tcp_socket.h>
11
12 #include <assert.h>
13
14 #ifdef _WIN32
15 #ifndef WIN32_LEAN_AND_MEAN
16 #define WIN32_LEAN_AND_MEAN
17 #endif
18 #include <Winsock2.h>
19 #else
20 #include <sys/time.h>
21 #endif
22
23 #include "utils.h"
24
25 int main(int argc, char const *const *argv) {
26 char const *hostname;
27 int port;
28 amqp_socket_t *socket;
29 amqp_connection_state_t conn;
30 struct timeval tval;
31 struct timeval *tv;
32
33 if (argc < 3) {
34 fprintf(stderr,
35 "Usage: amqp_connect_timeout host port [timeout_sec "
36 "[timeout_usec=0]]\n");
37 return 1;
38 }
39
40 if (argc > 3) {
41 tv = &tval;
42
43 tv->tv_sec = atoi(argv[3]);
44
45 if (argc > 4) {
46 tv->tv_usec = atoi(argv[4]);
47 } else {
48 tv->tv_usec = 0;
49 }
50
51 } else {
52 tv = NULL;
53 }
54
55 hostname = argv[1];
56 port = atoi(argv[2]);
57
58 conn = amqp_new_connection();
59
60 socket = amqp_tcp_socket_new(conn);
61
62 if (!socket) {
63 die("creating TCP socket");
64 }
65
66 die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv),
67 "opening TCP socket");
68
69 die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
70 "guest", "guest"),
71 "Logging in");
72
73 die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
74 "Closing connection");
75 die_on_error(amqp_destroy_connection(conn), "Ending connection");
76
77 printf("Done\n");
78 return 0;
79 }