ffi-nats-core 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/ffi-nats-core.gemspec +8 -0
  3. data/lib/ffi/nats/core/version.rb +1 -1
  4. data/vendor/cnats/CMakeLists.txt +137 -0
  5. data/vendor/cnats/adapters/libevent.h +220 -0
  6. data/vendor/cnats/adapters/libuv.h +472 -0
  7. data/vendor/cnats/examples/CMakeLists.txt +56 -0
  8. data/vendor/cnats/examples/asynctimeout.c +83 -0
  9. data/vendor/cnats/examples/examples.h +322 -0
  10. data/vendor/cnats/examples/libevent-pub.c +136 -0
  11. data/vendor/cnats/examples/libevent-sub.c +104 -0
  12. data/vendor/cnats/examples/libuv-pub.c +120 -0
  13. data/vendor/cnats/examples/libuv-sub.c +114 -0
  14. data/vendor/cnats/examples/publisher.c +62 -0
  15. data/vendor/cnats/examples/queuegroup.c +132 -0
  16. data/vendor/cnats/examples/replier.c +149 -0
  17. data/vendor/cnats/examples/requestor.c +75 -0
  18. data/vendor/cnats/examples/subscriber.c +133 -0
  19. data/vendor/cnats/src/CMakeLists.txt +31 -0
  20. data/vendor/cnats/src/asynccb.c +66 -0
  21. data/vendor/cnats/src/asynccb.h +42 -0
  22. data/vendor/cnats/src/buf.c +246 -0
  23. data/vendor/cnats/src/buf.h +116 -0
  24. data/vendor/cnats/src/comsock.c +474 -0
  25. data/vendor/cnats/src/comsock.h +81 -0
  26. data/vendor/cnats/src/conn.c +2725 -0
  27. data/vendor/cnats/src/conn.h +75 -0
  28. data/vendor/cnats/src/err.h +31 -0
  29. data/vendor/cnats/src/gc.h +27 -0
  30. data/vendor/cnats/src/hash.c +725 -0
  31. data/vendor/cnats/src/hash.h +141 -0
  32. data/vendor/cnats/src/include/n-unix.h +56 -0
  33. data/vendor/cnats/src/include/n-win.h +59 -0
  34. data/vendor/cnats/src/mem.h +20 -0
  35. data/vendor/cnats/src/msg.c +155 -0
  36. data/vendor/cnats/src/msg.h +43 -0
  37. data/vendor/cnats/src/nats.c +1734 -0
  38. data/vendor/cnats/src/nats.h +2024 -0
  39. data/vendor/cnats/src/natsp.h +518 -0
  40. data/vendor/cnats/src/natstime.c +79 -0
  41. data/vendor/cnats/src/natstime.h +27 -0
  42. data/vendor/cnats/src/nuid.c +265 -0
  43. data/vendor/cnats/src/nuid.h +21 -0
  44. data/vendor/cnats/src/opts.c +1030 -0
  45. data/vendor/cnats/src/opts.h +19 -0
  46. data/vendor/cnats/src/parser.c +869 -0
  47. data/vendor/cnats/src/parser.h +87 -0
  48. data/vendor/cnats/src/pub.c +293 -0
  49. data/vendor/cnats/src/srvpool.c +380 -0
  50. data/vendor/cnats/src/srvpool.h +71 -0
  51. data/vendor/cnats/src/stats.c +54 -0
  52. data/vendor/cnats/src/stats.h +21 -0
  53. data/vendor/cnats/src/status.c +60 -0
  54. data/vendor/cnats/src/status.h +95 -0
  55. data/vendor/cnats/src/sub.c +956 -0
  56. data/vendor/cnats/src/sub.h +34 -0
  57. data/vendor/cnats/src/timer.c +86 -0
  58. data/vendor/cnats/src/timer.h +57 -0
  59. data/vendor/cnats/src/unix/cond.c +103 -0
  60. data/vendor/cnats/src/unix/mutex.c +107 -0
  61. data/vendor/cnats/src/unix/sock.c +105 -0
  62. data/vendor/cnats/src/unix/thread.c +162 -0
  63. data/vendor/cnats/src/url.c +134 -0
  64. data/vendor/cnats/src/url.h +24 -0
  65. data/vendor/cnats/src/util.c +823 -0
  66. data/vendor/cnats/src/util.h +75 -0
  67. data/vendor/cnats/src/version.h +29 -0
  68. data/vendor/cnats/src/version.h.in +29 -0
  69. data/vendor/cnats/src/win/cond.c +86 -0
  70. data/vendor/cnats/src/win/mutex.c +54 -0
  71. data/vendor/cnats/src/win/sock.c +158 -0
  72. data/vendor/cnats/src/win/strings.c +108 -0
  73. data/vendor/cnats/src/win/thread.c +180 -0
  74. data/vendor/cnats/test/CMakeLists.txt +35 -0
  75. data/vendor/cnats/test/certs/ca.pem +38 -0
  76. data/vendor/cnats/test/certs/client-cert.pem +30 -0
  77. data/vendor/cnats/test/certs/client-key.pem +51 -0
  78. data/vendor/cnats/test/certs/server-cert.pem +31 -0
  79. data/vendor/cnats/test/certs/server-key.pem +51 -0
  80. data/vendor/cnats/test/dylib/CMakeLists.txt +10 -0
  81. data/vendor/cnats/test/dylib/nonats.c +13 -0
  82. data/vendor/cnats/test/list.txt +125 -0
  83. data/vendor/cnats/test/test.c +11655 -0
  84. data/vendor/cnats/test/tls.conf +15 -0
  85. data/vendor/cnats/test/tlsverify.conf +19 -0
  86. metadata +83 -1
