Skip to content

Commit 6e9c324

Browse files
robertheborden
authored andcommitted
Make setNonBlockIfNeeded public and document its use. (#242)
1 parent 3ba41f2 commit 6e9c324

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Network/Socket.hsc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ module Network.Socket
160160
-- in case you ever want to get at the underlying file descriptor..
161161
, fdSocket
162162
, mkSocket
163+
, setNonBlockIfNeeded
163164

164165
-- * Deprecated aliases
165166
-- $deprecated-aliases
@@ -262,6 +263,10 @@ socket2FD (MkSocket fd _ _ _ _) =
262263
FD{fdFD = fd,fdIsSocket_ = 1}
263264
#endif
264265

266+
-- | Smart constructor for constructing a 'Socket'. It should only be
267+
-- called once for every new file descriptor. The caller must make
268+
-- sure that the socket is in non-blocking mode. See
269+
-- 'setNonBlockIfNeeded'.
265270
mkSocket :: CInt
266271
-> Family
267272
-> SocketType
@@ -390,6 +395,9 @@ foreign import ccall unsafe "socketpair"
390395
#endif
391396

392397
-- | Set the socket to nonblocking, if applicable to this platform.
398+
--
399+
-- Depending on the platform this is required when using sockets from file
400+
-- descriptors that are passed in through 'recvFd' or other means.
393401
setNonBlockIfNeeded :: CInt -> IO ()
394402
setNonBlockIfNeeded fd =
395403
System.Posix.Internals.setNonBlockingFD fd True
@@ -1023,6 +1031,9 @@ sendFd sock outfd = do
10231031
-- incorrect (since socket FDs need to be closed via closesocket().)
10241032
closeFd outfd
10251033

1034+
-- | Receive a file descriptor over a domain socket. Note that the resulting
1035+
-- file descriptor may have to be put into non-blocking mode in order to be
1036+
-- used safely. See 'setNonBlockIfNeeded'.
10261037
recvFd :: Socket -> IO CInt
10271038
recvFd sock = do
10281039
theFd <- throwSocketErrorWaitRead sock "Network.Socket.recvFd" $

Network/Socket/Types.hsc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ import Foreign.Storable
8181
-- * Status flag
8282
--
8383
-- If you are calling the 'MkSocket' constructor directly you should ensure
84-
-- you have called 'Network.withSocketsDo'.
84+
-- you have called 'Network.withSocketsDo' and that the file descriptor is
85+
-- in non-blocking mode. See 'Network.Socket.setNonBlockIfNeeded'.
8586
data Socket
8687
= MkSocket
8788
CInt -- File Descriptor

0 commit comments

Comments
 (0)