Commit b942c100 authored by jan.koester's avatar jan.koester
Browse files

mac os comp

parent f751b50f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ elseif(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
    else()
        list(APPEND netplussrc event/iocp.cpp)
    endif()
elseif("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "BSD")
elseif("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "BSD" OR "${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Darwin")
    list(APPEND netplussrc event/kqueue.cpp)
else()
    list(APPEND netplussrc event/poll.cpp)
+10 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <sys/syscall.h>

@@ -10,6 +11,7 @@
namespace netplus {

static bool try_getrandom(uint8_t* dst, size_t len) {
#if defined(__linux__) && defined(SYS_getrandom)
    size_t done = 0;
    while (done < len) {
        ssize_t r = syscall(SYS_getrandom, dst + done, len - done, 0);
@@ -18,6 +20,14 @@ static bool try_getrandom(uint8_t* dst, size_t len) {
        return false;
    }
    return true;
#elif defined(__APPLE__) || defined(__FreeBSD__)
    arc4random_buf(dst, len);
    return true;
#else
    (void)dst;
    (void)len;
    return false;
#endif
}

void fillRandomBytes(uint8_t* dst, size_t len) {
+24 −2
Original line number Diff line number Diff line
@@ -39,6 +39,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sys/un.h>
#include <netinet/tcp.h>

#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif

namespace netplus {

tcp::tcp() : socket() {
@@ -182,9 +186,14 @@ void tcp::accept(std::unique_ptr<socket>& csock, bool nonblock) {
    sockaddr_storage peer{};
    socklen_t peerLen = sizeof(peer);

    // Use accept4 with SOCK_NONBLOCK to avoid extra fcntl syscall
    SOCKET cfd = -1;
#if defined(SOCK_NONBLOCK)
    // Fast path where accept4 is available.
    int flags = nonblock ? SOCK_NONBLOCK : 0;
    SOCKET cfd = ::accept4(_Socket, reinterpret_cast<sockaddr*>(&peer), &peerLen, flags);
    cfd = ::accept4(_Socket, reinterpret_cast<sockaddr*>(&peer), &peerLen, flags);
#else
    cfd = ::accept(_Socket, reinterpret_cast<sockaddr*>(&peer), &peerLen);
#endif
    if (cfd < 0) {
        NetException e;
        int et = (errno == EAGAIN || errno == EWOULDBLOCK) ? NetException::Note : NetException::Error;
@@ -192,6 +201,19 @@ void tcp::accept(std::unique_ptr<socket>& csock, bool nonblock) {
        throw e;
    }

#if !defined(SOCK_NONBLOCK)
    if (nonblock) {
        int sockFlags = ::fcntl(cfd, F_GETFL, 0);
        if (sockFlags < 0 || ::fcntl(cfd, F_SETFL, sockFlags | O_NONBLOCK) < 0) {
            int saved = errno;
            ::close(cfd);
            NetException e;
            e[NetException::Error] << "tcp::accept set non-blocking failed: " << strerror(saved);
            throw e;
        }
    }
#endif

    *csock = cfd;
    csock->_Addr = peer;
    csock->_AddrLen = peerLen;