Skip to content

Commit

Permalink
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
Browse files Browse the repository at this point in the history
…to nw_socket
  • Loading branch information
xiazhvera committed Nov 9, 2024
2 parents 1b79cbd + 8bd9808 commit e613e5d
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 109 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ jobs:
macos:
runs-on: macos-14 # latest
strategy:
fail-fast: false
matrix:
eventloop: ["-DAWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE=ON", "-DAWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE=OFF"]
eventloop: ["-DAWS_USE_APPLE_NETWORK_FRAMEWORK=ON", "-DAWS_USE_APPLE_NETWORK_FRAMEWORK=OFF"]
steps:
- name: Build ${{ env.PACKAGE_NAME }} + consumers
run: |
Expand All @@ -180,8 +181,9 @@ jobs:
macos-debug:
runs-on: macos-14 # latest
strategy:
fail-fast: false
matrix:
eventloop: ["-DAWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE=ON", "-DAWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE=OFF"]
eventloop: ["-DAWS_USE_APPLE_NETWORK_FRAMEWORK=ON", "-DAWS_USE_APPLE_NETWORK_FRAMEWORK=OFF"]
steps:
- name: Build ${{ env.PACKAGE_NAME }} + consumers
run: |
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ if (USE_VSOCK)
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DUSE_VSOCK")
endif()

if (AWS_USE_APPLE_NETWORK_FRAMEWORK)
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DAWS_USE_APPLE_NETWORK_FRAMEWORK")
endif()

target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
Expand Down
11 changes: 9 additions & 2 deletions include/aws/io/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ struct aws_shutdown_callback_options;
struct aws_task;

/**
* Event Loop Type. If set to `AWS_ELT_PLATFORM_DEFAULT`, the event loop will automatically use the platform’s default
* event loop type.
* Event Loop Type. If set to `AWS_ELT_PLATFORM_DEFAULT`, the event loop will automatically use the platform’s default.
*
* Default Event Loop Type
* Linux | AWS_ELT_EPOLL
Expand Down Expand Up @@ -70,6 +69,14 @@ struct aws_event_loop_group_options {
aws_io_clock_fn *clock_override;
};

/**
* Return the default event loop type. If the return value is `AWS_ELT_PLATFORM_DEFAULT`, the function failed to
* retrieve the default type value.
* If `aws_event_loop_override_default_type` has been called, return the override default type.
*/
AWS_IO_API
enum aws_event_loop_type aws_event_loop_get_default_type(void);

AWS_EXTERN_C_BEGIN

/**
Expand Down
8 changes: 0 additions & 8 deletions include/aws/io/private/event_loop_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ struct aws_event_loop *aws_event_loop_new_epoll_with_options(
struct aws_allocator *alloc,
const struct aws_event_loop_options *options);

/**
* Return the default event loop type. If the return value is `AWS_ELT_PLATFORM_DEFAULT`, the function failed to
* retrieve the default type value.
* If `aws_event_loop_override_default_type` has been called, return the override default type.
*/
AWS_IO_API
enum aws_event_loop_type aws_event_loop_get_default_type(void);

typedef struct aws_event_loop *(aws_new_event_loop_fn)(struct aws_allocator *alloc,
const struct aws_event_loop_options *options,
void *new_loop_user_data);
Expand Down
36 changes: 35 additions & 1 deletion include/aws/io/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,30 @@ enum aws_socket_type {
AWS_SOCKET_DGRAM,
};

/**
* Socket Implementation type. Decides which socket implementation is used. If set to `AWS_SIT_PLATFORM_DEFAULT`, it
* will automatically use the platform’s default.
*
* PLATFORM DEFAULT SOCKET IMPLEMENTATION TYPE
* Linux | AWS_SIT_POSIX
* Windows | AWS_SIT_WINSOCK
* BSD Variants| AWS_SIT_POSIX
* MacOS | AWS_SIT_POSIX
* iOS | AWS_SIT_APPLE_NETWORK_FRAMEWORK
*/
enum aws_socket_impl_type {
AWS_SIT_PLATFORM_DEFAULT = 0,
AWS_SIT_POSIX,
AWS_SIT_WINSOCK,
AWS_SIT_APPLE_NETWORK_FRAMEWORK,
};

#define AWS_NETWORK_INTERFACE_NAME_MAX 16

