Skip to content

Commit b6ffeff

Browse files
committed
fourmolu
1 parent 0af7a6a commit b6ffeff

File tree

15 files changed

+545
-364
lines changed

15 files changed

+545
-364
lines changed

Network/Socket.hs

Lines changed: 256 additions & 129 deletions
Large diffs are not rendered by default.

Network/Socket/Address.hs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
-- | This module provides extensible APIs for socket addresses.
2-
--
32
module Network.Socket.Address (
43
-- * Socket Address
5-
SocketAddress(..)
6-
, getPeerName
7-
, getSocketName
4+
SocketAddress (..),
5+
getPeerName,
6+
getSocketName,
7+
88
-- * Socket operations
9-
, connect
10-
, bind
11-
, accept
9+
connect,
10+
bind,
11+
accept,
12+
1213
-- * Sending and receiving ByteString
13-
, sendTo
14-
, sendAllTo
15-
, recvFrom
14+
sendTo,
15+
sendAllTo,
16+
recvFrom,
17+
1618
-- * Sending and receiving data from a buffer
17-
, sendBufTo
18-
, recvBufFrom
19+
sendBufTo,
20+
recvBufFrom,
21+
1922
-- * Advanced IO
20-
, sendBufMsg
21-
, recvBufMsg
22-
) where
23+
sendBufMsg,
24+
recvBufMsg,
25+
) where
2326

24-
import Network.Socket.ByteString.IO
2527
import Network.Socket.Buffer
28+
import Network.Socket.ByteString.IO
2629
import Network.Socket.Name
2730
import Network.Socket.Syscall
2831
import Network.Socket.Types

Network/Socket/ByteString.hs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,36 @@
1616
--
1717
-- > import Network.Socket
1818
-- > import Network.Socket.ByteString
19-
--
20-
module Network.Socket.ByteString
21-
(
19+
module Network.Socket.ByteString (
2220
-- * Send data to a socket
23-
send
24-
, sendAll
25-
, sendTo
26-
, sendAllTo
21+
send,
22+
sendAll,
23+
sendTo,
24+
sendAllTo,
2725

2826
-- ** Vectored I/O
2927
-- $vectored
30-
, sendMany
31-
, sendManyTo
32-
, sendManyWithFds
28+
sendMany,
29+
sendManyTo,
30+
sendManyWithFds,
3331

3432
-- * Receive data from a socket
35-
, recv
36-
, recvFrom
33+
recv,
34+
recvFrom,
3735

3836
-- * Advanced send and recv
39-
, sendMsg
40-
, recvMsg
41-
) where
37+
sendMsg,
38+
recvMsg,
39+
) where
4240

4341
import Data.ByteString (ByteString)
4442

43+
import Network.Socket.ByteString.IO hiding (recvFrom, sendAllTo, sendTo)
4544
import qualified Network.Socket.ByteString.IO as G
46-
import Network.Socket.ByteString.IO hiding (sendTo, sendAllTo, recvFrom)
4745
import Network.Socket.Types
4846

4947
-- ----------------------------------------------------------------------------
48+
5049
-- ** Vectored I/O
5150

5251
-- $vectored

Network/Socket/ByteString/Lazy.hs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
23
-- |
34
-- Module : Network.Socket.ByteString.Lazy
45
-- Copyright : (c) Bryan O'Sullivan 2009
@@ -18,46 +19,54 @@
1819
-- > import Network.Socket
1920
-- > import Network.Socket.ByteString.Lazy
2021
-- > import Prelude hiding (getContents)
21-
--
2222
module Network.Socket.ByteString.Lazy (
2323
-- * Send data to a socket
24-
send
25-
, sendAll
26-
, sendWithFds
24+
send,
25+
sendAll,
26+
sendWithFds,
27+
2728
-- * Receive data from a socket
28-
, getContents
29-
, recv
30-
) where
29+
getContents,
30+
recv,
31+
) where
3132

32-
import Data.ByteString.Lazy.Internal (ByteString(..), defaultChunkSize)
33-
import Network.Socket (ShutdownCmd (..), shutdown)
34-
import Prelude hiding (getContents)
35-
import System.IO.Unsafe (unsafeInterleaveIO)
36-
import System.IO.Error (catchIOError)
37-
import System.Posix.Types (Fd(..))
33+
import Data.ByteString.Lazy.Internal (
34+
ByteString (..),
35+
defaultChunkSize,
36+
)
37+
import Network.Socket (ShutdownCmd (..), shutdown)
38+
import System.IO.Error (catchIOError)
39+
import System.IO.Unsafe (unsafeInterleaveIO)
40+
import System.Posix.Types (Fd (..))
41+
import Prelude hiding (getContents)
3842

3943
#if defined(mingw32_HOST_OS)
4044
import Network.Socket.ByteString.Lazy.Windows (send, sendAll)
4145
#else
4246
import Network.Socket.ByteString.Lazy.Posix (send, sendAll)
4347
#endif
4448

