Hello
Is there a way to turn a POSIX filehandle into a Gambit scheme port, or could it be added?
I'm imaging something like: (posix-fd->port 0) => returning an input-output port or (open-port (list posix-fd: 0 direction: 'input)) ;; in case one wants ;; the direction to be preserved. or similar.
This would allow things like implement functions creating unix sockets, or maybe even UDP ports; well for the latter maybe more changes would actually be needed. Maybe I should ask (instead of the above) how one can, from a C function, read/write from/to a file descriptor without blocking the system (using the scheduler, thus possibly running other scheme code in the mean time).
Thanks Christian.
Afficher les réponses par date
PS. maybe simply adding unix socket and UDP socket functionality to the Gambit core would be just as justified. I'm just not sure. (If I'd have to provide a patch, it would also only be for linux/unix, as I don't know how to program Windows.)
Christian.
On 24-Jul-06, at 6:34 PM, Christian wrote:
Hello
Is there a way to turn a POSIX filehandle into a Gambit scheme port, or could it be added?
I'm imaging something like: (posix-fd->port 0) => returning an input-output port or (open-port (list posix-fd: 0 direction: 'input)) ;; in case one wants ;; the direction to be preserved. or similar.
There is an internal procedure for doing this. It is called ##open- predefined and you can use it like this:
(define in 1) (define out 2) (define inout 3) (define stderr (##open-predefined out '(STDERR) 2)) stderr
#<output-port #3 (STDERR)>
(display "hello\n" stderr) (force-output stderr)
hello
The third parameter of ##open-predefined is the file descriptor and the fourth optional parameter is the port settings. Note that this will only work on Unix (Windows does not use file descriptors). The special file descriptors -1, -2, -3 and -4 represent STDIN, STDOUT, STDERR and the console on Unix and Windows.
This would allow things like implement functions creating unix sockets, or maybe even UDP ports; well for the latter maybe more changes would actually be needed.
My working version of Gambit has preliminary support for UDP and RAW sockets.
Maybe I should ask (instead of the above) how one can, from a C function, read/write from/to a file descriptor without blocking the system (using the scheduler, thus possibly running other scheme code in the mean time).
One way it to use ##open-predefined and have the C code call into Scheme. Why do you need this? There is not a general solution that will work in all situations.
Marc
At 8:33 Uhr -0400 25.07.2006, Marc Feeley wrote:
There is an internal procedure for doing this. It is called ##open- predefined and you can use it like this:
Thanks, that works fine.
My working version of Gambit has preliminary support for UDP and RAW sockets.
Perfect. (I may want to use udp for something like (or communication to) memcached, or other things (I've used UDP for monitoring purposes in the past). It's not urgent.)
Maybe I should ask (instead of the above) how one can, from a C function, read/write from/to a file descriptor without blocking the system (using the scheduler, thus possibly running other scheme code in the mean time).
One way it to use ##open-predefined and have the C code call into Scheme. Why do you need this? There is not a general solution that will work in all situations.
One thing I've thought about is libraries like libmysql; but the problem with libmysql is that IIRC one cannot (without patching) make it call other functions for reading/writing, which makes it pretty hopeless probably, leaving only using pthread magic as solution for running multiple mysql connections in parallel in one Gambit. But I've pretty much decided to use forking/multiprocessing instead of multithreading for my web stuff for now, so that's no problem.
Christian.