Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (31 sloc) 901 Bytes
open Core.Std
open Async.Std
(* Copy data from the reader to the writer, using the provided buffer
as scratch space *)
let rec copy_blocks buffer r w =
Reader.read r buffer
>>= function
| `Eof -> return ()
| `Ok bytes_read ->
Writer.write w buffer ~len:bytes_read;
Writer.flushed w
>>= fun () ->
copy_blocks buffer r w
(* part 1 *)
(** Starts a TCP server, which listens on the specified port, invoking
copy_blocks every time a client connects. *)
let run () =
let host_and_port =
Tcp.Server.create
~on_handler_error:`Raise
(Tcp.on_port 8765)
(fun _addr r w ->
let buffer = String.create (16 * 1024) in
copy_blocks buffer r w)
in
ignore (host_and_port : (Socket.Address.Inet.t, int) Tcp.Server.t Deferred.t)
(* part 2 *)
(* Call [run], and then start the scheduler *)
let () =
run ();
never_returns (Scheduler.go ())