45-
import qualified Data.ByteString as S
46-
import qualified Data.ByteString.Lazy as L
47-
import qualified Network.Socket.ByteString as N
48-
import Network.Socket.Imports
49-
import Network.Socket.Types
49+
import qualified Data.ByteString as S
50+
import qualified Data.ByteString.Lazy as L
51+
import qualified Network.Socket.ByteString as N
52+
import Network.Socket.Imports
53+
import Network.Socket.Types
5054

5155
-- | Send data and file descriptors over a UNIX-domain socket in
5256
-- a single system call. This function does not work on Windows.
53-
sendWithFds :: Socket -- ^ Socket
54-
-> ByteString -- ^ Data to send
55-
-> [Fd] -- ^ File descriptors
56-
-> IO ()
57+
sendWithFds
58+
:: Socket
59+
-- ^ Socket
60+
-> ByteString
61+
-- ^ Data to send
62+
-> [Fd]
63+
-- ^ File descriptors
64+
-> IO ()
5765
sendWithFds s lbs fds = N.sendManyWithFds s (L.toChunks lbs) fds
5866

5967
-- -----------------------------------------------------------------------------
6068
-- Receiving
69+
6170
-- | Receive data from the socket. The socket must be in a connected
6271
-- state. Data is received on demand, in chunks; each chunk will be
6372
-- sized to reflect the amount of data received by individual 'recv'
@@ -68,16 +77,18 @@ sendWithFds s lbs fds = N.sendManyWithFds s (L.toChunks lbs) fds
6877
-- down. If there is an error and an exception is thrown, the socket
6978
-- is not shut down.
7079
getContents
71-
:: Socket -- ^ Connected socket
72-
-> IO ByteString -- ^ Data received
80+
:: Socket
81+
-- ^ Connected socket
82+
-> IO ByteString
83+
-- ^ Data received
7384
getContents s = loop
7485
where
7586
loop = unsafeInterleaveIO $ do
7687
sbs <- N.recv s defaultChunkSize
7788
if S.null sbs
7889
then do
79-
shutdown s ShutdownReceive `catchIOError` const (return ())
80-
return Empty
90+
shutdown s ShutdownReceive `catchIOError` const (return ())
91+
return Empty
8192
else Chunk sbs <$> loop
8293

8394
-- | Receive data from the socket. The socket must be in a connected
@@ -88,10 +99,14 @@ getContents s = loop
8899
--
89100
-- If there is no more data to be received, returns an empty 'ByteString'.
90101
recv
91-
:: Socket -- ^ Connected socket
92-
-> Int64 -- ^ Maximum number of bytes to receive
93-
-> IO ByteString -- ^ Data received
102+
:: Socket
103+
-- ^ Connected socket
104+
-> Int64
105+
-- ^ Maximum number of bytes to receive
106+
-> IO ByteString
107+
-- ^ Data received
94108
recv s nbytes = chunk <$> N.recv s (fromIntegral nbytes)
95109
where
96-
chunk k | S.null k = Empty
97-
| otherwise = Chunk k Empty
110+
chunk k
111+
| S.null k = Empty
112+
| otherwise = Chunk k Empty

Network/Socket/ByteString/Lazy/Posix.hs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,58 @@
22

33
module Network.Socket.ByteString.Lazy.Posix (
44
-- * Send data to a socket
5-
send
6-
, sendAll
7-
) where
5+
send,
6+
sendAll,
7+
) where
88

9-
import qualified Data.ByteString.Lazy as L
10-
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
11-
import Foreign.Marshal.Array (allocaArray)
9+
import qualified Data.ByteString.Lazy as L
10+
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
11+
import Foreign.Marshal.Array (allocaArray)
1212

13-
import Network.Socket.ByteString.IO (waitWhen0)
14-
import Network.Socket.ByteString.Internal (c_writev)
15-
import Network.Socket.Imports
16-
import Network.Socket.Internal
17-
import Network.Socket.Posix.IOVec (IOVec (IOVec))
18-
import Network.Socket.Types
13+
import Network.Socket.ByteString.IO (waitWhen0)
14+
import Network.Socket.ByteString.Internal (c_writev)
15+
import Network.Socket.Imports
16+
import Network.Socket.Internal
17+
import Network.Socket.Posix.IOVec (IOVec (IOVec))
18+
import Network.Socket.Types
1919

