[Lablgtk-list] [PATCH] Glib.Io + socket on win32 on Windows

Enrico Tassi enrico.tassi at inria.fr
Tue Dec 10 16:15:51 CET 2013


Hello, while porting CoqIDE to windows I had to write a patch for the
Glib.Io module.  The function to get an Io.channel on windows is not
the same, and it also depends on the nature of the descriptor
(pipe/file v.s. socket).

I had to add the channel_of_descr_socket variant, that under unix is
just an alias for g_io_channel_unix_new, but that on windows calls
g_io_channel_win32_new_socket instead of g_io_channel_win32_new_fd.

I'm attaching my patch, and a small interactive test (one has to netcat
to the printed port and see if the server correctly reacts when some
data is sent).  
According to my tests, if one uses g_io_channel_win32_new_fd on sockets
things go wrong.  Also using add_watch on a pipe (on which one can
call g_io_channel_win32_new_fd) does not work.

Best,
-- 
Enrico Tassi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: io-channels.diff
Type: text/x-diff
Size: 2305 bytes
Desc: not available
URL: <http://lists.ocamlcore.org/pipermail/lablgtk-list/attachments/20131210/701042da/attachment.diff>
-------------- next part --------------

let main () =
  let s = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
  Unix.bind s (Unix.ADDR_INET (Unix.inet_addr_loopback,0));
  Unix.listen s 1;
  let host, port =
    match Unix.getsockname s with
    | Unix.ADDR_INET(host, port) ->
        Unix.string_of_inet_addr host, string_of_int port
    | _ -> assert false in
  prerr_endline ("connect to " ^ host^" "^port);
  let cs, _ = Unix.accept s in
  prerr_endline "nc connected";

  let ch = Glib.Io.channel_of_descr_socket cs in

  let print_cond = function
    | `IN -> "IN"
    | `ERR -> "ERR"
    | `OUT -> "OUT"
    | `PRI -> "PRI"
    | `NVAL -> "NVAL"
    | `HUP -> "HUP"
  in

  let watch conds =
    prerr_endline "something happened";
    List.iter prerr_endline (List.map print_cond conds);
    true
  in

  let _ = Glib.Io.add_watch ~cond:[`IN] ~callback:watch ch in

  GMain.main ();
  exit 0
;;

main ()


More information about the Lablgtk-list mailing list