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,31 @@
1
+ #------------------------
2
+ # Add the sub-directories
3
+ #------------------------
4
+ include_directories(include)
5
+ include_directories(${NATS_PLATFORM_INCLUDE})
6
+ if(NATS_BUILD_WITH_TLS)
7
+ include_directories(${OPENSSL_INCLUDE_DIR})
8
+ set(NATS_OPENSSL_LIBS "${OPENSSL_LIBRARIES}")
9
+ endif(NATS_BUILD_WITH_TLS)
10
+
11
+ #---------------------------------------
12
+ # Grab all files in 'src' and 'src/unix'
13
+ # or 'src/win' depending on the platform
14
+ #---------------------------------------
15
+ file(GLOB SOURCES "*.c")
16
+ file(GLOB PS_SOURCES "${NATS_PLATFORM_INCLUDE}/*.c")
17
+
18
+ # --------------------------------------
19
+ # Create the shared and static libraries
20
+ # --------------------------------------
21
+ add_library(nats SHARED ${SOURCES} ${PS_SOURCES})
22
+ target_link_libraries(nats ${NATS_OPENSSL_LIBS} ${NATS_EXTRA_LIB})
23
+ add_library(nats_static STATIC ${SOURCES} ${PS_SOURCES})
24
+ target_link_libraries(nats_static ${NATS_OPENSSL_LIBS})
25
+
26
+ # --------------------------------------
27
+ # Install the libraries and header files
28
+ # --------------------------------------
29
+ install(TARGETS nats DESTINATION lib)
30
+ install(TARGETS nats_static DESTINATION lib)
31
+ install(FILES nats.h status.h version.h DESTINATION include)
@@ -0,0 +1,66 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #include "natsp.h"
4
+ #include "mem.h"
5
+ #include "conn.h"
6
+
7
+ static void
8
+ _freeAsyncCbInfo(natsAsyncCbInfo *info)
9
+ {
10
+ NATS_FREE(info);
11
+ }
12
+
13
+ static void
14
+ _createAndPostCb(natsAsyncCbType type, natsConnection *nc, natsSubscription *sub, natsStatus err)
15
+ {
16
+ natsStatus s = NATS_OK;
17
+ natsAsyncCbInfo *cb;
18
+
19
+ cb = NATS_CALLOC(1, sizeof(natsAsyncCbInfo));
20
+ if (cb == NULL)
21
+ {
22
+ // We will ignore for now since that makes the caller handle the
23
+ // possibility of having to run the callbacks in-line...
24
+ return;
25
+ }
26
+
27
+ cb->type = type;
28
+ cb->nc = nc;
29
+ cb->sub = sub;
30
+ cb->err = err;
31
+
32
+ natsConn_retain(nc);
33
+
34
+ s = nats_postAsyncCbInfo(cb);
35
+ if (s != NATS_OK)
36
+ {
37
+ _freeAsyncCbInfo(cb);
38
+ natsConn_release(nc);
39
+ }
40
+ }
41
+
42
+ void
43
+ natsAsyncCb_PostConnHandler(natsConnection *nc, natsAsyncCbType type)
44
+ {
45
+ _createAndPostCb(type, nc, NULL, NATS_OK);
46
+ }
47
+
48
+ void
49
+ natsAsyncCb_PostErrHandler(natsConnection *nc, natsSubscription *sub, natsStatus err)
50
+ {
51
+ _createAndPostCb(ASYNC_ERROR, nc, sub, err);
52
+ }
53
+
54
+ void
55
+ natsAsyncCb_Destroy(natsAsyncCbInfo *info)
56
+ {
57
+ natsConnection *nc;
58
+
59
+ if (info == NULL)
60
+ return;
61
+
62
+ nc = info->nc;
63
+
64
+ _freeAsyncCbInfo(info);
65
+ natsConn_release(nc);
66
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #ifndef ASYNCCB_H_
4
+ #define ASYNCCB_H_
5
+
6
+ #include "status.h"
7
+
8
+ typedef enum
9
+ {
10
+ ASYNC_CLOSED = 0,
11
+ ASYNC_DISCONNECTED,
12
+ ASYNC_RECONNECTED,
13
+ ASYNC_ERROR
14
+
15
+ } natsAsyncCbType;
16
+
17
+ struct __natsConnection;
18
+ struct __natsSubscription;
19
+ struct __natsAsyncCbInfo;
20
+
21
+ typedef struct __natsAsyncCbInfo
22
+ {
23
+ natsAsyncCbType type;
24
+ struct __natsConnection *nc;
25
+ struct __natsSubscription *sub;
26
+ natsStatus err;
27
+
28
+ struct __natsAsyncCbInfo *next;
29
+
30
+ } natsAsyncCbInfo;
31
+
32
+ void
33
+ natsAsyncCb_PostConnHandler(struct __natsConnection *nc, natsAsyncCbType type);
34
+
35
+ void
36
+ natsAsyncCb_PostErrHandler(struct __natsConnection *nc,
37
+ struct __natsSubscription *sub, natsStatus err);
38
+
39
+ void
40
+ natsAsyncCb_Destroy(natsAsyncCbInfo *info);
41
+
42
+ #endif /* ASYNCCB_H_ */
@@ -0,0 +1,246 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #include <string.h>
4
+ #include <assert.h>
5
+
6
+ #include "err.h"
7
+ #include "mem.h"
8
+ #include "buf.h"
9
+
10
+ static natsStatus
11
+ _init(natsBuffer *newBuf, char *data, int len, int capacity)
12
+ {
13
+ natsBuffer *buf = newBuf;
14
+
15
+ // Since we explicitly set all fields, no need for memset
16
+
17
+ buf->doFree = false;
18
+
19
+ if (data != NULL)
20
+ {
21
+ buf->data = data;
22
+ buf->ownData = false;
23
+ }
24
+ else
25
+ {
26
+ buf->data = (char*) NATS_MALLOC(capacity);
27
+ if (buf->data == NULL)
28
+ return nats_setDefaultError(NATS_NO_MEMORY);
29
+
30
+ buf->ownData = true;
31
+ }
32
+
33
+ buf->pos = buf->data + len;
34
+ buf->len = len;
35
+ buf->capacity = capacity;
36
+
37
+ return NATS_OK;
38
+ }
39
+
40
+ natsStatus
41
+ natsBuf_InitWithBackend(natsBuffer *newBuf, char *data, int len, int capacity)
42
+ {
43
+ if (data == NULL)
44
+ return NATS_INVALID_ARG;
45
+
46
+ return _init(newBuf, data, len, capacity);
47
+ }
48
+
49
+ natsStatus
50
+ natsBuf_Init(natsBuffer *buf, int capacity)
51
+ {
52
+ return _init(buf, NULL, 0, capacity);
53
+ }
54
+
55
+ static natsStatus
56
+ _newBuf(natsBuffer **newBuf, char *data, int len, int capacity)
57
+ {
58
+ natsBuffer *buf;
59
+
60
+ buf = (natsBuffer*) NATS_MALLOC(sizeof(natsBuffer));
61
+ if (buf == NULL)
62
+ return nats_setDefaultError(NATS_NO_MEMORY);
63
+
64
+ if (_init(buf, data, len, capacity) != NATS_OK)
65
+ {
66
+ NATS_FREE(buf);
67
+ return NATS_UPDATE_ERR_STACK(NATS_NO_MEMORY);
68
+ }
69
+
70
+ buf->doFree = true;
71
+
72
+ *newBuf = buf;
73
+
74
+ return NATS_OK;
75
+ }
76
+
77
+ natsStatus
78
+ natsBuf_CreateWithBackend(natsBuffer **newBuf, char *data, int len, int capacity)
79
+ {
80
+ natsStatus s;
81
+
82
+ if (data == NULL)
83
+ return nats_setDefaultError(NATS_INVALID_ARG);
84
+
85
+ s = _newBuf(newBuf, data, len, capacity);
86
+
87
+ return NATS_UPDATE_ERR_STACK(s);
88
+ }
89
+
90
+ natsStatus
91
+ natsBuf_Create(natsBuffer **newBuf, int capacity)
92
+ {
93
+ natsStatus s = _newBuf(newBuf, NULL, 0, capacity);
94
+ return NATS_UPDATE_ERR_STACK(s);
95
+ }
96
+
97
+ void
98
+ natsBuf_Reset(natsBuffer *buf)
99
+ {
100
+ buf->len = 0;
101
+ buf->pos = buf->data;
102
+ }
103
+
104
+ void
105
+ natsBuf_RewindTo(natsBuffer *buf, int newPosition)
106
+ {
107
+ assert(newPosition < buf->capacity);
108
+
109
+ buf->len = newPosition;
110
+ buf->pos = buf->data + newPosition;
111
+ }
112
+
113
+
114
+ natsStatus
115
+ natsBuf_Expand(natsBuffer *buf, int newSize)
116
+ {
117
+ int offset = (int) (buf->pos - buf->data);
118
+ char *newData = NULL;
119
+
120
+ if (newSize <= buf->capacity)
121
+ return nats_setDefaultError(NATS_INVALID_ARG);
122
+
123
+ if (buf->ownData)
124
+ {
125
+ newData = NATS_REALLOC(buf->data, newSize);
126
+ if (newData == NULL)
127
+ return nats_setDefaultError(NATS_NO_MEMORY);
128
+ }
129
+ else
130
+ {
131
+ newData = NATS_MALLOC(newSize);
132
+ if (newData == NULL)
133
+ return nats_setDefaultError(NATS_NO_MEMORY);
134
+
135
+ memcpy(newData, buf->data, buf->len);
136
+ buf->ownData = true;
137
+ }
138
+
139
+ if (buf->data != newData)
140
+ {
141
+ buf->data = newData;
142
+ buf->pos = (char*) (buf->data + offset);
143
+ }
144
+
145
+ buf->capacity = newSize;
146
+
147
+ return NATS_OK;
148
+ }
149
+
150
+ natsStatus
151
+ natsBuf_Append(natsBuffer *buf, const char* data, int dataLen)
152
+ {
153
+ natsStatus s = NATS_OK;
154
+ int n = buf->len + dataLen;
155
+
156
+ // We could use int64_t and check for 0x7FFFFFFF, but keeping
157
+ // all int is faster.
158
+ if (n < 0)
159
+ return nats_setDefaultError(NATS_NO_MEMORY);
160
+
161
+ if (n > buf->capacity)
162
+ {
163
+ // Increase by 10%
164
+ int extra = (int) (n * 0.1);
165
+ int newSize;
166
+
167
+ // Make sure that we have at least some bytes left after adding.
168
+ newSize = (n + (extra < 64 ? 64 : extra));
169
+
170
+ // Overrun.
171
+ if (newSize < 0)
172
+ return nats_setDefaultError(NATS_NO_MEMORY);
173
+
174
+ s = natsBuf_Expand(buf, newSize);
175
+ }
176
+
177
+ if (s == NATS_OK)
178
+ {
179
+ memcpy(buf->pos, data, dataLen);
180
+ buf->pos += dataLen;
181
+ buf->len += dataLen;
182
+ }
183
+
184
+ return NATS_UPDATE_ERR_STACK(s);
185
+ }
186
+
187
+ natsStatus
188
+ natsBuf_AppendByte(natsBuffer *buf, char b)
189
+ {
190
+ natsStatus s = NATS_OK;
191
+ int c = buf->capacity;
192
+
193
+ if (buf->len == c)
194
+ {
195
+ // Increase by 10%
196
+ int extra = (int) (c * 0.1);
197
+ int newSize;
198
+
199
+ // Make sure that we have at least some bytes left after adding.
200
+ newSize = (c + (extra < 64 ? 64 : extra));
201
+
202
+ // Overrun.
203
+ if (newSize < 0)
204
+ return nats_setDefaultError(NATS_NO_MEMORY);
205
+
206
+ s = natsBuf_Expand(buf, newSize);
207
+ }
208
+
209
+ if (s == NATS_OK)
210
+ {
211
+ *(buf->pos++) = b;
212
+ buf->len++;
213
+ }
214
+
215
+ return NATS_UPDATE_ERR_STACK(s);
216
+ }
217
+
218
+ void
219
+ natsBuf_Consume(natsBuffer *buf, int n)
220
+ {
221
+ int remaining;
222
+
223
+ assert(n <= buf->len);
224
+
225
+ remaining = buf->len - n;
226
+ if (remaining > 0)
227
+ memmove(buf->data, buf->data + n, remaining);
228
+
229
+ buf->len = remaining;
230
+ buf->pos = buf->data + remaining;
231
+ }
232
+
233
+ void
234
+ natsBuf_Destroy(natsBuffer *buf)
235
+ {
236
+ if (buf == NULL)
237
+ return;
238
+
239
+ if (buf->ownData)
240
+ NATS_FREE(buf->data);
241
+
242
+ if (buf->doFree)
243
+ NATS_FREE(buf);
244
+ else
245
+ memset(buf, 0, sizeof(natsBuffer));
246
+ }
@@ -0,0 +1,116 @@
1
+ // Copyright 2015 Apcera Inc. All rights reserved.
2
+
3
+ #ifndef BUF_H_
4
+ #define BUF_H_
5
+
6
+ #include <stdbool.h>
7
+
8
+ #include "status.h"
9
+
10
+ typedef struct __natsBuffer
11
+ {
12
+ char* data;
13
+ char* pos;
14
+ int len;
15
+ int capacity;
16
+ bool ownData;
17
+ bool doFree;
18
+
19
+ } natsBuffer;
20
+
21
+ #define natsBuf_Data(b) ((b)->data)
22
+ #define natsBuf_Capacity(b) ((b)->capacity)
23
+ #define natsBuf_Len(b) ((b)->len)
24
+ #define natsBuf_Available(b) ((b)->capacity - (b)->len)
25
+
26
+
27
+ // Initializes a natsBuffer using 'data' as the back-end byte array.
28
+ // The length and capacity are set based on the given parameters.
29
+ // Since the 'data' is not owned, and as long as the buffer does not need
30
+ // to be expanded, the byte buffer will not be freed when this natsBuffer
31
+ // is destroyed. Check natsBuf_Expand() for more details.
32
+ //
33
+ // One would use this call to initialize a natsBuffer without the added cost
34
+ // of allocating memory for the natsBuffer structure itself, for instance
35
+ // initializing an natsBuffer on the stack.
36
+ natsStatus
37
+ natsBuf_InitWithBackend(natsBuffer *buf, char *data, int len, int capacity);
38
+
39
+ // Initializes a natsBuffer and creates a byte buffer of 'capacity' bytes.
40
+ // The natsBuffer owns the buffer and will therefore free the memory used
41
+ // when destroyed.
42
+ //
43
+ // One would use this call to initialize a natsBuffer without the added cost
44
+ // of allocating memory for the natsBuffer structure itself, for instance
45
+ // initializing an natsBuffer on the stack.
46
+ natsStatus
47
+ natsBuf_Init(natsBuffer *buf, int capacity);
48
+
49
+ // Creates a new natsBuffer using 'data' as the back-end byte array.
50
+ // The length and capacity are set based on the given parameters.
51
+ // Since the 'data' is not owned, and as long as the buffer does not need
52
+ // to be expanded, the byte buffer will not be freed when this natsBuffer
53
+ // is destroyed. Check natsBuf_Expand() for more details.
54
+ natsStatus
55
+ natsBuf_CreateWithBackend(natsBuffer **newBuf, char *data, int len, int capacity);
56
+
57
+ // Creates a new natsBuffer and creates a byte buffer of 'capacity' bytes.
58
+ // The natsBuffer owns the buffer and will therefore free the memory used
59
+ // when destroyed.
60
+ natsStatus
61
+ natsBuf_Create(natsBuffer **newBuf, int capacity);
62
+
63
+ // Resets the length to zero, and the position to the beginning of the buffer.
64
+ void
65
+ natsBuf_Reset(natsBuffer *buf);
66
+
67
+ // Sets the size of the buffer to 'newPosition' and new data will be appended
68
+ // starting at this position.
69
+ void
70
+ natsBuf_RewindTo(natsBuffer *buf, int newPosition);
71
+
72
+ // Expands 'buf' underlying buffer to the given new size 'newSize'.
73
+ //
74
+ // If 'buf' did not own the underlying buffer, a new buffer is
75
+ // created and data copied over. The original data is now detached.
76
+ // The underlying buffer is now owned by 'buf' and will be freed when
77
+ // the natsBuffer is destroyed.
78
+ //
79
+ // When 'buf' owns the underlying buffer and it is expanded, a memory
80
+ // reallocation of the buffer occurs to satisfy the new size requirement.
81
+ //
82
+ // Note that one should not save the returned value of natsBuf_Data() and
83
+ // use it after any call to natsBuf_Expand/Append/AppendByte() since
84
+ // the memory address for the underlying byte buffer may have changed due
85
+ // to the buffer expansion.
86
+ natsStatus
87
+ natsBuf_Expand(natsBuffer *buf, int newSize);
88
+
89
+ // Appends 'dataLen' bytes from the 'data' byte array to the buffer,
90
+ // potentially expanding the buffer.
91
+ // See natsBuf_Expand for details about natsBuffer not owning the data.
92
+ natsStatus
93
+ natsBuf_Append(natsBuffer *buf, const char* data, int dataLen);
94
+
95
+ // Appends a byte to the buffer, potentially expanding the buffer.
96
+ // See natsBuf_Expand for details about natsBuffer not owning the data.
97
+ natsStatus
98
+ natsBuf_AppendByte(natsBuffer *buf, char b);
99
+
100
+ // Consume data from a buffer, overwriting the 'n' first bytes by the remaining
101
+ // of data in this buffer.
102
+ void
103
+ natsBuf_Consume(natsBuffer *buf, int n);
104
+
105
+ // Reads better when dealing with a buffer that was initialized as opposed to
106
+ // created, but calling natsBuf_Destroy() will do the right thing regardless
107
+ // of how the buffer was created.
108
+ #define natsBuf_Cleanup(b) natsBuf_Destroy((b))
109
+
110
+ // Frees the data if owned (otherwise leaves it untouched) and the structure
111
+ // if the buffer was created with one of the natsBuf_CreateX() function,
112
+ // otherwise simply 'memset' the structure.
113
+ void
114
+ natsBuf_Destroy(natsBuffer *buf);
115
+
116
+ #endif /* BUF_H_ */