
Untitled
By: a guest on
Jul 31st, 2012 | syntax:
None | size: 1.53 KB | hits: 11 | expires: Never
how to handle signal on windows with haskell?
import IO
import Control.Exception hiding (catch)
import Control.Concurrent
import Network
import System.Posix ---cannot be run on windows.
main = withSocketsDo (installHandler sigPIPE Ignore Nothing >> main')
--so the signal handler cannot be used
main' = listenOn (PortNumber 9900) >>= acceptConnections
acceptConnections sock = do
putStrLn "trying to accept" -- debug msg
conn@(h,host,port) <- accept sock
print conn -- debug msg
forkIO $ catch (talk conn `finally` hClose h) (e -> print e)
acceptConnections sock
talk conn@(h,_,_) = hGetLine h >>= hPutStrLn h >> hFlush h >> talk conn
void callback(int sig)
{
// printf("catchn");
}
...
signal(SIGINT,callback);
-- unix-src/OSCompat.hs
module OSCompat where
import System.Posix
ignorePipe = installHandler sigPIPE Ignore Nothing
-- Win32-src/OSCompat.hs
module OSCompat where
import System.Win32
ignorePipe = -- ???
if os(windows)
build-depends: Win32
hs-source-dirs: Win32-src
else
build-depends: unix
hs-source-dirs: unix-src
import OSCompat
main = withSocketsDo (ignorePipe >> main')
import Foreign
import Foreign.C.Types
type Handler = CInt->IO ()
foreign import ccall "wrapper"
genHandler:: (Handler) -> IO (FunPtr Handler)
foreign import ccall safe "signal.h signal"
install:: CInt->FunPtr Handler->IO CInt
main=do
s<-genHandler (x->putStrLn $ "catch "++(show x))
res<- install 2 s
putStrLn $ show res
s<-getLine