struct aws_socket_options {
enum aws_socket_type type;
enum aws_socket_domain domain;
enum aws_socket_impl_type impl_type;
uint32_t connect_timeout_ms;
/* Keepalive properties are TCP only.
* Set keepalive true to periodically transmit messages for detecting a disconnected peer.
Expand Down Expand Up @@ -80,7 +99,7 @@ typedef void(aws_socket_on_connection_result_fn)(struct aws_socket *socket, int
* A user may want to call aws_socket_set_options() on the new socket if different options are desired.
*
* new_socket is not yet assigned to an event-loop. The user should call aws_socket_assign_to_event_loop() before
* performing IO operations. The user is resposnbile to releasing the socket memory after use.
* performing IO operations. The user is responsible to releasing the socket memory after use.
*
* When error_code is AWS_ERROR_SUCCESS, new_socket is the recently accepted connection.
* If error_code is non-zero, an error occurred and you should aws_socket_close() the socket.
Expand Down Expand Up @@ -186,6 +205,21 @@ aws_ms_fn_ptr aws_winsock_get_connectex_fn(void);
aws_ms_fn_ptr aws_winsock_get_acceptex_fn(void);
#endif

AWS_IO_API int aws_socket_init_posix(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options);

AWS_IO_API int aws_socket_init_winsock(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options);

AWS_IO_API int aws_socket_init_apple_nw_socket(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options);

AWS_EXTERN_C_BEGIN

/**
Expand Down
19 changes: 12 additions & 7 deletions source/event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct aws_event_loop *aws_event_loop_new_with_options(
break;
default:
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Invalid event loop type on the platform.");
aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
break;
}

Expand Down Expand Up @@ -552,10 +552,15 @@ void aws_event_loop_override_default_type(enum aws_event_loop_type default_type_
s_default_event_loop_type_override = default_type_override;
}

/**
* Return the default event loop type. If the return value is `AWS_ELT_PLATFORM_DEFAULT`, the function failed to
* retrieve the default type value.
* If `aws_event_loop_override_default_type` has been called, return the override default type.
*/
enum aws_event_loop_type aws_event_loop_get_default_type(void) {
#ifdef AWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE
#ifdef AWS_USE_APPLE_NETWORK_FRAMEWORK
aws_event_loop_override_default_type(AWS_ELT_DISPATCH_QUEUE);
#endif // AWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE
#endif // AWS_USE_APPLE_NETWORK_FRAMEWORK
if (s_default_event_loop_type_override != AWS_ELT_PLATFORM_DEFAULT) {
return s_default_event_loop_type_override;
}
Expand Down Expand Up @@ -584,25 +589,25 @@ static int aws_event_loop_type_validate_platform(enum aws_event_loop_type type)
case AWS_ELT_EPOLL:
#ifndef AWS_ENABLE_EPOLL
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Event loop type EPOLL is not supported on the platform.");
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
#endif // AWS_ENABLE_EPOLL
break;
case AWS_ELT_IOCP:
#ifndef AWS_ENABLE_IO_COMPLETION_PORTS
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Event loop type IOCP is not supported on the platform.");
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
#endif // AWS_ENABLE_IO_COMPLETION_PORTS
break;
case AWS_ELT_KQUEUE:
#ifndef AWS_ENABLE_KQUEUE
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Event loop type KQUEUE is not supported on the platform.");
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
#endif // AWS_ENABLE_KQUEUE
break;
case AWS_ELT_DISPATCH_QUEUE:
#ifndef AWS_ENABLE_DISPATCH_QUEUE
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Event loop type Dispatch Queue is not supported on the platform.");
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
#endif // AWS_ENABLE_DISPATCH_QUEUE
break;
default:
Expand Down
37 changes: 6 additions & 31 deletions source/posix/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static int s_socket_write(
static int s_socket_get_error(struct aws_socket *socket);
static bool s_socket_is_open(struct aws_socket *socket);

static struct aws_socket_vtable s_vtable = {
struct aws_socket_vtable g_posix_socket_vtable = {
.socket_cleanup_fn = s_socket_clean_up,
.socket_connect_fn = s_socket_connect,
.socket_bind_fn = s_socket_bind,
Expand Down Expand Up @@ -263,7 +263,7 @@ static int s_socket_init(
socket->state = INIT;
socket->options = *options;
socket->impl = posix_socket;
socket->vtable = &s_vtable;
socket->vtable = &g_posix_socket_vtable;

if (existing_socket_fd < 0) {
int err = s_create_socket(socket, options);
Expand Down Expand Up @@ -292,12 +292,13 @@ static int s_socket_init(
return AWS_OP_SUCCESS;
}

#if defined(AWS_USE_KQUEUE) || defined(AWS_USE_EPOLL)
int aws_socket_init(struct aws_socket *socket, struct aws_allocator *alloc, const struct aws_socket_options *options) {
int aws_socket_init_posix(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options) {
AWS_ASSERT(options);
return s_socket_init(socket, alloc, options, -1);
}
#endif // #ifdef AWS_USE_KQUEUE || AWS_USE_EPOLL

static void s_socket_clean_up(struct aws_socket *socket) {
if (!socket->impl) {
Expand Down Expand Up @@ -949,21 +950,6 @@ static int s_socket_bind(struct aws_socket *socket, const struct aws_socket_endp
return AWS_OP_ERR;
}

#if defined(AWS_USE_KQUEUE) || defined(AWS_USE_EPOLL)
int aws_socket_get_bound_address(const struct aws_socket *socket, struct aws_socket_endpoint *out_address) {
if (socket->local_endpoint.address[0] == 0) {
AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET,
"id=%p fd=%d: Socket has no local address. Socket must be bound first.",
(void *)socket,
socket->io_handle.data.fd);
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE);
}
*out_address = socket->local_endpoint;
return AWS_OP_SUCCESS;
}
#endif // AWS_USE_KQUEUE || AWS_USE_EPOLL

static int s_socket_listen(struct aws_socket *socket, int backlog_size) {
if (socket->state != BOUND) {
AWS_LOGF_ERROR(
Expand Down Expand Up @@ -2054,17 +2040,6 @@ static bool s_socket_is_open(struct aws_socket *socket) {
return socket->io_handle.data.fd >= 0;
}

#if defined(AWS_USE_KQUEUE) || defined(AWS_USE_EPOLL)
void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint *endpoint) {
struct aws_uuid uuid;
AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS);
char uuid_str[AWS_UUID_STR_LEN] = {0};
struct aws_byte_buf uuid_buf = aws_byte_buf_from_empty_array(uuid_str, sizeof(uuid_str));
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS);
snprintf(endpoint->address, sizeof(endpoint->address), "testsock" PRInSTR ".sock", AWS_BYTE_BUF_PRI(uuid_buf));
}
#endif // AWS_USE_KQUEUE || AWS_USE_EPOLL

bool aws_is_network_interface_name_valid(const char *interface_name) {
if (if_nametoindex(interface_name) == 0) {
AWS_LOGF_ERROR(AWS_LS_IO_SOCKET, "network_interface_name(%s) is invalid with errno: %d", interface_name, errno);
Expand Down
Loading

0 comments on commit e613e5d

Please sign in to comment.