Class: NanoMsg::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/nanomsg.rb,
ext/init.c

Instance Method Summary collapse

Instance Method Details

#bind(bind) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'ext/init.c', line 119

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; 
}

#closeObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/init.c', line 238

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. 
  WITHOUT_GVL(sock_close_no_gvl, &io, RUBY_UBF_IO, 0);

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

  return Qnil;
}

#connect(connect) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/init.c', line 133

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; 
}

#getsockopt(level, option) ⇒ Object



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
# File 'ext/init.c', line 293

static VALUE 
sock_getsockopt(VALUE self, VALUE level, VALUE option)
{
  int sock = sock_get(self);
  int level_arg = FIX2INT(level); 
  int option_arg = FIX2INT(option);
  int err; 
 
  int out; 
  size_t sz; 

  if (level_arg == NN_SOL_SOCKET) {
    // This list of supported options is not complete yet. However, having
    // a few supported sockets here is more useful than none at all. WIP. 
    // Yes, pr please ;) 
    switch (option_arg) {
      case NN_SNDFD: 
      case NN_RCVFD: 
        // Return will be int, which is a system fd. 
        sz = sizeof(out);
        err = nn_getsockopt(sock, level_arg, option_arg, &out, &sz);
        if (err < 0)
          RAISE_SOCK_ERROR;

        return INT2NUM(out);
      default: 
        // Unsupported option, returning nil. 
        return Qnil;
    }  
  }
  else {
    // And yet more code here. 
    return Qnil; 
  }
}

#recvObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/init.c', line 207

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

  io.sock = sock_get(socket);

  WITHOUT_GVL(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;
}

#send(buffer) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/init.c', line 177

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);

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

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

  return INT2NUM(io.return_code);
}

#setsockopt(level, option, val) ⇒ Object



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
# File 'ext/init.c', line 255

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; 
}