Source

nanomsg / ext / init.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

#include "ruby/ruby.h"

#include <nanomsg/nn.h>
#include <nanomsg/reqrep.h>
#include <nanomsg/pair.h>
#include <nanomsg/pubsub.h>
#include <nanomsg/survey.h>
#include <nanomsg/pipeline.h>
#include <nanomsg/bus.h>

#include "constants.h"

static VALUE mNanoMsg;
static VALUE cSocket; 

static VALUE cPairSocket; 

static VALUE cReqSocket;
static VALUE cRepSocket;

static VALUE cPubSocket;
static VALUE cSubSocket;

static VALUE cSurveySocket;
static VALUE cRespondSocket;

static VALUE cPushSocket;
static VALUE cPullSocket;

static VALUE cBusSocket;

static VALUE ceSocketError;

struct nmsg_socket {
  int socket; 
};


static void
sock_mark(void *ptr)
{
  // struct nmsg_socket *sock = ptr;
}

static void
sock_free(void *ptr)
{
  struct nmsg_socket *sock = ptr;
  xfree(sock);
}

static size_t
sock_memsize(const void *ptr)
{
  return ptr ? sizeof(struct nmsg_socket) : 0;
}

static const rb_data_type_t socket_data_type = {
  "nanomsg_socket",
  {sock_mark, sock_free, sock_memsize,},
};

static struct nmsg_socket*
sock_get_ptr(VALUE socket) 
{
  struct nmsg_socket *psock; 
  TypedData_Get_Struct(socket, struct nmsg_socket, &socket_data_type, psock); 

  return psock; 
}

static int 
sock_get(VALUE socket)
{
  struct nmsg_socket *psock = sock_get_ptr(socket);
  return psock->socket;
}

static int
nn_get_rcv_fd(int sock)
{
  int fd; 
  size_t size = sizeof(fd);
  int err; 

  err = nn_getsockopt(sock, NN_SOL_SOCKET, NN_RCVFD, &fd, &size);
  if (err < 0)
    return err; 

  return fd; 
}

static VALUE
sock_alloc(VALUE klass) 
{
  VALUE obj;
  struct nmsg_socket *psock;

  obj = TypedData_Make_Struct(klass, struct nmsg_socket, &socket_data_type, psock);
  return obj;
}

static void
sock_raise_error(int nn_errno)
{
  VALUE error = errno_lookup(nn_errno);

  if (error != Qnil) {
    VALUE exc = rb_class_new_instance(0, NULL, error);
    rb_exc_raise(exc);
  } 

  rb_raise(ceSocketError, "General failure, no such error code %d found.", nn_errno);
}
#define RAISE_SOCK_ERROR { sock_raise_error(nn_errno()); }

static VALUE
sock_bind(VALUE socket, VALUE bind)
{
  int sock = sock_get(socket);
  int endpoint; 

  endpoint = nn_bind(sock, StringValueCStr(bind));
  if (endpoint < 0) 
    RAISE_SOCK_ERROR; 

  // TODO do something with the endpoint, returning it in a class for example. 
  return Qnil; 
}

static VALUE
sock_connect(VALUE socket, VALUE connect)
{
  int sock = sock_get(socket);
  int endpoint; 

  endpoint = nn_connect(sock, StringValueCStr(connect));
  if (endpoint < 0) 
    RAISE_SOCK_ERROR; 

  // TODO do something with the endpoint, returning it in a class for example. 
  return Qnil; 
}

static void
sock_init(VALUE socket, int domain, int protocol)
{
  struct nmsg_socket *psock = sock_get_ptr(socket); 

  psock->socket = nn_socket(domain, protocol);
  if (psock->socket < 0)
    RAISE_SOCK_ERROR;
}

struct ioop {
  int nn_errno; 
  int return_code; 
  int sock; 
  char* buffer; 
  long len; 
};

static VALUE
sock_send_blocking(void* data)
{
  struct ioop *pio = data; 

  pio->return_code = nn_send(pio->sock, pio->buffer, pio->len, 0 /* flags */);
  if (pio->return_code < 0)
    pio->nn_errno = nn_errno(); 

  return Qnil;   
}

static VALUE
sock_send(VALUE socket, VALUE buffer)
{
  struct ioop io; 

  io.sock = sock_get(socket);
  io.buffer = StringValuePtr(buffer);
  io.len = RSTRING_LEN(buffer);

  rb_thread_blocking_region(sock_send_blocking, &io, RUBY_UBF_IO, 0);

  if (io.return_code < 0)
    sock_raise_error(io.nn_errno);

  return INT2NUM(io.return_code);
}

static VALUE
sock_recv_blocking(void* data)
{
  struct ioop *pio = data; 

  pio->return_code = nn_recv(pio->sock, &pio->buffer, NN_MSG, 0 /* flags */);

  if (pio->return_code < 0)
    pio->nn_errno = nn_errno(); 

  return Qnil; 
}