2020
-- -----------------------------------------------------------------------------
2121
-- Sending
2222
send
23-
:: Socket -- ^ Connected socket
24-
-> L.ByteString -- ^ Data to send
25-
-> IO Int64 -- ^ Number of bytes sent
23+
:: Socket
24+
-- ^ Connected socket
25+
-> L.ByteString
26+
-- ^ Data to send
27+
-> IO Int64
28+
-- ^ Number of bytes sent
2629
send s lbs = do
27-
let cs = take maxNumChunks (L.toChunks lbs)
30+
let cs = take maxNumChunks (L.toChunks lbs)
2831
len = length cs
2932
siz <- withFdSocket s $ \fd -> allocaArray len $ \ptr ->
30-
withPokes cs ptr $ \niovs ->
31-
throwSocketErrorWaitWrite s "writev" $ c_writev fd ptr niovs
33+
withPokes cs ptr $ \niovs ->
34+
throwSocketErrorWaitWrite s "writev" $ c_writev fd ptr niovs
3235
return $ fromIntegral siz
3336
where
3437
withPokes ss p f = loop ss p 0 0
3538
where
36-
loop (c:cs) q k niovs
39+
loop (c : cs) q k niovs
3740
| k < maxNumBytes = unsafeUseAsCStringLen c $ \(ptr, len) -> do
3841
poke q $ IOVec (castPtr ptr) (fromIntegral len)
39-
loop cs
40-
(q `plusPtr` sizeOf (IOVec nullPtr 0))
41-
(k + fromIntegral len)
42-
(niovs + 1)
42+
loop
43+
cs
44+
(q `plusPtr` sizeOf (IOVec nullPtr 0))
45+
(k + fromIntegral len)
46+
(niovs + 1)
4347
| otherwise = f niovs
4448
loop _ _ _ niovs = f niovs
45-
maxNumBytes = 4194304 :: Int -- maximum number of bytes to transmit in one system call
49+
maxNumBytes = 4194304 :: Int -- maximum number of bytes to transmit in one system call
4650
maxNumChunks = 1024 :: Int -- maximum number of chunks to transmit in one system call
4751

4852
sendAll
49-
:: Socket -- ^ Connected socket
50-
-> L.ByteString -- ^ Data to send
53+
:: Socket
54+
-- ^ Connected socket
55+
-> L.ByteString
56+
-- ^ Data to send
5157
-> IO ()
5258
sendAll _ "" = return ()
5359
sendAll s bs0 = loop bs0

Network/Socket/ByteString/Lazy/Windows.hs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@
22

33
module Network.Socket.ByteString.Lazy.Windows (
44
-- * Send data to a socket
5-
send
6-
, sendAll
7-
) where
5+
send,
6+
sendAll,
7+
) where
88

9-
import qualified Data.ByteString as S
10-
import qualified Data.ByteString.Lazy as L
9+
import qualified Data.ByteString as S
10+
import qualified Data.ByteString.Lazy as L
1111
import qualified Network.Socket.ByteString as Socket
12-
import Network.Socket.Imports
13-
import Network.Socket.ByteString.IO (waitWhen0)
14-
import Network.Socket.Types
12+
import Network.Socket.ByteString.IO (waitWhen0)
13+
import Network.Socket.Imports
14+
import Network.Socket.Types
1515

1616
-- -----------------------------------------------------------------------------
1717
-- Sending
1818
send
19-
:: Socket -- ^ Connected socket
20-
-> L.ByteString -- ^ Data to send
21-
-> IO Int64 -- ^ Number of bytes sent
19+
:: Socket
20+
-- ^ Connected socket
21+
-> L.ByteString
22+
-- ^ Data to send
23+
-> IO Int64
24+
-- ^ Number of bytes sent
2225
send s lbs = case L.toChunks lbs of
2326
-- TODO: Consider doing nothing if the string is empty.
24-
[] -> fromIntegral <$> Socket.send s S.empty
25-
(x:_) -> fromIntegral <$> Socket.send s x
27+
[] -> fromIntegral <$> Socket.send s S.empty
28+
(x : _) -> fromIntegral <$> Socket.send s x
2629

2730
sendAll
28-
:: Socket -- ^ Connected socket
29-
-> L.ByteString -- ^ Data to send
31+
:: Socket
32+
-- ^ Connected socket
33+
-> L.ByteString
34+
-- ^ Data to send
3035
-> IO ()
3136
sendAll _ "" = return ()
3237
sendAll s bs0 = loop bs0

Network/Socket/Handle.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Network.Socket.Handle where
22

3-
import qualified GHC.IO.Device (IODeviceType(Stream))
3+
import qualified GHC.IO.Device (IODeviceType (Stream))
44
import GHC.IO.Handle.FD (fdToHandle')
5-
import System.IO (IOMode(..), Handle, BufferMode(..), hSetBuffering)
5+
import System.IO (BufferMode (..), Handle, IOMode (..), hSetBuffering)
66

77
import Network.Socket.Types
88

@@ -15,14 +15,13 @@ import Network.Socket.Types
1515
-- close the 'Socket' after 'socketToHandle', call 'System.IO.hClose'
1616
-- on the 'Handle'.
1717
--
18-
-- Caveat 'Handle' is not recommended for network programming in
18+
-- Caveat 'Handle' is not recommended for network programming in
1919
-- Haskell, e.g. merely performing 'hClose' on a TCP socket won't
2020
-- cooperate with peer's 'gracefulClose', i.e. proper shutdown
2121
-- sequence with appropriate handshakes specified by the protocol.
22-
2322
socketToHandle :: Socket -> IOMode -> IO Handle
2423
socketToHandle s mode = invalidateSocket s err $ \oldfd -> do
25-
h <- fdToHandle' oldfd (Just GHC.IO.Device.Stream) True (show s) mode True{-bin-}
24+
h <- fdToHandle' oldfd (Just GHC.IO.Device.Stream) True (show s) mode True {-bin-}
2625
hSetBuffering h NoBuffering
2726
return h
2827
where

0 commit comments

Comments
 (0)