agoo 2.0.5 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of agoo might be problematic. Click here for more details.

@@ -7,7 +7,33 @@
7
7
 
8
8
  #include <ruby.h>
9
9
 
10
+ struct _Con;
11
+ struct _Subject;
12
+
13
+ typedef struct _Upgraded {
14
+ struct _Upgraded *next;
15
+ struct _Upgraded *prev;
16
+ struct _Con *con;
17
+ VALUE handler;
18
+ VALUE wrap;
19
+ atomic_int pending;
20
+ atomic_int ref_cnt;
21
+ struct _Subject *subjects;
22
+ bool on_empty;
23
+ bool on_close;
24
+ bool on_shut;
25
+ bool on_msg;
26
+ } *Upgraded;
27
+
10
28
  extern void upgraded_init(VALUE mod);
11
- extern void upgraded_extend(uint64_t cid, VALUE obj);
29
+ extern Upgraded upgraded_create(struct _Con *c, VALUE obj);
30
+ extern void upgraded_release(Upgraded up);
31
+ extern void upgraded_release_con(Upgraded up);
32
+
33
+ extern void upgraded_ref(Upgraded up);
34
+
35
+ extern void upgraded_add_subject(Upgraded up, struct _Subject *subject);
36
+ extern void upgraded_del_subject(Upgraded up, struct _Subject *subject);
37
+ extern bool upgraded_match(Upgraded up, const char *subject);
12
38
 
13
39
  #endif // __AGOO_UPGRADED_H__
@@ -167,7 +167,7 @@ ws_create_req(Con c, long mlen) {
167
167
  log_cat(&error_cat, "Out of memory attempting to allocate request.");
168
168
  return true;
169
169
  }
