0 | module System.UV.Raw.TCP
  1 |
  2 | import System.UV.Raw.Callback
  3 | import System.UV.Raw.Handle
  4 | import System.UV.Raw.Loop
  5 | import System.UV.Raw.Pointer
  6 | import System.UV.Raw.Req
  7 | import System.UV.Raw.Util
  8 |
  9 | %default total
 10 |
 11 | --------------------------------------------------------------------------------
 12 | -- FFI
 13 | --------------------------------------------------------------------------------
 14 |
 15 | %foreign (idris_uv "uv_tcp_init")
 16 | prim__uv_tcp_init : Ptr Loop -> Ptr Tcp -> PrimIO Int32
 17 |
 18 | %foreign (idris_uv "uv_tcp_keepalive")
 19 | prim__uv_tcp_keepalive : Ptr Tcp -> Int32 -> Bits32 -> PrimIO Int32
 20 |
 21 | %foreign (idris_uv "uv_tcp_simultaneous_accepts")
 22 | prim__uv_tcp_simultaneous_accepts : Ptr Tcp -> Int32 -> PrimIO Int32
 23 |
 24 | %foreign (idris_uv "uv_tcp_bind")
 25 | prim__uv_tcp_bind : Ptr Tcp -> Ptr SockAddr -> Bits32 -> PrimIO Int32
 26 |
 27 | %foreign (idris_uv "uv_tcp_getsocketname")
 28 | prim__uv_tcp_getsocketname : Ptr Tcp -> Ptr SockAddr -> Int32 -> PrimIO Int32
 29 |
 30 | %foreign (idris_uv "uv_tcp_getpeername")
 31 | prim__uv_tcp_getpeername : Ptr Tcp -> Ptr SockAddr -> Int32 -> PrimIO Int32
 32 |
 33 | %foreign (idris_uv "uv_tcp_connect")
 34 | prim__uv_tcp_connect :
 35 |      Ptr Connect
 36 |   -> Ptr Tcp
 37 |   -> Ptr SockAddr
 38 |   -> AnyPtr
 39 |   -> PrimIO Int32
 40 |
 41 | %foreign (idris_uv "uv_ip4_addr")
 42 | prim__uv_ip4_addr : String -> Bits16 -> Ptr SockAddrIn -> PrimIO Int32
 43 |
 44 | %foreign (idris_uv "uv_ip6_addr")
 45 | prim__uv_ip6_addr : String -> Bits16 -> Ptr SockAddrIn6 -> PrimIO Int32
 46 |
 47 | %foreign (idris_uv "uv_ip4_name")
 48 | prim__uv_ip4_name : Ptr SockAddrIn -> Ptr Char -> Bits32 -> PrimIO Int32
 49 |
 50 | %foreign (idris_uv "uv_ip6_name")
 51 | prim__uv_ip6_name : Ptr SockAddrIn6 -> Ptr Char -> Bits32 -> PrimIO Int32
 52 |
 53 | %foreign (idris_uv "uv_ip6_name")
 54 | prim__uv_ip_name : Ptr SockAddr -> Ptr Char -> Bits32 -> PrimIO Int32
 55 |
 56 | --------------------------------------------------------------------------------
 57 | -- API
 58 | --------------------------------------------------------------------------------
 59 |
 60 | parameters {auto has : HasIO io}
 61 |
 62 |   ||| Initialize the handle. No socket is created as of yet.
 63 |   export %inline
 64 |   uv_tcp_init : Ptr Loop -> Ptr Tcp -> io Int32
 65 |   uv_tcp_init l p = primIO $ prim__uv_tcp_init l p
 66 |
 67 |   ||| Enable / disable TCP keep-alive. delay is the initial
 68 |   ||| delay in seconds, ignored when enable is zero.
 69 |   ||| After delay has been reached, 10 successive probes,
 70 |   ||| each spaced 1 second from the previous one, will still happen.
 71 |   ||| If the connection is still lost at the end of this procedure,
 72 |   ||| then the handle is destroyed with a
 73 |   ||| UV_ETIMEDOUT error passed to the corresponding callback.
 74 |   export %inline
 75 |   uv_tcp_keepalive : Ptr Tcp -> Bool -> (delay : Bits32)-> io Int32
 76 |   uv_tcp_keepalive tcp b delay =
 77 |     primIO $ prim__uv_tcp_keepalive tcp (boolToInt32 b) delay
 78 |
 79 |   ||| Enable / disable simultaneous asynchronous accept requests
 80 |   ||| that are queued by the operating system when listening for new TCP
 81 |   ||| connections.
 82 |   |||
 83 |   ||| This setting is used to tune a TCP server for the desired
 84 |   ||| performance. Having simultaneous accepts can significantly
 85 |   ||| improve the rate of accepting connections (which is why
 86 |   ||| it is enabled by default) but may lead to un‐
 87 |   ||| even load distribution in multi-process setups.
 88 |   export %inline
 89 |   uv_tcp_simultaneous_accepts : Ptr Tcp -> Bool -> io Int32
 90 |   uv_tcp_simultaneous_accepts tcp b =
 91 |     primIO $ prim__uv_tcp_simultaneous_accepts tcp (boolToInt32 b)
 92 |
 93 |   ||| Bind the handle to an address and port. addr should point
 94 |   ||| to an initialized struct sockaddr_in or struct sockaddr_in6.
 95 |   |||
 96 |   ||| When the port is already taken, you can expect to see an UV_EADDRINUSE
 97 |   ||| error from uv_listen() or uv_tcp_connect(). That is, a successful call
 98 |   ||| to this function does not guarantee that the call to uv_listen() or
 99 |   ||| uv_tcp_connect() will succeed as well.