static VALUE
sock_recv(VALUE socket)
{
  VALUE result; 
  struct ioop io; 

  io.sock = sock_get(socket);

  rb_thread_blocking_region(sock_recv_blocking, &io, RUBY_UBF_IO, 0);

  if (io.return_code < 0)
    sock_raise_error(io.nn_errno);

  result = rb_str_new(io.buffer, io.return_code);
  nn_freemsg(io.buffer); io.buffer = (char*) 0;

  return result;
}

static VALUE
sock_close_no_gvl(void* data)
{
  struct ioop *pio = (struct ioop*) data; 

  pio->return_code = nn_close(pio->sock);
  if (pio->return_code < 0)
    pio->nn_errno = nn_errno();

  return Qnil;
}

static VALUE
sock_close(VALUE socket)
{
  struct ioop io; 

  io.sock = sock_get(socket);

  // I've no idea on how to abort a close (which may block for NN_LINGER 
  // seconds), so we'll be uninterruptible. 
  rb_thread_blocking_region(sock_close_no_gvl, &io, RUBY_UBF_IO, 0);

  if (io.return_code < 0)
    sock_raise_error(io.nn_errno);

  return Qnil;
}

static VALUE
sock_setsockopt(VALUE self, VALUE level, VALUE option, VALUE val)
{
  int sock = sock_get(self);
  int level_arg = FIX2INT(level); 
  int option_arg = FIX2INT(option);
  int err; 
  int i; 
  void* v; 
  size_t vlen = 0;

  switch (TYPE(val)) {
    case T_FIXNUM:
      i = FIX2INT(val);
      goto numval;
    case T_FALSE:
      i = 0;
      goto numval;
    case T_TRUE:
      i = 1;
    numval: 
      v = (void*)&i; vlen = sizeof(i);
      break;
    
    default:
      StringValue(val);
      v = RSTRING_PTR(val);
      vlen = RSTRING_LEN(val);
      break;
  }

  err = nn_setsockopt(sock, level_arg, option_arg, v, vlen);
  if (err < 0)
    RAISE_SOCK_ERROR; 

  return self; 
}

#define SOCK_INIT_FUNC(name, type) \
static VALUE \
name(int argc, VALUE *argv, VALUE self) \
{ \
  VALUE domain; \
  \
  rb_scan_args(argc, argv, "01", &domain); \
  if (NIL_P(domain)) \
    sock_init(self, AF_SP, (type)); \
  else \
    sock_init(self, FIX2INT(domain), (type)); \
  \
  return self; \
}

SOCK_INIT_FUNC(pair_sock_init, NN_PAIR);
SOCK_INIT_FUNC(req_sock_init, NN_REQ);
SOCK_INIT_FUNC(rep_sock_init, NN_REP);
SOCK_INIT_FUNC(pub_sock_init, NN_PUB);
SOCK_INIT_FUNC(sub_sock_init, NN_SUB);
SOCK_INIT_FUNC(srvy_sock_init, NN_SURVEYOR);
SOCK_INIT_FUNC(resp_sock_init, NN_RESPONDENT);
SOCK_INIT_FUNC(push_sock_init, NN_PUSH);
SOCK_INIT_FUNC(pull_sock_init, NN_PULL);
SOCK_INIT_FUNC(bus_sock_init, NN_BUS);

static VALUE
sub_sock_subscribe(VALUE socket, VALUE channel)
{
  int sock = sock_get(socket);
  int err;

  err = nn_setsockopt(
    sock, NN_SUB, NN_SUB_SUBSCRIBE,
    StringValuePtr(channel),
    RSTRING_LEN(channel)
  );
  if (err < 0)
    RAISE_SOCK_ERROR;

  return socket;
}

static VALUE
sub_sock_unsubscribe(VALUE socket, VALUE channel)
{
  int sock = sock_get(socket);
  int err;

  err = nn_setsockopt(
    sock, NN_SUB, NN_SUB_UNSUBSCRIBE,
    StringValuePtr(channel),
    RSTRING_LEN(channel)
  );
  if (err < 0)
    RAISE_SOCK_ERROR;

  return socket;
}

static VALUE
srvy_set_deadline(VALUE self, VALUE deadline)
{
  int sock = sock_get(self);
  VALUE miliseconds = rb_funcall(deadline, rb_intern("*"), 1, INT2NUM(1000));
  int timeout = FIX2INT(miliseconds);
  int err; 

  err = nn_setsockopt(sock, NN_SURVEYOR, NN_SURVEYOR_DEADLINE, &timeout, sizeof(int));
  if (err < 0)
    RAISE_SOCK_ERROR;

  return deadline; 
}

