File: Windows.hs

package info (click to toggle)
haskell-network 3.1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: sh: 3,264; haskell: 2,002; ansic: 536; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE OverloadedStrings #-}

module Network.Socket.ByteString.Lazy.Windows (
    -- * Send data to a socket
    send
  , sendAll
  ) where

import qualified Data.ByteString           as S
import qualified Data.ByteString.Lazy      as L
import qualified Network.Socket.ByteString as Socket
import           Network.Socket.Imports
import           Network.Socket.ByteString.IO       (waitWhen0)
import           Network.Socket.Types

-- -----------------------------------------------------------------------------
-- Sending
send
    :: Socket -- ^ Connected socket
    -> L.ByteString -- ^ Data to send
    -> IO Int64 -- ^ Number of bytes sent
send s lbs = case L.toChunks lbs of
    -- TODO: Consider doing nothing if the string is empty.
    []    -> fromIntegral <$> Socket.send s S.empty
    (x:_) -> fromIntegral <$> Socket.send s x

sendAll
    :: Socket -- ^ Connected socket
    -> L.ByteString -- ^ Data to send
    -> IO ()
sendAll _ "" = return ()
sendAll s bs0 = loop bs0
  where
    loop bs = do
        -- "send" throws an exception.
        sent <- send s bs
        waitWhen0 (fromIntegral sent) s
        when (sent /= L.length bs) $ loop $ L.drop sent bs