Commits

Dmitry Grebeniuk  committed a8d3e3a

+ shutdown + flush_socket + close_socket

  • Participants
  • Parent commits 02a046b

Comments (0)

Files changed (1)

 value write_fd fd str = write_fd_loop fd str 0 (String.length str)
 ;
 
-value shutdown = Lwt_unix.shutdown
+value shutdown fd how =
+  try return (Lwt_unix.shutdown fd how)
+  with [ e -> error e ]
 ;
 
+(* based on http://stackoverflow.com/questions/12730477/
+            close-is-not-closing-socket-properly/12730776#12730776 *)
+value flush_socket =
+  let bufsz = 4096 in
+  let buf = String.make bufsz '\x00' in
+  fun fd ->
+    shutdown fd Unix.SHUTDOWN_SEND >>= fun () ->
+    loop ()
+    where rec loop () =
+      read_fd_into fd buf 0 bufsz >>= fun was_read ->
+      if was_read = 0
+      then return ()
+      else loop ()
+;
+
+value close_socket fd =
+  let () = ignore (Lwt_unix.getsockopt_error fd) in
+  shutdown fd Unix.SHUTDOWN_ALL >>= fun () ->
+  close_fd fd
+;
+
+
+
+
 
 (********************************************************)