diff --git a/acceptor_bsd.go b/acceptor_bsd.go new file mode 100644 index 000000000..df5de320c --- /dev/null +++ b/acceptor_bsd.go @@ -0,0 +1,25 @@ +// Copyright (c) 2023 The Gnet Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build freebsd || dragonfly || darwin +// +build freebsd dragonfly darwin + +package gnet + +// The canonical BSD sockets implementation will inherit file status flags +// from the listening socket, so we don't need to set the non-blocking flag +// for the accepted sockets explicitly. +func setNonBlock(_ int, _ bool) error { + return nil +} diff --git a/acceptor_linux.go b/acceptor_linux.go new file mode 100644 index 000000000..bde964912 --- /dev/null +++ b/acceptor_linux.go @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 The Gnet Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package gnet + +import "golang.org/x/sys/unix" + +func setNonBlock(fd int, nonBlocking bool) error { + return unix.SetNonblock(fd, nonBlocking) +} diff --git a/acceptor_unix.go b/acceptor_unix.go index 26063e4cd..5bba090af 100644 --- a/acceptor_unix.go +++ b/acceptor_unix.go @@ -44,7 +44,7 @@ func (eng *engine) accept(fd int, _ netpoll.IOEvent) error { } } - if err = os.NewSyscallError("fcntl nonblock", unix.SetNonblock(nfd, true)); err != nil { + if err = os.NewSyscallError("fcntl nonblock", setNonBlock(nfd, true)); err != nil { return err } remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa) @@ -83,7 +83,7 @@ func (el *eventloop) accept(fd int, ev netpoll.IOEvent) error { } } - if err = os.NewSyscallError("fcntl nonblock", unix.SetNonblock(nfd, true)); err != nil { + if err = os.NewSyscallError("fcntl nonblock", setNonBlock(nfd, true)); err != nil { return err } remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa)