Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple Network Framework Socket Changes #662

Open
wants to merge 199 commits into
base: grand_dispatch_queue
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 196 commits
Commits
Show all changes
199 commits
Select commit Hold shift + click to select a range
14a3386
socket related from network_framework_integration branch
sbSteveK Jul 29, 2024
9715a3e
Merge branch 'grand_dispatch_queue' into nw_socket
sbSteveK Jul 29, 2024
b830a86
missed s_socket_listen
sbSteveK Jul 29, 2024
cccbda2
move aws_socket_init_poll_based platform not supported function into …
sbSteveK Jul 30, 2024
caac9a5
small cleanups/comments
sbSteveK Jul 30, 2024
eb59ff1
Merge branch 'grand_dispatch_queue' into nw_socket
sbSteveK Jul 30, 2024
8a794de
nw_socket.c changes
sbSteveK Aug 8, 2024
553d45f
add nw_connection_t to nw_socket
sbSteveK Aug 9, 2024
cf610e9
read from socket works
sbSteveK Aug 12, 2024
07bac64
remove prints
sbSteveK Aug 13, 2024
dd1fbf2
trivial edits
sbSteveK Aug 13, 2024
2fd514c
check correct vtable func
sbSteveK Aug 13, 2024
2a0da42
clang format
sbSteveK Aug 13, 2024
9cc6620
socket_test add a manual way to set event_loop_style in options
sbSteveK Aug 13, 2024
e3281ee
event_loop add undefined event loop style and clang format
sbSteveK Aug 13, 2024
43fd436
clang format
sbSteveK Aug 13, 2024
1c1cd02
event_loop.c clang formatting and configurations
sbSteveK Aug 13, 2024
88f6de3
formatting
sbSteveK Aug 13, 2024
cf53cc6
format
sbSteveK Aug 13, 2024
a7ab224
macos errors
sbSteveK Aug 13, 2024
62fd06d
fix test
sbSteveK Aug 13, 2024
cbb8c42
formatting
sbSteveK Aug 13, 2024
4ce33ee
test fix
sbSteveK Aug 13, 2024
29ab896
prototype void
sbSteveK Aug 13, 2024
f9cd5d0
fix style func
sbSteveK Aug 13, 2024
2e55d2d
sprintf -> snprintf
sbSteveK Aug 13, 2024
6938bc3
manual default change for testing
sbSteveK Aug 21, 2024
4658492
Merge branch 'grand_dispatch_queue' into nw_socket
sbSteveK Sep 3, 2024
ef8d53f
Merge branch 'grand_dispatch_queue' of https://github.com/awslabs/aws…
xiazhvera Sep 11, 2024
731ba49
setup connection timeout
xiazhvera Sep 13, 2024
bac8b07
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Sep 17, 2024
429bf26
remove debug changes
xiazhvera Sep 17, 2024
2282f8f
Branched nw_socket work (#674)
sbSteveK Sep 24, 2024
8e7b351
veriify_peer spelling
sbSteveK Sep 24, 2024
54e58e9
remove redundant secure_transport_ctx
sbSteveK Sep 24, 2024
b01b510
channel_args creation simplification
sbSteveK Sep 24, 2024
5b5a953
empty line
sbSteveK Sep 24, 2024
b7f13df
clang formatting
sbSteveK Sep 24, 2024
b325809
more clang
sbSteveK Sep 24, 2024
86b4343
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Sep 26, 2024
9471d05
fix merge error and socket completion hanlder changes
xiazhvera Sep 26, 2024
789d446
fix cmakefile
xiazhvera Sep 26, 2024
33aa640
Merge branch 'grand_dispatch_queue' into nw_socket
sbSteveK Oct 3, 2024
68fa441
Clean Up Socket Vtable (#678)
xiazhvera Oct 4, 2024
a47fd44
remove platform.h
xiazhvera Oct 4, 2024
b4cd993
remove platform.h
xiazhvera Oct 4, 2024
d071082
[WIP]nw_socket protect socket data with lock (#685)
xiazhvera Oct 15, 2024
dd33861
clean up comments
xiazhvera Oct 15, 2024
f950453
more read me and clean up
xiazhvera Oct 15, 2024
c188594
add logs to track tls_channel_shutdown_with_cache_window_update_after…
xiazhvera Oct 15, 2024
6a1c03a
clang-format
xiazhvera Oct 15, 2024
871ae9b
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Oct 15, 2024
a0be9d2
clean up comments
xiazhvera Oct 16, 2024
32728ba
wait for socket release
xiazhvera Oct 16, 2024
9e8015c
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Oct 16, 2024
3350e6c
clang-format fix
xiazhvera Oct 16, 2024
2592e47
fix cmake changes
xiazhvera Oct 16, 2024
f8024d2
kqueue and epoll used the posix socket
xiazhvera Oct 16, 2024
0e24c22
update compile flags...
xiazhvera Oct 16, 2024
40b6b81
update dispatch queue
xiazhvera Oct 16, 2024
81992ba
fix merge change
xiazhvera Oct 16, 2024
e89119a
clang-format
xiazhvera Oct 16, 2024
776977e
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Oct 25, 2024
2aafe5f
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Oct 25, 2024
ada3e09
lint
xiazhvera Oct 25, 2024
fbe538f
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Oct 25, 2024
86f3b2d
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Oct 25, 2024
72b51a0
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 8, 2024
69c22b4
clean up merge changes
xiazhvera Nov 8, 2024
1b79cbd
fix dispatch queue test flag
xiazhvera Nov 8, 2024
e613e5d
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 9, 2024
28b7546
update socket api changes for network framework
xiazhvera Nov 9, 2024
351c682
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 9, 2024
f24d637
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 9, 2024
0d69dd7
add apple socket init
xiazhvera Nov 9, 2024
c408e30
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 11, 2024
145e7d7
hide dispatch_queue.h
xiazhvera Nov 11, 2024
3a4eaca
fix merge error
xiazhvera Nov 11, 2024
35934d3
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 12, 2024
11d0f84
rename enum
xiazhvera Nov 12, 2024
8320127
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 12, 2024
652195b
remove warning on nw socket
xiazhvera Nov 12, 2024
1637a88
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 12, 2024
658463a
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 12, 2024
d0dddda
extend the shutdown wait time
xiazhvera Nov 12, 2024
31fd471
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Nov 29, 2024
d784f96
include private socket header
xiazhvera Nov 29, 2024
d880859
improve readme
xiazhvera Dec 3, 2024
6b9a05b
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Dec 20, 2024
630d0bb
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 7, 2025
1dc7c7d
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 8, 2025
0b7ba13
update renamed files
xiazhvera Jan 8, 2025
e9576c0
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 8, 2025
9b8fb15
[WIP]socket shutdown test
xiazhvera Jan 9, 2025
535d92b
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 9, 2025
95464ab
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 9, 2025
a5094d2
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 9, 2025
4992f4e
Merge branch 'nw_socket' of github.com:awslabs/aws-c-io into nw_socke…
xiazhvera Jan 10, 2025
0588e5c
[WIP] quick test releasing connection in aws_socket_close
xiazhvera Jan 13, 2025
c094abf
verify connection is released
xiazhvera Jan 13, 2025
96c3c71
[WIP]test remove shutdown callbacks
xiazhvera Jan 13, 2025
d114817
[WIP]remove sleep for tests
xiazhvera Jan 13, 2025
84b3299
[WIP]test socket without channel change
xiazhvera Jan 16, 2025
c65daa0
[WIP]partially fixed the race condition
xiazhvera Jan 17, 2025
a457935
[wip]fix print and test again
xiazhvera Jan 17, 2025
967379a
validate close state before processing
xiazhvera Jan 17, 2025
0397c14
avoid state update after closing socket.
xiazhvera Jan 21, 2025
aabba3c
quick dispatch queue test
xiazhvera Jan 21, 2025
5f95766
fix event loop lock
xiazhvera Jan 21, 2025
e9e5ee3
fix compile
xiazhvera Jan 21, 2025
cff2fd1
WIP try not null out state_change handler
xiazhvera Jan 23, 2025
dbeb628
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 23, 2025
44bb099
Merge branch 'nw_socket' of github.com:awslabs/aws-c-io into nw_socke…
xiazhvera Jan 23, 2025
d960c5a
WIP: shutdown callback invoked after aws_socket_clean_up()
xiazhvera Jan 27, 2025
a4c1468
revert cancel changes
xiazhvera Jan 28, 2025
b98c09a
experiment for close callback in socket handler
xiazhvera Jan 29, 2025
03eb5a1
improve channel handler shutdown...
xiazhvera Jan 29, 2025
7e709cc
setup shutdown callback on socket close
xiazhvera Jan 30, 2025
b1fcf8d
fix cmake merge
xiazhvera Jan 30, 2025
9733a56
fix use-after-free print
xiazhvera Jan 30, 2025
9fc5d4a
fix syntax
xiazhvera Jan 30, 2025
d0b3143
WIP add lock for socket state
xiazhvera Jan 31, 2025
1f9b1ef
TEST: test non-callback unit tests
xiazhvera Jan 31, 2025
c8484df
workaround deadlock...
xiazhvera Jan 31, 2025
1fa11ed
improve event loop validation
xiazhvera Feb 3, 2025
b3cc904
add cleanup callback...
xiazhvera Feb 3, 2025
4becc61
update test to avoid deadlock
xiazhvera Feb 3, 2025
b79b9a3
fix nw_socket state update workflow
xiazhvera Feb 3, 2025
2c54a76
TEST: clean up socket access
xiazhvera Feb 3, 2025
5e24039
TEST socket wrapped with lock
xiazhvera Feb 4, 2025
19cd3c1
TEST: sleep to wait for event loop cleanup
xiazhvera Feb 4, 2025
fe95355
clean up socket and more comments
xiazhvera Feb 4, 2025
73f701a
rename some functions
xiazhvera Feb 5, 2025
7bb9d06
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Feb 6, 2025
96e8643
fix merge conflicts
xiazhvera Feb 6, 2025
7590685
Merge branch 'nw_socket' of github.com:awslabs/aws-c-io into nw_socke…
xiazhvera Feb 6, 2025
7809d15
acquire event loop group for nw_socket
xiazhvera Feb 6, 2025
cdfab7e
set CI us DAWS_USE_APPLE_NETWORK_FRAMEWORK
xiazhvera Feb 6, 2025
43ee31f
use shutdown callback in test
xiazhvera Feb 6, 2025
c4d7096
add more sleep
xiazhvera Feb 6, 2025
36bf219
add more sleep & improve readable task
xiazhvera Feb 6, 2025
8709bb2
TEST: a quick hacky adjustment for unit tests..
xiazhvera Feb 7, 2025
1f4def1
clean up channel_bootstrap
xiazhvera Feb 7, 2025
f641e4e
add shutdown callbacks for windows
xiazhvera Feb 7, 2025
e6fcc3c
fix windows&setup listener state task
xiazhvera Feb 7, 2025
439b5ef
revert complete changes
xiazhvera Feb 7, 2025
1dfd6fa
TEST: verify event loop group ref count
xiazhvera Feb 7, 2025
3fc15b3
use event loop group in unit test
xiazhvera Feb 7, 2025
8ed037c
fix missing lock
xiazhvera Feb 7, 2025
de14a43
fix condition_var notify
xiazhvera Feb 10, 2025
35f94d9
clang-format
xiazhvera Feb 10, 2025
369eb0b
socket close on peer close
xiazhvera Feb 10, 2025
8c4bdba
NW Socket Shutdown Process Update (#705)
xiazhvera Feb 10, 2025
be1c90e
clean up
xiazhvera Feb 10, 2025
acd6659
Merge branch 'nw_socket' of github.com:awslabs/aws-c-io into nw_socket
xiazhvera Feb 10, 2025
07c46bd
Checkpoint
Feb 10, 2025
79e8b0e
fix channel destory after callback
xiazhvera Feb 10, 2025
e258edc
Checkpoint before migrating permanently to socket branch
Feb 11, 2025
b5098c0
add listener connect callback
xiazhvera Feb 11, 2025
0ad1471
Shutdown rework initial impl
Feb 11, 2025
a25c892
Merge branch 'dq_shutdown' into nw_socket_shutdown2
Feb 11, 2025
0c2face
use default socket impl
xiazhvera Feb 12, 2025
6aa2ee9
Updates
Feb 12, 2025
469615f
Merge branch 'dq_shutdown' into nw_socket_shutdown2
Feb 12, 2025
f661727
Apply updates and remove dispatch queue sleep
Feb 12, 2025
a49b771
fix listener test and on peer socket shutdown
xiazhvera Feb 12, 2025
2627d1c
Race condition fix. A connection that completes instantly would caus…
Feb 12, 2025
8d66c7f
fix state lock
xiazhvera Feb 12, 2025
7d7fc09
expose aws_socket_get_default_impl_type for test
xiazhvera Feb 12, 2025
9bb0a1e
Merge branch 'nw_socket' into nw_socket_shutdown2
Feb 12, 2025
cd41c8f
Doc updates
Feb 12, 2025
4c2a4d1
Merge branch 'dq_shutdown' into nw_socket_shutdown2
Feb 12, 2025
f81423f
Oops
Feb 12, 2025
d49533c
Merge branch 'dq_shutdown' into nw_socket_shutdown2
Feb 12, 2025
bd91776
schedule read request even after the last peer closed
xiazhvera Feb 12, 2025
e837f77
Merge branch 'nw_socket' of github.com:awslabs/aws-c-io into nw_socke…
xiazhvera Feb 12, 2025
6a95f72
bug fix: wrong lock
xiazhvera Feb 13, 2025
5fe71c6
Merge branch 'nw_socket' of github.com:awslabs/aws-c-io into nw_socke…
xiazhvera Feb 13, 2025
8dc88fd
use is_complete to close the socket
xiazhvera Feb 13, 2025
83981c3
add prints to trace read queue
xiazhvera Feb 13, 2025
7dec84e
try fix processing read data on error
xiazhvera Feb 14, 2025
bdcd5ee
WIP: do not cancel connection before write finished
xiazhvera Feb 17, 2025
b5a0e85
WIP: do not cancel connection before write finished
xiazhvera Feb 17, 2025
2d312da
WIP DEBUG read operation on closing
xiazhvera Feb 17, 2025
6043ce4
clean up socket
xiazhvera Feb 17, 2025
2f7eec7
update code review
xiazhvera Feb 18, 2025
043b468
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Feb 18, 2025
5ebcf77
Merge branch 'nw_socket_shutdown' of github.com:awslabs/aws-c-io into…
xiazhvera Feb 18, 2025
17d2190
fix merge
xiazhvera Feb 18, 2025
4b1a553
generaize aws_socket_start_accept api
xiazhvera Feb 18, 2025
b86e381
improve socket state setup
xiazhvera Feb 18, 2025
098b1bd
improve task mem allocation
xiazhvera Feb 19, 2025
c973549
fix socket_cancel and windows
xiazhvera Feb 19, 2025
0368bd9
make sure nw_socket close has an event loop
xiazhvera Feb 19, 2025
304f588
more code review feedback
xiazhvera Feb 19, 2025
214913a
fix windows socket compilation
xiazhvera Feb 19, 2025
031a0e0
fix race condition for releasing socket
xiazhvera Feb 24, 2025
fbcafd8
[WIP] Test nw_socket with downstream (#711)
xiazhvera Feb 26, 2025
f4e37dc
rename locks...
xiazhvera Feb 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ jobs:
run: |
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
chmod a+x builder
./builder build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-DAWS_USE_APPLE_DISPATCH_QUEUE=${{ matrix.eventloop == 'dispatch_queue' && 'ON' || 'OFF' }} --cmake-extra=-DENABLE_SANITIZERS=ON --cmake-extra=-DSANITIZERS="${{ matrix.sanitizers }}"
./builder build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-DAWS_USE_APPLE_NETWORK_FRAMEWORK=${{ matrix.eventloop == 'dispatch_queue' && 'ON' || 'OFF' }} --cmake-extra=-DENABLE_SANITIZERS=ON --cmake-extra=-DSANITIZERS="${{ matrix.sanitizers }}"

macos-x64:
runs-on: macos-14-large # latest
Expand Down Expand Up @@ -274,7 +274,7 @@ jobs:
run: |
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
chmod a+x builder
./builder build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-DAWS_USE_APPLE_DISPATCH_QUEUE=${{ matrix.eventloop == 'dispatch_queue' && 'ON' || 'OFF' }} --cmake-extra=-DENABLE_SANITIZERS=ON --cmake-extra=-DSANITIZERS="${{ matrix.sanitizers }}" --config Debug
./builder build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-DAWS_USE_APPLE_NETWORK_FRAMEWORK=${{ matrix.eventloop == 'dispatch_queue' && 'ON' || 'OFF' }} --cmake-extra=-DENABLE_SANITIZERS=ON --cmake-extra=-DSANITIZERS="${{ matrix.sanitizers }}" --config Debug

freebsd:
runs-on: ubuntu-24.04 # latest
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ file(GLOB AWS_IO_TESTING_HEADERS
"include/aws/testing/*.h"
)


file(GLOB AWS_IO_PRIV_HEADERS
"include/aws/io/private/*.h"
)
Expand Down
65 changes: 52 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ Core to Async-IO is the event-loop. We provide an implementation for most platfo
Platform | Implementation
--- | ---
Linux | Edge-Triggered Epoll
BSD Variants and Apple Devices | KQueue
BSD Variants | KQueue
Apple Devices | KQueue or Apple Dispatch Queue
Windows | IOCP (IO Completion Ports)

Also, you can always implement your own as well.
Expand Down Expand Up @@ -645,12 +646,49 @@ All exported functions, simply shim into the v-table and return.

We include a cross-platform API for sockets. We support TCP and UDP using IPv4 and IPv6, and Unix Domain sockets. On Windows,
we use Named Pipes to support the functionality of Unix Domain sockets. On Windows, this is implemented with winsock2, and on
all unix platforms we use the posix API.
all unix platforms we use the posix API. Then we will use Apple Network Framework on Apple platforms.

Upon a connection being established, the new socket (either as the result of a `connect()` or `start_accept()` call)
will not be attached to any event loops. It is your responsibility to register it with an event loop to begin receiving
notifications.


#### V-Table
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this documentation necessary in the public readme? A giant list of functions feels like a distraction

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would help with anybody who would like to extend the socket implementation. Also, we listed the vtable for other classes as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this does not belong here. Are people supposed to keep this in sync with the real definition? Anyone looking at the code can look at the real definition.

As far as I'm concerned, no vtables should be present in a library README.


struct aws_socket_vtable {
void (*socket_cleanup_fn)(struct aws_socket *socket);
int (*socket_connect_fn)(
struct aws_socket *socket,
const struct aws_socket_endpoint *remote_endpoint,
struct aws_event_loop *event_loop,
aws_socket_on_connection_result_fn *on_connection_result,
void *user_data);
int (*socket_bind_fn)(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint);
int (*socket_listen_fn)(struct aws_socket *socket, int backlog_size);
int (*socket_start_accept_fn)(
struct aws_socket *socket,
struct aws_event_loop *accept_loop,
aws_socket_on_accept_result_fn *on_accept_result,
void *user_data);
int (*socket_stop_accept_fn)(struct aws_socket *socket);
int (*socket_close_fn)(struct aws_socket *socket);
int (*socket_shutdown_dir_fn)(struct aws_socket *socket, enum aws_channel_direction dir);
int (*socket_set_options_fn)(struct aws_socket *socket, const struct aws_socket_options *options);
int (*socket_assign_to_event_loop_fn)(struct aws_socket *socket, struct aws_event_loop *event_loop);
int (*socket_subscribe_to_readable_events_fn)(
struct aws_socket *socket,
aws_socket_on_readable_fn *on_readable,
void *user_data);
int (*socket_read_fn)(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read);
int (*socket_write_fn)(
struct aws_socket *socket,
const struct aws_byte_cursor *cursor,
aws_socket_on_write_completed_fn *written_fn,
void *user_data);
int (*socket_get_error_fn)(struct aws_socket *socket);
bool (*socket_is_open_fn)(struct aws_socket *socket);
};

#### API
typedef enum aws_socket_domain {
AWS_SOCKET_IPV4,
Expand Down Expand Up @@ -717,32 +755,33 @@ Shuts down any pending operations on the socket, and cleans up state. The socket

int aws_socket_connect(struct aws_socket *socket, struct aws_socket_endpoint *remote_endpoint);

Connects to a remote endpoint. In UDP, this simply binds the socket to a remote address for use with `aws_socket_write()`,
and if the operation is successful, the socket can immediately be used for write operations.
Connects to a remote endpoint. In TCP and all Apple Network Framework connections (regardless it is UDP, TCP or LOCAL), when the connection succeed, you still must wait on the `on_connection_established()` callback to
be invoked before using the socket.

In TCP, this will function will not block. If the return value is successful, then you must wait on the `on_connection_established()`
callback to be invoked before using the socket.
In UDP, this simply binds the socket to a remote address for use with `aws_socket_write()`, and if the operation is successful,
the socket can immediately be used for write operations.

For LOCAL (Unix Domain Sockets or Named Pipes), the socket will be immediately ready for use upon a successful return.

int aws_socket_bind(struct aws_socket *socket, struct aws_socket_endpoint *local_endpoint);

Binds the socket to a local address. In UDP mode, the socket is ready for `aws_socket_read()` operations. In connection oriented
modes, you still must call `aws_socket_listen()` and `aws_socket_start_accept()` before using the socket.
Binds the socket to a local address. In UDP mode, the socket is ready for `aws_socket_read()` operations. In connection oriented
modes or if you are using Apple Network Framework (regardless it is UDP or TCP), you still must call `aws_socket_listen()` and
`aws_socket_start_accept()` before using the socket.

int aws_socket_listen(struct aws_socket *socket, int backlog_size);

TCP and LOCAL only. Sets up the socket to listen on the address bound to in `aws_socket_bind()`.
TCP, LOCAL, and Apple Network Framework only. Sets up the socket to listen on the address bound to in `aws_socket_bind()`.

int aws_socket_start_accept(struct aws_socket *socket);

TCP and LOCAL only. The socket will begin accepting new connections. This is an asynchronous operation. New connections will
arrive via the `on_incoming_connection()` callback.
TCP, LOCAL, and Apple Network Framework only. The socket will begin accepting new connections. This is an asynchronous operation. New
connections will arrive via the `on_incoming_connection()` callback.

int aws_socket_stop_accept(struct aws_socket *socket);

TCP and LOCAL only. The socket will shutdown the listener. It is safe to call `aws_socket_start_accept()` again after this
operation.
TCP, LOCAL, and Apple Network Framework only. The socket will shutdown the listener. It is safe to call `aws_socket_start_accept()`
again after this operation.

int aws_socket_close(struct aws_socket *socket);

Expand Down
12 changes: 12 additions & 0 deletions include/aws/io/channel_bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ typedef void(aws_server_bootstrap_on_accept_channel_shutdown_fn)(
struct aws_channel *channel,
void *user_data);

/**
* This function is only used for async listener (Apple Network Framework in this case).
* Once the server listener socket is finished setup and starting listening, this fuction
* will be invoked.
*/
typedef void(
aws_server_bootstrap_on_listener_setup_fn)(struct aws_server_bootstrap *bootstrap, int error_code, void *user_data);

/**
* Once the server listener socket is finished destroying, and all the existing connections are closed, this fuction
* will be invoked.
Expand Down Expand Up @@ -212,6 +220,7 @@ struct aws_server_socket_channel_bootstrap_options {
const struct aws_tls_connection_options *tls_options;
aws_server_bootstrap_on_accept_channel_setup_fn *incoming_callback;
aws_server_bootstrap_on_accept_channel_shutdown_fn *shutdown_callback;
aws_server_bootstrap_on_listener_setup_fn *setup_callback;
aws_server_bootstrap_on_server_listener_destroy_fn *destroy_callback;
bool enable_read_back_pressure;
void *user_data;
Expand Down Expand Up @@ -296,6 +305,9 @@ AWS_IO_API int aws_server_bootstrap_set_alpn_callback(
AWS_IO_API struct aws_socket *aws_server_bootstrap_new_socket_listener(
const struct aws_server_socket_channel_bootstrap_options *bootstrap_options);

AWS_IO_API struct aws_socket *aws_server_bootstrap_new_socket_listener_async(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just make all listener implementations use an async path?

const struct aws_server_socket_channel_bootstrap_options *bootstrap_options);

/**
* Shuts down 'listener' and cleans up any resources associated with it. Any incoming channels on `listener` will still
* be active. `destroy_callback` will be invoked after the server socket listener is destroyed, and all associated
Expand Down
19 changes: 17 additions & 2 deletions include/aws/io/private/socket_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ typedef void (*aws_ms_fn_ptr)(void);
void aws_check_and_init_winsock(void);
aws_ms_fn_ptr aws_winsock_get_connectex_fn(void);
aws_ms_fn_ptr aws_winsock_get_acceptex_fn(void);
#else // NOT ON WINDOWS
struct socket_address {
union sock_addr_types {
struct sockaddr_in addr_in;
struct sockaddr_in6 addr_in6;
struct sockaddr_un un_addr;
#ifdef USE_VSOCK
struct sockaddr_vm vm_addr;
#endif
} sock_addr_types;
};
#endif

int aws_socket_init_posix(
Expand Down Expand Up @@ -48,8 +59,7 @@ struct aws_socket_vtable {
int (*socket_start_accept_fn)(
struct aws_socket *socket,
struct aws_event_loop *accept_loop,
aws_socket_on_accept_result_fn *on_accept_result,
void *user_data);
struct aws_socket_listener_options options);
int (*socket_stop_accept_fn)(struct aws_socket *socket);
int (*socket_close_fn)(struct aws_socket *socket);
int (*socket_shutdown_dir_fn)(struct aws_socket *socket, enum aws_channel_direction dir);
Expand All @@ -67,6 +77,11 @@ struct aws_socket_vtable {
void *user_data);
int (*socket_get_error_fn)(struct aws_socket *socket);
bool (*socket_is_open_fn)(struct aws_socket *socket);
int (*socket_set_close_callback)(struct aws_socket *socket, aws_socket_on_shutdown_complete_fn fn, void *user_data);
int (*socket_set_cleanup_callback)(
struct aws_socket *socket,
aws_socket_on_shutdown_complete_fn fn,
void *user_data);
};

#endif // AWS_IO_SOCKET_IMPL_H
59 changes: 55 additions & 4 deletions include/aws/io/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum aws_socket_impl_type {

#define AWS_NETWORK_INTERFACE_NAME_MAX 16

typedef void(aws_socket_on_shutdown_complete_fn)(void *user_data);

struct aws_socket_options {
enum aws_socket_type type;
enum aws_socket_domain domain;
Expand All @@ -65,6 +67,9 @@ struct aws_socket_options {
uint16_t keep_alive_max_failed_probes;
bool keepalive;

aws_socket_on_shutdown_complete_fn *on_shutdown_complete;
void *shutdown_user_data;

/**
* THIS IS AN EXPERIMENTAL AND UNSTABLE API
* (Optional)
Expand All @@ -90,6 +95,15 @@ struct aws_event_loop;
*/
typedef void(aws_socket_on_connection_result_fn)(struct aws_socket *socket, int error_code, void *user_data);

/**
* Called by a listening socket when a listener accept has successfully initialized or an error has occurred.
* If the listener was successful error_code will be AWS_ERROR_SUCCESS and the socket has already been assigned
* to the event loop specified in aws_socket_start_accept().
*
* If an error occurred error_code will be non-zero.
*/
typedef void(aws_socket_on_accept_started_fn)(struct aws_socket *socket, int error_code, void *user_data);

/**
* Called by a listening socket when either an incoming connection has been received or an error occurred.
*
Expand All @@ -116,14 +130,15 @@ typedef void(aws_socket_on_accept_result_fn)(
* Callback for when the data passed to a call to aws_socket_write() has either completed or failed.
* On success, error_code will be AWS_ERROR_SUCCESS.
*
* `socket` may be NULL in the callback if the socket is released and cleaned up before a callback is triggered.
* by the system I/O handler,
* `socket` may be NULL in the callback if the socket is released and cleaned up before the callback is triggered.
*/
typedef void(
aws_socket_on_write_completed_fn)(struct aws_socket *socket, int error_code, size_t bytes_written, void *user_data);
/**
* Callback for when socket is either readable (edge-triggered) or when an error has occurred. If the socket is
* readable, error_code will be AWS_ERROR_SUCCESS.
*
* `socket` may be NULL in the callback if the socket is released and cleaned up before the callback is triggered.
*/
typedef void(aws_socket_on_readable_fn)(struct aws_socket *socket, int error_code, void *user_data);

Expand Down Expand Up @@ -156,6 +171,16 @@ struct aws_socket {
void *impl;
};

struct aws_socket_listener_options {
aws_socket_on_accept_result_fn *on_accept_result;
void *on_accept_result_user_data;

// This callback is invoked when the listener starts accepting incoming connections.
// It is only triggered in asynchronous listener APIs while using nw_socket.
aws_socket_on_accept_started_fn *on_accept_start_result;
void *on_accept_start_user_data;
};

struct aws_byte_buf;
struct aws_byte_cursor;

Expand Down Expand Up @@ -227,8 +252,7 @@ AWS_IO_API int aws_socket_listen(struct aws_socket *socket, int backlog_size);
AWS_IO_API int aws_socket_start_accept(
struct aws_socket *socket,
struct aws_event_loop *accept_loop,
aws_socket_on_accept_result_fn *on_accept_result,
void *user_data);
struct aws_socket_listener_options options);

/**
* TCP, LOCAL and VSOCK only. The listening socket will stop accepting new connections.
Expand All @@ -239,12 +263,33 @@ AWS_IO_API int aws_socket_start_accept(
*/
AWS_IO_API int aws_socket_stop_accept(struct aws_socket *socket);

/**
* Apple Network Framework only. The callback that will triggered when aws_socket_close() finished. The callback
* will be called from the socket event loop.
*/
AWS_IO_API int aws_socket_set_close_complete_callback(
struct aws_socket *socket,
aws_socket_on_shutdown_complete_fn fn,
void *user_data);

/**
* Apple Network Framework only. The callback that will triggered when aws_socket_cleanup() finished. And
* it is only safe to release the socket afterwards. The callback will be called from the socket event loop.
*/
AWS_IO_API int aws_socket_set_cleanup_complete_callback(
struct aws_socket *socket,
aws_socket_on_shutdown_complete_fn fn,
void *user_data);

/**
* Calls `close()` on the socket and unregisters all io operations from the event loop. This function must be called
* from the event-loop's thread unless this is a listening socket. If it's a listening socket it can be called from any
* non-event-loop thread or the event-loop the socket is currently assigned to. If called from outside the event-loop,
* this function will block waiting on the socket to close. If this is called from an event-loop thread other than
* the one it's assigned to, it presents the possibility of a deadlock, so don't do it.
*
* If you are using Apple Network Framework, you should always call this function from an event-loop thread regardless
* it is a server or client socket.
*/
AWS_IO_API int aws_socket_close(struct aws_socket *socket);

Expand Down Expand Up @@ -358,6 +403,12 @@ AWS_IO_API void aws_socket_endpoint_init_local_address_for_test(struct aws_socke
* network_interface_name on Windows */
AWS_IO_API bool aws_is_network_interface_name_valid(const char *interface_name);

/**
* Get default impl type based on the platform.
* For user in internal tests only
*/
AWS_IO_API enum aws_socket_impl_type aws_socket_get_default_impl_type(void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeating in-person comment: can we look into privatizing APIs that are only used internally by the socket handler? The more we can make private, the better I feel about all the special conditions and terms on the socket APIs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aws_socket_get_default_impl_type is also used in downstream c-http library to determine if we should wait for async callbacks. Even with a generalize API, we might still need handle socket differently..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but I'm talking about the entire public API for sockets. How much of it needs to be public?


AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

Expand Down
Loading
Loading