100 |   |||
101 |   ||| flags can contain UV_TCP_IPV6ONLY, in which case dual-stack support
102 |   ||| is disabled and only IPv6 is used.
103 |   export %inline
104 |   uv_tcp_bind :
105 |        {auto 0 _ : PCast t SockAddr}
106 |     -> Ptr Tcp
107 |     -> Ptr t
108 |     -> (flags : Bits32)
109 |     -> io Int32
110 |   uv_tcp_bind tcp sa flags = primIO $ prim__uv_tcp_bind tcp (castPtr sa) flags
111 |
112 |   ||| Establish an IPv4 or IPv6 TCP connection. Provide an initialized
113 |   ||| TCP handle and an uninitialized uv_connect_t. addr should
114 |   ||| point to an initialized struct sockaddr_in or struct sockaddr_in6.
115 |   |||
116 |   ||| On Windows if the addr is initialized to point to an
117 |   ||| unspecified address (0.0.0.0 or ::) it will be changed to
118 |   ||| point to localhost.  This is done to match the behavior of Linux systems.
119 |   |||
120 |   ||| The callback is made when the connection has been established
121 |   ||| or when a connection error happened.
122 |   export %inline
123 |   uv_tcp_connect :
124 |        {auto 0 _ : PCast t SockAddr}
125 |     -> Ptr Tcp
126 |     -> Ptr t
127 |     -> (Ptr Connect -> Int32 -> IO ())
128 |     -> io Int32
129 |   uv_tcp_connect tcp sa act = do
130 |     pc <- mallocPtr Connect
131 |     cb <- ptrIntCB (\x,y => act x y >> freeReq x)
132 |     uv_req_set_data pc cb
133 |     primIO $ prim__uv_tcp_connect pc tcp (castPtr sa) cb
134 |
135 | --------------------------------------------------------------------------------
136 | -- Addresses
137 | --------------------------------------------------------------------------------
138 |
139 |   ||| Convert a string containing an IPv4 address to a binary structure.
140 |   export %inline
141 |   uv_ip4_addr : String -> Bits16 -> Ptr SockAddrIn -> io Int32
142 |   uv_ip4_addr addr port ptr = primIO $ prim__uv_ip4_addr addr port ptr
143 |
144 |   ||| Convert a string containing an IPv6 address to a binary structure.
145 |   export %inline
146 |   uv_ip6_addr : String -> Bits16 -> Ptr SockAddrIn6 -> io Int32
147 |   uv_ip6_addr addr port ptr = primIO $ prim__uv_ip6_addr addr port ptr
148 |
149 |   ||| Convert a binary structure containing an IPv4 address to a string.
150 |   export %inline
151 |   uv_ip4_name : Ptr SockAddrIn -> Ptr Char -> Bits32 -> io Int32
152 |   uv_ip4_name sa str len = primIO $ prim__uv_ip4_name sa str len
153 |
154 |   ||| Convert a binary structure containing an IPv6 address to a string.
155 |   export %inline
156 |   uv_ip6_name : Ptr SockAddrIn6 -> Ptr Char -> Bits32 -> io Int32
157 |   uv_ip6_name sa str len = primIO $ prim__uv_ip6_name sa str len
158 |
159 |   ||| Convert a binary structure containing an IPv4 address or an
160 |   ||| IPv6 address to a string.
161 |   export %inline
162 |   uv_ip_name : Ptr SockAddr -> Ptr Char -> Bits32 -> io Int32
163 |   uv_ip_name sa str len = primIO $ prim__uv_ip_name sa str len
164 |