[plt-scheme] FFI - C file handles -> scheme ports

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Jan 14 16:40:00 EST 2007

At Sun, 14 Jan 2007 16:12:42 -0500, Eli Barzilay wrote:
> On Jan 14, Hans Oesterholt-Dijkema wrote:
> > Eli Barzilay schreef:
> > > On Jan 14, Hans Oesterholt-Dijkema wrote:
> > >   
> > >> Eli Barzilay schreef:
> > >>     
> > >>> On Jan 14, Hans Oesterholt-Dijkema wrote:
> > >>>   
> > >>>       
> > >>>> Dear All,
> > >>>>
> > >>>> Is it possible to reuse, or encapsulate C file handles (actually
> > >>>> sockets) in a scheme port, using the FFI?
> > >>>>     
> > >>>>         
> > >>> I don't think so.  MzScheme works on a lower level than FILE*.  The
> > >>> best way I can think of is to create a custom input or output port
> > >>> that uses the C level for its IO.
> > >>>
> > >>>       
> > >> Wouldn't a call to "select" on the C level block mzscheme entirely?
> > >>     
> > >
> > > Yes, you'll need to avoid any blocking C calls.
> > 
> > Isn't there something like
> > 
> > ¤ Scheme_Object *scheme_make_fd_input_port(int |fd|, Scheme_Object *|name|,
> >     int |regfile|, int |win_textmode|)
> > 
> > ?
> > 
> > To use?
> 
> Yes you can use that -- but that requires a file descriptor.  By "file
> handles" I thought that you meant a FILE* pointer.  (Or maybe you're
> asking how to use that function from Scheme?)

If I understand, then the question is Windows-specific.

Under Unix, a socket is a file descriptor, and
scheme_make_fd_input_port() is ok. Under Windows,
scheme_make_fd_input_port() actually works with file handles, which
have the C type HANDLE, and it does not work with Windows sockets.

So, I think the question is whether there's anything exported by
MzScheme's C API that is like scheme_make_fd_input_port() but for
Windows sockets.

Internally, there is such a function. It's not currently exposed in the
C API. Would it be useful for me to change MzScheme so that it's
exposed?


Another approach is to just create ports in Scheme using
`make-input-port' and `make-output-port', and implement the various
callbacks by using the FFI to access recv(), etc.

The hardest part of this strategy is implementing the evt to indicate
when data is available or when data can be written. To do that, you
need to create a new type of evt via scheme_add_evt(), where the wakeup
function uses scheme_fdset() (which, confusingly enough, works on
sockets and not file handles under Windows).

Matthew



Posted on the users mailing list.