amqp_connect_timeout.c 1.6 KB

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