1///|
2type SockaddrIn 
Bytes
Bytes
3 4///| 5extern "c" fn uv_sockaddr_in_make() ->
type SockaddrIn Bytes
SockaddrIn
= "moonbit_uv_sockaddr_in_make"
6 7///| 8type Tcp 9 10///| 11pub impl
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
}
ToHandle
for
type Tcp
Tcp
with to_handle(self :
type Tcp
Tcp
) ->
type Handle
Handle
= "%identity"
12 13///| 14pub impl
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
}
ToHandle
for
type Tcp
Tcp
with of_handle(self :
type Handle
Handle
) ->
type Tcp
Tcp
= "%identity"
15 16///| 17pub impl
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
for
type Tcp
Tcp
with to_stream(self :
type Tcp
Tcp
) ->
type Stream
Stream
= "%identity"
18 19///| 20pub impl
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
for
type Tcp
Tcp
with of_stream(self :
type Stream
Stream
) ->
type Tcp
Tcp
= "%identity"
21 22///| 23extern "c" fn uv_tcp_make() ->
type Tcp
Tcp
= "moonbit_uv_tcp_make"
24 25///| 26extern "c" fn uv_tcp_init(uv :
type Loop Bytes
Loop
, tcp :
type Tcp
Tcp
) ->
Int
Int
= "moonbit_uv_tcp_init"
27 28///| 29pub fn
type Tcp
Tcp
::
(uv : Loop) -> Tcp!Errno
new
(
Loop
uv
:
type Loop Bytes
Loop
) ->
type Tcp
Tcp
!
type! Errno Status
Errno
{
30 let
Tcp
tcp
=
() -> Tcp
uv_tcp_make
()
31 let
Int
result
=
(uv : Loop, tcp : Tcp) -> Int
uv_tcp_init
(
Loop
uv
,
Tcp
tcp
)
32 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
33 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
34 } 35
Tcp
tcp
36} 37 38///| 39extern "c" fn uv_ip4_addr(ip :
Bytes
Bytes
, port :
Int
Int
, addr :
type SockaddrIn Bytes
SockaddrIn
) = "moonbit_uv_ip4_addr"
40 41///| 42pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(ip : Ntbs, port : Int) -> SockaddrIn
ip4_addr
(
Ntbs
ip
:

type parameter Ntbs

Ntbs
,
Int
port
:
Int
Int
) ->
type SockaddrIn Bytes
SockaddrIn
{
43 let
Bytes
ip
=
Ntbs
ip
.
(Ntbs) -> Bytes
to_ntbs
()
44 let
SockaddrIn
addr
=
() -> SockaddrIn
uv_sockaddr_in_make
()
45
(ip : Bytes, port : Int, addr : SockaddrIn) -> Unit
uv_ip4_addr
(
Bytes
ip
,
Int
port
,
SockaddrIn
addr
)
46
SockaddrIn
addr
47} 48 49///| 50type Sockaddr
Bytes
Bytes
51 52///| 53pub impl
trait ToSockaddr {
  to_sockaddr(Self) -> Sockaddr
}
ToSockaddr
for
type Sockaddr Bytes
Sockaddr
with to_sockaddr(self :
type Sockaddr Bytes
Sockaddr
) ->
type Sockaddr Bytes
Sockaddr
= "%identity"
54 55///| 56pub impl
trait ToSockaddr {
  to_sockaddr(Self) -> Sockaddr
}
ToSockaddr
for
type SockaddrIn Bytes
SockaddrIn
with to_sockaddr(addr :
type SockaddrIn Bytes
SockaddrIn
) ->
type Sockaddr Bytes
Sockaddr
= "%identity"
57 58///| 59extern "c" fn uv_tcp_bind(tcp :
type Tcp
Tcp
, addr :
type Sockaddr Bytes
Sockaddr
, flags :
UInt
UInt
) ->
Int
Int
= "moonbit_uv_tcp_bind"
60 61///| 62type TcpBindFlags
UInt
UInt
63 64///| 65const
UInt
TCP_IPV6ONLY
:
UInt
UInt
= 1
66 67///| 68const
UInt
TCP_REUSEPORT
:
UInt
UInt
= 2
69 70///| 71pub fn
type TcpBindFlags UInt
TcpBindFlags
::
(ipv6_only~ : Bool = .., reuse_port~ : Bool = ..) -> TcpBindFlags
new
(
72
Bool
ipv6_only
~ :
Bool
Bool
= false,
73
Bool
reuse_port
~ :
Bool
Bool
= false
74) ->
type TcpBindFlags UInt
TcpBindFlags
{
75 let mut
UInt
flags
= 0U
76 if
Bool
ipv6_only
{
77
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
UInt
TCP_IPV6ONLY
78 } 79 if
Bool
reuse_port
{
80
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
UInt
TCP_REUSEPORT
81 } 82 return
UInt
flags
83} 84 85///| 86pub fn[Sockaddr :
trait ToSockaddr {
  to_sockaddr(Self) -> Sockaddr
}
ToSockaddr
]
type Tcp
Tcp
::
(self : Tcp, addr : Sockaddr, flags : TcpBindFlags) -> Unit!Errno
bind
(
87
Tcp
self
:
type Tcp
Tcp
,
88
Sockaddr
addr
:

type parameter Sockaddr

Sockaddr
,
89
TcpBindFlags
flags
:
type TcpBindFlags UInt
TcpBindFlags
90) ->
Unit
Unit
!
type! Errno Status
Errno
{
91 let
Int
status
=
(tcp : Tcp, addr : Sockaddr, flags : UInt) -> Int
uv_tcp_bind
(
Tcp
self
,
Sockaddr
addr
.
(Sockaddr) -> Sockaddr
to_sockaddr
(),
TcpBindFlags
flags
.
UInt
_
)
92 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
93 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
94 } 95} 96 97///| 98extern "c" fn uv_listen( 99 stream :
type Stream
Stream
,
100 backlog :
Int
Int
,
101 cb : (
type Stream
Stream
,
Int
Int
) ->
Unit
Unit
102) ->
Int
Int
= "moonbit_uv_listen"
103 104///| 105pub fn[Stream :
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
]
(stream : Stream, backlog : Int, connection_cb : (Stream) -> Unit, error_cb : (Stream, Errno) -> Unit) -> Unit!Errno
listen
(
106
Stream
stream
:

type parameter Stream

Stream
,
107
Int
backlog
:
Int
Int
,
108
(Stream) -> Unit
connection_cb
: (

type parameter Stream

Stream
) ->
Unit
Unit
,
109
(Stream, Errno) -> Unit
error_cb
: (

type parameter Stream

Stream
,
type! Errno Status
Errno
) ->
Unit
Unit
110) ->
Unit
Unit
!
type! Errno Status
Errno
{
111
Stream
stream
.
(Stream, Int, (Stream) -> Unit, (Stream, Errno) -> Unit) -> Unit!Errno
listen
!(
Int
backlog
,
(Stream) -> Unit
connection_cb
,
(Stream, Errno) -> Unit
error_cb
)
112} 113 114///| 115impl
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
with
(self : Self, backlog : Int, connection_cb : (Self) -> Unit, error_cb : (Self, Errno) -> Unit) -> Unit!Errno
listen
(
Self
self
,
Int
backlog
,
(Self) -> Unit
connection_cb
,
(Self, Errno) -> Unit
error_cb
) {
116 fn
(Stream, Int) -> Unit
uv_cb
(
Stream
self
:
type Stream
Stream
,
Int
status
:
Int
Int
) {
117 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
118
(Self, Errno) -> Unit
error_cb
(
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
(_ : Stream) -> Self
::of_stream
(
Stream
self
),
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
119 } else { 120
(Self) -> Unit
connection_cb
(
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
(_ : Stream) -> Self
::of_stream
(
Stream
self
))
121 } 122 } 123 124 let
Int
status
=
(stream : Stream, backlog : Int, cb : (Stream, Int) -> Unit) -> Int
uv_listen
(
Self
self
.
(Self) -> Stream
to_stream
(),
Int
backlog
,
(Stream, Int) -> Unit
uv_cb
)
125 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
126 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
127 } 128} 129 130///| 131extern "c" fn uv_accept(server :
type Stream
Stream
, client :
type Stream
Stream
) ->
Int
Int
= "moonbit_uv_accept"
132 133///| 134pub fn[Server :
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
, Client :
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  read_stop(Self) -> Unit!Errno
  write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream
]
(server : Server, client : Client) -> Unit!Errno
accept
(
135
Server
server
:

type parameter Server

Server
,
136
Client
client
:

type parameter Client

Client
137) ->
Unit
Unit
!
type! Errno Status
Errno
{
138 let
Int
status
=
(server : Stream, client : Stream) -> Int
uv_accept
(
Server
server
.
(Server) -> Stream
to_stream
(),
Client
client
.
(Client) -> Stream
to_stream
())
139 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
140 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
141 } 142} 143 144///| 145type Connect
Bytes
Bytes
146 147///| 148pub impl
trait ToReq {
  to_req(Self) -> Req
}
ToReq
for
type Connect Bytes
Connect
with to_req(self :
type Connect Bytes
Connect
) ->
type Req
Req
= "%identity"
149 150///| 151extern "c" fn uv_connect_make() ->
type Connect Bytes
Connect
= "moonbit_uv_connect_make"
152 153///| 154extern "c" fn uv_tcp_connect( 155 req :
type Connect Bytes
Connect
,
156 handle :
type Tcp
Tcp
,
157 addr :
type Sockaddr Bytes
Sockaddr
,
158 cb : (
type Connect Bytes
Connect
,
Int
Int
) ->
Unit
Unit
159) ->
Int
Int
= "moonbit_uv_tcp_connect"
160 161///| 162trait
trait ToSockaddr {
  to_sockaddr(Self) -> Sockaddr
}
ToSockaddr
{
163
(Self) -> Sockaddr
to_sockaddr
(self :

type parameter Self

Self
) ->
type Sockaddr Bytes
Sockaddr
164} 165 166///| 167pub fn[Sockaddr :
trait ToSockaddr {
  to_sockaddr(Self) -> Sockaddr
}
ToSockaddr
]
type Tcp
Tcp
::
(self : Tcp, addr : Sockaddr, connect_cb : (Connect) -> Unit, error_cb : (Connect, Errno) -> Unit) -> Unit!Errno
connect
(
168
Tcp
self
:
type Tcp
Tcp
,
169
Sockaddr
addr
:

type parameter Sockaddr

Sockaddr
,
170
(Connect) -> Unit
connect_cb
: (
type Connect Bytes
Connect
) ->
Unit
Unit
,
171
(Connect, Errno) -> Unit
error_cb
: (
type Connect Bytes
Connect
,
type! Errno Status
Errno
) ->
Unit
Unit
172) ->
Unit
Unit
!
type! Errno Status
Errno
{
173 fn
(Connect, Int) -> Unit
cb
(
Connect
req
:
type Connect Bytes
Connect
,
Int
status
:
Int
Int
) {
174 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
175
(Connect, Errno) -> Unit
error_cb
(
Connect
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
176 } else { 177
(Connect) -> Unit
connect_cb
(
Connect
req
)
178 } 179 } 180 181 let
Connect
req
=
() -> Connect
uv_connect_make
()
182 let
Int
status
=
(req : Connect, handle : Tcp, addr : Sockaddr, cb : (Connect, Int) -> Unit) -> Int
uv_tcp_connect
(
Connect
req
,
Tcp
self
,
Sockaddr
addr
.
(Sockaddr) -> Sockaddr
to_sockaddr
(),
(Connect, Int) -> Unit
cb
)
183 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
184 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
185 } 186} 187