12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576openLwttypeerror=[Tcpip.Tcp.error|`Exnofexn]typewrite_error=[Tcpip.Tcp.write_error|`Exnofexn]letpp_errorppf=function|#Tcpip.Tcp.errorase->Tcpip.Tcp.pp_errorppfe|`Exne->Fmt.exnppfeletpp_write_errorppf=function|#Tcpip.Tcp.write_errorase->Tcpip.Tcp.pp_write_errorppfe|`Exne->Fmt.exnppfeletignore_canceled=function|Lwt.Canceled->Lwt.return_unit|exn->raiseexnletdisconnect_=return_unitletreadfd=letbuflen=65536inletbuf=Cstruct.createbufleninLwt.catch(fun()->Lwt_cstruct.readfdbuf>>=function|0->return(Ok`Eof)|nwhenn=buflen->return(Ok(`Databuf))|n->return@@Ok(`Data(Cstruct.subbuf0n)))(funexn->return(Error(`Exnexn)))letrecwritefdbuf=Lwt.catch(fun()->Lwt_cstruct.writefdbuf>>=function|nwhenn=Cstruct.lengthbuf->return@@Ok()|0->return@@Error`Closed|n->writefd(Cstruct.subbufn(Cstruct.lengthbuf-n)))(function|Unix.Unix_error(Unix.EPIPE,_,_)->return@@Error`Closed|e->return(Error(`Exne)))letwritevfdbufs=Lwt_list.fold_left_s(funresbuf->matchreswith|Error_ase->returne|Ok()->writefdbuf)(Ok())bufs(* TODO make nodelay a flow option *)letwrite_nodelayfdbuf=writefdbuf(* TODO make nodelay a flow option *)letwritev_nodelayfdbufs=writevfdbufsletclosefd=Lwt.catch(fun()->Lwt_unix.closefd)(function|Unix.Unix_error(Unix.EBADF,_,_)->Lwt.return_unit|e->Lwt.faile)letshutdownfdmode=letcmd=matchmodewith|`read->Lwt_unix.SHUTDOWN_RECEIVE|`write->Lwt_unix.SHUTDOWN_SEND|`read_write->Lwt_unix.SHUTDOWN_ALLinLwt.return(Lwt_unix.shutdownfdcmd)letinput_t~src:_~dst:__buf=Lwt.return_unit