Class: NanoMsg::Socket

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

Direct Known Subclasses

PairSocket

Instance Method Summary collapse

Instance Method Details

#bind(bind) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'ext/init.c', line 109

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

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

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

#connect(connect) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/init.c', line 123

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

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

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

#recvObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'ext/init.c', line 214

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

  io.sock = sock_get(socket);
  io.buffer = (char*) 0; 
  io.abort  = Qfalse; 
  io.last_code = EAGAIN;

  rb_thread_blocking_region(sock_recv_no_gvl, &io, sock_recv_abort, &io); 

  if (io.abort) 
    return Qnil; 

  if (io.last_code < 0)
    sock_raise_error(io.last_code);

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

  return result;
}

#send(buffer) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/init.c', line 167

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

  io.sock = sock_get(socket);
  io.last_code = EAGAIN; 
  io.buffer = StringValuePtr(buffer);
  io.len = RSTRING_LEN(buffer);
  io.abort = Qfalse; 

  rb_thread_blocking_region(sock_send_no_gvl, &io, sock_send_abort, &io);

  // Unclear what to do in this situation, but we'll simply return nil, 
  // leaving Ruby to handle the abort. 
  if (io.abort) 
    return Qnil; 

  if (io.last_code < 0)
    sock_raise_error(io.last_code);

  return INT2NUM(io.last_code);
}