如何使用haskell处理窗口上的信号?

vze*_*zex 6 haskell signals

在Windows上有类似System.Posix的东西吗?

我希望下面的代码在Windows上运行,我应该更改吗?

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
Run Code Online (Sandbox Code Playgroud)

例如,如果我希望程序在ctrl + c时退出,我必须为SIGINT添加一个处理程序,所以在c ++中,编写如下代码:

void callback(int sig) 
{ 
   // printf("catch\n"); 
} 
... 
signal(SIGINT,callback); 
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在haskell中使用FFI?

rem*_*ezx 5

晚会晚了,但是我写了一个图书馆可以提供帮助。它允许在Windows和Linux上进行信号处理。它可用于黑客攻击:https://hackage.haskell.org/package/signal


vze*_*zex 4

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
Run Code Online (Sandbox Code Playgroud)

上面的代码就是我想要做的,只需导入函数signal,并使用 haskell 回调。