123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120(*----------------------------------------------------------------------------
* Copyright (c) 2019-2020 António Nuno Monteiro
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*---------------------------------------------------------------------------*)openLwt.InfixmoduleBuffered_flow=structtype'at={flow:'a;mutablebuf:Cstruct.t}letcreateflow={flow;buf=Cstruct.empty}endmoduleMake_IO(Flow:Mirage_flow.S):Gluten_lwt.IOwithtypesocket=Flow.flowBuffered_flow.tandtypeaddr=unit=structtypesocket=Flow.flowBuffered_flow.ttypeaddr=unitletshutdown(sock:socket)=Flow.closesock.flowletshutdown_receivesock=Lwt.async(fun()->shutdownsock)letclose=shutdownletbuffered_read(sock:socket)len=lettruncbuf=matchCstruct.lengthbuf>lenwith|false->buf|true->lethead,rest=Cstruct.splitbufleninsock.buf<-rest;headinletbuffered_data=matchCstruct.is_emptysock.bufwith|true->None|false->letbuf=sock.bufinsock.buf<-Cstruct.empty;Some(Ok(`Data(truncbuf)))inmatchbuffered_datawith|Somedata->Lwt.returndata|None->Flow.readsock.flow>|=fundata->assert(Cstruct.is_emptysock.buf);(matchdatawithOk(`Databuf)->Ok(`Data(truncbuf))|x->x)letreadsockbigstring~off~len=Lwt.catch(fun()->buffered_readsocklen>|=function|Ok(`Databuf)->Bigstringaf.blitbuf.buffer~src_off:buf.offbigstring~dst_off:off~len:buf.len;buf.len|Ok`Eof->raiseEnd_of_file|Errorerror->failwith(Format.asprintf"%a"Flow.pp_errorerror))(funexn->shutdownsock>>=fun()->Lwt.failexn)letwritev(sock:socket)iovecs=letdata_len=List.fold_left(funacce->acc+e.Faraday.len)0iovecsinletdata=Cstruct.create_unsafedata_leninletcopy_len=List.fold_left(fundst_off{Faraday.buffer;off;len}->Bigstringaf.blitbuffer~src_off:offdata.buffer~dst_off~len;dst_off+len)0iovecsinassert(data_len=copy_len);Lwt.catch(fun()->Flow.writesock.flowdata>|=funx->matchxwith|Ok()->`Okdata_len|Error`Closed->`Closed|Errorother_error->raise(Failure(Format.asprintf"%a"Flow.pp_write_errorother_error)))(funexn->shutdownsock>>=fun()->Lwt.failexn)endmoduleServer(Flow:Mirage_flow.S)=Gluten_lwt.Server(Make_IO(Flow))moduleClient(Flow:Mirage_flow.S)=Gluten_lwt.Client(Make_IO(Flow))