External.hs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. {-# LANGUAGE ForeignFunctionInterface #-}
  2. {-# LANGUAGE InterruptibleFFI #-}
  3. {-# LANGUAGE EmptyDataDecls #-}
  4. module System.IO.Uniform.External where
  5. import Foreign
  6. import Foreign.C.Types
  7. import Foreign.C.String
  8. import System.Posix.Types (Fd(..))
  9. data Nethandler
  10. -- | A bounded IP port from where to accept SocketIO connections.
  11. newtype BoundedPort = BoundedPort {lis :: (Ptr Nethandler)}
  12. data Ds
  13. newtype SocketIO = SocketIO {sock :: (Ptr Ds)}
  14. newtype FileIO = FileIO {fd :: (Ptr Ds)}
  15. data TlsDs
  16. newtype TlsIO = TlsIO {tls :: (Ptr TlsDs)}
  17. data StdIO
  18. closeFd :: Fd -> IO ()
  19. closeFd (Fd f) = c_closeFd f
  20. -- | Closes a BoundedPort, and releases any resource used by it.
  21. closePort :: BoundedPort -> IO ()
  22. closePort p = c_closePort (lis p)
  23. foreign import ccall interruptible "getPort" c_getPort :: CInt -> IO (Ptr Nethandler)
  24. foreign import ccall interruptible "createFromHandler" c_accept :: Ptr Nethandler -> IO (Ptr Ds)
  25. foreign import ccall safe "createFromFileName" c_createFile :: CString -> IO (Ptr Ds)
  26. foreign import ccall interruptible "createToIPv4Host" c_connect4 :: CUInt -> CInt -> IO (Ptr Ds)
  27. foreign import ccall interruptible "createToIPv6Host" c_connect6 :: Ptr CUChar -> CInt -> IO (Ptr Ds)
  28. foreign import ccall interruptible "startSockTls" c_startSockTls :: Ptr Ds -> CString -> CString -> CString -> IO (Ptr TlsDs)
  29. foreign import ccall safe "getPeer" c_getPeer :: Ptr Ds -> Ptr CUInt -> Ptr CUChar -> Ptr CInt -> IO (CInt)
  30. --foreign import ccall safe "getFd" c_getFd :: Ptr Ds -> IO CInt
  31. --foreign import ccall safe "getTlsFd" c_getTlsFd :: Ptr TlsDs -> IO CInt
  32. foreign import ccall safe "closeFd" c_closeFd :: CInt -> IO ()
  33. foreign import ccall safe "prepareToClose" c_prepareToClose :: Ptr Ds -> IO CInt
  34. foreign import ccall safe "closeHandler" c_closePort :: Ptr Nethandler -> IO ()
  35. foreign import ccall safe "closeTls" c_closeTls :: Ptr TlsDs -> IO (Ptr Ds)
  36. foreign import ccall interruptible "sendDs" c_send :: Ptr Ds -> Ptr CChar -> CInt -> IO CInt
  37. foreign import ccall interruptible "stdDsSend" c_sendStd :: Ptr CChar -> CInt -> IO CInt
  38. foreign import ccall interruptible "tlsDsSend" c_sendTls :: Ptr TlsDs -> Ptr CChar -> CInt -> IO CInt
  39. foreign import ccall interruptible "recvDs" c_recv :: Ptr Ds -> Ptr CChar -> CInt -> IO CInt
  40. foreign import ccall interruptible "stdDsRecv" c_recvStd :: Ptr CChar -> CInt -> IO CInt
  41. foreign import ccall interruptible "tlsDsRecv" c_recvTls :: Ptr TlsDs -> Ptr CChar -> CInt -> IO CInt