External.hs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. {-# LANGUAGE ForeignFunctionInterface, InterruptibleFFI, EmptyDataDecls #-}
  2. module System.IO.Uniform.External where
  3. import Foreign
  4. import Foreign.C.Types
  5. import Foreign.C.String
  6. import System.Posix.Types (Fd(..))
  7. data Nethandler
  8. -- | A bounded IP port from where to accept SocketIO connections.
  9. newtype BoundedPort = BoundedPort {lis :: (Ptr Nethandler)}
  10. data Ds
  11. data TlsDs
  12. -- | UniformIO IP connections.
  13. data SocketIO = SocketIO {sock :: (Ptr Ds)} | TlsSocketIO {bio :: (Ptr TlsDs)}
  14. -- | UniformIO type for file IO.
  15. newtype FileIO = FileIO {fd :: (Ptr Ds)}
  16. -- | UniformIO that reads from stdin and writes to stdout.
  17. data StdIO = 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