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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/init.c', line 104

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/init.c', line 223

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

#connect(connect) ⇒ Object



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

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

#recvObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/init.c', line 192

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

#send(buffer) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/init.c', line 162

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

#setsockopt(level, option, val) ⇒ Object



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

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