Skip to content
Browse files

Move buffering back out of the Irc_transport module

Signed-off-by: John Else <john.else@citrix.com>
  • Loading branch information...
1 parent 6e7b15c commit c3145e6ba9c2f4ffb12c8bc293e004b7087bd75d @johnelse committed Apr 3, 2013
Showing with 18 additions and 28 deletions.
  1. +12 −4 lib/irc_client.ml
  2. +2 −2 lib/irc_transport.ml
  3. +2 −11 lwt/irc_client_lwt.ml
  4. +2 −11 unix/irc_client_unix.ml
View
16 lib/irc_client.ml
@@ -3,10 +3,20 @@ module Make(Io: Irc_transport.IO) = struct
sock: Io.file_descr;
}
+ open Io
+
+ let rec really_write ~connection ~data ~offset ~length =
+ if length = 0 then return () else
+ Io.write connection.sock data offset length
+ >>= (fun chars_written ->
+ really_write ~connection ~data
+ ~offset:(offset + chars_written)
+ ~length:(length - chars_written))
+
let send_raw ~connection ~data =
let formatted_data = Printf.sprintf "%s\r\n" data in
- let len = String.length formatted_data in
- Io.buffered_write connection.sock formatted_data 0 len
+ let length = String.length formatted_data in
+ really_write ~connection ~data:formatted_data ~offset:0 ~length
let send_join ~connection ~channel =
send_raw ~connection ~data:(Printf.sprintf "JOIN %s" channel)
@@ -30,8 +40,6 @@ module Make(Io: Irc_transport.IO) = struct
send_raw ~connection
~data:(Printf.sprintf "USER %s %i * :%s" username mode realname)
- open Io
-
let connect ~server ~port ~username ~mode ~realname ~nick ~password =
Io.open_socket server port >>= (fun sock ->
let connection = {sock = sock} in
View
4 lib/irc_transport.ml
@@ -8,6 +8,6 @@ module type IO = sig
val open_socket : string -> int -> file_descr t
val close_socket : file_descr -> unit t
- val buffered_read : file_descr -> string -> int -> int -> unit t
- val buffered_write : file_descr -> string -> int -> int -> unit t
+ val read : file_descr -> string -> int -> int -> int t
+ val write : file_descr -> string -> int -> int -> int t
end
View
13 lwt/irc_client_lwt.ml
@@ -13,17 +13,8 @@ module Io : Irc_transport.IO = struct
let close_socket = Lwt_unix.close
- let rec buffered_read fd str offset length =
- if length = 0 then return () else
- lwt chars_read = Lwt_unix.read fd str offset length in
- if chars_read = 0
- then Lwt.fail End_of_file
- else buffered_read fd str (offset + chars_read) (length - chars_read)
-
- let rec buffered_write fd str offset length =
- if length = 0 then return () else
- lwt chars_written = Lwt_unix.write fd str offset length in
- buffered_write fd str (offset + chars_written) (length - chars_written)
+ let read = Lwt_unix.read
+ let write = Lwt_unix.write
end
module Client = Irc_client.Make(Io)
View
13 unix/irc_client_unix.ml
@@ -13,17 +13,8 @@ module Io : Irc_transport.IO = struct
let close_socket = Unix.close
- let rec buffered_read fd str offset length =
- if length = 0 then () else
- let chars_read = Unix.read fd str offset length in
- if chars_read = 0
- then raise End_of_file
- else buffered_read fd str (offset + chars_read) (length - chars_read)
-
- let rec buffered_write fd str offset length =
- if length = 0 then () else
- let chars_written = Unix.write fd str offset length in
- buffered_write fd str (offset + chars_written) (length - chars_written)
+ let read = Unix.read
+ let write = Unix.write
end
module Client = Irc_client.Make(Io)

0 comments on commit c3145e6

Please sign in to comment.
Something went wrong with that request. Please try again.