Skip to content

Commit

Permalink
add socket
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAlphaFox committed Dec 3, 2024
1 parent b161e49 commit b7e412a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ if(UNIX)
${SOURCE_FILES}
src/unix/core.cpp
src/unix/pipe.cpp
src/unix/socket.cpp
src/unix/unix_base_engine.cpp
src/unix/select_engine.cpp)
endif()
Expand Down
10 changes: 7 additions & 3 deletions src/unix/core.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <cerrno>
#include <fcntl.h>
#include <sys/ioctl.h>


#include "unix_platform.h"
int mark_cloexec(int fd,int set) {
int unix_cloexec(int fd,int set) {
int r = 0;
int flags = 0;
do
Expand Down Expand Up @@ -37,7 +39,7 @@ int mark_cloexec(int fd,int set) {
return 0;
}

int mark_nonblock_ioctl(int fd, int set)
int unix_nonblock_ioctl(int fd, int set)
{
int r;

Expand All @@ -55,7 +57,7 @@ int mark_nonblock_ioctl(int fd, int set)
}


int mark_nonblock_fcntl(int fd, int set)
int unix_nonblock_fcntl(int fd, int set)
{
int flags;
int r;
Expand Down Expand Up @@ -97,3 +99,5 @@ int mark_nonblock_fcntl(int fd, int set)
return 0;
}



6 changes: 3 additions & 3 deletions src/unix/pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "../chez_socket.h"
#include "unix_platform.h"

int make_pipe(fd_t fds[2], int read_flags, int write_flags) {
int unix_make_pipe(fd_t fds[2], int read_flags, int write_flags) {
fd_t temp[2];
int err;
#if defined(__linux__) || \
Expand Down Expand Up @@ -42,11 +42,11 @@ defined(__NetBSD__)
#endif

if (read_flags & O_NONBLOCK)
if ((err = mark_nonblock(temp[0], 1)))
if ((err = unix_nonblock(temp[0], 1)))
goto fail;

if (write_flags & O_NONBLOCK)
if ((err = mark_nonblock(temp[1], 1)))
if ((err = unix_nonblock(temp[1], 1)))
goto fail;

fds[0] = temp[0];
Expand Down
43 changes: 43 additions & 0 deletions src/unix/socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Created by david on 12/3/24.
//
#include <cerrno>
#include <unistd.h>
#include <sys/socket.h>
#include "unix_platform.h"

int unix_make_socket(int domain, int type, int protocol) {
int sockfd;
int err;

#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
if (sockfd != -1)
return sockfd;

if (errno != EINVAL)
return errno;
#endif

sockfd = socket(domain, type, protocol);
if (sockfd == -1)
return errno;

err = unix_nonblock(sockfd, 1);
if (err == 0)
err = unix_cloexec(sockfd, 1);

if (err) {
close(sockfd);
return err;
}

#if defined(SO_NOSIGPIPE)
{
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
}
#endif

return sockfd;
}
2 changes: 1 addition & 1 deletion src/unix/unix_base_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

unix_base_engine::unix_base_engine()
{
if (0 != make_pipe(_wakeup_fd,O_NONBLOCK,O_NONBLOCK))
if (0 != unix_make_pipe(_wakeup_fd,O_NONBLOCK,O_NONBLOCK))
{
throw std::runtime_error("Failed to create pipe for unix_base_engine");
}
Expand Down
12 changes: 7 additions & 5 deletions src/unix/unix_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "../chez_socket.h"

int mark_nonblock_ioctl(int fd, int set);
int mark_nonblock_fcntl(int fd, int set);
int make_pipe(fd_t fds[2], int read_flags, int write_flags);
int unix_cloexec(int fd,int set);
int unix_nonblock_ioctl(int fd, int set);
int unix_nonblock_fcntl(int fd, int set);
int unix_make_pipe(fd_t fds[2], int read_flags, int write_flags);
int unix_make_socket(int domain, int type, int protocol);

#ifndef UNIX_PLATFORM_H
#define UNIX_PLATFORM_H
Expand All @@ -16,8 +18,8 @@ int make_pipe(fd_t fds[2], int read_flags, int write_flags);
defined(__linux__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__)
#define mark_nonblock mark_nonblock_ioctl
#define unix_nonblock unix_nonblock_ioctl
#else
#define mark_nonblock mark_nonblock_fcntl
#define unix_nonblock unix_nonblock_fcntl
#endif
#endif //UNIX_PLATFORM_H

0 comments on commit b7e412a

Please sign in to comment.