0 | module Node.Net.Socket.Connect
  1 |
  2 | import Node
  3 | import Node.Internal.Support
  4 | import Node.Net.Socket.Type
  5 |
  6 | public export
  7 | data IpAddressFamily
  8 |   = IPv4
  9 |   | IPv6
 10 |   | Both
 11 |
 12 | export
 13 | familyAsInt : IpAddressFamily -> Int
 14 | familyAsInt = \case
 15 |   IPv4 => 4
 16 |   IPv6 => 6
 17 |   Both => 0
 18 |
 19 | public export
 20 | record TCPOptions where
 21 |   constructor MkTCPOptons
 22 |   port: Int
 23 |   host: Maybe String
 24 |   localAddress: Maybe String
 25 |   localPort: Maybe Int
 26 |   family: IpAddressFamily
 27 |   -- TODO: hints
 28 |   -- TODO: lookup
 29 |   noDelay: Bool
 30 |   keepAlive: Bool
 31 |   keepAliveInitialDelay: Int
 32 |
 33 | public export
 34 | record IPCOptions where
 35 |   constructor MkIPCOptions
 36 |   path: String
 37 |
 38 | public export
 39 | options : SocketType -> Type
 40 | options TCP = TCPOptions
 41 | options IPC = IPCOptions
 42 |
 43 | export
 44 | defaultTCPOptions : (port : Int) -> options TCP
 45 | defaultTCPOptions port = MkTCPOptons
 46 |   { port = port
 47 |   , host = Nothing
 48 |   , localAddress = Nothing
 49 |   , localPort = Nothing
 50 |   , family = Both
 51 |   , noDelay = False
 52 |   , keepAlive = False
 53 |   , keepAliveInitialDelay = 0
 54 |   }
 55 |
 56 | export
 57 | defaultIPCOptions : (path : String) -> options IPC
 58 | defaultIPCOptions path = MkIPCOptions
 59 |   { path = path }
 60 |
 61 | %foreign """
 62 |   node:lambda:
 63 |   ( port
 64 |   , host
 65 |   , localAddress
 66 |   , localPort
 67 |   , family
 68 |   , noDelay
 69 |   , keepAlive
 70 |   , keepAliveInitialDelay
 71 |   ) => _keepDefined({
 72 |     port,
 73 |     host: _maybe(host),
 74 |     localAddress: _maybe(localAddress),
 75 |     localPort: _maybe(localPort),
 76 |     family,
 77 |     noDelay,
 78 |     keepAlive,
 79 |     keepAliveInitialDelay
 80 |   })
 81 |   """
 82 | ffi_convertTCPOptions :
 83 |   (port: Int)
 84 |   -> (host: Maybe String)
 85 |   -> (localAddress: Maybe String)
 86 |   -> (localPort: Maybe Int)
 87 |   -> (family: Int)
 88 |   -> (noDelay: Bool)
 89 |   -> (keepAlive: Bool)
 90 |   -> (keepAliveInitialDelay: Int)
 91 |   -> Node $ options TCP
 92 |
 93 | %foreign """
 94 |   node:lambda:
 95 |   ( path
 96 |   ) => ({
 97 |     path
 98 |   })
 99 |   """
100 | ffi_convertIPCOptions :
101 |   (path: String)
102 |   -> Node $ options IPC
103 |
104 | export
105 | convertOptions : (t : SocketType) -> options t -> Node $ options t
106 | convertOptions TCP o = ffi_convertTCPOptions
107 |     o.port
108 |     o.host
109 |     o.localAddress
110 |     o.localPort
111 |     (familyAsInt o.family)
112 |     o.noDelay
113 |     o.keepAlive
114 |     o.keepAliveInitialDelay
115 | convertOptions IPC o = ffi_convertIPCOptions o.path
116 |
117 |