C socket to Ruby socket

A

Andre Nathan

Hello

What is the correct way to convert a C socket (int) to a Ruby Socket
object in a C extension? Something like

int s = socket(...);
VALUE sock = rb_some_func(s);

In my extension there's a callback function that receives a socket as an
argument, and I'd like to pass that socket as an argument to a ruby
block, so I need to convert it to a Socket object first.

Thanks in advance,
Andre
 
F

Francis Cianfrocca

Hello

What is the correct way to convert a C socket (int) to a Ruby Socket
object in a C extension? Something like

int s = socket(...);
VALUE sock = rb_some_func(s);

In my extension there's a callback function that receives a socket as an
argument, and I'd like to pass that socket as an argument to a ruby
block, so I need to convert it to a Socket object first.

Thanks in advance,
Andre

does it need to be a Socket? class IO can be initialized with a file descriptor.
 
A

Andre Nathan

Hi!

You probably can call Socket.for_fd using rb_funcall.

I got it to work with an IO:

rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2NUM(socket));

However I can't seem to be able to make rb_cSocket visible to my
extension. Any hints on how to do that?

Thanks,
Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top