@@ -0,0 +1,2024 @@
1
+ // Copyright 2015-2016 Apcera Inc. All rights reserved.
2
+
3
+ #ifndef NATS_H_
4
+ #define NATS_H_
5
+
6
+ #include <stdlib.h>
7
+ #include <stdint.h>
8
+ #include <stdbool.h>
9
+ #include <inttypes.h>
10
+ #include <stdio.h>
11
+
12
+ #include "status.h"
13
+ #include "version.h"
14
+
15
+ /** \def NATS_EXTERN
16
+ * \brief Needed for shared library.
17
+ *
18
+ * Based on the platform this is compiled on, it will resolve to
19
+ * the appropriate instruction so that objects are properly exported
20
+ * when building the shared library.
21
+ */
22
+ #if defined(_WIN32)
23
+ #include <winsock2.h>
24
+ #if defined(nats_EXPORTS)
25
+ #define NATS_EXTERN __declspec(dllexport)
26
+ #elif defined(nats_IMPORTS)
27
+ #define NATS_EXTERN __declspec(dllimport)
28
+ #else
29
+ #define NATS_EXTERN
30
+ #endif
31
+
32
+ typedef SOCKET natsSock;
33
+ #else
34
+ #define NATS_EXTERN
35
+ typedef int natsSock;
36
+ #endif
37
+
38
+ #ifdef __cplusplus
39
+ extern "C" {
40
+ #endif
41
+
42
+ /*! \mainpage %NATS C client.
43
+ *
44
+ * \section intro_sec Introduction
45
+ *
46
+ * The %NATS C Client is part of %NATS, an open-source cloud-native
47
+ * messaging system, and is supported by [Apcera](http://www.apcera.com).
48
+ * This client, written in C, follows the go client closely, but
49
+ * diverges in some places.
50
+ *
51
+ * \section install_sec Installation
52
+ *
53
+ * Instructions to build and install the %NATS C Client can be
54
+ * found at the [NATS C Client GitHub page](https://github.com/nats-io/cnats)
55
+ *
56
+ * \section other_doc_section Other Documentation
57
+ *
58
+ * This documentation focuses on the %NATS C Client API; for additional
59
+ * information, refer to the following:
60
+ *
61
+ * - [General Documentation for nats.io](http://nats.io/documentation)
62
+ * - [NATS C Client found on GitHub](https://github.com/nats-io/cnats)
63
+ * - [The NATS Server (gnatsd) found on GitHub](https://github.com/nats-io/gnatsd)
64
+ */
65
+
66
+ /** \brief The default `NATS Server` URL.
67
+ *
68
+ * This is the default URL a `NATS Server`, running with default listen
69
+ * port, can be reached at.
70
+ */
71
+ #define NATS_DEFAULT_URL "nats://localhost:4222"
72
+
73
+ //
74
+ // Types.
75
+ //
76
+ /** \defgroup typesGroup Types
77
+ *
78
+ * NATS Types.
79
+ * @{
80
+ */
81
+
82
+ /** \brief A connection to a `NATS Server`.
83
+ *
84
+ * A #natsConnection represents a bare connection to a `NATS Server`. It will
85
+ * send and receive byte array payloads.
86
+ */
87
+ typedef struct __natsConnection natsConnection;
88
+
89
+ /** \brief Statistics of a #natsConnection
90
+ *
91
+ * Tracks various statistics received and sent on a connection,
92
+ * including counts for messages and bytes.
93
+ */
94
+ typedef struct __natsStatistics natsStatistics;
95
+
96
+ /** \brief Interest on a given subject.
97
+ *
98
+ * A #natsSubscription represents interest in a given subject.
99
+ */
100
+ typedef struct __natsSubscription natsSubscription;
101
+
102
+ /** \brief A structure holding a subject, optional reply and payload.
103
+ *
104
+ * #natsMsg is a structure used by Subscribers and
105
+ * #natsConnection_PublishMsg().
106
+ */
107
+ typedef struct __natsMsg natsMsg;
108
+
109
+ /** \brief Way to configure a #natsConnection.
110
+ *
111
+ * Options can be used to create a customized #natsConnection.
112
+ */
113
+ typedef struct __natsOptions natsOptions;
114
+
115
+ /** \brief Unique subject often used for point-to-point communication.
116
+ *
117
+ * This can be used as the reply for a request. Inboxes are meant to be
118
+ * unique so that replies can be sent to a specific subscriber. That
119
+ * being said, inboxes can be shared across multiple subscribers if
120
+ * desired.
121
+ */
122
+ typedef char natsInbox;
123
+
124
+ /** @} */ // end of typesGroup
125
+
126
+ //
127
+ // Callbacks.
128
+ //
129
+
130
+ /** \defgroup callbacksGroup Callbacks
131
+ *
132
+ * NATS Callbacks.
133
+ * @{
134
+ */
135
+
136
+ /** \brief Callback used to deliver messages to the application.
137
+ *
138
+ * This is the callback that one provides when creating an asynchronous
139
+ * subscription. The library will invoke this callback for each message
140
+ * arriving through the subscription's connection.
141
+ *
142
+ * @see natsConnection_Subscribe()
143
+ * @see natsConnection_QueueSubscribe()
144
+ */
145
+ typedef void (*natsMsgHandler)(
146
+ natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure);
147
+
148
+ /** \brief Callback used to notify the user of asynchronous connection events.
149
+ *
150
+ * This callback is used for asynchronous events such as disconnected
151
+ * and closed connections.
152
+ *
153
+ * @see natsOptions_SetClosedCB()
154
+ * @see natsOptions_SetDisconnectedCB()
155
+ * @see natsOptions_SetReconnectedCB()
156
+ *
157
+ * \warning Such callback is invoked from a dedicated thread and the state
158
+ * of the connection that triggered the event may have changed since
159
+ * that event was generated.
160
+ */
161
+ typedef void (*natsConnectionHandler)(
162
+ natsConnection *nc, void *closure);
163
+
164
+ /** \brief Callback used to notify the user of errors encountered while processing
165
+ * inbound messages.
166
+ *
167
+ * This callback is used to process asynchronous errors encountered while processing
168
+ * inbound messages, such as #NATS_SLOW_CONSUMER.
169
+ */
170
+ typedef void (*natsErrHandler)(
171
+ natsConnection *nc, natsSubscription *subscription, natsStatus err,
172
+ void *closure);
173
+
174
+ /** \brief Attach this connection to the external event loop.
175
+ *
176
+ * After a connection has (re)connected, this callback is invoked. It should
177
+ * perform the necessary work to start polling the given socket for READ events.
178
+ *
179
+ * @param userData location where the adapter implementation will store the
180
+ * object it created and that will later be passed to all other callbacks. If
181
+ * `*userData` is not `NULL`, this means that this is a reconnect event.
182
+ * @param loop the event loop (as a generic void*) this connection is being
183
+ * attached to.
184
+ * @param nc the connection being attached to the event loop.
185
+ * @param socket the socket to poll for read/write events.
186
+ */
187
+ typedef natsStatus (*natsEvLoop_Attach)(
188
+ void **userData,
189
+ void *loop,
190
+ natsConnection *nc,
191
+ int socket);
192
+
193
+ /** \brief Read event needs to be added or removed.
194
+ *
195
+ * The `NATS` library will invoked this callback to indicate if the event
196
+ * loop should start (`add is `true`) or stop (`add` is `false`) polling
197
+ * for read events on the socket.
198
+ *
199
+ * @param userData the pointer to an user object created in #natsEvLoop_Attach.
200
+ * @param add `true` if the event library should start polling, `false` otherwise.
201
+ */
202
+ typedef natsStatus (*natsEvLoop_ReadAddRemove)(
203
+ void *userData,
204
+ bool add);
205
+
206
+ /** \brief Write event needs to be added or removed.
207
+ *
208
+ * The `NATS` library will invoked this callback to indicate if the event
209
+ * loop should start (`add is `true`) or stop (`add` is `false`) polling
210
+ * for write events on the socket.
211
+ *
212
+ * @param userData the pointer to an user object created in #natsEvLoop_Attach.
213
+ * @param add `true` if the event library should start polling, `false` otherwise.
214
+ */
215
+ typedef natsStatus (*natsEvLoop_WriteAddRemove)(
216
+ void *userData,
217
+ bool add);
218
+
219
+ /** \brief Detach from the event loop.
220
+ *
221
+ * The `NATS` library will invoked this callback to indicate that the connection
222
+ * no longer needs to be attached to the event loop. User can cleanup some state.
223
+ *
224
+ * @param userData the pointer to an user object created in #natsEvLoop_Attach.
225
+ */
226
+ typedef natsStatus (*natsEvLoop_Detach)(
227
+ void *userData);
228
+
229
+ /** @} */ // end of callbacksGroup
230
+
231
+ //
232
+ // Functions.
233
+ //
234
+ /** \defgroup funcGroup Functions
235
+ *
236
+ * NATS Functions.
237
+ * @{
238
+ */
239
+
240
+ /** \defgroup libraryGroup Library
241
+ *
242
+ * Library and helper functions.
243
+ * @{
244
+ */
245
+
246
+ /** \brief Initializes the library.
247
+ *
248
+ * This initializes the library.
249
+ *
250
+ * It is invoked automatically when creating a connection, using a default
251
+ * spin count. However, you can call this explicitly before creating the very
252
+ * first connection in order for your chosen spin count to take effect.
253
+ *
254
+ * @param lockSpinCount The number of times the library will spin trying to
255
+ * lock a mutex object.
256
+ */
257
+ NATS_EXTERN natsStatus
258
+ nats_Open(int64_t lockSpinCount);
259
+
260
+
261
+ /** \brief Returns the Library's version
262
+ *
263
+ * Returns the version of the library your application is linked with.
264
+ */
265
+ NATS_EXTERN const char*
266
+ nats_GetVersion(void);
267
+
268
+ /** \brief Returns the Library's version as a number.
269
+ *
270
+ * The version is returned as an hexadecimal number. For instance, if the
271
+ * string version is "1.2.3", the value returned will be:
272
+ *
273
+ * > 0x010203
274
+ */
275
+ NATS_EXTERN uint32_t
276
+ nats_GetVersionNumber(void);
277
+
278
+ #ifdef BUILD_IN_DOXYGEN
279
+ /** \brief Check that the header is compatible with the library.
280
+ *
281
+ * The version of the header you used to compile your application may be
282
+ * incompatible with the library the application is linked with.
283
+ *
284
+ * This function will check that the two are compatibles. If they are not,
285
+ * a message is printed and the application will exit.
286
+ *
287
+ * @return `true` if the header and library are compatibles, otherwise the
288
+ * application exits.
289
+ *
290
+ * @see nats_GetVersion
291
+ * @see nats_GetVersionNumber
292
+ */
293
+ NATS_EXTERN bool nats_CheckCompatibility(void);
294
+ #else
295
+
296
+ #define nats_CheckCompatibility() nats_CheckCompatibilityImpl(NATS_VERSION_REQUIRED_NUMBER, \
297
+ NATS_VERSION_NUMBER, \
298
+ NATS_VERSION_STRING)
299
+
300
+ NATS_EXTERN bool
301
+ nats_CheckCompatibilityImpl(uint32_t reqVerNumber, uint32_t verNumber, const char *verString);
302
+
303
+ #endif
304
+
305
+ /** \brief Gives the current time in milliseconds.
306
+ *
307
+ * Gives the current time in milliseconds.
308
+ */
309
+ NATS_EXTERN int64_t
310
+ nats_Now(void);
311
+
312
+ /** \brief Gives the current time in nanoseconds.
313
+ *
314
+ * Gives the current time in nanoseconds. When such granularity is not
315
+ * available, the time returned is still expressed in nanoseconds.
316
+ */
317
+ NATS_EXTERN int64_t
318
+ nats_NowInNanoSeconds(void);
319
+
320
+ /** \brief Sleeps for a given number of milliseconds.
321
+ *
322
+ * Causes the current thread to be suspended for at least the number of
323
+ * milliseconds.
324
+ *
325
+ * @param sleepTime the number of milliseconds.
326
+ */
327
+ NATS_EXTERN void
328
+ nats_Sleep(int64_t sleepTime);
329
+
330
+ /** \brief Returns the calling thread's last known error.
331
+ *
332
+ * Returns the calling thread's last known error. This can be useful when
333
+ * #natsConnection_Connect fails. Since no connection object is returned,
334
+ * you would not be able to call #natsConnection_GetLastError.
335
+ *
336
+ * @param status if not `NULL`, this function will store the last error status
337
+ * in there.
338
+ * @return the thread local error string.
339
+ *
340
+ * \warning Do not free the string returned by this function.
341
+ */
342
+ NATS_EXTERN const char*
343
+ nats_GetLastError(natsStatus *status);
344
+
345
+ /** \brief Returns the calling thread's last known error stack.
346
+ *
347
+ * Copies the calling thread's last known error stack into the provided buffer.
348
+ * If the buffer is not big enough, #NATS_INSUFFICIENT_BUFFER is returned.
349
+ *
350
+ * @param buffer the buffer into the stack is copied.
351
+ * @param bufLen the size of the buffer
352
+ */
353
+ NATS_EXTERN natsStatus
354
+ nats_GetLastErrorStack(char *buffer, size_t bufLen);
355
+
356
+ /** \brief Prints the calling thread's last known error stack into the file.
357
+ *
358
+ * This call prints the calling thread's last known error stack into the file `file`.
359
+ * It first prints the error status and the error string, then the stack.
360
+ *
361
+ * Here is an example for a call:
362
+ *
363
+ * \code{.unparsed}
364
+ * Error: 29 - SSL Error - (conn.c:565): SSL handshake error: sslv3 alert bad certificate
365
+ * Stack: (library version: 1.2.3-beta)
366
+ * 01 - _makeTLSConn
367
+ * 02 - _checkForSecure
368
+ * 03 - _processExpectedInfo
369
+ * 04 - _processConnInit
370
+ * 05 - _connect
371
+ * 06 - natsConnection_Connect
372
+ * \endcode
373
+ *
374
+ * @param file the file the stack is printed to.
375
+ */
376
+ NATS_EXTERN void
377
+ nats_PrintLastErrorStack(FILE *file);
378
+
379
+ /** \brief Sets the maximum size of the global message delivery thread pool.
380
+ *
381
+ * Normally, each asynchronous subscriber that is created has its own
382
+ * message delivery thread. The advantage is that it reduces lock
383
+ * contentions, therefore improving performance.<br>
384
+ * However, if an application creates many subscribers, this is not scaling
385
+ * well since the process would use too many threads.
386
+ *
387
+ * The library has a thread pool that can perform message delivery. If
388
+ * a connection is created with the proper option set
389
+ * (#natsOptions_UseGlobalMessageDelivery), then this thread pool
390
+ * will be responsible for delivering the messages. The thread pool is
391
+ * lazily initialized, that is, no thread is used as long as no subscriber
392
+ * (requiring global message delivery) is created.
393
+ *
394
+ * Each subscriber will be attached to a given worker on the pool so that
395
+ * message delivery order is guaranteed.
396
+ *
397
+ * This call allows you to set the maximum size of the pool.
398
+ *
399
+ * \note At this time, a pool does not shrink, but the caller will not get
400
+ * an error when calling this function with a size smaller than the current
401
+ * size.
402
+ *
403
+ * @see natsOptions_UseGlobalMessageDelivery()
404
+ * @see \ref envVariablesGroup
405
+ *
406
+ * @param max the maximum size of the pool.
407
+ */
408
+ NATS_EXTERN natsStatus
409
+ nats_SetMessageDeliveryPoolSize(int max);
410
+
411
+ /** \brief Tear down the library.
412
+ *
413
+ * Releases memory used by the library.
414
+ *
415
+ * \note For this to take effect, all NATS objects that you have created
416
+ * must first be destroyed.
417
+ */
418
+ NATS_EXTERN void
419
+ nats_Close(void);
420
+
421
+ /** @} */ // end of libraryGroup
422
+
423
+ /** \defgroup statusGroup Status
424
+ *
425
+ * Functions related to #natsStatus.
426
+ * @{
427
+ */
428
+
429
+ /** \brief Get the text corresponding to a #natsStatus.
430
+ *
431
+ * Returns the static string corresponding to the given status.
432
+ *
433
+ * \warning The returned string is a static string, do not attempt to free
434
+ * it.
435
+ *
436
+ * @param s status to get the text representation from.
437
+ */
438
+ NATS_EXTERN const char*
439
+ natsStatus_GetText(natsStatus s);
440
+
441
+ /** @} */ // end of statusGroup
442
+
443
+ /** \defgroup statsGroup Statistics
444
+ *
445
+ * Statistics Functions.
446
+ * @{
447
+ */
448
+
449
+ /** \brief Creates a #natsStatistics object.
450
+ *
451
+ * Creates a statistics object that can be passed to #natsConnection_GetStats().
452
+ *
453
+ * \note The object needs to be destroyed when no longer needed.
454
+ *
455
+ * @see #natsStatistics_Destroy()
456
+ *
457
+ * @param newStats the location where to store the pointer to the newly created
458
+ * #natsStatistics object.
459
+ */
460
+ NATS_EXTERN natsStatus
461
+ natsStatistics_Create(natsStatistics **newStats);
462
+
463
+ /** \brief Extracts the various statistics values.
464
+ *
465
+ * Gets the counts out of the statistics object.
466
+ *
467
+ * \note You can pass `NULL` to any of the count your are not interested in
468
+ * getting.
469
+ *
470
+ * @see natsConnection_GetStats()
471
+ *
472
+ * @param stats the pointer to the #natsStatistics object to get the values from.
473
+ * @param inMsgs total number of inbound messages.
474
+ * @param inBytes total size (in bytes) of inbound messages.
475
+ * @param outMsgs total number of outbound messages.
476
+ * @param outBytes total size (in bytes) of outbound messages.
477
+ * @param reconnects total number of times the client has reconnected.
478
+ */
479
+ NATS_EXTERN natsStatus
480
+ natsStatistics_GetCounts(natsStatistics *stats,
481
+ uint64_t *inMsgs, uint64_t *inBytes,
482
+ uint64_t *outMsgs, uint64_t *outBytes,
483
+ uint64_t *reconnects);
484
+
485
+ /** \brief Destroys the #natsStatistics object.
486
+ *
487
+ * Destroys the statistics object, freeing up memory.
488
+ *
489
+ * @param stats the pointer to the #natsStatistics object to destroy.
490
+ */
491
+ NATS_EXTERN void
492
+ natsStatistics_Destroy(natsStatistics *stats);
493
+
494
+ /** @} */ // end of statsGroup
495
+
496
+ /** \defgroup optsGroup Options
497
+ *
498
+ * NATS Options.
499
+ * @{
500
+ */
501
+
502
+ /** \brief Creates a #natsOptions object.
503
+ *
504
+ * Creates a #natsOptions object. This object is used when one wants to set
505
+ * specific options prior to connecting to the `NATS Server`.
506
+ *
507
+ * After making the appropriate natsOptions_Set calls, this object is passed
508
+ * to the #natsConnection_Connect() call, which will clone this object. After
509
+ * natsConnection_Connect() returns, modifications to the options object
510
+ * will not affect the connection.
511
+ *
512
+ * \note The object needs to be destroyed when no longer needed.*
513
+ *
514
+ * @see natsConnection_Connect()
515
+ * @see natsOptions_Destroy()
516
+ *
517
+ * @param newOpts the location where store the pointer to the newly created
518
+ * #natsOptions object.
519
+ */
520
+ NATS_EXTERN natsStatus
521
+ natsOptions_Create(natsOptions **newOpts);
522
+
523
+ /** \brief Sets the URL to connect to.
524
+ *
525
+ * Sets the URL of the `NATS Server` the client should try to connect to.
526
+ * The URL can contain optional user name and password.
527
+ *
528
+ * Some valid URLS:
529
+ *
530
+ * - nats://localhost:4222
531
+ * - nats://user\@localhost:4222
532
+ * - nats://user:password\@localhost:4222
533
+ *
534
+ * @see natsOptions_SetServers
535
+ * @see natsOptions_SetUserInfo
536
+ * @see natsOptions_SetToken
537
+ *
538
+ * @param opts the pointer to the #natsOptions object.
539
+ * @param url the string representing the URL the connection should use
540
+ * to connect to the server.
541
+ *
542
+ */
543
+ /*
544
+ * The above is for doxygen. The proper syntax for username/password
545
+ * is without the '\' character:
546
+ *
547
+ * nats://localhost:4222
548
+ * nats://user@localhost:4222
549
+ * nats://user:password@localhost:4222
550
+ */
551
+ NATS_EXTERN natsStatus
552
+ natsOptions_SetURL(natsOptions *opts, const char *url);
553
+
554
+ /** \brief Set the list of servers to try to (re)connect to.
555
+ *
556
+ * This specifies a list of servers to try to connect (or reconnect) to.
557
+ * Note that if you call #natsOptions_SetURL() too, the actual list will contain
558
+ * the one from #natsOptions_SetURL() and the ones specified in this call.
559
+ *
560
+ * @see natsOptions_SetURL
561
+ * @see natsOptions_SetUserInfo
562
+ * @see natsOptions_SetToken
563
+ *
564
+ * @param opts the pointer to the #natsOptions object.
565
+ * @param servers the array of strings representing the server URLs.
566
+ * @param serversCount the size of the array.
567
+ */
568
+ NATS_EXTERN natsStatus
569
+ natsOptions_SetServers(natsOptions *opts, const char** servers, int serversCount);
570
+
571
+ /** \brief Sets the user name/password to use when not specified in the URL.
572
+ *
573
+ * Credentials are usually provided through the URL in the form:
574
+ * <c>nats://foo:bar\@localhost:4222</c>.<br>
575
+ * Until now, you could specify URLs in two ways, with #natsOptions_SetServers
576
+ * or #natsConnection_ConnectTo. The client library would connect (or reconnect)
577
+ * only to this given list of URLs, so if any of the server in the list required
578
+ * authentication, you were responsible for providing the appropriate credentials
579
+ * in the URLs.<br>
580
+ * <br>
581
+ * However, with cluster auto-discovery, the client library asynchronously receives
582
+ * URLs of servers in the cluster. These URLs do not contain any embedded credentials.
583
+ * <br>
584
+ * You need to use this function (or #natsOptions_SetToken) to instruct the client
585
+ * library to use those credentials when connecting to a server that requires
586
+ * authentication and for which there is no embedded credentials in the URL.
587
+ *
588
+ * @see natsOptions_SetToken
589
+ * @see natsOptions_SetURL
590
+ * @see natsOptions_SetServers
591
+ *
592
+ * @param opts the pointer to the #natsOptions object.
593
+ * @param user the user name to send to the server during connect.
594
+ * @param password the password to send to the server during connect.
595
+ */
596
+ NATS_EXTERN natsStatus
597
+ natsOptions_SetUserInfo(natsOptions *opts, const char *user, const char *password);
598
+
599
+ /** \brief Sets the token to use when not specified in the URL.
600
+ *
601
+ * Tokens are usually provided through the URL in the form:
602
+ * <c>nats://mytoken\@localhost:4222</c>.<br>
603
+ * Until now, you could specify URLs in two ways, with #natsOptions_SetServers
604
+ * or #natsConnection_ConnectTo. The client library would connect (or reconnect)
605
+ * only to this given list of URLs, so if any of the server in the list required
606
+ * authentication, you were responsible for providing the appropriate token
607
+ * in the URLs.<br>
608
+ * <br>
609
+ * However, with cluster auto-discovery, the client library asynchronously receives
610
+ * URLs of servers in the cluster. These URLs do not contain any embedded tokens.
611
+ * <br>
612
+ * You need to use this function (or #natsOptions_SetUserInfo) to instruct the client
613
+ * library to use this token when connecting to a server that requires
614
+ * authentication and for which there is no embedded token in the URL.
615
+ *
616
+ * @see natsOptions_SetUserInfo
617
+ * @see natsOptions_SetURL
618
+ * @see natsOptions_SetServers
619
+ *
620
+ * @param opts the pointer to the #natsOptions object.
621
+ * @param token the token to send to the server during connect.
622
+ */
623
+ NATS_EXTERN natsStatus
624
+ natsOptions_SetToken(natsOptions *opts, const char *token);
625
+
626
+ /** \brief Indicate if the servers list should be randomized.
627
+ *
628
+ * If 'noRandomize' is true, then the list of server URLs is used in the order
629
+ * provided by #natsOptions_SetURL() + #natsOptions_SetServers(). Otherwise, the
630
+ * list is formed in a random order.
631
+ *
632
+ * @param opts the pointer to the #natsOptions object.
633
+ * @param noRandomize if `true`, the list will be used as-is.
634
+ */
635
+ NATS_EXTERN natsStatus
636
+ natsOptions_SetNoRandomize(natsOptions *opts, bool noRandomize);
637
+
638
+ /** \brief Sets the (re)connect process timeout.
639
+ *
640
+ * This timeout, expressed in milliseconds, is used to interrupt a (re)connect
641
+ * attempt to a `NATS Server`. This timeout is used both for the low level TCP
642
+ * connect call, and for timing out the response from the server to the client's
643
+ * initial `PING` protocol.
644
+ *
645
+ * @param opts the pointer to the #natsOptions object.
646
+ * @param timeout the time, in milliseconds, allowed for an individual connect
647
+ * (or reconnect) to complete.
648
+ *
649
+ */
650
+ NATS_EXTERN natsStatus
651
+ natsOptions_SetTimeout(natsOptions *opts, int64_t timeout);
652
+
653
+ /** \brief Sets the name.
654
+ *
655
+ * This name is sent as part of the `CONNECT` protocol. There is no default name.
656
+ *
657
+ * @param opts the pointer to the #natsOptions object.
658
+ * @param name the name to set.
659
+ */
660
+ NATS_EXTERN natsStatus
661
+ natsOptions_SetName(natsOptions *opts, const char *name);
662
+
663
+ /** \brief Sets the secure mode.
664
+ *
665
+ * Indicates to the server if the client wants a secure (SSL/TLS) connection.
666
+ *
667
+ * The default is `false`.
668
+ *
669
+ * @param opts the pointer to the #natsOptions object.
670
+ * @param secure `true` for a secure connection, `false` otherwise.
671
+ */
672
+ NATS_EXTERN natsStatus
673
+ natsOptions_SetSecure(natsOptions *opts, bool secure);
674
+
675
+ /** \brief Loads the trusted CA certificates from a file.
676
+ *
677
+ * Loads the trusted CA certificates from a file.
678
+ *
679
+ * Note that the certificates
680
+ * are added to a SSL context for this #natsOptions object at the time of
681
+ * this call, so possible errors while loading the certificates will be
682
+ * reported now instead of when a connection is created. You can get extra
683
+ * information by calling #nats_GetLastError.
684
+ *
685
+ * @param opts the pointer to the #natsOptions object.
686
+ * @param fileName the file containing the CA certificates.
687
+ *
688
+ */
689
+ NATS_EXTERN natsStatus
690
+ natsOptions_LoadCATrustedCertificates(natsOptions *opts, const char *fileName);
691
+
692
+ /** \brief Loads the certificate chain from a file, using the given key.
693
+ *
694
+ * The certificates must be in PEM format and must be sorted starting with
695
+ * the subject's certificate, followed by intermediate CA certificates if
696
+ * applicable, and ending at the highest level (root) CA.
697
+ *
698
+ * The private key file format supported is also PEM.
699
+ *
700
+ * See #natsOptions_LoadCATrustedCertificates regarding error reports.
701
+ *
702
+ * @param opts the pointer to the #natsOptions object.
703
+ * @param certsFileName the file containing the client certificates.
704
+ * @param keyFileName the file containing the client private key.
705
+ */
706
+ NATS_EXTERN natsStatus
707
+ natsOptions_LoadCertificatesChain(natsOptions *opts,
708
+ const char *certsFileName,
709
+ const char *keyFileName);
710
+
711
+ /** \brief Sets the list of available ciphers.
712
+ *
713
+ * Sets the list of available ciphers.
714
+ * Check https://www.openssl.org/docs/manmaster/apps/ciphers.html for the
715
+ * proper syntax. Here is an example:
716
+ *
717
+ * > "-ALL:HIGH"
718
+ *
719
+ * See #natsOptions_LoadCATrustedCertificates regarding error reports.
720
+ *
721
+ * @param opts the pointer to the #natsOptions object.
722
+ * @param ciphers the ciphers suite.
723
+ */
724
+ NATS_EXTERN natsStatus
725
+ natsOptions_SetCiphers(natsOptions *opts, const char *ciphers);
726
+
727
+ /** \brief Sets the server certificate's expected hostname.
728
+ *
729
+ * If set, the library will check that the hostname in the server
730
+ * certificate matches the given `hostname`. This will occur when a connection
731
+ * is created, not at the time of this call.
732
+ *
733
+ * @param opts the pointer to the #natsOptions object.
734
+ * @param hostname the expected server certificate hostname.
735
+ */
736
+ NATS_EXTERN natsStatus
737
+ natsOptions_SetExpectedHostname(natsOptions *opts, const char *hostname);
738
+
739
+ /** \brief Switch server certificate verification.
740
+ *
741
+ * By default, the server certificate is verified. You can disable the verification
742
+ * by passing <c>true</c> to this function.
743
+ *
744
+ * \warning This is fine for tests but use with caution since this is not secure.
745
+ *
746
+ * @param opts the pointer to the #natsOptions object.
747
+ * @param skip set it to <c>true</c> to disable - or skip - server certificate verification.
748
+ */
749
+ NATS_EXTERN natsStatus
750
+ natsOptions_SkipServerVerification(natsOptions *opts, bool skip);
751
+
752
+ /** \brief Sets the verbose mode.
753
+ *
754
+ * Sets the verbose mode. If `true`, sends are echoed by the server with
755
+ * an `OK` protocol message.
756
+ *
757
+ * The default is `false`.
758
+ *
759
+ * @param opts the pointer to the #natsOptions object.
760
+ * @param verbose `true` for a verbose protocol, `false` otherwise.
761
+ */
762
+ NATS_EXTERN natsStatus
763
+ natsOptions_SetVerbose(natsOptions *opts, bool verbose);
764
+
765
+ /** \brief Sets the pedantic mode.
766
+ *
767
+ * Sets the pedantic mode. If `true` some extra checks will be performed
768
+ * by the server.
769
+ *
770
+ * The default is `false`
771
+ *
772
+ * @param opts the pointer to the #natsOptions object.
773
+ * @param pedantic `true` for a pedantic protocol, `false` otherwise.
774
+ */
775
+ NATS_EXTERN natsStatus
776
+ natsOptions_SetPedantic(natsOptions *opts, bool pedantic);
777
+
778
+ /** \brief Sets the ping interval.
779
+ *
780
+ * Interval, expressed in milliseconds, in which the client sends `PING`
781
+ * protocols to the `NATS Server`.
782
+ *
783
+ * @param opts the pointer to the #natsOptions object.
784
+ * @param interval the interval, in milliseconds, at which the connection
785
+ * will send `PING` protocols to the server.
786
+ */
787
+ NATS_EXTERN natsStatus
788
+ natsOptions_SetPingInterval(natsOptions *opts, int64_t interval);
789
+
790
+ /** \brief Sets the limit of outstanding `PING`s without corresponding `PONG`s.
791
+ *
792
+ * Specifies the maximum number of `PING`s without corresponding `PONG`s (which
793
+ * should be received from the server) before closing the connection with
794
+ * the #NATS_STALE_CONNECTION status. If reconnection is allowed, the client
795
+ * library will try to reconnect.
796
+ *
797
+ * @param opts the pointer to the #natsOptions object.
798
+ * @param maxPingsOut the maximum number of `PING`s without `PONG`s
799
+ * (positive number).
800
+ */
801
+ NATS_EXTERN natsStatus
802
+ natsOptions_SetMaxPingsOut(natsOptions *opts, int maxPingsOut);
803
+
804
+ /** \brief Indicates if the connection will be allowed to reconnect.
805
+ *
806
+ * Specifies whether or not the client library should try to reconnect when
807
+ * losing the connection to the `NATS Server`.
808
+ *
809
+ * The default is `true`.
810
+ *
811
+ * @param opts the pointer to the #natsOptions object.
812
+ * @param allow `true` if the connection is allowed to reconnect, `false`
813
+ * otherwise.
814
+ */
815
+ NATS_EXTERN natsStatus
816
+ natsOptions_SetAllowReconnect(natsOptions *opts, bool allow);
817
+
818
+ /** \brief Sets the maximum number of reconnect attempts.
819
+ *
820
+ * Specifies the maximum number of reconnect attempts.
821
+ *
822
+ * @param opts the pointer to the #natsOptions object.
823
+ * @param maxReconnect the maximum number of reconnects (positive number).
824
+ */
825
+ NATS_EXTERN natsStatus
826
+ natsOptions_SetMaxReconnect(natsOptions *opts, int maxReconnect);
827
+
828
+ /** \brief Sets the time between reconnect attempts.
829
+ *
830
+ * Specifies how long to wait between two reconnect attempts from the same
831
+ * server. This means that if you have a list with S1,S2 and are currently
832
+ * connected to S1, and get disconnected, the library will immediately
833
+ * attempt to connect to S2. If this fails, it will go back to S1, and this
834
+ * time will wait for `reconnectWait` milliseconds since the last attempt
835
+ * to connect to S1.
836
+ *
837
+ * @param opts the pointer to the #natsOptions object.
838
+ * @param reconnectWait the time, in milliseconds, to wait between attempts
839
+ * to reconnect to the same server.
840
+ */
841
+ NATS_EXTERN natsStatus
842
+ natsOptions_SetReconnectWait(natsOptions *opts, int64_t reconnectWait);
843
+
844
+ /** \brief Sets the size of the backing buffer used during reconnect.
845
+ *
846
+ * Sets the size, in bytes, of the backing buffer holding published data
847
+ * while the library is reconnecting. Once this buffer has been exhausted,
848
+ * publish operations will return the #NATS_INSUFFICIENT_BUFFER error.
849
+ * If not specified, or the value is 0, the library will use a default value,
850
+ * currently set to 8MB.
851
+ *
852
+ * @param opts the pointer to the #natsOptions object.
853
+ * @param reconnectBufSize the size, in bytes, of the backing buffer for
854
+ * write operations during a reconnect.
855
+ */
856
+ NATS_EXTERN natsStatus
857
+ natsOptions_SetReconnectBufSize(natsOptions *opts, int reconnectBufSize);
858
+
859
+ /** \brief Sets the maximum number of pending messages per subscription.
860
+ *
861
+ * Specifies the maximum number of inbound messages that can be buffered in the
862
+ * library, for each subscription, before inbound messages are dropped and
863
+ * #NATS_SLOW_CONSUMER status is reported to the #natsErrHandler callback (if
864
+ * one has been set).
865
+ *
866
+ * @see natsOptions_SetErrorHandler()
867
+ *
868
+ * @param opts the pointer to the #natsOptions object.
869
+ * @param maxPending the number of messages allowed to be buffered by the
870
+ * library before triggering a slow consumer scenario.
871
+ */
872
+ NATS_EXTERN natsStatus
873
+ natsOptions_SetMaxPendingMsgs(natsOptions *opts, int maxPending);
874
+
875
+ /** \brief Sets the error handler for asynchronous events.
876
+ *
877
+ * Specifies the callback to invoke when an asynchronous error
878
+ * occurs. This is used by applications having only asynchronous
879
+ * subscriptions that would not know otherwise that a problem with the
880
+ * connection occurred.
881
+ *
882
+ * @see natsErrHandler
883
+ *
884
+ * @param opts the pointer to the #natsOptions object.
885
+ * @param errHandler the error handler callback.
886
+ * @param closure a pointer to an user object that will be passed to
887
+ * the callback. `closure` can be `NULL`.
888
+ */
889
+ NATS_EXTERN natsStatus
890
+ natsOptions_SetErrorHandler(natsOptions *opts, natsErrHandler errHandler,
891
+ void *closure);
892
+
893
+ /** \brief Sets the callback to be invoked when a connection to a server
894
+ * is permanently lost.
895
+ *
896
+ * Specifies the callback to invoke when a connection is terminally closed,
897
+ * that is, after all reconnect attempts have failed (when reconnection is
898
+ * allowed).
899
+ *
900
+ * @param opts the pointer to the #natsOptions object.
901
+ * @param closedCb the callback to be invoked when the connection is closed.
902
+ * @param closure a pointer to an user object that will be passed to
903
+ * the callback. `closure` can be `NULL`.
904
+ */
905
+ NATS_EXTERN natsStatus
906
+ natsOptions_SetClosedCB(natsOptions *opts, natsConnectionHandler closedCb,
907
+ void *closure);
908
+
909
+ /** \brief Sets the callback to be invoked when the connection to a server is
910
+ * lost.
911
+ *
912
+ * Specifies the callback to invoke when a connection to the `NATS Server`
913
+ * is lost. There could be two instances of the callback when reconnection
914
+ * is allowed: one before attempting the reconnect attempts, and one when
915
+ * all reconnect attempts have failed and the connection is going to be
916
+ * permanently closed.
917
+ *
918
+ * \warning Invocation of this callback is asynchronous, which means that
919
+ * the state of the connection may have changed when this callback is
920
+ * invoked.
921
+ *
922
+ * @param opts the pointer to the #natsOptions object.
923
+ * @param disconnectedCb the callback to be invoked when a connection to
924
+ * a server is lost
925
+ * @param closure a pointer to an user object that will be passed to
926
+ * the callback. `closure` can be `NULL`.
927
+ */
928
+ NATS_EXTERN natsStatus
929
+ natsOptions_SetDisconnectedCB(natsOptions *opts,
930
+ natsConnectionHandler disconnectedCb,
931
+ void *closure);
932
+
933
+ /** \brief Sets the callback to be invoked when the connection has reconnected.
934
+ *
935
+ * Specifies the callback to invoke when the client library has successfully
936
+ * reconnected to a `NATS Server`.
937
+ *
938
+ * \warning Invocation of this callback is asynchronous, which means that
939
+ * the state of the connection may have changed when this callback is
940
+ * invoked.
941
+ *
942
+ * @param opts the pointer to the #natsOptions object.
943
+ * @param reconnectedCb the callback to be invoked when the connection to
944
+ * a server has been re-established.
945
+ * @param closure a pointer to an user object that will be passed to
946
+ * the callback. `closure` can be `NULL`.
947
+ */
948
+ NATS_EXTERN natsStatus
949
+ natsOptions_SetReconnectedCB(natsOptions *opts,
950
+ natsConnectionHandler reconnectedCb,
951
+ void *closure);
952
+
953
+ /** \brief Sets the external event loop and associated callbacks.
954
+ *
955
+ * If you want to use an external event loop, the `NATS` library will not
956
+ * create a thread to read data from the socket, and will not directly write
957
+ * data to the socket. Instead, the library will invoke those callbacks
958
+ * for various events.
959
+ *
960
+ * @param opts the pointer to the #natsOptions object.
961
+ * @param loop the `void*` pointer to the external event loop.
962
+ * @param attachCb the callback invoked after the connection is connected,
963
+ * or reconnected.
964
+ * @param readCb the callback invoked when the event library should start or
965
+ * stop polling for read events.
966
+ * @param writeCb the callback invoked when the event library should start or
967
+ * stop polling for write events.
968
+ * @param detachCb the callback invoked when a connection is closed.
969
+ */
970
+ NATS_EXTERN natsStatus
971
+ natsOptions_SetEventLoop(natsOptions *opts,
972
+ void *loop,
973
+ natsEvLoop_Attach attachCb,
974
+ natsEvLoop_ReadAddRemove readCb,
975
+ natsEvLoop_WriteAddRemove writeCb,
976
+ natsEvLoop_Detach detachCb);
977
+
978
+ /** \brief Switch on/off the use of a central message delivery thread pool.
979
+ *
980
+ * Normally, each asynchronous subscriber that is created has its own
981
+ * message delivery thread. The advantage is that it reduces lock
982
+ * contentions, therefore improving performance.<br>
983
+ * However, if an application creates many subscribers, this is not scaling
984
+ * well since the process would use too many threads.
985
+ *
986
+ * When a connection is created from a `nats_Options` that has enabled
987
+ * global message delivery, asynchronous subscribers from this connection
988
+ * will use a shared thread pool responsible for message delivery.
989
+ *
990
+ * \note The message order per subscription is still guaranteed.
991
+ *
992
+ * @see nats_SetMessageDeliveryPoolSize()
993
+ * @see \ref envVariablesGroup
994
+ *
995
+ * @param opts the pointer to the #natsOptions object.
996
+ * @param global if `true`, uses the global message delivery thread pool,
997
+ * otherwise, each asynchronous subscriber will create their own message
998
+ * delivery thread.
999
+ */
1000
+ NATS_EXTERN natsStatus
1001
+ natsOptions_UseGlobalMessageDelivery(natsOptions *opts, bool global);
1002
+
1003
+ /** \brief Dictates the order in which host name are resolved during connect.
1004
+ *
1005
+ * The library would previously favor IPv6 addresses during the connect process.
1006
+ * <br>
1007
+ * You can now change the order, or even exclude a family of addresses, using
1008
+ * this option. Here is the list of possible values:
1009
+ * <br>
1010
+ * Value | Meaning
1011
+ * ------|--------
1012
+ * 46 | try IPv4 first, if it fails try IPv6
1013
+ * 64 | try IPv6 first, if it fails try IPv4
1014
+ * 4 | use only IPv4
1015
+ * 6 | use only IPv6
1016
+ * 0 | any family, no specific order
1017
+ *
1018
+ * \note If this option is not set, or you specify `0` for the order, the
1019
+ * library will use the first IP (based on the DNS configuration) for which
1020
+ * a successful connection can be made.
1021
+ *
1022
+ * @param opts the pointer to the #natsOptions object.
1023
+ * @param order a string representing the order for the IP resolution.
1024
+ */
1025
+ NATS_EXTERN natsStatus
1026
+ natsOptions_IPResolutionOrder(natsOptions *opts, int order);
1027
+
1028
+ /** \brief Destroys a #natsOptions object.
1029
+ *
1030
+ * Destroys the natsOptions object, freeing used memory. See the note in
1031
+ * the natsOptions_Create() call.
1032
+ *
1033
+ * @param opts the pointer to the #natsOptions object to destroy.
1034
+ */
1035
+ NATS_EXTERN void
1036
+ natsOptions_Destroy(natsOptions *opts);
1037
+
1038
+ /** @} */ // end of optsGroup
1039
+
1040
+ /** \defgroup inboxGroup Inboxes
1041
+ *
1042
+ * NATS Inboxes.
1043
+ * @{
1044
+ */
1045
+
1046
+ /** \brief Creates an inbox.
1047
+ *
1048
+ * Returns an inbox string which can be used for directed replies from
1049
+ * subscribers. These are guaranteed to be unique, but can be shared
1050
+ * and subscribed to by others.
1051
+ *
1052
+ * \note The inbox needs to be destroyed when no longer needed.
1053
+ *
1054
+ * @see #natsInbox_Destroy()
1055
+ *
1056
+ * @param newInbox the location where to store a pointer to the newly
1057
+ * created #natsInbox.
1058
+ */
1059
+ NATS_EXTERN natsStatus
1060
+ natsInbox_Create(char **newInbox);
1061
+
1062
+ /** \brief Destroys the inbox.
1063
+ *
1064
+ * Destroys the inbox.
1065
+ *
1066
+ * @param inbox the pointer to the #natsInbox object to destroy.
1067
+ */
1068
+ NATS_EXTERN void
1069
+ natsInbox_Destroy(char *inbox);
1070
+
1071
+ /** @} */ // end of inboxGroup
1072
+
1073
+ /** \defgroup msgGroup Message
1074
+ *
1075
+ * NATS Message.
1076
+ * @{
1077
+ */
1078
+
1079
+ /** \brief Creates a #natsMsg object.
1080
+ *
1081
+ * Creates a #natsMsg object. This is used by the subscription related calls
1082
+ * and by #natsConnection_PublishMsg().
1083
+ *
1084
+ * \note Messages need to be destroyed with #natsMsg_Destroy() when no
1085
+ * longer needed.
1086
+ *
1087
+ * @see natsMsg_Destroy()
1088
+ *
1089
+ * @param newMsg the location where to store the pointer to the newly created
1090
+ * #natsMsg object.
1091
+ * @param subj the subject this message will be sent to. Cannot be `NULL`.
1092
+ * @param reply the optional reply for this message.
1093
+ * @param data the optional message payload.
1094
+ * @param dataLen the size of the payload.
1095
+ */
1096
+ NATS_EXTERN natsStatus
1097
+ natsMsg_Create(natsMsg **newMsg, const char *subj, const char *reply,
1098
+ const char *data, int dataLen);
1099
+
1100
+ /** \brief Returns the subject set in this message.
1101
+ *
1102
+ * Returns the subject set on that message.
1103
+ *
1104
+ * \warning The string belongs to the message and must not be freed.
1105
+ * Copy it if needed.
1106
+ * @param msg the pointer to the #natsMsg object.
1107
+ */
1108
+ NATS_EXTERN const char*
1109
+ natsMsg_GetSubject(natsMsg *msg);
1110
+
1111
+ /** \brief Returns the reply set in this message.
1112
+ *
1113
+ * Returns the reply, possibly `NULL`.
1114
+ *
1115
+ * \warning The string belongs to the message and must not be freed.
1116
+ * Copy it if needed.
1117
+ *
1118
+ * @param msg the pointer to the #natsMsg object.
1119
+ */
1120
+ NATS_EXTERN const char*
1121
+ natsMsg_GetReply(natsMsg *msg);
1122
+
1123
+ /** \brief Returns the message payload.
1124
+ *
1125
+ * Returns the message payload, possibly `NULL`.
1126
+ *
1127
+ * Note that although the data sent and received from the server is not `NULL`
1128
+ * terminated, the NATS C Client does add a `NULL` byte to the received payload.
1129
+ * If you expect the received data to be a "string", then this conveniently
1130
+ * allows you to call #natsMsg_GetData() without having to copy the returned
1131
+ * data to a buffer to add the `NULL` byte at the end.
1132
+ *
1133
+ * \warning The string belongs to the message and must not be freed.
1134
+ * Copy it if needed.
1135
+ *
1136
+ * @param msg the pointer to the #natsMsg object.
1137
+ */
1138
+ NATS_EXTERN const char*
1139
+ natsMsg_GetData(natsMsg *msg);
1140
+
1141
+ /** \brief Returns the message length.
1142
+ *
1143
+ * Returns the message's payload length, possibly 0.
1144
+ *
1145
+ * @param msg the pointer to the #natsMsg object.
1146
+ */
1147
+ NATS_EXTERN int
1148
+ natsMsg_GetDataLength(natsMsg *msg);
1149
+
1150
+ /** \brief Destroys the message object.
1151
+ *
1152
+ * Destroys the message, freeing memory.
1153
+ *
1154
+ * @param msg the pointer to the #natsMsg object to destroy.
1155
+ */
1156
+ NATS_EXTERN void
1157
+ natsMsg_Destroy(natsMsg *msg);
1158
+
1159
+ /** @} */ // end of msgGroup
1160
+
1161
+ /** \defgroup connGroup Connection
1162
+ *
1163
+ * NATS Connection
1164
+ * @{
1165
+ */
1166
+
1167
+ /** \defgroup connMgtGroup Management
1168
+ *
1169
+ * Functions related to connection management.
1170
+ * @{
1171
+ */
1172
+
1173
+ /** \brief Connects to a `NATS Server` using the provided options.
1174
+ *
1175
+ * Attempts to connect to a `NATS Server` with multiple options.
1176
+ *
1177
+ * This call is cloning the #natsOptions object. Once this call returns,
1178
+ * changes made to the `options` will not have an effect to this
1179
+ * connection. The `options` can however be changed prior to be
1180
+ * passed to another #natsConnection_Connect() call if desired.
1181
+ *
1182
+ * @see #natsOptions
1183
+ * @see #natsConnection_Destroy()
1184
+ *
1185
+ * @param nc the location where to store the pointer to the newly created
1186
+ * #natsConnection object.
1187
+ * @param options the options to use for this connection. If `NULL`
1188
+ * this call is equivalent to #natsConnection_ConnectTo() with #NATS_DEFAULT_URL.
1189
+ *
1190
+ */
1191
+ NATS_EXTERN natsStatus
1192
+ natsConnection_Connect(natsConnection **nc, natsOptions *options);
1193
+
1194
+ /** \brief Process a read event when using external event loop.
1195
+ *
1196
+ * When using an external event loop, and the callback indicating that
1197
+ * the socket is ready for reading, this call will read data from the
1198
+ * socket and process it.
1199
+ *
1200
+ * @param nc the pointer to the #natsConnection object.
1201
+ *
1202
+ * \warning This API is reserved for external event loop adapters.
1203
+ */
1204
+ NATS_EXTERN void
1205
+ natsConnection_ProcessReadEvent(natsConnection *nc);
1206
+
1207
+ /** \brief Process a write event when using external event loop.
1208
+ *
1209
+ * When using an external event loop, and the callback indicating that
1210
+ * the socket is ready for writing, this call will write data to the
1211
+ * socket.
1212
+ *
1213
+ * @param nc the pointer to the #natsConnection object.
1214
+ *
1215
+ * \warning This API is reserved for external event loop adapters.
1216
+ */
1217
+ NATS_EXTERN void
1218
+ natsConnection_ProcessWriteEvent(natsConnection *nc);
1219
+
1220
+ /** \brief Connects to a `NATS Server` using any of the URL from the given list.
1221
+ *
1222
+ * Attempts to connect to a `NATS Server`.
1223
+ *
1224
+ * This call supports multiple comma separated URLs. If more than one is
1225
+ * specified, it behaves as if you were using a #natsOptions object and
1226
+ * called #natsOptions_SetServers() with the equivalent array of URLs.
1227
+ * The list is randomized before the connect sequence starts.
1228
+ *
1229
+ * @see #natsConnection_Destroy()
1230
+ * @see #natsOptions_SetServers()
1231
+ *
1232
+ * @param nc the location where to store the pointer to the newly created
1233
+ * #natsConnection object.
1234
+ * @param urls the URL to connect to, or the list of URLs to chose from.
1235
+ */
1236
+ NATS_EXTERN natsStatus
1237
+ natsConnection_ConnectTo(natsConnection **nc, const char *urls);
1238
+
1239
+ /** \brief Test if connection has been closed.
1240
+ *
1241
+ * Tests if connection has been closed.
1242
+ *
1243
+ * @param nc the pointer to the #natsConnection object.
1244
+ */
1245
+ NATS_EXTERN bool
1246
+ natsConnection_IsClosed(natsConnection *nc);
1247
+
1248
+ /** \brief Test if connection is reconnecting.
1249
+ *
1250
+ * Tests if connection is reconnecting.
1251
+ *
1252
+ * @param nc the pointer to the #natsConnection object.
1253
+ */
1254
+ NATS_EXTERN bool
1255
+ natsConnection_IsReconnecting(natsConnection *nc);
1256
+
1257
+ /** \brief Returns the current state of the connection.
1258
+ *
1259
+ * Returns the current state of the connection.
1260
+ *
1261
+ * @see #natsConnStatus
1262
+ *
1263
+ * @param nc the pointer to the #natsConnection object.
1264
+ */
1265
+ NATS_EXTERN natsConnStatus
1266
+ natsConnection_Status(natsConnection *nc);
1267
+
1268
+ /** \brief Returns the number of bytes to be sent to the server.
1269
+ *
1270
+ * When calling any of the publish functions, data is not necessarily
1271
+ * immediately sent to the server. Some buffering occurs, allowing
1272
+ * for better performance. This function indicates if there is any
1273
+ * data not yet transmitted to the server.
1274
+ *
1275
+ * @param nc the pointer to the #natsConnection object.
1276
+ * @return the number of bytes to be sent to the server, or -1 if the
1277
+ * connection is closed.
1278
+ */
1279
+ NATS_EXTERN int
1280
+ natsConnection_Buffered(natsConnection *nc);
1281
+
1282
+ /** \brief Flushes the connection.
1283
+ *
1284
+ * Performs a round trip to the server and return when it receives the
1285
+ * internal reply.
1286
+ *
1287
+ * Note that if this call occurs when the connection to the server is
1288
+ * lost, the `PING` will not be echoed even if the library can connect
1289
+ * to a new (or the same) server. Therefore, in such situation, this
1290
+ * call will fail with the status #NATS_CONNECTION_DISCONNECTED.
1291
+ *
1292
+ * If the connection is closed while this call is in progress, then the
1293
+ * status #CLOSED would be returned instead.
1294
+ *
1295
+ * @param nc the pointer to the #natsConnection object.
1296
+ */
1297
+ NATS_EXTERN natsStatus
1298
+ natsConnection_Flush(natsConnection *nc);
1299
+
1300
+ /** \brief Flushes the connection with a given timeout.
1301
+ *
1302
+ * Performs a round trip to the server and return when it receives the
1303
+ * internal reply, or if the call times-out (timeout is expressed in
1304
+ * milliseconds).
1305
+ *
1306
+ * See possible failure case described in #natsConnection_Flush().
1307
+ *
1308
+ * @param nc the pointer to the #natsConnection object.
1309
+ * @param timeout in milliseconds, is the time allowed for the flush
1310
+ * to complete before #NATS_TIMEOUT error is returned.
1311
+ */
1312
+ NATS_EXTERN natsStatus
1313
+ natsConnection_FlushTimeout(natsConnection *nc, int64_t timeout);
1314
+
1315
+ /** \brief Returns the maximum message payload.
1316
+ *
1317
+ * Returns the maximum message payload accepted by the server. The
1318
+ * information is gathered from the `NATS Server` when the connection is
1319
+ * first established.
1320
+ *
1321
+ * @param nc the pointer to the #natsConnection object.
1322
+ * @return the maximum message payload.
1323
+ */
1324
+ NATS_EXTERN int64_t
1325
+ natsConnection_GetMaxPayload(natsConnection *nc);
1326
+
1327
+ /** \brief Gets the connection statistics.
1328
+ *
1329
+ * Copies in the provided statistics structure, a snapshot of the statistics for
1330
+ * this connection.
1331
+ *
1332
+ * @param nc the pointer to the #natsConnection object.
1333
+ * @param stats the pointer to a #natsStatistics object in which statistics
1334
+ * will be copied.
1335
+ */
1336
+ NATS_EXTERN natsStatus
1337
+ natsConnection_GetStats(natsConnection *nc, natsStatistics *stats);
1338
+
1339
+ /** \brief Gets the URL of the currently connected server.
1340
+ *
1341
+ * Copies in the given buffer, the connected server's Url. If the buffer is
1342
+ * too small, an error is returned.
1343
+ *
1344
+ * @param nc the pointer to the #natsConnection object.
1345
+ * @param buffer the buffer in which the URL is copied.
1346
+ * @param bufferSize the size of the buffer.
1347
+ */
1348
+ NATS_EXTERN natsStatus
1349
+ natsConnection_GetConnectedUrl(natsConnection *nc, char *buffer, size_t bufferSize);
1350
+
1351
+ /** \brief Gets the server Id.
1352
+ *
1353
+ * Copies in the given buffer, the connected server's Id. If the buffer is
1354
+ * too small, an error is returned.
1355
+ *
1356
+ * @param nc the pointer to the #natsConnection object.
1357
+ * @param buffer the buffer in which the server id is copied.
1358
+ * @param bufferSize the size of the buffer.
1359
+ */
1360
+ NATS_EXTERN natsStatus
1361
+ natsConnection_GetConnectedServerId(natsConnection *nc, char *buffer, size_t bufferSize);
1362
+
1363
+ /** \brief Returns the list of server URLs known to this connection.
1364
+ *
1365
+ * Returns the list of known servers, including additional servers
1366
+ * discovered after a connection has been established (with servers
1367
+ * version 0.9.2 and above).
1368
+ *
1369
+ * No credential information is included in any of the server URLs
1370
+ * returned by this call.<br>
1371
+ * If you want to use any of these URLs to connect to a server that
1372
+ * requires authentication, you will need to use #natsOptions_SetUserInfo
1373
+ * or #natsOptions_SetToken.
1374
+ *
1375
+ * \note The user is responsible for freeing the memory of the returned array.
1376
+ *
1377
+ * @param nc the pointer to the #natsConnection object.
1378
+ * @param servers the location where to store the pointer to the array
1379
+ * of server URLs.
1380
+ * @param count the location where to store the number of elements of the
1381
+ * returned array.
1382
+ */
1383
+ NATS_EXTERN natsStatus
1384
+ natsConnection_GetServers(natsConnection *nc, char ***servers, int *count);
1385
+
1386
+ /** \brief Returns the list of discovered server URLs.
1387
+ *
1388
+ * Unlike #natsConnection_GetServers, this function only returns
1389
+ * the list of servers that have been discovered after the a connection
1390
+ * has been established (with servers version 0.9.2 and above).
1391
+ *
1392
+ * No credential information is included in any of the server URLs
1393
+ * returned by this call.<br>
1394
+ * If you want to use any of these URLs to connect to a server that
1395
+ * requires authentication, you will need to use #natsOptions_SetUserInfo
1396
+ * or #natsOptions_SetToken.
1397
+ *
1398
+ * \note The user is responsible for freeing the memory of the returned array.
1399
+ *
1400
+ * @param nc the pointer to the #natsConnection object.
1401
+ * @param servers the location where to store the pointer to the array
1402
+ * of server URLs.
1403
+ * @param count the location where to store the number of elements of the
1404
+ * returned array.
1405
+ */
1406
+ NATS_EXTERN natsStatus
1407
+ natsConnection_GetDiscoveredServers(natsConnection *nc, char ***servers, int *count);
1408
+
1409
+ /** \brief Gets the last connection error.
1410
+ *
1411
+ * Returns the last known error as a 'natsStatus' and the location to the
1412
+ * null-terminated error string.
1413
+ *
1414
+ * \warning The returned string is owned by the connection object and
1415
+ * must not be freed.
1416
+ *
1417
+ * @param nc the pointer to the #natsConnection object.
1418
+ * @param lastError the location where the pointer to the connection's last
1419
+ * error string is copied.
1420
+ */
1421
+ NATS_EXTERN natsStatus
1422
+ natsConnection_GetLastError(natsConnection *nc, const char **lastError);
1423
+
1424
+ /** \brief Closes the connection.
1425
+ *
1426
+ * Closes the connection to the server. This call will release all blocking
1427
+ * calls, such as #natsConnection_Flush() and #natsSubscription_NextMsg().
1428
+ * The connection object is still usable until the call to
1429
+ * #natsConnection_Destroy().
1430
+ *
1431
+ * @param nc the pointer to the #natsConnection object.
1432
+ */
1433
+ NATS_EXTERN void
1434
+ natsConnection_Close(natsConnection *nc);
1435
+
1436
+ /** \brief Destroys the connection object.
1437
+ *
1438
+ * Destroys the connection object, freeing up memory.
1439
+ * If not already done, this call first closes the connection to the server.
1440
+ *
1441
+ * @param nc the pointer to the #natsConnection object.
1442
+ */
1443
+ NATS_EXTERN void
1444
+ natsConnection_Destroy(natsConnection *nc);
1445
+
1446
+ /** @} */ // end of connMgtGroup
1447
+
1448
+ /** \defgroup connPubGroup Publishing
1449
+ *
1450
+ * Publishing functions
1451
+ * @{
1452
+ */
1453
+
1454
+ /** \brief Publishes data on a subject.
1455
+ *
1456
+ * Publishes the data argument to the given subject. The data argument is left
1457
+ * untouched and needs to be correctly interpreted on the receiver.
1458
+ *
1459
+ * @param nc the pointer to the #natsConnection object.
1460
+ * @param subj the subject the data is sent to.
1461
+ * @param data the data to be sent, can be `NULL`.
1462
+ * @param dataLen the length of the data to be sent.
1463
+ */
1464
+ NATS_EXTERN natsStatus
1465
+ natsConnection_Publish(natsConnection *nc, const char *subj,
1466
+ const void *data, int dataLen);
1467
+
1468
+ /** \brief Publishes a string on a subject.
1469
+ *
1470
+ * Convenient function to publish a string. This call is equivalent to:
1471
+ *
1472
+ * \code{.c}
1473
+ * const char* myString = "hello";
1474
+ *
1475
+ * natsConnection_Publish(nc, subj, (const void*) myString, (int) strlen(myString));
1476
+ * \endcode
1477
+ *
1478
+ * @param nc the pointer to the #natsConnection object.
1479
+ * @param subj the subject the data is sent to.
1480
+ * @param str the string to be sent.
1481
+ */
1482
+ NATS_EXTERN natsStatus
1483
+ natsConnection_PublishString(natsConnection *nc, const char *subj,
1484
+ const char *str);
1485
+
1486
+ /** \brief Publishes a message on a subject.
1487
+ *
1488
+ * Publishes the #natsMsg, which includes the subject, an optional reply and
1489
+ * optional data.
1490
+ *
1491
+ * @see #natsMsg_Create()
1492
+ *
1493
+ * @param nc the pointer to the #natsConnection object.
1494
+ * @param msg the pointer to the #natsMsg object to send.
1495
+ */
1496
+ NATS_EXTERN natsStatus
1497
+ natsConnection_PublishMsg(natsConnection *nc, natsMsg *msg);
1498
+
1499
+ /** \brief Publishes data on a subject expecting replies on the given reply.
1500
+ *
1501
+ * Publishes the data argument to the given subject expecting a response on
1502
+ * the reply subject. Use #natsConnection_Request() for automatically waiting
1503
+ * for a response inline.
1504
+ *
1505
+ * @param nc the pointer to the #natsConnection object.
1506
+ * @param subj the subject the request is sent to.
1507
+ * @param reply the reply on which resonses are expected.
1508
+ * @param data the data to be sent, can be `NULL`.
1509
+ * @param dataLen the length of the data to be sent.
1510
+ */
1511
+ NATS_EXTERN natsStatus
1512
+ natsConnection_PublishRequest(natsConnection *nc, const char *subj,
1513
+ const char *reply, const void *data, int dataLen);
1514
+
1515
+ /** \brief Publishes a string on a subject expecting replies on the given reply.
1516
+ *
1517
+ * Convenient function to publish a request as a string. This call is
1518
+ * equivalent to:
1519
+ *
1520
+ * \code{.c}
1521
+ * const char* myString = "hello";
1522
+ *
1523
+ * natsPublishRequest(nc, subj, reply, (const void*) myString, (int) strlen(myString));
1524
+ * \endcode
1525
+ *
1526
+ * @param nc the pointer to the #natsConnection object.
1527
+ * @param subj the subject the request is sent to.
1528
+ * @param reply the reply on which resonses are expected.
1529
+ * @param str the string to send.
1530
+ */
1531
+ NATS_EXTERN natsStatus
1532
+ natsConnection_PublishRequestString(natsConnection *nc, const char *subj,
1533
+ const char *reply, const char *str);
1534
+
1535
+ /** \brief Sends a request and waits for a reply.
1536
+ *
1537
+ * Creates a #natsInbox and performs a #natsConnection_PublishRequest() call
1538
+ * with the reply set to that inbox. Returns the first reply received.
1539
+ * This is optimized for the case of multiple responses.
1540
+ *
1541
+ * @param replyMsg the location where to store the pointer to the received
1542
+ * #natsMsg reply.
1543
+ * @param nc the pointer to the #natsConnection object.
1544
+ * @param subj the subject the request is sent to.
1545
+ * @param data the data of the request, can be `NULL`.
1546
+ * @param dataLen the length of the data to send.
1547
+ * @param timeout in milliseconds, before this call returns #NATS_TIMEOUT
1548
+ * if no response is received in this alloted time.
1549
+ */
1550
+ NATS_EXTERN natsStatus
1551
+ natsConnection_Request(natsMsg **replyMsg, natsConnection *nc, const char *subj,
1552
+ const void *data, int dataLen, int64_t timeout);
1553
+
1554
+ /** \brief Sends a request (as a string) and waits for a reply.
1555
+ *
1556
+ * Convenient function to send a request as a string. This call is
1557
+ * equivalent to:
1558
+ *
1559
+ * \code{.c}
1560
+ * const char* myString = "hello";
1561
+ *
1562
+ * natsConnection_Request(replyMsg, nc, subj, (const void*) myString, (int) strlen(myString));
1563
+ * \endcode
1564
+ *
1565
+ * @param replyMsg the location where to store the pointer to the received
1566
+ * #natsMsg reply.
1567
+ * @param nc the pointer to the #natsConnection object.
1568
+ * @param subj the subject the request is sent to.
1569
+ * @param str the string to send.
1570
+ * @param timeout in milliseconds, before this call returns #NATS_TIMEOUT
1571
+ * if no response is received in this alloted time.
1572
+ */
1573
+ NATS_EXTERN natsStatus
1574
+ natsConnection_RequestString(natsMsg **replyMsg, natsConnection *nc,
1575
+ const char *subj, const char *str,
1576
+ int64_t timeout);
1577
+
1578
+ /** @} */ // end of connPubGroup
1579
+
1580
+ /** \defgroup connSubGroup Subscribing
1581
+ *
1582
+ * Subscribing functions.
1583
+ * @{
1584
+ */
1585
+
1586
+ /** \brief Creates an asynchronous subscription.
1587
+ *
1588
+ * Expresses interest in the given subject. The subject can have wildcards
1589
+ * (see \ref wildcardsGroup). Messages will be delivered to the associated
1590
+ * #natsMsgHandler.
1591
+ *
1592
+ * @param sub the location where to store the pointer to the newly created
1593
+ * #natsSubscription object.
1594
+ * @param nc the pointer to the #natsConnection object.
1595
+ * @param subject the subject this subscription is created for.
1596
+ * @param cb the #natsMsgHandler callback.
1597
+ * @param cbClosure a pointer to an user defined object (can be `NULL`). See
1598
+ * the #natsMsgHandler prototype.
1599
+ */
1600
+ NATS_EXTERN natsStatus
1601
+ natsConnection_Subscribe(natsSubscription **sub, natsConnection *nc,
1602
+ const char *subject, natsMsgHandler cb,
1603
+ void *cbClosure);
1604
+
1605
+ /** \brief Creates an asynchronous subscription with a timeout.
1606
+ *
1607
+ * Expresses interest in the given subject. The subject can have wildcards
1608
+ * (see \ref wildcardsGroup). Messages will be delivered to the associated
1609
+ * #natsMsgHandler.
1610
+ *
1611
+ * If no message is received by the given timeout (in milliseconds), the
1612
+ * message handler is invoked with a `NULL` message.<br>
1613
+ * You can then destroy the subscription in the callback, or simply
1614
+ * return, in which case, the message handler will fire again when a
1615
+ * message is received or the subscription times-out again.
1616
+ *
1617
+ * \note Receiving a message reset the timeout. Until all pending messages
1618
+ * are processed, no timeout will occur. The timeout starts when the
1619
+ * message handler for the last pending message returns.
1620
+ *
1621
+ * \warning If you re-use message handler code between subscriptions with
1622
+ * and without timeouts, keep in mind that the message passed in the
1623
+ * message handler may be `NULL`.
1624
+ *
1625
+ * @param sub the location where to store the pointer to the newly created
1626
+ * #natsSubscription object.
1627
+ * @param nc the pointer to the #natsConnection object.
1628
+ * @param subject the subject this subscription is created for.
1629
+ * @param timeout the interval (in milliseconds) after which, if no message
1630
+ * is received, the message handler is invoked with a `NULL` message.
1631
+ * @param cb the #natsMsgHandler callback.
1632
+ * @param cbClosure a pointer to an user defined object (can be `NULL`). See
1633
+ * the #natsMsgHandler prototype.
1634
+ */
1635
+ NATS_EXTERN natsStatus
1636
+ natsConnection_SubscribeTimeout(natsSubscription **sub, natsConnection *nc,
1637
+ const char *subject, int64_t timeout,
1638
+ natsMsgHandler cb, void *cbClosure);
1639
+
1640
+ /** \brief Creates a synchronous subcription.
1641
+ *
1642
+ * Similar to #natsConnection_Subscribe, but creates a synchronous subscription
1643
+ * that can be polled via #natsSubscription_NextMsg().
1644
+ *
1645
+ * @param sub the location where to store the pointer to the newly created
1646
+ * #natsSubscription object.
1647
+ * @param nc the pointer to the #natsConnection object.
1648
+ * @param subject the subject this subscription is created for.
1649
+ */
1650
+ NATS_EXTERN natsStatus
1651
+ natsConnection_SubscribeSync(natsSubscription **sub, natsConnection *nc,
1652
+ const char *subject);
1653
+
1654
+ /** \brief Creates an asynchronous queue subscriber.
1655
+ *
1656
+ * Creates an asynchronous queue subscriber on the given subject.
1657
+ * All subscribers with the same queue name will form the queue group and
1658
+ * only one member of the group will be selected to receive any given
1659
+ * message asynchronously.
1660
+ *
1661
+ * @param sub the location where to store the pointer to the newly created
1662
+ * #natsSubscription object.
1663
+ * @param nc the pointer to the #natsConnection object.
1664
+ * @param subject the subject this subscription is created for.
1665
+ * @param queueGroup the name of the group.
1666
+ * @param cb the #natsMsgHandler callback.
1667
+ * @param cbClosure a pointer to an user defined object (can be `NULL`). See
1668
+ * the #natsMsgHandler prototype.
1669
+ *
1670
+ */
1671
+ NATS_EXTERN natsStatus
1672
+ natsConnection_QueueSubscribe(natsSubscription **sub, natsConnection *nc,
1673
+ const char *subject, const char *queueGroup,
1674
+ natsMsgHandler cb, void *cbClosure);
1675
+
1676
+ /** \brief Creates an asynchronous queue subscriber with a timeout.
1677
+ *
1678
+ * Creates an asynchronous queue subscriber on the given subject.
1679
+ * All subscribers with the same queue name will form the queue group and
1680
+ * only one member of the group will be selected to receive any given
1681
+ * message asynchronously.
1682
+ *
1683
+ * If no message is received by the given timeout (in milliseconds), the
1684
+ * message handler is invoked with a `NULL` message.<br>
1685
+ * You can then destroy the subscription in the callback, or simply
1686
+ * return, in which case, the message handler will fire again when a
1687
+ * message is received or the subscription times-out again.
1688
+ *
1689
+ * \note Receiving a message reset the timeout. Until all pending messages
1690
+ * are processed, no timeout will occur. The timeout starts when the
1691
+ * message handler for the last pending message returns.
1692
+ *
1693
+ * \warning If you re-use message handler code between subscriptions with
1694
+ * and without timeouts, keep in mind that the message passed in the
1695
+ * message handler may be `NULL`.
1696
+ *
1697
+ * @param sub the location where to store the pointer to the newly created
1698
+ * #natsSubscription object.
1699
+ * @param nc the pointer to the #natsConnection object.
1700
+ * @param subject the subject this subscription is created for.
1701
+ * @param queueGroup the name of the group.
1702
+ * @param timeout the interval (in milliseconds) after which, if no message
1703
+ * is received, the message handler is invoked with a `NULL` message.
1704
+ * @param cb the #natsMsgHandler callback.
1705
+ * @param cbClosure a pointer to an user defined object (can be `NULL`). See
1706
+ * the #natsMsgHandler prototype.
1707
+ */
1708
+ NATS_EXTERN natsStatus
1709
+ natsConnection_QueueSubscribeTimeout(natsSubscription **sub, natsConnection *nc,
1710
+ const char *subject, const char *queueGroup,
1711
+ int64_t timeout, natsMsgHandler cb, void *cbClosure);
1712
+
1713
+ /** \brief Creates a synchronous queue subscriber.
1714
+ *
1715
+ * Similar to #natsConnection_QueueSubscribe, but creates a synchronous
1716
+ * subscription that can be polled via #natsSubscription_NextMsg().
1717
+ *
1718
+ * @param sub the location where to store the pointer to the newly created
1719
+ * #natsSubscription object.
1720
+ * @param nc the pointer to the #natsConnection object.
1721
+ * @param subject the subject this subscription is created for.
1722
+ * @param queueGroup the name of the group.
1723
+ */
1724
+ NATS_EXTERN natsStatus
1725
+ natsConnection_QueueSubscribeSync(natsSubscription **sub, natsConnection *nc,
1726
+ const char *subject, const char *queueGroup);
1727
+
1728
+ /** @} */ // end of connSubGroup
1729
+
1730
+ /** @} */ // end of connGroup
1731
+
1732
+ /** \defgroup subGroup Subscription
1733
+ *
1734
+ * NATS Subscriptions.
1735
+ * @{
1736
+ */
1737
+
1738
+ /** \brief Enables the No Delivery Delay mode.
1739
+ *
1740
+ * By default, messages that arrive are not immediately delivered. This
1741
+ * generally improves performance. However, in case of request-reply,
1742
+ * this delay has a negative impact. In such case, call this function
1743
+ * to have the subscriber be notified immediately each time a message
1744
+ * arrives.
1745
+ *
1746
+ * @param sub the pointer to the #natsSubscription object.
1747
+ *
1748
+ * \deprecated No longer needed. Will be removed in the future.
1749
+ */
1750
+ NATS_EXTERN natsStatus
1751
+ natsSubscription_NoDeliveryDelay(natsSubscription *sub);
1752
+
1753
+ /** \brief Returns the next available message.
1754
+ *
1755
+ * Return the next message available to a synchronous subscriber or block until
1756
+ * one is available.
1757
+ * A timeout (expressed in milliseconds) can be used to return when no message
1758
+ * has been delivered. If the value is zero, then this call will not wait and
1759
+ * return the next message that was pending in the client, and #NATS_TIMEOUT
1760
+ * otherwise.
1761
+ *
1762
+ * @param nextMsg the location where to store the pointer to the next availabe
1763
+ * message.
1764
+ * @param sub the pointer to the #natsSubscription object.
1765
+ * @param timeout time, in milliseconds, after which this call will return
1766
+ * #NATS_TIMEOUT if no message is available.
1767
+ */
1768
+ NATS_EXTERN natsStatus
1769
+ natsSubscription_NextMsg(natsMsg **nextMsg, natsSubscription *sub,
1770
+ int64_t timeout);
1771
+
1772
+ /** \brief Unsubscribes.
1773
+ *
1774
+ * Removes interest on the subject. Asynchronous subscription may still have
1775
+ * a callback in progress, in that case, the subscription will still be valid
1776
+ * until the callback returns.
1777
+ *
1778
+ * @param sub the pointer to the #natsSubscription object.
1779
+ */
1780
+ NATS_EXTERN natsStatus
1781
+ natsSubscription_Unsubscribe(natsSubscription *sub);
1782
+
1783
+ /** \brief Auto-Unsubscribes.
1784
+ *
1785
+ * This call issues an automatic #natsSubscription_Unsubscribe that is
1786
+ * processed by the server when 'max' messages have been received.
1787
+ * This can be useful when sending a request to an unknown number
1788
+ * of subscribers.
1789
+ *
1790
+ * @param sub the pointer to the #natsSubscription object.
1791
+ * @param max the maximum number of message you want this subscription
1792
+ * to receive.
1793
+ */
1794
+ NATS_EXTERN natsStatus
1795
+ natsSubscription_AutoUnsubscribe(natsSubscription *sub, int max);
1796
+
1797
+ /** \brief Gets the number of pending messages.
1798
+ *
1799
+ * Returns the number of queued messages in the client for this subscription.
1800
+ *
1801
+ * \deprecated Use #natsSubscription_GetPending instead.
1802
+ *
1803
+ * @param sub the pointer to the #natsSubscription object.
1804
+ * @param queuedMsgs the location where to store the number of queued messages.
1805
+ */
1806
+ NATS_EXTERN natsStatus
1807
+ natsSubscription_QueuedMsgs(natsSubscription *sub, uint64_t *queuedMsgs);
1808
+
1809
+ /** \brief Sets the limit for pending messages and bytes.
1810
+ *
1811
+ * Specifies the maximum number and size of incoming messages that can be
1812
+ * buffered in the library for this subscription, before new incoming messages are
1813
+ * dropped and #NATS_SLOW_CONSUMER status is reported to the #natsErrHandler
1814
+ * callback (if one has been set).
1815
+ *
1816
+ * If no limit is set at the subscription level, the limit set by #natsOptions_SetMaxPendingMsgs
1817
+ * before creating the connection will be used.
1818
+ *
1819
+ * \note If no option is set, there is still a default of `65536` messages and
1820
+ * `65536 * 1024` bytes.
1821
+ *
1822
+ * @see natsOptions_SetMaxPendingMsgs
1823
+ * @see natsSubscription_GetPendingLimits
1824
+ *
1825
+ * @param sub he pointer to the #natsSubscription object.
1826
+ * @param msgLimit the limit in number of messages that the subscription can hold.
1827
+ * @param bytesLimit the limit in bytes that the subscription can hold.
1828
+ */
1829
+ NATS_EXTERN natsStatus
1830
+ natsSubscription_SetPendingLimits(natsSubscription *sub, int msgLimit, int bytesLimit);
1831
+
1832
+ /** \brief Returns the current limit for pending messages and bytes.
1833
+ *
1834
+ * Regardless if limits have been explicitly set with #natsSubscription_SetPendingLimits,
1835
+ * this call will store in the provided memory locations, the limits set for
1836
+ * this subscription.
1837
+ *
1838
+ * \note It is possible for `msgLimit` and/or `bytesLimits` to be `NULL`, in which
1839
+ * case the corresponding value is obviously not stored, but the function will
1840
+ * not return an error.
1841
+ *
1842
+ * @see natsOptions_SetMaxPendingMsgs
1843
+ * @see natsSubscription_SetPendingLimits
1844
+ *
1845
+ * @param sub the pointer to the #natsSubscription object.
1846
+ * @param msgLimit if not `NULL`, the memory location where to store the maximum
1847
+ * number of pending messages for this subscription.
1848
+ * @param bytesLimit if not `NULL`, the memory location where to store the maximum
1849
+ * size of pending messages for this subscription.
1850
+ */
1851
+ NATS_EXTERN natsStatus
1852
+ natsSubscription_GetPendingLimits(natsSubscription *sub, int *msgLimit, int *bytesLimit);
1853
+
1854
+ /** \brief Returns the number of pending messages and bytes.
1855
+ *
1856
+ * Returns the total number and size of pending messages on this subscription.
1857
+ *
1858
+ * \note It is possible for `msgs` and `bytes` to be NULL, in which case the
1859
+ * corresponding values are obviously not stored, but the function will not return
1860
+ * an error.
1861
+ *
1862
+ * @param sub the pointer to the #natsSubscription object.
1863
+ * @param msgs if not `NULL`, the memory location where to store the number of
1864
+ * pending messages.
1865
+ * @param bytes if not `NULL`, the memory location where to store the total size of
1866
+ * pending messages.
1867
+ */
1868
+ NATS_EXTERN natsStatus
1869
+ natsSubscription_GetPending(natsSubscription *sub, int *msgs, int *bytes);
1870
+
1871
+ /** \brief Returns the number of delivered messages.
1872
+ *
1873
+ * Returns the number of delivered messages for this subscription.
1874
+ *
1875
+ * @param sub the pointer to the #natsSubscription object.
1876
+ * @param msgs the memory location where to store the number of
1877
+ * delivered messages.
1878
+ */
1879
+ NATS_EXTERN natsStatus
1880
+ natsSubscription_GetDelivered(natsSubscription *sub, int64_t *msgs);
1881
+
1882
+ /** \brief Returns the number of dropped messages.
1883
+ *
1884
+ * Returns the number of known dropped messages for this subscription. This happens
1885
+ * when a consumer is not keeping up and the library starts to drop messages
1886
+ * when the maximum number (and/or size) of pending messages has been reached.
1887
+ *
1888
+ * \note If the server declares the connection a slow consumer, this number may
1889
+ * not be valid.
1890
+ *
1891
+ * @see natsOptions_SetMaxPendingMsgs
1892
+ * @see natsSubscription_SetPendingLimits
1893
+ *
1894
+ * @param sub the pointer to the #natsSubscription object.
1895
+ * @param msgs the memory location where to store the number of dropped messages.
1896
+ */
1897
+ NATS_EXTERN natsStatus
1898
+ natsSubscription_GetDropped(natsSubscription *sub, int64_t *msgs);
1899
+
1900
+ /** \brief Returns the maximum number of pending messages and bytes.
1901
+ *
1902
+ * Returns the maximum of pending messages and bytes seen so far.
1903
+ *
1904
+ * \note `msgs` and/or `bytes` can be NULL.
1905
+ *
1906
+ * @param sub the pointer to the #natsSubscription object.
1907
+ * @param msgs if not `NULL`, the memory location where to store the maximum
1908
+ * number of pending messages seen so far.
1909
+ * @param bytes if not `NULL`, the memory location where to store the maximum
1910
+ * number of bytes pending seen so far.
1911
+ */
1912
+ NATS_EXTERN natsStatus
1913
+ natsSubscription_GetMaxPending(natsSubscription *sub, int *msgs, int *bytes);
1914
+
1915
+ /** \brief Clears the statistics regarding the maximum pending values.
1916
+ *
1917
+ * Clears the statistics regarding the maximum pending values.
1918
+ *
1919
+ * @param sub the pointer to the #natsSubscription object.
1920
+ */
1921
+ NATS_EXTERN natsStatus
1922
+ natsSubscription_ClearMaxPending(natsSubscription *sub);
1923
+
1924
+ /** \brief Get various statistics from this subscription.
1925
+ *
1926
+ * This is a convenient function to get several subscription's statistics
1927
+ * in one call.
1928
+ *
1929
+ * \note Any or all of the statistics pointers can be `NULL`.
1930
+ *
1931
+ * @see natsSubscription_GetPending
1932
+ * @see natsSubscription_GetMaxPending
1933
+ * @see natsSubscription_GetDelivered
1934
+ * @see natsSubscription_GetDropped
1935
+ *
1936
+ * @param sub the pointer to the #natsSubscription object.
1937
+ * @param pendingMsgs if not `NULL`, memory location where to store the
1938
+ * number of pending messages.
1939
+ * @param pendingBytes if not `NULL`, memory location where to store the
1940
+ * total size of pending messages.
1941
+ * @param maxPendingMsgs if not `NULL`, memory location where to store the
1942
+ * maximum number of pending messages seen so far.
1943
+ * @param maxPendingBytes if not `NULL`, memory location where to store the
1944
+ * maximum total size of pending messages seen so far.
1945
+ * @param deliveredMsgs if not `NULL`, memory location where to store the
1946
+ * number of delivered messages.
1947
+ * @param droppedMsgs if not `NULL`, memory location where to store the
1948
+ * number of dropped messages.
1949
+ */
1950
+ NATS_EXTERN natsStatus
1951
+ natsSubscription_GetStats(natsSubscription *sub,
1952
+ int *pendingMsgs,
1953
+ int *pendingBytes,
1954
+ int *maxPendingMsgs,
1955
+ int *maxPendingBytes,
1956
+ int64_t *deliveredMsgs,
1957
+ int64_t *droppedMsgs);
1958
+
1959
+ /** \brief Checks the validity of the subscription.
1960
+ *
1961
+ * Returns a boolean indicating whether the subscription is still active.
1962
+ * This will return false if the subscription has already been closed,
1963
+ * or auto unsubscribed.
1964
+ *
1965
+ * @param sub the pointer to the #natsSubscription object.
1966
+ */
1967
+ NATS_EXTERN bool
1968
+ natsSubscription_IsValid(natsSubscription *sub);
1969
+
1970
+ /** \brief Destroys the subscription.
1971
+ *
1972
+ * Destroys the subscription object, freeing up memory.
1973
+ * If not already done, this call will removes interest on the subject.
1974
+ *
1975
+ * @param sub the pointer to the #natsSubscription object to destroy.
1976
+ */
1977
+ NATS_EXTERN void
1978
+ natsSubscription_Destroy(natsSubscription *sub);
1979
+
1980
+ /** @} */ // end of subGroup
1981
+
1982
+ /** @} */ // end of funcGroup
1983
+
1984
+ /** \defgroup wildcardsGroup Wildcards
1985
+ * @{
1986
+ * Use of wildcards. There are two type of wildcards: `*` for partial,
1987
+ * and `>` for full.
1988
+ *
1989
+ * A subscription on subject `foo.*` would receive messages sent to:
1990
+ * - `foo.bar`
1991
+ * - `foo.baz`
1992
+ *
1993
+ * but not on:
1994
+ *
1995
+ * - `foo.bar.baz` (too many elements)
1996
+ * - `bar.foo`. (does not start with `foo`).
1997
+ *
1998
+ * A subscription on subject `foo.>` would receive messages sent to:
1999
+ * - `foo.bar`
2000
+ * - `foo.baz`
2001
+ * - `foo.bar.baz`
2002
+ *
2003
+ * but not on:
2004
+ * - `foo` (only one element, needs at least two)
2005
+ * - `bar.baz` (does not start with `foo`).
2006
+ ** @} */
2007
+
2008
+ /** \defgroup envVariablesGroup Environment Variables
2009
+ * @{
2010
+ * You will find here the environment variables that change the default behavior
2011
+ * of the NATS C Client library.
2012
+ * <br><br>
2013
+ * Name | Effect
2014
+ * -----|:-----:
2015
+ * <b>`NATS_DEFAULT_TO_LIB_MSG_DELIVERY`</b> | On #nats_Open, the library looks for this environment variable. If set (to any value), the library will default to using a global thread pool to perform message delivery. See #natsOptions_UseGlobalMessageDelivery and #nats_SetMessageDeliveryPoolSize.
2016
+ *
2017
+ ** @} */
2018
+
2019
+
2020
+ #ifdef __cplusplus
2021
+ }
2022
+ #endif
2023
+
2024
+ #endif /* NATS_H_ */