External.hs 2.5 KB

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