on master help sign in
Fix #4346 - add Network.Socket.recvAll
commit0e31dcb1e9ea353daff874f511e6f931dfa358d3 authorGareth Smith <garethdanielsmith@gmail.com> dateSun 25 Feb 2018, 21:16 (9 years ago) parentcc7ef9a
1 file changed+20−1
libs/contrib/Network/Socket.idr+20−1
libs/contrib/Network/Socket.idr+20−1
@@ -133,7 +133,7 @@ send sock dat = do
133133||| + `ResultCode` :: The result of the underlying function.
134134|||
135135||| @sock The socket on which to receive the message.
136−||| @len How much of the data to send.
136+||| @len How much of the data to receive.
137137recv : (sock : Socket)
138138 -> (len : ByteLength)
139139 -> IO (Either SocketError (String, ResultCode))
@@ -164,6 +164,25 @@ recv sock len = do
164164 freeRecvStruct (RSPtr recv_struct_ptr)
165165 pure $ Right (payload, recv_res)
166166
167+||| Receive all the remaining data on the specified socket.
168+|||
169+||| Returns on failure a `SocketError`
170+||| Returns on success the payload `String`
171+|||
172+||| @sock The socket on which to receive the message.
173+partial
174+recvAll : (sock : Socket) -> IO (Either SocketError String)
175+recvAll sock = recvRec sock [] 64
176+ where
177+ partial
178+ recvRec : Socket -> List String -> ByteLength -> IO (Either SocketError String)
179+ recvRec sock acc n = do res <- recv sock n
180+ case res of
181+ Left 0 => pure (Right $ concat $ reverse acc)
182+ Left c => pure (Left c)
183+ Right (str, _) => let n' = min (n * 2) 65536 in
184+ recvRec sock (str :: acc) n'
185+
167186||| Send a message.
168187|||
169188||| Returns on failure a `SocketError`