Skip to content

Commit 0c2df5c

Browse files
committed
socketPair for Windows
1 parent 21e327b commit 0c2df5c

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

Network/Socket/Unix.hsc

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
23

34
#include "HsNet.h"
45
##include "HsNetDef.h"
@@ -13,28 +14,32 @@ module Network.Socket.Unix (
1314
, getPeerEid
1415
) where
1516

16-
import System.Posix.Types (Fd(..))
17-
17+
import Foreign.Marshal.Alloc (allocaBytes)
1818
import Network.Socket.Buffer
19+
import Network.Socket.Fcntl
1920
import Network.Socket.Imports
21+
import Network.Socket.Types
22+
import System.Posix.Types (Fd(..))
23+
2024
#if defined(mingw32_HOST_OS)
25+
import Network.Socket.Syscall
2126
import Network.Socket.Win32.Cmsg
27+
import System.Directory
28+
import System.IO
29+
import System.IO.Temp
2230
#else
31+
import Foreign.Marshal.Array (peekArray)
32+
import Network.Socket.Internal
2333
import Network.Socket.Posix.Cmsg
2434
#endif
25-
import Network.Socket.Types
2635

2736
#if defined(HAVE_GETPEEREID)
2837
import System.IO.Error (catchIOError)
2938
#endif
3039
#ifdef HAVE_GETPEEREID
3140
import Foreign.Marshal.Alloc (alloca)
3241
#endif
33-
import Foreign.Marshal.Alloc (allocaBytes)
34-
import Foreign.Marshal.Array (peekArray)
3542

36-
import Network.Socket.Fcntl
37-
import Network.Socket.Internal
3843
#ifdef HAVE_STRUCT_UCRED_SO_PEERCRED
3944
import Network.Socket.Options
4045
#endif
@@ -165,6 +170,21 @@ socketPair :: Family -- Family Name (usually AF_UNIX)
165170
-> SocketType -- Socket Type (usually Stream)
166171
-> ProtocolNumber -- Protocol Number
167172
-> IO (Socket, Socket) -- unnamed and connected.
173+
#if defined(mingw32_HOST_OS)
174+
socketPair _ _ _ = withSystemTempFile "temp-for-pair" $ \file hdl -> do
175+
hClose hdl
176+
removeFile file
177+
listenSock <- socket AF_UNIX Stream defaultProtocol
178+
bind listenSock $ SockAddrUnix file
179+
listen listenSock 10
180+
clientSock <- socket AF_UNIX Stream defaultProtocol
181+
connect clientSock $ SockAddrUnix file
182+
(serverSock, _ :: SockAddr) <- accept listenSock
183+
close listenSock
184+
withFdSocket clientSock setNonBlockIfNeeded
185+
withFdSocket serverSock setNonBlockIfNeeded
186+
return (clientSock, serverSock)
187+
#else
168188
socketPair family stype protocol =
169189
allocaBytes (2 * sizeOf (1 :: CInt)) $ \ fdArr -> do
170190
let c_stype = packSocketType stype
@@ -179,3 +199,4 @@ socketPair family stype protocol =
179199

180200
foreign import ccall unsafe "socketpair"
181201
c_socketpair :: CInt -> CInt -> CInt -> Ptr CInt -> IO CInt
202+
#endif

network.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ library
168168
cpp-options: -D_WIN32_WINNT=0x0600
169169
cc-options: -D_WIN32_WINNT=0x0600
170170

171+
build-depends:
172+
temporary
173+
171174
test-suite spec
172175
type: exitcode-stdio-1.0
173176
main-is: Spec.hs

0 commit comments

Comments
 (0)