Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Core
- open Async
- (* 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 (Bytes.to_string 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.Where_to_listen.of_port 8765)
- (fun _addr r w ->
- let buffer = Bytes.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 ())
Add Comment
Please, Sign In to add comment