ffi-nats-core 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,518 @@
1
+ // Copyright 2015-2016 Apcera Inc. All rights reserved.
2
+
3
+ #ifndef NATSP_H_
4
+ #define NATSP_H_
5
+
6
+ #if defined(_WIN32)
7
+ # include "include/n-win.h"
8
+ #else
9
+ # include "include/n-unix.h"
10
+ #endif
11
+
12
+ #if defined(NATS_HAS_TLS)
13
+ #include <openssl/ssl.h>
14
+ #include <openssl/err.h>
15
+ #include <openssl/x509v3.h>
16
+ #include <openssl/rand.h>
17
+ #else
18
+ #define SSL void*
19
+ #define SSL_free(c) { (c) = NULL; }
20
+ #define SSL_CTX void*
21
+ #define SSL_CTX_free(c) { (c) = NULL; }
22
+ #define NO_SSL_ERR "The library was built without SSL support!"
23
+ #endif
24
+
25
+ #include "err.h"
26
+ #include "nats.h"
27
+ #include "buf.h"
28
+ #include "parser.h"
29
+ #include "timer.h"
30
+ #include "url.h"
31
+ #include "srvpool.h"
32
+ #include "msg.h"
33
+ #include "asynccb.h"
34
+ #include "hash.h"
35
+ #include "stats.h"
36
+ #include "natstime.h"
37
+ #include "nuid.h"
38
+
39
+ // Comment/uncomment to replace some function calls with direct structure
40
+ // access
41
+ //#define DEV_MODE (1)
42
+
43
+ #define LIB_NATS_VERSION_STRING NATS_VERSION_STRING
44
+ #define LIB_NATS_VERSION_NUMBER NATS_VERSION_NUMBER
45
+ #define LIB_NATS_VERSION_REQUIRED_NUMBER NATS_VERSION_REQUIRED_NUMBER
46
+
47
+ #define CString "C"
48
+
49
+ #define _OK_OP_ "+OK"
50
+ #define _ERR_OP_ "-ERR"
51
+ #define _MSG_OP_ "MSG"
52
+ #define _PING_OP_ "PING"
53
+ #define _PONG_OP_ "PONG"
54
+ #define _INFO_OP_ "INFO"
55
+
56
+ #define _CRLF_ "\r\n"
57
+ #define _SPC_ " "
58
+ #define _PUB_P_ "PUB "
59
+
60
+ #define _PING_PROTO_ "PING\r\n"
61
+ #define _PONG_PROTO_ "PONG\r\n"
62
+ #define _PUB_PROTO_ "PUB %s %s %d\r\n"
63
+ #define _SUB_PROTO_ "SUB %s %s %d\r\n"
64
+ #define _UNSUB_PROTO_ "UNSUB %" PRId64 " %d\r\n"
65
+ #define _UNSUB_NO_MAX_PROTO_ "UNSUB %" PRId64 " \r\n"
66
+
67
+ #define STALE_CONNECTION "Stale Connection"
68
+
69
+ #define _CRLF_LEN_ (2)
70
+ #define _SPC_LEN_ (1)
71
+ #define _PUB_P_LEN_ (4)
72
+ #define _PING_OP_LEN_ (4)
73
+ #define _PONG_OP_LEN_ (4)
74
+ #define _PING_PROTO_LEN_ (6)
75
+ #define _PONG_PROTO_LEN_ (6)
76
+ #define _OK_OP_LEN_ (3)
77
+ #define _ERR_OP_LEN_ (4)
78
+
79
+ static const char *inboxPrefix = "_INBOX.";
80
+ #define NATS_INBOX_PRE_LEN (7)
81
+
82
+ #define WAIT_FOR_READ (0)
83
+ #define WAIT_FOR_WRITE (1)
84
+ #define WAIT_FOR_CONNECT (2)
85
+
86
+ #define MAX_FRAMES (50)
87
+
88
+ extern int64_t gLockSpinCount;
89
+
90
+ typedef void (*natsInitOnceCb)(void);
91
+
92
+ typedef struct __natsControl
93
+ {
94
+ char *op;
95
+ char *args;
96
+
97
+ } natsControl;
98
+
99
+ typedef struct __natsServerInfo
100
+ {
101
+ char *id;
102
+ char *host;
103
+ int port;
104
+ char *version;
105
+ bool authRequired;
106
+ bool tlsRequired;
107
+ int64_t maxPayload;
108
+ char **connectURLs;
109
+ int connectURLsCount;
110
+
111
+ } natsServerInfo;
112
+
113
+ typedef struct __natsSSLCtx
114
+ {
115
+ natsMutex *lock;
116
+ int refs;
117
+ SSL_CTX *ctx;
118
+ char *expectedHostname;
119
+ bool skipVerify;
120
+
121
+ } natsSSLCtx;
122
+
123
+ #define natsSSLCtx_getExpectedHostname(ctx) ((ctx)->expectedHostname)
124
+
125
+ typedef struct
126
+ {
127
+ natsEvLoop_Attach attach;
128
+ natsEvLoop_ReadAddRemove read;
129
+ natsEvLoop_WriteAddRemove write;
130
+ natsEvLoop_Detach detach;
131
+
132
+ } natsEvLoopCallbacks;
133
+
134
+ struct __natsOptions
135
+ {
136
+ // This field must be the first (see natsOptions_clone, same if you add
137
+ // allocated fields such as strings).
138
+ natsMutex *mu;
139
+
140
+ char *url;
141
+ char **servers;
142
+ int serversCount;
143
+ bool noRandomize;
144
+ int64_t timeout;
145
+ char *name;
146
+ bool verbose;
147
+ bool pedantic;
148
+ bool allowReconnect;
149
+ bool secure;
150
+ int maxReconnect;
151
+ int64_t reconnectWait;
152
+ int reconnectBufSize;
153
+
154
+ char *user;
155
+ char *password;
156
+ char *token;
157
+
158
+ natsConnectionHandler closedCb;
159
+ void *closedCbClosure;
160
+
161
+ natsConnectionHandler disconnectedCb;
162
+ void *disconnectedCbClosure;
163
+
164
+ natsConnectionHandler reconnectedCb;
165
+ void *reconnectedCbClosure;
166
+
167
+ natsErrHandler asyncErrCb;
168
+ void *asyncErrCbClosure;
169
+
170
+ int64_t pingInterval;
171
+ int maxPingsOut;
172
+ int maxPendingMsgs;
173
+
174
+ natsSSLCtx *sslCtx;
175
+
176
+ void *evLoop;
177
+ natsEvLoopCallbacks evCbs;
178
+
179
+ bool libMsgDelivery;
180
+
181
+ int orderIP; // possible values: 0,4,6,46,64
182
+
183
+ };
184
+
185
+ typedef struct __natsMsgList
186
+ {
187
+ natsMsg *head;
188
+ natsMsg *tail;
189
+ int msgs;
190
+ int bytes;
191
+
192
+ } natsMsgList;
193
+
194
+ typedef struct __natsMsgDlvWorker
195
+ {
196
+ natsMutex *lock;
197
+ natsCondition *cond;
198
+ natsThread *thread;
199
+ bool inWait;
200
+ bool shutdown;
201
+ natsMsgList msgList;
202
+
203
+ } natsMsgDlvWorker;
204
+
205
+ struct __natsSubscription
206
+ {
207
+ natsMutex *mu;
208
+
209
+ int refs;
210
+
211
+ // This is non-zero when auto-unsubscribe is used.
212
+ uint64_t max;
213
+
214
+ // This is updated in the delivery thread (or NextMsg) and indicates
215
+ // how many message have been presented to the callback (or returned
216
+ // from NextMsg). Like 'msgs', this is also used to determine if we
217
+ // have reached the max number of messages.
218
+ uint64_t delivered;
219
+
220
+ // The list of messages waiting to be delivered to the callback (or
221
+ // returned from NextMsg).
222
+ natsMsgList msgList;
223
+
224
+ // True if msgList.count is over pendingMax
225
+ bool slowConsumer;
226
+
227
+ // Condition variable used to wait for message delivery.
228
+ natsCondition *cond;
229
+
230
+ // This is > 0 when the delivery thread (or NextMsg) goes into a
231
+ // condition wait.
232
+ int inWait;
233
+
234
+ // The subscriber is closed (or closing).
235
+ bool closed;
236
+
237
+ // If true, the subscription is closed, but because the connection
238
+ // was closed, not because of subscription (auto-)unsubscribe.
239
+ bool connClosed;
240
+
241
+ // Subscriber id. Assigned during the creation, does not change after that.
242
+ int64_t sid;
243
+
244
+ // Subject that represents this subscription. This can be different
245
+ // than the received subject inside a Msg if this is a wildcard.
246
+ char *subject;
247
+
248
+ // Optional queue group name. If present, all subscriptions with the
249
+ // same name will form a distributed queue, and each message will
250
+ // only be processed by one member of the group.
251
+ char *queue;
252
+
253
+ // Reference to the connection that created this subscription.
254
+ struct __natsConnection *conn;
255
+
256
+ // Delivery thread (for async subscription).
257
+ natsThread *deliverMsgsThread;
258
+
259
+ // If message delivery is done by the library instead, this is the
260
+ // reference to the worker handling this subscription.
261
+ natsMsgDlvWorker *libDlvWorker;
262
+
263
+ // Message callback and closure (for async subscription).
264
+ natsMsgHandler msgCb;
265
+ void *msgCbClosure;
266
+
267
+ int64_t timeout;
268
+ natsTimer *timeoutTimer;
269
+ bool timedOut;
270
+ bool timeoutSuspended;
271
+
272
+ // Pending limits, etc..
273
+ int msgsMax;
274
+ int bytesMax;
275
+ int msgsLimit;
276
+ int bytesLimit;
277
+ int64_t dropped;
278
+
279
+ };
280
+
281
+ typedef struct __natsPong
282
+ {
283
+ int64_t id;
284
+
285
+ struct __natsPong *prev;
286
+ struct __natsPong *next;
287
+
288
+ } natsPong;
289
+
290
+ typedef struct __natsPongList
291
+ {
292
+ natsPong *head;
293
+ natsPong *tail;
294
+
295
+ int64_t incoming;
296
+ int64_t outgoingPings;
297
+
298
+ natsPong cached;
299
+
300
+ natsCondition *cond;
301
+
302
+ } natsPongList;
303
+
304
+ typedef struct __natsSockCtx
305
+ {
306
+ natsSock fd;
307
+ bool fdActive;
308
+
309
+ // We switch to blocking socket after receiving the PONG to the first PING
310
+ // during the connect process. Should we make all read/writes non blocking,
311
+ // then we will use two different fd sets, and also probably pass deadlines
312
+ // individually as opposed to use one at the connection level.
313
+ fd_set *fdSet;
314
+ #ifdef _WIN32
315
+ fd_set *errSet;
316
+ #endif
317
+ natsDeadline deadline;
318
+
319
+ SSL *ssl;
320
+
321
+ // This is true when we are using an external event loop (such as libuv).
322
+ bool useEventLoop;
323
+
324
+ int orderIP; // possible values: 0,4,6,46,64
325
+
326
+ } natsSockCtx;
327
+
328
+ struct __natsConnection
329
+ {
330
+ natsMutex *mu;
331
+ natsOptions *opts;
332
+ const natsUrl *url;
333
+
334
+ int refs;
335
+
336
+ natsSockCtx sockCtx;
337
+
338
+ natsSrvPool *srvPool;
339
+
340
+ natsBuffer *pending;
341
+ bool usePending;
342
+
343
+ natsBuffer *bw;
344
+ natsBuffer *scratch;
345
+
346
+ natsServerInfo info;
347
+
348
+ int64_t ssid;
349
+ natsHash *subs;
350
+
351
+ natsConnStatus status;
352
+ natsStatus err;
353
+ char errStr[256];
354
+
355
+ natsParser *ps;
356
+ natsTimer *ptmr;
357
+ int pout;
358
+
359
+ natsPongList pongs;
360
+
361
+ natsThread *readLoopThread;
362
+
363
+ natsThread *flusherThread;
364
+ natsCondition *flusherCond;
365
+ bool flusherSignaled;
366
+ bool flusherStop;
367
+
368
+ natsThread *reconnectThread;
369
+
370
+ natsStatistics stats;
371
+
372
+ struct
373
+ {
374
+ bool attached;
375
+ bool writeAdded;
376
+ void *buffer;
377
+ void *data;
378
+ } el;
379
+ };
380
+
381
+ //
382
+ // Library
383
+ //
384
+ void
385
+ natsSys_Init(void);
386
+
387
+ void
388
+ natsLib_Retain(void);
389
+
390
+ void
391
+ natsLib_Release(void);
392
+
393
+ void
394
+ nats_resetTimer(natsTimer *t, int64_t newInterval);
395
+
396
+ void
397
+ nats_stopTimer(natsTimer *t);
398
+
399
+ // Returns the number of timers that have been created and not stopped.
400
+ int
401
+ nats_getTimersCount(void);
402
+
403
+ // Returns the number of timers actually in the list. This should be
404
+ // equal to nats_getTimersCount() or nats_getTimersCount() - 1 when a
405
+ // timer thread is invoking a timer's callback.
406
+ int
407
+ nats_getTimersCountInList(void);
408
+
409
+ natsStatus
410
+ nats_postAsyncCbInfo(natsAsyncCbInfo *info);
411
+
412
+ void
413
+ nats_sslRegisterThreadForCleanup(void);
414
+
415
+ natsStatus
416
+ nats_sslInit(void);
417
+
418
+ natsStatus
419
+ natsInbox_init(char *inbox, int inboxLen);
420
+
421
+ natsStatus
422
+ natsLib_msgDeliveryPostControlMsg(natsSubscription *sub);
423
+
424
+ natsStatus
425
+ natsLib_msgDeliveryAssignWorker(natsSubscription *sub);
426
+
427
+ bool
428
+ natsLib_isLibHandlingMsgDeliveryByDefault(void);
429
+
430
+ void
431
+ natsLib_getMsgDeliveryPoolInfo(int *maxSize, int *size, int *idx, natsMsgDlvWorker ***workersArray);
432
+
433
+ //
434
+ // Threads
435
+ //
436
+ typedef void (*natsThreadCb)(void *arg);
437
+
438
+ natsStatus
439
+ natsThread_Create(natsThread **t, natsThreadCb cb, void *arg);
440
+
441
+ bool
442
+ natsThread_IsCurrent(natsThread *t);
443
+
444
+ void
445
+ natsThread_Join(natsThread *t);
446
+
447
+ void
448
+ natsThread_Detach(natsThread *t);
449
+
450
+ void
451
+ natsThread_Yield(void);
452
+
453
+ void
454
+ natsThread_Destroy(natsThread *t);
455
+
456
+ natsStatus
457
+ natsThreadLocal_CreateKey(natsThreadLocal *tl, void (*destructor)(void*));
458
+
459
+ void*
460
+ natsThreadLocal_Get(natsThreadLocal tl);
461
+
462
+ #define natsThreadLocal_Set(k, v) natsThreadLocal_SetEx((k), (v), true)
463
+
464
+ natsStatus
465
+ natsThreadLocal_SetEx(natsThreadLocal tl, const void *value, bool setErr);
466
+
467
+ void
468
+ natsThreadLocal_DestroyKey(natsThreadLocal tl);
469
+
470
+ bool
471
+ nats_InitOnce(natsInitOnceType *control, natsInitOnceCb cb);
472
+
473
+
474
+ //
475
+ // Conditions
476
+ //
477
+ natsStatus
478
+ natsCondition_Create(natsCondition **cond);
479
+
480
+ void
481
+ natsCondition_Wait(natsCondition *cond, natsMutex *mutex);
482
+
483
+ natsStatus
484
+ natsCondition_TimedWait(natsCondition *cond, natsMutex *mutex, int64_t timeout);
485
+
486
+ natsStatus
487
+ natsCondition_AbsoluteTimedWait(natsCondition *cond, natsMutex *mutex,
488
+ int64_t absoluteTime);
489
+
490
+ void
491
+ natsCondition_Signal(natsCondition *cond);
492
+
493
+ void
494
+ natsCondition_Broadcast(natsCondition *cond);
495
+
496
+ void
497
+ natsCondition_Destroy(natsCondition *cond);
498
+
499
+ //
500
+ // Mutexes
501
+ //
502
+ natsStatus
503
+ natsMutex_Create(natsMutex **newMutex);
504
+
505
+ void
506
+ natsMutex_Lock(natsMutex *m);
507
+
508
+ bool
509
+ natsMutex_TryLock(natsMutex *m);
510
+
511
+ void
512
+ natsMutex_Unlock(natsMutex *m);
513
+
514
+ void
515
+ natsMutex_Destroy(natsMutex *m);
516
+
517
+
518
+ #endif /* NATSP_H_ */