Skip to content

Commit c198f03

Browse files
committed
fix MacOS support
1 parent 15f58b7 commit c198f03

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

vlib/fasthttp/fasthttp.v

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ fn C.htons(__hostshort u16) u16
4444

4545
fn C.fcntl(fd int, cmd int, arg int) int
4646

47-
fn C.accept4(sockfd int, addr &net.Addr, addrlen &u32, flags int) int
48-
4947
pub struct Slice {
5048
pub:
5149
start int
@@ -178,7 +176,13 @@ fn create_server_socket(port int) int {
178176

179177
fn handle_accept_loop(mut notifier notify.FdNotifier, listen_fd int) {
180178
for {
181-
client_fd := C.accept4(listen_fd, C.NULL, C.NULL, C.SOCK_NONBLOCK)
179+
client_fd := $if linux {
180+
// On Linux, we can use accept4 with SOCK_NONBLOCK to avoid an extra fcntl call
181+
C.accept4(listen_fd, C.NULL, C.NULL, C.SOCK_NONBLOCK)
182+
} $else {
183+
C.accept(listen_fd, C.NULL, C.NULL)
184+
}
185+
182186
if client_fd < 0 {
183187
if C.errno == C.EAGAIN || C.errno == C.EWOULDBLOCK {
184188
break // No more incoming connections; exit loop.
@@ -187,6 +191,11 @@ fn handle_accept_loop(mut notifier notify.FdNotifier, listen_fd int) {
187191
C.perror(c'Accept failed')
188192
break
189193
}
194+
195+
$if !linux {
196+
set_blocking(client_fd, false)
197+
}
198+
190199
// Enable TCP_NODELAY for lower latency
191200
opt := 1
192201
C.setsockopt(client_fd, C.IPPROTO_TCP, C.TCP_NODELAY, &opt, sizeof(opt))
@@ -228,6 +237,8 @@ fn process_events(mut server Server, listen_fd int) {
228237
request_buffer.flags.set(.noslices | .nogrow | .noshrink)
229238
}
230239
for {
240+
// It is a little bit slow to allocate on each iteration, but it
241+
// is simpler to implement. Optimization can be done later if needed.
231242
for event in notifier.wait(time.infinite) {
232243
if event.fd == listen_fd {
233244
handle_accept_loop(mut notifier, listen_fd)

vlib/net/aasocket.c.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ fn C.listen(sockfd int, backlog int) int
6262
// fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int
6363
fn C.accept(sockfd int, addr &Addr, addrlen &u32) int
6464

65+
fn C.accept4(sockfd int, addr &net.Addr, addrlen &u32, flags int) int
66+
6567
fn C.getaddrinfo(node &char, service &char, hints &C.addrinfo, res &&C.addrinfo) int
6668

6769
fn C.freeaddrinfo(info &C.addrinfo)

0 commit comments

Comments
 (0)