moduleSystem.ZMQ.Internal(Context(..),Socket(..),Message(..),Flag(..),Timeout,Size,messageOf,messageOfLazy,messageClose,messageInit,messageInitSize,setIntOpt,setStrOpt,getBoolOpt,getIntOpt,getStrOpt,toZMQFlag,combine,mkSocket,onSocket)whereimportControl.ApplicativeimportControl.Monad(foldM_)importControl.ExceptionimportData.IORef(IORef)importForeignimportForeign.C.ErrorimportForeign.C.StringimportForeign.C.Types(CInt,CSize)importqualifiedData.ByteStringasSBimportqualifiedData.ByteString.LazyasLBimportqualifiedData.ByteString.UnsafeasUBimportData.IORef(newIORef)importSystem.ZMQ.BasetypeTimeout=Int64typeSize=Word-- | Flags to apply on send operations (cf. man zmq_send)---- [@NoBlock@] Send operation should be performed in non-blocking mode.-- If it cannot be performed immediatley an error will be thrown (errno-- is set to EAGAIN).dataFlag=NoBlock-- ^ ZMQ_NOBLOCK|SndMore-- ^ ZMQ_SNDMOREderiving(Eq,Ord,Show)-- | A 0MQ context representation.newtypeContext=Context{ctx::ZMQCtx}-- | A 0MQ Socket.dataSocketa=Socket{_socket::ZMQSocket,_sockLive::IORefBool}-- A 0MQ Message representation.newtypeMessage=Message{msgPtr::ZMQMsgPtr}-- internal helpers:onSocket::String->Socketa->(ZMQSocket->IOb)->IObonSocket_func(Socketsock_state)act=actsock{-# INLINE onSocket #-}mkSocket::ZMQSocket->IO(Socketa)mkSockets=Sockets<$>newIORefTruemessageOf::SB.ByteString->IOMessagemessageOfb=UB.unsafeUseAsCStringLenb$\(cstr,len)->domsg<-messageInitSize(fromIntegrallen)data_ptr<-c_zmq_msg_data(msgPtrmsg)copyBytesdata_ptrcstrlenreturnmsgmessageOfLazy::LB.ByteString->IOMessagemessageOfLazylbs=domsg<-messageInitSize(fromIntegrallen)data_ptr<-c_zmq_msg_data(msgPtrmsg)letfnoffsetbs=UB.unsafeUseAsCStringLenbs$\(cstr,str_len)->docopyBytes(data_ptr`plusPtr`offset)cstrstr_lenreturn(offset+str_len)foldM_fn0(LB.toChunkslbs)returnmsgwherelen=LB.lengthlbsmessageClose::Message->IO()messageClose(Messageptr)=dothrowErrnoIfMinus1_"messageClose"$c_zmq_msg_closeptrfreeptrmessageInit::IOMessagemessageInit=doptr<-new(ZMQMsgnullPtr)throwErrnoIfMinus1_"messageInit"$c_zmq_msg_initptrreturn(Messageptr)messageInitSize::Size->IOMessagemessageInitSizes=doptr<-new(ZMQMsgnullPtr)throwErrnoIfMinus1_"messageInitSize"$c_zmq_msg_init_sizeptr(fromIntegrals)return(Messageptr)setIntOpt::(Storableb,Integralb)=>Socketa->ZMQOption->b->IO()setIntOptsock(ZMQOptiono)i=onSocket"setIntOpt"sock$\s->throwErrnoIfMinus1_"setIntOpt"$withi$\ptr->c_zmq_setsockopts(fromIntegralo)(castPtrptr)(fromIntegral.sizeOf$i)setStrOpt::Socketa->ZMQOption->String->IO()setStrOptsock(ZMQOptiono)str=onSocket"setStrOpt"sock$\s->throwErrnoIfMinus1_"setStrOpt"$withCStringLenstr$\(cstr,len)->c_zmq_setsockopts(fromIntegralo)(castPtrcstr)(fromIntegrallen)getBoolOpt::Socketa->ZMQOption->IOBoolgetBoolOptso=((1::Int64)==)<$>getIntOptsogetIntOpt::(Storableb,Integralb)=>Socketa->ZMQOption->IObgetIntOptsock(ZMQOptiono)=onSocket"getIntOpt"sock$\s->doleti=0bracket(newi)free$\iptr->bracket(new(fromIntegral.sizeOf$i::CSize))free$\jptr->dothrowErrnoIfMinus1_"integralOpt"$c_zmq_getsockopts(fromIntegralo)(castPtriptr)jptrpeekiptrgetStrOpt::Socketa->ZMQOption->IOStringgetStrOptsock(ZMQOptiono)=onSocket"getStrOpt"sock$\s->bracket(mallocBytes255)free$\bPtr->bracket(new(255::CSize))free$\sPtr->dothrowErrnoIfMinus1_"getStrOpt"$c_zmq_getsockopts(fromIntegralo)(castPtrbPtr)sPtrpeeksPtr>>=\len->peekCStringLen(bPtr,fromIntegrallen)toZMQFlag::Flag->ZMQFlagtoZMQFlagNoBlock=noBlocktoZMQFlagSndMore=sndMorecombine::[Flag]->CIntcombine=fromIntegral.foldr((.|.).flagVal.toZMQFlag)0