static VALUE
srvy_get_deadline(VALUE self)
{
  int sock = sock_get(self);
  int deadline; 
  size_t size = sizeof(int);

  int err; 

  err = nn_getsockopt(sock, NN_SURVEYOR, NN_SURVEYOR_DEADLINE, &deadline, &size);
  if (err < 0)
    RAISE_SOCK_ERROR; 

  return rb_funcall(INT2NUM(deadline), rb_intern("/"), 1, rb_float_new(1000));  
}

static VALUE
nanomsg_terminate(VALUE self)
{
  nn_term(); 

  return Qnil; 
}

struct device_op {
  int sa, sb; 
  int err; 
};

static VALUE
nanomsg_run_device_no_gvl(void* data)
{
  struct device_op *pop = (struct device_op*) data;

  pop->err = nn_device(pop->sa, pop->sb);

  return Qnil; 
}

static VALUE
nanomsg_run_device(VALUE self, VALUE a, VALUE b)
{
  struct device_op dop; 

  dop.sa = sock_get(a);
  dop.sb = sock_get(b);

  rb_thread_blocking_region(nanomsg_run_device_no_gvl, &dop, RUBY_UBF_IO, 0);
  if (dop.err < 0)
    RAISE_SOCK_ERROR;

  return Qnil; 
}

static VALUE
nanomsg_run_loopback(VALUE self, VALUE a)
{
  struct device_op dop; 

  dop.sa = sock_get(a);
  dop.sb = -1;          // invalid socket, see documentation

  rb_thread_blocking_region(nanomsg_run_device_no_gvl, &dop, RUBY_UBF_IO, 0);
  if (dop.err < 0)
    RAISE_SOCK_ERROR;

  return Qnil; 
}

void
Init_nanomsg(void)
{
  mNanoMsg = rb_define_module("NanoMsg"); 
  cSocket = rb_define_class_under(mNanoMsg, "Socket", rb_cObject);
  cPairSocket = rb_define_class_under(mNanoMsg, "PairSocket", cSocket);
  
  cReqSocket = rb_define_class_under(mNanoMsg, "ReqSocket", cSocket);
  cRepSocket = rb_define_class_under(mNanoMsg, "RepSocket", cSocket);
  
  cPubSocket = rb_define_class_under(mNanoMsg, "PubSocket", cSocket);
  cSubSocket = rb_define_class_under(mNanoMsg, "SubSocket", cSocket);

  cSurveySocket = rb_define_class_under(mNanoMsg, "SurveySocket", cSocket);
  cRespondSocket = rb_define_class_under(mNanoMsg, "RespondSocket", cSocket);

  cPushSocket = rb_define_class_under(mNanoMsg, "PushSocket", cSocket);
  cPullSocket = rb_define_class_under(mNanoMsg, "PullSocket", cSocket);

  cBusSocket = rb_define_class_under(mNanoMsg, "BusSocket", cSocket);

  ceSocketError = rb_define_class_under(mNanoMsg, "SocketError", rb_eIOError);

  rb_define_singleton_method(mNanoMsg, "terminate", nanomsg_terminate, 0);
  rb_define_singleton_method(mNanoMsg, "run_device", nanomsg_run_device, 2);
  rb_define_singleton_method(mNanoMsg, "run_loopback", nanomsg_run_loopback, 1);

  rb_define_alloc_func(cSocket, sock_alloc);
  rb_define_method(cSocket, "bind", sock_bind, 1);
  rb_define_method(cSocket, "connect", sock_connect, 1);
  rb_define_method(cSocket, "send", sock_send, 1);
  rb_define_method(cSocket, "recv", sock_recv, 0);
  rb_define_method(cSocket, "close", sock_close, 0);
  rb_define_method(cSocket, "setsockopt", sock_setsockopt, 3);

  rb_define_method(cPairSocket, "initialize", pair_sock_init, -1);

  rb_define_method(cReqSocket, "initialize", req_sock_init, -1);
  rb_define_method(cRepSocket, "initialize", rep_sock_init, -1);

  rb_define_method(cPubSocket, "initialize", pub_sock_init, -1);
  rb_define_method(cSubSocket, "initialize", sub_sock_init, -1);
  rb_define_method(cSubSocket, "subscribe", sub_sock_subscribe, 1);
  rb_define_method(cSubSocket, "unsubscribe", sub_sock_unsubscribe, 1);

  rb_define_method(cSurveySocket, "initialize", srvy_sock_init, -1);
  rb_define_method(cSurveySocket, "deadline=", srvy_set_deadline, 1);
  rb_define_method(cSurveySocket, "deadline", srvy_get_deadline, 0);
  rb_define_method(cRespondSocket, "initialize", resp_sock_init, -1);

  rb_define_method(cPushSocket, "initialize", push_sock_init, -1);
  rb_define_method(cPullSocket, "initialize", pull_sock_init, -1);

  rb_define_method(cBusSocket, "initialize", bus_sock_init, -1);

  Init_constants(mNanoMsg);
}