170
- if (NULL == c->slot || Qnil == c->slot->handler) {
170
+ if (NULL == c->up || Qnil == c->up->handler) {
171
171
  return true;
172
172
  }
173
173
  memset(c->req, 0, sizeof(struct _Req));
@@ -183,11 +183,11 @@ ws_create_req(Con c, long mlen) {
183
183
  c->req->mlen = mlen;
184
184
  c->req->method = (WS_OP_BIN == op) ? ON_BIN : ON_MSG;
185
185
  c->req->upgrade = UP_NONE;
186
- c->req->cid = c->id;
186
+ c->req->up = c->up;
187
187
  c->req->res = NULL;
188
188
  c->req->handler_type = PUSH_HOOK;
189
- if (c->slot->on_msg) {
190
- c->req->handler = c->slot->handler;
189
+ if (c->up->on_msg) {
190
+ c->req->handler = c->up->handler;
191
191
  } else {
192
192
  c->req->handler = Qnil;
193
193
  }
@@ -196,13 +196,14 @@ ws_create_req(Con c, long mlen) {
196
196
 
197
197
  void
198
198
  ws_req_close(Con c) {
199
- if (NULL != c->slot && Qnil != c->slot->handler && c->slot->on_close) {
199
+ if (NULL != c->up && Qnil != c->up->handler && c->up->on_close) {
200
200
  Req req = request_create(0);
201
201
 
202
- req->cid = c->id;
202
+ req->up = c->up;
203
203
  req->method = ON_CLOSE;
204
204
  req->handler_type = PUSH_HOOK;
205
- req->handler = c->slot->handler;
205
+ req->handler = c->up->handler;
206
+ atomic_fetch_add(&c->up->ref_cnt, 1);
206
207
  queue_push(&the_server.eval_queue, (void*)req);
207
208
  }
208
209
  }
@@ -211,7 +212,7 @@ void
211
212
  ws_ping(Con c) {
212
213
  Res res;
213
214
 
214
- if (NULL == (res = res_create())) {
215
+ if (NULL == (res = res_create(c))) {
215
216
  log_cat(&error_cat, "Memory allocation of response failed on connection %llu.", c->id);
216
217
  } else {
217
218
  DEBUG_ALLOC(mem_res, res)
@@ -231,7 +232,7 @@ void
231
232
  ws_pong(Con c) {
232
233
  Res res;
233
234
 
234
- if (NULL == (res = res_create())) {
235
+ if (NULL == (res = res_create(c))) {
235
236
  log_cat(&error_cat, "Memory allocation of response failed on connection %llu.", c->id);
236
237
  } else {
237
238
  DEBUG_ALLOC(mem_res, res)
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Agoo
3
3
  # Agoo version.
4
- VERSION = '2.0.5'
4
+ VERSION = '2.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-06 00:00:00.000000000 Z
11
+ date: 2018-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -48,8 +48,6 @@ files:
48
48
  - ext/agoo/agoo.c
49
49
  - ext/agoo/base64.c
50
50
  - ext/agoo/base64.h
51
- - ext/agoo/ccache.c
52
- - ext/agoo/ccache.h
53
51
  - ext/agoo/con.c
54
52
  - ext/agoo/con.h
55
53
  - ext/agoo/debug.c
@@ -90,8 +88,8 @@ files:
90
88
  - ext/agoo/sse.h
91
89
  - ext/agoo/sub.c
92
90
  - ext/agoo/sub.h
93
- - ext/agoo/subscription.c
94
- - ext/agoo/subscription.h
91
+ - ext/agoo/subject.c
92
+ - ext/agoo/subject.h
95
93
  - ext/agoo/text.c
96
94
  - ext/agoo/text.h
97
95
  - ext/agoo/types.h
@@ -1,301 +0,0 @@
1
- // Copyright (c) 2018, Peter Ohler, All rights reserved.
2
-
3
- #include <stdlib.h>
4
- #include <string.h>
5
- #include <stdint.h>
6
- #include <stdio.h>
7
-
8
- #include "ccache.h"
9
- #include "con.h"
10
- #include "debug.h"
11
-
12
- static void
13
- cc_mark(void *ptr) {
14
- if (NULL != ptr) {
15
- CCache cc = (CCache)ptr;
16
- CSlot slot;
17
- CSlot *bp = cc->buckets;
18
- CSlot *end = bp + CC_BUCKET_SIZE;
19
-
20
- if (Qnil != cc->wrap) {
21
- rb_gc_mark(cc->wrap);
22
- }
23
- pthread_mutex_lock(&cc->lock);
24
- for (bp = cc->buckets; bp < end; bp++) {
25
- for (slot = *bp; NULL != slot; slot = slot->next) {
26
- if (Qnil != slot->handler) {
27
- rb_gc_mark(slot->handler);
28
- }
29
- }
30
- }
31
- pthread_mutex_unlock(&cc->lock);
32
- }
33
- }
34
-
35
-
36
- void
37
- cc_init(CCache cc) {
38
- memset(cc, 0, sizeof(struct _CCache));
39
- pthread_mutex_init(&cc->lock, 0);
40
- cc->wrap = Data_Wrap_Struct(rb_cObject, cc_mark, NULL, cc);
41
- }
42
-
43
- void
44
- cc_cleanup(CCache cc) {
45
- CSlot *sp = cc->buckets;
46
- CSlot s;
47
- CSlot n;
48
- int i;
49
-
50
- for (i = CC_BUCKET_SIZE; 0 < i; i--, sp++) {
51
- for (s = *sp; NULL != s; s = n) {
52
- n = s->next;
53
- DEBUG_FREE(mem_cslot, s);
54
- free(s);
55
- }
56
- *sp = NULL;
57
- }
58
- }
59
-
60
- CSlot
61
- cc_set_con(CCache cc, Con con) {
62
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & con->id);
63
- CSlot slot;
64
-
65
- pthread_mutex_lock(&cc->lock);
66
- for (slot = *bucket; NULL != slot; slot = slot->next) {
67
- if (con->id == slot->cid) {
68
- slot->con = con;
69
- break;
70
- }
71
- }
72
- if (NULL == slot) {
73
- if (NULL != (slot = (CSlot)malloc(sizeof(struct _CSlot)))) {
74
- DEBUG_ALLOC(mem_cslot, slot);
75
- slot->cid = con->id;
76
- slot->con = con;
77
- slot->handler = Qnil;
78
- slot->on_empty = false;
79
- slot->on_close = false;
80
- slot->on_shut = false;
81
- slot->on_msg = false;
82
- atomic_init(&slot->pending, 0);
83
- atomic_init(&slot->ref_cnt, 1);
84
- slot->next = *bucket;
85
- *bucket = slot;
86
- }
87
- }
88
- pthread_mutex_unlock(&cc->lock);
89
-
90
- return slot;
91
- }
92
-
93
- void
94
- cc_set_handler(CCache cc, uint64_t cid, VALUE handler, bool on_empty, bool on_close, bool on_shut, bool on_msg) {
95
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
96
- CSlot slot;
97
-
98
- pthread_mutex_lock(&cc->lock);
99
- for (slot = *bucket; NULL != slot; slot = slot->next) {
100
- if (cid == slot->cid) {
101
- slot->handler = handler;
102
- break;
103
- }
104
- }
105
- if (NULL == slot) {
106
- // TBD is is faster to lock twice or allocate while locked?
107
- if (NULL != (slot = (CSlot)malloc(sizeof(struct _CSlot)))) {
108
- DEBUG_ALLOC(mem_cslot, slot);
109
- slot->cid = cid;
110
- slot->con = NULL;
111
- slot->handler = handler;
112
- slot->on_empty = on_empty;
113
- slot->on_close = on_close;
114
- slot->on_shut = on_shut;
115
- slot->on_msg = on_msg;
116
- atomic_init(&slot->pending, 0);
117
- atomic_init(&slot->ref_cnt, 1);
118
- slot->next = *bucket;
119
- slot->on_empty = on_empty;
120
- slot->on_close = on_close;
121
- slot->on_shut = on_shut;
122
- slot->on_msg = on_msg;
123
- *bucket = slot;
124
- }
125
- }
126
- pthread_mutex_unlock(&cc->lock);
127
- }
128
-
129
- void
130
- cc_remove(CCache cc, uint64_t cid) {
131
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
132
- CSlot slot;
133
- CSlot prev = NULL;
134
-
135
- pthread_mutex_lock(&cc->lock);
136
- for (slot = *bucket; NULL != slot; slot = slot->next) {
137
- if (cid == slot->cid) {
138
- if (NULL == prev) {
139
- *bucket = slot->next;
140
- } else {
141
- prev->next = slot->next;
142
- }
143
- break;
144
- }
145
- prev = slot;
146
- }
147
- pthread_mutex_unlock(&cc->lock);
148
- if (NULL != slot) {
149
- DEBUG_FREE(mem_cslot, slot)
150
- free(slot);
151
- }
152
- }
153
-
154
- void
155
- cc_remove_con(CCache cc, uint64_t cid) {
156
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
157
- CSlot slot;
158
- CSlot prev = NULL;
159
-
160
- pthread_mutex_lock(&cc->lock);
161
- for (slot = *bucket; NULL != slot; slot = slot->next) {
162
- if (cid == slot->cid) {
163
- if (atomic_fetch_sub(&slot->ref_cnt, 1) <= 1) {
164
- if (NULL == prev) {
165
- *bucket = slot->next;
166
- } else {
167
- prev->next = slot->next;
168
- }
169
- } else {
170
- atomic_store(&slot->pending, -1);
171
- slot = NULL;
172
- }
173
- break;
174
- }
175
- prev = slot;
176
- }
177
- pthread_mutex_unlock(&cc->lock);
178
- if (NULL != slot) {
179
- DEBUG_FREE(mem_cslot, slot)
180
- free(slot);
181
- }
182
- }
183
-
184
- VALUE
185
- cc_ref_dec(CCache cc, uint64_t cid) {
186
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
187
- CSlot slot;
188
- VALUE handler = Qnil;
189
- CSlot prev = NULL;
190
-
191
- pthread_mutex_lock(&cc->lock);
192
- for (slot = *bucket; NULL != slot; slot = slot->next) {
193
- if (cid == slot->cid) {
194
- int rcnt = atomic_fetch_sub(&slot->ref_cnt, 1);
195
-
196
- handler = slot->handler;
197
- if (rcnt <= 1) {
198
- if (NULL == prev) {
199
- *bucket = slot->next;
200
- } else {
201
- prev->next = slot->next;
202
- }
203
- } else {
204
- slot = NULL;
205
- }
206
- break;
207
- }
208
- prev = slot;
209
- }
210
- pthread_mutex_unlock(&cc->lock);
211
- if (NULL != slot) {
212
- DEBUG_FREE(mem_cslot, slot)
213
- free(slot);
214
- }
215
- return handler;
216
- }
217
-
218
- Con
219
- cc_get_con(CCache cc, uint64_t cid) {
220
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
221
- CSlot slot;
222
- Con con = NULL;
223
-
224
- pthread_mutex_lock(&cc->lock);
225
- for (slot = *bucket; NULL != slot; slot = slot->next) {
226
- if (cid == slot->cid) {
227
- con = slot->con;
228
- break;
229
- }
230
- }
231
- pthread_mutex_unlock(&cc->lock);
232
-
233
- return con;
234
- }
235
-
236
- VALUE
237
- cc_get_handler(CCache cc, uint64_t cid) {
238
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
239
- CSlot slot;
240
- VALUE handler = Qnil;
241
-
242
- pthread_mutex_lock(&cc->lock);
243
- for (slot = *bucket; NULL != slot; slot = slot->next) {
244
- if (cid == slot->cid) {
245
- handler = slot->handler;
246
- break;
247
- }
248
- }
249
- pthread_mutex_unlock(&cc->lock);
250
-
251
- return handler;
252
- }
253
-
254
- int
255
- cc_get_pending(CCache cc, uint64_t cid) {
256
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
257
- CSlot slot;
258
- int pending = -1;
259
-
260
- pthread_mutex_lock(&cc->lock);
261
- for (slot = *bucket; NULL != slot; slot = slot->next) {
262
- if (cid == slot->cid) {
263
- pending = atomic_load(&slot->pending);
264
- break;
265
- }
266
- }
267
- pthread_mutex_unlock(&cc->lock);
268
-
269
- return pending;
270
- }
271
-
272
- CSlot
273
- cc_get_slot(CCache cc, uint64_t cid) {
274
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
275
- CSlot slot;
276
-
277
- pthread_mutex_lock(&cc->lock);
278
- for (slot = *bucket; NULL != slot; slot = slot->next) {
279
- if (cid == slot->cid) {
280
- break;
281
- }
282
- }
283
- pthread_mutex_unlock(&cc->lock);
284
-
285
- return slot;
286
- }
287
-
288
- void
289
- cc_pending_inc(CCache cc, uint64_t cid) {
290
- CSlot *bucket = cc->buckets + (CC_BUCKET_MASK & cid);
291
- CSlot slot;
292
-
293
- pthread_mutex_lock(&cc->lock);
294
- for (slot = *bucket; NULL != slot; slot = slot->next) {
295
- if (cid == slot->cid) {
296
- atomic_fetch_add(&slot->pending, 1);
297
- break;
298
- }
299
- }
300
- pthread_mutex_unlock(&cc->lock);
301
- }
@@ -1,53 +0,0 @@
1
- // Copyright (c) 2018, Peter Ohler, All rights reserved.
2
-
3
- #ifndef __AGOO_CCACHE_H__
4
- #define __AGOO_CCACHE_H__
5
-
6
- #include <pthread.h>
7
- #include <stdatomic.h>
8
- #include <stdbool.h>
9
- #include <stdint.h>
10
-
11
- #include <ruby.h>
12
-
13
- #define CC_BUCKET_SIZE 1024
14
- #define CC_BUCKET_MASK 1023
15
-
16
- struct _Con;
17
-
18
- typedef struct _CSlot {
19
- struct _CSlot *next;
20
- uint64_t cid;
21
- struct _Con *con;
22
- VALUE handler;
23
- atomic_int pending;
24
- atomic_int ref_cnt;
25
- bool on_empty;
26
- bool on_close;
27
- bool on_shut;
28
- bool on_msg;
29
- } *CSlot;
30
-
31
- typedef struct _CCache {
32
- CSlot buckets[CC_BUCKET_SIZE];
33
- pthread_mutex_t lock;
34
- VALUE wrap;
35
- } *CCache;
36
-
37
- extern void cc_init(CCache cc);
38
- extern void cc_cleanup(CCache cc);
39
- extern CSlot cc_set_con(CCache cc, struct _Con *con);
40
- extern void cc_set_handler(CCache cc, uint64_t cid, VALUE handler, bool on_empty, bool on_close, bool on_shut, bool on_msg);
41
- extern void cc_remove(CCache cc, uint64_t cid);
42
-
43
- extern void cc_remove_con(CCache cc, uint64_t cid);
44
- extern VALUE cc_ref_dec(CCache cc, uint64_t cid);
45
-
46
- extern struct _Con* cc_get_con(CCache cc, uint64_t cid);
47
- extern VALUE cc_get_handler(CCache cc, uint64_t cid);
48
- extern int cc_get_pending(CCache cc, uint64_t cid);
49
- extern CSlot cc_get_slot(CCache cc, uint64_t cid);
50
-
51
- extern void cc_pending_inc(CCache cc, uint64_t cid);
52
-
53
- #endif /* __AGOO_CCACHE_H__ */