connection.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018-2019 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #pragma once
  23. KS_BEGIN_EXTERN_C
  24. typedef struct swclt_ttl_tracker swclt_ttl_tracker_t;
  25. typedef struct swclt_conn swclt_conn_t;
  26. typedef ks_status_t (*swclt_conn_incoming_cmd_cb_t)(swclt_conn_t *conn, swclt_cmd_t cmd, void *cb_data);
  27. typedef ks_status_t (*swclt_conn_connect_cb_t)(swclt_conn_t *conn, ks_json_t *error, blade_connect_rpl_t *connect_rpl, void *cb_data);
  28. typedef ks_status_t (*swclt_conn_failed_cb_t)(swclt_conn_t *conn, void *cb_data);
  29. /* Information about this connection */
  30. typedef struct swclt_conn_info_s {
  31. /* We also store a copy of the wss's info structure */
  32. swclt_wss_info_t wss;
  33. /* Pulled from the blade connect result */
  34. ks_uuid_t sessionid;
  35. const char *nodeid;
  36. const char *master_nodeid;
  37. } swclt_conn_info_t;
  38. /* Ths client connection context represents a connections state */
  39. struct swclt_conn {
  40. ks_pool_t *pool;
  41. /* When we receive an incoming request we call this callback with the prepared command */
  42. swclt_conn_incoming_cmd_cb_t incoming_cmd_cb;
  43. void *incoming_cmd_cb_data;
  44. /* Optional callbacks for getting the initial connect result payload */
  45. swclt_conn_connect_cb_t connect_cb;
  46. void *connect_cb_data;
  47. /* Optional callbacks for getting notified of connection failure */
  48. swclt_conn_failed_cb_t failed_cb;
  49. void *failed_cb_data;
  50. /* Connection failed state */
  51. int failed;
  52. ks_mutex_t *failed_mutex;
  53. /* Our websocket transport, basically our connection to blade */
  54. swclt_wss_t *wss;
  55. /* Basic connection info that the caller can examine, contains
  56. * our sessionid, nodeid, and master_nodeid variables returned from
  57. * a connect result from blade, including our connected address and
  58. * ssl context ptr (from websocket info) */
  59. swclt_conn_info_t info;
  60. /* The result of our last connect, kept around for reference */
  61. blade_connect_rpl_t *blade_connect_rpl;
  62. /* A hash of outstanding commands, keyed by their request ids.
  63. * This is the outgoing queue for requests born from the client or
  64. * requests which have been sent from blade. Since the uuids are
  65. * globally unique we can just use one hash for both */
  66. ks_hash_t *outstanding_requests;
  67. /* TTLs to expire */
  68. swclt_ttl_tracker_t *ttl;
  69. };
  70. SWCLT_DECLARE(void) swclt_conn_destroy(swclt_conn_t **conn);
  71. SWCLT_DECLARE(ks_status_t) swclt_conn_connect(
  72. ks_pool_t *pool,
  73. swclt_conn_t **conn,
  74. swclt_conn_incoming_cmd_cb_t incoming_command_callback,
  75. void *incoming_command_cb_data,
  76. swclt_ident_t *ident,
  77. ks_json_t **authentication,
  78. const char *agent,
  79. const char *identity,
  80. const SSL_CTX *ssl);
  81. SWCLT_DECLARE(ks_status_t) swclt_conn_connect_ex(
  82. ks_pool_t *pool,
  83. swclt_conn_t **conn,
  84. swclt_conn_incoming_cmd_cb_t incoming_command_callback,
  85. void *incoming_command_cb_data,
  86. swclt_conn_connect_cb_t connect_callback,
  87. void *connect_cb_data,
  88. swclt_conn_failed_cb_t failed_callback,
  89. void *failed_cb_data,
  90. swclt_ident_t *ident,
  91. ks_uuid_t previous_sessionid,
  92. ks_json_t **authentication,
  93. const char *agent,
  94. const char *identity,
  95. const SSL_CTX *ssl);
  96. SWCLT_DECLARE(ks_status_t) swclt_conn_submit_request(swclt_conn_t *conn, swclt_cmd_t cmd);
  97. SWCLT_DECLARE(ks_status_t) swclt_conn_submit_result(swclt_conn_t *conn, swclt_cmd_t cmd);
  98. SWCLT_DECLARE(ks_status_t) swclt_conn_info(swclt_conn_t *conn, swclt_conn_info_t *info);
  99. SWCLT_DECLARE(char *) swclt_conn_describe(swclt_conn_t *conn);
  100. KS_END_EXTERN_C
  101. /* For Emacs:
  102. * Local Variables:
  103. * mode:c
  104. * indent-tabs-mode:t
  105. * tab-width:4
  106. * c-basic-offset:4
  107. * End:
  108. * For VIM:
  109. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  110. */