123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354openSigsmoduleLog=(valletsrc=Logs.Src.create"smart_flow"inLogs.src_logsrc:Logs.LOG)letio_buffer_size=65536type('a,'s)raise=exn->('a,'s)ioletrun:typefls.sscheduler->('a,s)raise->(fl,'error,s)flow->fl->('res,[`ProtocolofSmart.error])Smart.t->('res,s)io=fun{bind;return}raise{recv;send;pp_error}flowfiber->let(>>=)=bindinlettmp=Cstruct.createio_buffer_sizeinletfailwithffmt=Format.kasprintf(funerr->raise(Failureerr))fmtinletrecgo=function|Smart.Read{k;buffer;off;len;eof}->(letmax=min(Cstruct.lengthtmp)leninLog.debug(funm->m"Start to read %d byte(s)."max);recvflow(Cstruct.subtmp0max)>>=function|Ok`End_of_flow->Log.debug(funm->m"Got end of input.");go(eof())|Ok(`Inputlen)->Log.debug(funm->m"Got %d/%d byte(s)."lenmax);Cstruct.blit_to_bytestmp0bufferofflen;go(klen)|Errorerr->Log.err(funm->m"Got an error: %a."pp_errorerr);failwithf"%a"pp_errorerr)|Smart.Write{k;buffer;off;len}->letreclooptmp=ifCstruct.lengthtmp=0thengo(klen)elsesendflowtmp>>=function|Okshift->loop(Cstruct.shifttmpshift)|Errorerr->failwithf"%a"pp_errorerrinLog.debug(funm->m"Write %d byte(s)."len);loop(Cstruct.of_stringbuffer~off~len)|Smart.Returnv->returnv|Smart.Error(`Protocolerr)->Log.err(funm->m"Got a protocol error: %a."Smart.pp_errorerr);failwithf"%a"Smart.pp_errorerringofiber