1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type Tcp
17
18///|
19pub impl 
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
  loop_(Self) -> Loop
}
ToHandle
for
type Tcp
Tcp
with to_handle(self :
type Tcp
Tcp
) ->
type Handle
Handle
= "%identity"
20 21///| 22pub impl
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
  loop_(Self) -> Loop
}
ToHandle
for
type Tcp
Tcp
with of_handle(self :
type Handle
Handle
) ->
type Tcp
Tcp
= "%identity"
23 24///| 25pub 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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
for
type Tcp
Tcp
with to_stream(self :
type Tcp
Tcp
) ->
type Stream
Stream
= "%identity"
26 27///| 28pub 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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
for
type Tcp
Tcp
with of_stream(self :
type Stream
Stream
) ->
type Tcp
Tcp
= "%identity"
29 30///| 31extern "c" fn uv_tcp_make() ->
type Tcp
Tcp
= "moonbit_uv_tcp_make"
32 33///| 34extern "c" fn uv_tcp_init(uv :
type Loop Bytes
Loop
, tcp :
type Tcp
Tcp
) ->
Int
Int
= "moonbit_uv_tcp_init"
35 36///| 37pub fn
type Tcp
Tcp
::
(uv : Loop) -> Tcp raise Errno
new
(
Loop
uv
:
type Loop Bytes
Loop
) ->
type Tcp
Tcp
raise
type! Errno Status
Errno
{
38 let
Tcp
tcp
=
() -> Tcp
uv_tcp_make
()
39 let
Int
result
=
(uv : Loop, tcp : Tcp) -> Int
uv_tcp_init
(
Loop
uv
,
Tcp
tcp
)
40 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
41 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
42 } 43
Tcp
tcp
44} 45 46///| 47extern "c" fn uv_tcp_bind(tcp :
type Tcp
Tcp
, addr :
type Sockaddr Bytes
Sockaddr
, flags :
UInt
UInt
) ->
Int
Int
= "moonbit_uv_tcp_bind"
48 49///| 50type TcpBindFlags
UInt
UInt
51 52///| 53const
UInt
TCP_IPV6ONLY
:
UInt
UInt
= 1
54 55///| 56const
UInt
TCP_REUSEPORT
:
UInt
UInt
= 2
57 58///| 59pub fn
type TcpBindFlags UInt
TcpBindFlags
::
(ipv6_only~ : Bool = .., reuse_port~ : Bool = ..) -> TcpBindFlags
new
(
60
Bool
ipv6_only
~ :
Bool
Bool
= false,
61
Bool
reuse_port
~ :
Bool
Bool
= false
62) ->
type TcpBindFlags UInt
TcpBindFlags
{
63 let mut
UInt
flags
= 0U
64 if
Bool
ipv6_only
{
65
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
UInt
TCP_IPV6ONLY
66 } 67 if
Bool
reuse_port
{
68
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
UInt
TCP_REUSEPORT
69 } 70 return
UInt
flags
71} 72 73///| 74pub fn[Sockaddr :
trait ToSockaddr {
  to_sockaddr(self : Self) -> Sockaddr
  ip_name(self : Self) -> String raise Errno
}
ToSockaddr
]
type Tcp
Tcp
::
(self : Tcp, addr : Sockaddr, flags : TcpBindFlags) -> Unit raise Errno
bind
(
75
Tcp
self
:
type Tcp
Tcp
,
76
Sockaddr
addr
:

type parameter Sockaddr

Sockaddr
,
77
TcpBindFlags
flags
:
type TcpBindFlags UInt
TcpBindFlags
78) ->
Unit
Unit
raise
type! Errno Status
Errno
{
79 let
Int
status
=
(tcp : Tcp, addr : Sockaddr, flags : UInt) -> Int
uv_tcp_bind
(
Tcp
self
,
Sockaddr
addr
.
(Sockaddr) -> Sockaddr
to_sockaddr
(),
TcpBindFlags
flags
.
(self : TcpBindFlags) -> UInt

Convert newtype to its underlying type, automatically derived.

inner
())
80 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
81 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
82 } 83} 84 85///| 86extern "c" fn uv_listen( 87 stream :
type Stream
Stream
,
88 backlog :
Int
Int
,
89 cb : (
type Stream
Stream
,
Int
Int
) ->
Unit
Unit
90) ->
Int
Int
= "moonbit_uv_listen"
91 92///| 93pub 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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
]
(stream : Stream, backlog : Int, connection_cb : (Stream) -> Unit, error_cb : (Stream, Errno) -> Unit) -> Unit raise Errno
listen
(
94
Stream
stream
:

type parameter Stream

Stream
,
95
Int
backlog
:
Int
Int
,
96
(Stream) -> Unit
connection_cb
: (

type parameter Stream

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

type parameter Stream

Stream
,
type! Errno Status
Errno
) ->
Unit
Unit
98) ->
Unit
Unit
raise
type! Errno Status
Errno
{
99
Stream
stream
.
(Stream, Int, (Stream) -> Unit, (Stream, Errno) -> Unit) -> Unit raise Errno
listen
(
Int
backlog
,
(Stream) -> Unit
connection_cb
,
(Stream, Errno) -> Unit
error_cb
)
100} 101 102///| 103impl
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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
with
(self : Self, backlog : Int, connection_cb : (Self) -> Unit, error_cb : (Self, Errno) -> Unit) -> Unit raise Errno
listen
(
Self
self
,
Int
backlog
,
(Self) -> Unit
connection_cb
,
(Self, Errno) -> Unit
error_cb
) {
104 fn
(Stream, Int) -> Unit
uv_cb
(
Stream
self
:
type Stream
Stream
,
Int
status
:
Int
Int
) {
105 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
106
(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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
(_ : Stream) -> Self
::of_stream
(
Stream
self
),
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
107 } else { 108
(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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
(_ : Stream) -> Self
::of_stream
(
Stream
self
))
109 } 110 } 111 112 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
)
113 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
114 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
115 } 116} 117 118///| 119extern "c" fn uv_accept(server :
type Stream
Stream
, client :
type Stream
Stream
) ->
Int
Int
= "moonbit_uv_accept"
120 121///| 122pub 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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise 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 raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
]
(server : Server, client : Client) -> Unit raise Errno
accept
(
123
Server
server
:

type parameter Server

Server
,
124
Client
client
:

type parameter Client

Client
125) ->
Unit
Unit
raise
type! Errno Status
Errno
{
126 let
Int
status
=
(server : Stream, client : Stream) -> Int
uv_accept
(
Server
server
.
(Server) -> Stream
to_stream
(),
Client
client
.
(Client) -> Stream
to_stream
())
127 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
128 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
129 } 130} 131 132///| 133type Connect
Bytes
Bytes
134 135///| 136pub 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"
137 138///| 139extern "c" fn uv_connect_make() ->
type Connect Bytes
Connect
= "moonbit_uv_connect_make"
140 141///| 142extern "c" fn uv_tcp_connect( 143 req :
type Connect Bytes
Connect
,
144 handle :
type Tcp
Tcp
,
145 addr :
type Sockaddr Bytes
Sockaddr
,
146 cb : (
type Connect Bytes
Connect
,
Int
Int
) ->
Unit
Unit
147) ->
Int
Int
= "moonbit_uv_tcp_connect"
148 149///| 150pub fn[Sockaddr :
trait ToSockaddr {
  to_sockaddr(self : Self) -> Sockaddr
  ip_name(self : Self) -> String raise Errno
}
ToSockaddr
]
type Tcp
Tcp
::
(self : Tcp, addr : Sockaddr, connect_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Connect raise Errno
connect
(
151
Tcp
self
:
type Tcp
Tcp
,
152
Sockaddr
addr
:

type parameter Sockaddr

Sockaddr
,
153
() -> Unit
connect_cb
: () ->
Unit
Unit
,
154
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
155) ->
type Connect Bytes
Connect
raise
type! Errno Status
Errno
{
156 fn
(Connect, Int) -> Unit
cb
(_ :
type Connect Bytes
Connect
,
Int
status
:
Int
Int
) {
157 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
158
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
159 } else { 160
() -> Unit
connect_cb
()
161 } 162 } 163 164 let
Connect
req
=
() -> Connect
uv_connect_make
()
165 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
)
166 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
167 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
168 } 169 return
Connect
req
170} 171