-
-
Save filipnavara/d5fb55bdb5edcceb1981f73078b855c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Network/Network.h> | |
#include <assert.h> | |
#include <stdio.h> | |
static void dump_hex(const void* data, size_t size) | |
{ | |
char ascii[17]; | |
size_t i, j; | |
ascii[16] = '\0'; | |
for (i = 0; i < size; ++i) { | |
printf("%02X ", ((unsigned char*)data)[i]); | |
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') { | |
ascii[i % 16] = ((unsigned char*)data)[i]; | |
} else { | |
ascii[i % 16] = '.'; | |
} | |
if ((i+1) % 8 == 0 || i+1 == size) { | |
printf(" "); | |
if ((i+1) % 16 == 0) { | |
printf("| %s \n", ascii); | |
} else if (i+1 == size) { | |
ascii[(i+1) % 16] = '\0'; | |
if ((i+1) % 16 <= 8) { | |
printf(" "); | |
} | |
for (j = (i+1) % 16; j < 16; ++j) { | |
printf(" "); | |
} | |
printf("| %s \n", ascii); | |
} | |
} | |
} | |
} | |
int main() | |
{ | |
nw_framer_output_handler_t framer_output_handler = ^(nw_framer_t framer, nw_framer_message_t message, size_t message_length, bool is_complete) { | |
printf("framer_output_handler got message with length %zu\n", message_length); | |
uint8_t *buffer = alloca(message_length); | |
nw_framer_parse_output(framer, 1, message_length, buffer, ^size_t(uint8_t *buffer, size_t buffer_length, bool is_complete) { | |
dump_hex(buffer, buffer_length); | |
return buffer_length; | |
}); | |
}; | |
nw_framer_input_handler_t framer_input_handler = ^size_t(nw_framer_t framer) { | |
printf("framer_input_handler\n"); | |
return 0; | |
}; | |
nw_framer_start_handler_t framer_start = ^nw_framer_start_result_t(nw_framer_t framer) { | |
printf("framer start\n"); | |
nw_protocol_options_t tlsOptions = nw_tls_create_options(); | |
sec_protocol_options_t sec_options = nw_tls_copy_sec_protocol_options(tlsOptions); | |
sec_protocol_options_set_tls_server_name(sec_options, "SERVER"); | |
nw_release(sec_options); | |
nw_framer_prepend_application_protocol(framer, tlsOptions); | |
nw_framer_set_output_handler(framer, framer_output_handler); | |
nw_framer_set_input_handler(framer, framer_input_handler); | |
return nw_framer_start_result_ready; | |
}; | |
nw_protocol_definition_t framer = nw_framer_create_definition("tlsFramer", NW_FRAMER_CREATE_FLAGS_DEFAULT, framer_start); | |
nw_protocol_options_t framer_options = nw_framer_create_options(framer); | |
nw_parameters_t parameters; // = nw_parameters_create(); | |
parameters = nw_parameters_create_secure_udp(NW_PARAMETERS_DISABLE_PROTOCOL, NW_PARAMETERS_DEFAULT_CONFIGURATION); | |
nw_protocol_stack_t protocol_stack = nw_parameters_copy_default_protocol_stack(parameters); | |
nw_protocol_stack_prepend_application_protocol(protocol_stack, framer_options); | |
//nw_protocol_stack_set_transport_protocol(protocol_stack, framer_options); | |
nw_release(protocol_stack); | |
nw_endpoint_t endpoint = nw_endpoint_create_host("127.0.0.1", "42"); | |
nw_connection_t connection = nw_connection_create(endpoint, parameters); | |
assert(connection != NULL); | |
//nw_retain(connection); | |
/*nw_connection_set_state_changed_handler(connection, ^(nw_connection_state_t state, nw_error_t error) { | |
nw_endpoint_t remote = nw_connection_copy_endpoint(connection); | |
errno = error ? nw_error_get_error_code(error) : 0; | |
if (state == nw_connection_state_waiting) { | |
printf("connect to %s port %u failed, is waiting\n", | |
nw_endpoint_get_hostname(remote), | |
nw_endpoint_get_port(remote)); | |
} else if (state == nw_connection_state_failed) { | |
printf("connect to %s port %u failed\n", | |
nw_endpoint_get_hostname(remote), | |
nw_endpoint_get_port(remote)); | |
} else if (state == nw_connection_state_ready) { | |
//if (g_verbose) { | |
fprintf(stderr, "Connection to %s port %u succeeded!\n", | |
nw_endpoint_get_hostname(remote), | |
nw_endpoint_get_port(remote)); | |
//} | |
} else if (state == nw_connection_state_cancelled) { | |
// Release the primary reference on the connection | |
// that was taken at creation time | |
nw_release(connection); | |
} | |
nw_release(remote); | |
});*/ | |
nw_connection_set_queue(connection, dispatch_get_main_queue()); | |
nw_connection_start(connection); | |
dispatch_data_t data = dispatch_data_create("DATA", 4, dispatch_get_main_queue(), ^{ }); | |
nw_connection_send(connection, data, NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT, true, ^(nw_error_t _Nullable error) { | |
if (error != NULL) { | |
errno = nw_error_get_error_code(error); | |
assert(false); | |
} | |
}); | |
dispatch_main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: