Skip to content

Commit

Permalink
clang formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sbSteveK committed Sep 24, 2024
1 parent 5b5a953 commit b7f13df
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 147 deletions.
2 changes: 1 addition & 1 deletion include/aws/io/private/dispatch_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* SPDX-License-Identifier: Apache-2.0.
*/

#include <dispatch/dispatch.h>
#include <Security/Security.h>
#include <aws/io/tls_channel_handler.h>
#include <dispatch/dispatch.h>

struct secure_transport_ctx {
struct aws_tls_ctx ctx;
Expand Down
2 changes: 1 addition & 1 deletion source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include <unistd.h>

#include <Block.h>
#include <aws/io/private/dispatch_queue.h>
#include <dispatch/dispatch.h>
#include <dispatch/queue.h>
#include <aws/io/private/dispatch_queue.h>

static void s_destroy(struct aws_event_loop *event_loop);
static int s_run(struct aws_event_loop *event_loop);
Expand Down
275 changes: 131 additions & 144 deletions source/darwin/nw_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <aws/io/logging.h>

#include <Network/Network.h>
#include <aws/io/private/tls_channel_handler_shared.h>
#include <aws/io/private/dispatch_queue.h>
#include <aws/io/private/tls_channel_handler_shared.h>

#include <arpa/inet.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -481,100 +481,103 @@ static int s_socket_connect_fn(
* was disconnected etc .... */
nw_connection_set_state_changed_handler(
socket->io_handle.data.handle, ^(nw_connection_state_t state, nw_error_t error) {
/* we're connected! */
if (state == nw_connection_state_ready) {
AWS_LOGF_INFO(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: connection success",
(void *)socket,
socket->io_handle.data.handle);
nw_socket->socket_open = true;
nw_path_t path = nw_connection_copy_current_path(socket->io_handle.data.handle);
nw_endpoint_t local_endpoint = nw_path_copy_effective_local_endpoint(path);
nw_release(path);
const char *hostname = nw_endpoint_get_hostname(local_endpoint);
uint16_t port = nw_endpoint_get_port(local_endpoint);

size_t hostname_len = strlen(hostname);
size_t buffer_size = AWS_ARRAY_SIZE(socket->local_endpoint.address);
size_t to_copy = aws_min_size(hostname_len, buffer_size);
memcpy(socket->local_endpoint.address, hostname, to_copy);
socket->local_endpoint.port = port;
nw_release(local_endpoint);

AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: local endpoint %s:%d",
(void *)socket,
socket->io_handle.data.handle,
socket->local_endpoint.address,
port);
// Cancel the connection timeout task
aws_event_loop_cancel_task(event_loop, &nw_socket->timeout_args->task);
socket->state = CONNECTED_WRITE | CONNECTED_READ;
nw_socket->setup_run = true;
aws_ref_count_acquire(&nw_socket->ref_count);
on_connection_result(socket, AWS_OP_SUCCESS, user_data);
aws_ref_count_release(&nw_socket->ref_count);
} else if (error) {
/* any error, including if closed remotely in error */
int error_code = nw_error_get_error_code(error);
AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: connection error %d",
(void *)socket,
socket->io_handle.data.handle,
error_code);

// Cancel the connection timeout task
aws_event_loop_cancel_task(event_loop, &nw_socket->timeout_args->task);

/* we don't let this thing do DNS or TLS. Everything had better be a posix error. */
// AWS_ASSERT(nw_error_get_error_domain(error) == nw_error_domain_posix);
// DEBUG WIP we do in fact allow this to do TLS
error_code = s_determine_socket_error(error_code);
nw_socket->last_error = error_code;
aws_raise_error(error_code);
socket->state = ERROR;
aws_ref_count_acquire(&nw_socket->ref_count);
if (!nw_socket->setup_run) {
on_connection_result(socket, error_code, user_data);
nw_socket->setup_run = true;
} else if (socket->readable_fn) {
socket->readable_fn(socket, nw_socket->last_error, socket->readable_user_data);
}
aws_ref_count_release(&nw_socket->ref_count);
} else if (state == nw_connection_state_cancelled || state == nw_connection_state_failed) {
/* this should only hit when the socket was closed by not us. Note,
* we uninstall this handler right before calling close on the socket so this shouldn't
* get hit unless it was triggered remotely */
// Cancel the connection timeout task
aws_event_loop_cancel_task(event_loop, &nw_socket->timeout_args->task);
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket closed remotely.",
(void *)socket, socket->io_handle.data.handle);
socket->state = CLOSED;
aws_ref_count_acquire(&nw_socket->ref_count);
aws_raise_error(AWS_IO_SOCKET_CLOSED);
if (!nw_socket->setup_run) {
on_connection_result(socket, AWS_IO_SOCKET_CLOSED, user_data);
nw_socket->setup_run = true;
} else if (socket->readable_fn) {
socket->readable_fn(socket, AWS_IO_SOCKET_CLOSED, socket->readable_user_data);
}
aws_ref_count_release(&nw_socket->ref_count);
} else if (state == nw_connection_state_waiting) {
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket connection is waiting for a usable network before re-attempting.",
(void *)socket, socket->io_handle.data.handle);
} else if (state == nw_connection_state_preparing) {
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket connection is in the process of establishing.",
(void *)socket, socket->io_handle.data.handle);
}
/* we're connected! */
if (state == nw_connection_state_ready) {
AWS_LOGF_INFO(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: connection success",
(void *)socket,
socket->io_handle.data.handle);
nw_socket->socket_open = true;
nw_path_t path = nw_connection_copy_current_path(socket->io_handle.data.handle);
nw_endpoint_t local_endpoint = nw_path_copy_effective_local_endpoint(path);
nw_release(path);
const char *hostname = nw_endpoint_get_hostname(local_endpoint);
uint16_t port = nw_endpoint_get_port(local_endpoint);

size_t hostname_len = strlen(hostname);
size_t buffer_size = AWS_ARRAY_SIZE(socket->local_endpoint.address);
size_t to_copy = aws_min_size(hostname_len, buffer_size);
memcpy(socket->local_endpoint.address, hostname, to_copy);
socket->local_endpoint.port = port;
nw_release(local_endpoint);

AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: local endpoint %s:%d",
(void *)socket,
socket->io_handle.data.handle,
socket->local_endpoint.address,
port);
// Cancel the connection timeout task
aws_event_loop_cancel_task(event_loop, &nw_socket->timeout_args->task);
socket->state = CONNECTED_WRITE | CONNECTED_READ;
nw_socket->setup_run = true;
aws_ref_count_acquire(&nw_socket->ref_count);
on_connection_result(socket, AWS_OP_SUCCESS, user_data);
aws_ref_count_release(&nw_socket->ref_count);
} else if (error) {
/* any error, including if closed remotely in error */
int error_code = nw_error_get_error_code(error);
AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: connection error %d",
(void *)socket,
socket->io_handle.data.handle,
error_code);

// Cancel the connection timeout task
aws_event_loop_cancel_task(event_loop, &nw_socket->timeout_args->task);

/* we don't let this thing do DNS or TLS. Everything had better be a posix error. */
// AWS_ASSERT(nw_error_get_error_domain(error) == nw_error_domain_posix);
// DEBUG WIP we do in fact allow this to do TLS
error_code = s_determine_socket_error(error_code);
nw_socket->last_error = error_code;
aws_raise_error(error_code);
socket->state = ERROR;
aws_ref_count_acquire(&nw_socket->ref_count);
if (!nw_socket->setup_run) {
on_connection_result(socket, error_code, user_data);
nw_socket->setup_run = true;
} else if (socket->readable_fn) {
socket->readable_fn(socket, nw_socket->last_error, socket->readable_user_data);
}
aws_ref_count_release(&nw_socket->ref_count);
} else if (state == nw_connection_state_cancelled || state == nw_connection_state_failed) {
/* this should only hit when the socket was closed by not us. Note,
* we uninstall this handler right before calling close on the socket so this shouldn't
* get hit unless it was triggered remotely */
// Cancel the connection timeout task
aws_event_loop_cancel_task(event_loop, &nw_socket->timeout_args->task);
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket closed remotely.",
(void *)socket,
socket->io_handle.data.handle);
socket->state = CLOSED;
aws_ref_count_acquire(&nw_socket->ref_count);
aws_raise_error(AWS_IO_SOCKET_CLOSED);
if (!nw_socket->setup_run) {
on_connection_result(socket, AWS_IO_SOCKET_CLOSED, user_data);
nw_socket->setup_run = true;
} else if (socket->readable_fn) {
socket->readable_fn(socket, AWS_IO_SOCKET_CLOSED, socket->readable_user_data);
}
aws_ref_count_release(&nw_socket->ref_count);
} else if (state == nw_connection_state_waiting) {
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket connection is waiting for a usable network before re-attempting.",
(void *)socket,
socket->io_handle.data.handle);
} else if (state == nw_connection_state_preparing) {
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket connection is in the process of establishing.",
(void *)socket,
socket->io_handle.data.handle);
}
});

nw_connection_start(socket->io_handle.data.handle);
Expand Down Expand Up @@ -1082,55 +1085,39 @@ static int s_socket_write_fn(
AWS_ASSERT(written_fn);

dispatch_data_t data = dispatch_data_create(cursor->ptr, cursor->len, NULL, DISPATCH_DATA_DESTRUCTOR_FREE);
nw_connection_send(
handle, data, _nw_content_context_default_message, true, ^(nw_error_t error) {
AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: processing write requests, called from aws_socket_write",
(void *)socket,
handle);

if (!nw_socket->socket_open) {
AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: socket closed",
(void *)socket,
handle);
written_fn(socket, 0, 0, user_data);
goto nw_socket_release;
}

AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: DEBUG:: callback writing message: %p",
(void *)socket,
handle, user_data);
int error_code = !error || nw_error_get_error_code(error) == 0
? AWS_OP_SUCCESS
: s_determine_socket_error(nw_error_get_error_code(error));

if (error_code) {
nw_socket->last_error = error_code;
aws_raise_error(error_code);
AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: error during write %d",
(void *)socket,
handle,
error_code);
}

size_t written_size = dispatch_data_get_size(data);
AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: send written size %d",
(void *)socket,
handle,
(int)written_size);
written_fn(socket, error_code, !error_code ? written_size : 0, user_data);
nw_socket_release:
aws_ref_count_release(&nw_socket->ref_count);
});
nw_connection_send(handle, data, _nw_content_context_default_message, true, ^(nw_error_t error) {
AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET,
"id=%p handle=%p: processing write requests, called from aws_socket_write",
(void *)socket,
handle);

if (!nw_socket->socket_open) {
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET, "id=%p handle=%p: socket closed", (void *)socket, handle);
written_fn(socket, 0, 0, user_data);
goto nw_socket_release;
}

AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET, "id=%p handle=%p: DEBUG:: callback writing message: %p", (void *)socket, handle, user_data);
int error_code = !error || nw_error_get_error_code(error) == 0
? AWS_OP_SUCCESS
: s_determine_socket_error(nw_error_get_error_code(error));

if (error_code) {
nw_socket->last_error = error_code;
aws_raise_error(error_code);
AWS_LOGF_ERROR(
AWS_LS_IO_SOCKET, "id=%p handle=%p: error during write %d", (void *)socket, handle, error_code);
}

size_t written_size = dispatch_data_get_size(data);
AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET, "id=%p handle=%p: send written size %d", (void *)socket, handle, (int)written_size);
written_fn(socket, error_code, !error_code ? written_size : 0, user_data);
nw_socket_release:
aws_ref_count_release(&nw_socket->ref_count);
});

return AWS_OP_SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion source/darwin/secure_transport_tls_channel_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include <aws/io/channel.h>
#include <aws/io/file_utils.h>
#include <aws/io/private/dispatch_queue.h>
#include <aws/io/private/pki_utils.h>
#include <aws/io/private/tls_channel_handler_shared.h>
#include <aws/io/private/dispatch_queue.h>
#include <aws/io/statistics.h>

#include <aws/io/logging.h>
Expand Down

0 comments on commit b7f13df

Please sign in to comment.