34 lines
687 B
Haskell
34 lines
687 B
Haskell
|
module Server.Communication where
|
||
|
|
||
|
import Control.Monad
|
||
|
|
||
|
import Network.Socket
|
||
|
|
||
|
import System.Posix.Signals
|
||
|
|
||
|
-- internal imports
|
||
|
|
||
|
import Server.Util
|
||
|
|
||
|
bindSocket :: FilePath -> IO Socket
|
||
|
bindSocket path = do
|
||
|
let sockAddr = SockAddrUnix path
|
||
|
unless (isSupportedSockAddr sockAddr)
|
||
|
(error $ "invalid socket path " <> path)
|
||
|
removeIfExists path
|
||
|
sock <- socket AF_UNIX Stream 0
|
||
|
bind sock sockAddr
|
||
|
pure sock
|
||
|
|
||
|
dropSocketOnSigint :: Socket -> IO ()
|
||
|
dropSocketOnSigint sock =
|
||
|
void $ installHandler
|
||
|
keyboardSignal
|
||
|
(CatchOnce $ do
|
||
|
(SockAddrUnix path) <- getSocketName sock
|
||
|
close' sock
|
||
|
removeIfExists path
|
||
|
raiseSignal keyboardSignal
|
||
|
)
|
||
|
Nothing
|