blob: ea3580edea61e90228a6b72294239c5b7400b64f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import Control.Concurrent
import Control.Monad
import Foreign.C
import GHC.Clock
import GHC.IO.Device
import GHC.IO.Handle.FD
import System.IO
import System.Posix.IO
import System.Posix.Types
import System.Timeout
main :: IO ()
main = do
socketHandle <- makeTestSocketHandle
let nanoSecondsPerSecond = 1000 * 1000 * 1000
let milliSecondsPerSecond = 1000
let timeToSpend = 1
let timeToSpendNano = timeToSpend * nanoSecondsPerSecond
let timeToSpendMilli = timeToSpend * milliSecondsPerSecond
start <- getMonotonicTimeNSec
b <- hWaitForInput socketHandle timeToSpendMilli
end <- getMonotonicTimeNSec
let timeSpentNano = fromIntegral $ end - start
let delta = timeSpentNano - timeToSpendNano
-- We can never wait for a shorter amount of time than specified
putStrLn $ "delta >= 0: " ++ show (delta >= 0)
foreign import ccall unsafe "socket" c_socket ::
CInt -> CInt -> CInt -> IO CInt
makeTestSocketHandle :: IO Handle
makeTestSocketHandle = do
sockNum <-
c_socket
1 -- PF_LOCAL
2 -- SOCK_DGRAM
0
let fd = fromIntegral sockNum :: Fd
h <-
fdToHandle'
(fromIntegral fd)
(Just GHC.IO.Device.Stream)
True
"testsocket"
ReadMode
True
hSetBuffering h NoBuffering
pure h
|