From 795aebbee77bb74d8792df96dfe1aa79ec9548fc Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 10 Jul 2017 15:02:38 -0700 Subject: Give up on uncrustify; switch to clang-format. --- .clang-format | 14 ++ .travis.yml | 2 +- etc/README.adoc | 39 ++-- etc/format-check.sh | 55 +++++ etc/nng.sublime-project | 5 +- etc/uncrustify.cfg | 408 ---------------------------------- etc/uncrustify_check.sh | 51 ----- src/core/aio.c | 65 +++--- src/core/aio.h | 44 ++-- src/core/clock.c | 1 - src/core/defs.h | 173 +++++++------- src/core/device.c | 20 +- src/core/endpt.c | 72 ++---- src/core/endpt.h | 64 +++--- src/core/event.c | 3 +- src/core/event.h | 18 +- src/core/idhash.c | 66 +++--- src/core/idhash.h | 40 ++-- src/core/init.c | 4 +- src/core/list.c | 48 ++-- src/core/list.h | 36 +-- src/core/message.c | 99 +++------ src/core/message.h | 36 +-- src/core/msgqueue.c | 151 ++++++------- src/core/msgqueue.h | 8 +- src/core/nng_impl.h | 4 +- src/core/objhash.c | 126 +++++------ src/core/objhash.h | 17 +- src/core/options.c | 57 ++--- src/core/options.h | 10 +- src/core/panic.c | 25 +-- src/core/panic.h | 2 +- src/core/pipe.c | 33 +-- src/core/pipe.h | 32 +-- src/core/platform.h | 19 +- src/core/protocol.c | 9 +- src/core/protocol.h | 48 ++-- src/core/random.c | 153 +++++++------ src/core/socket.c | 113 +++------- src/core/socket.h | 95 ++++---- src/core/taskq.c | 49 ++-- src/core/taskq.h | 20 +- src/core/thread.c | 23 +- src/core/thread.h | 18 +- src/core/timer.c | 37 ++- src/core/timer.h | 12 +- src/core/transport.c | 15 +- src/core/transport.h | 45 ++-- src/nng.c | 109 +++------ src/nng.h | 126 ++++++----- src/nng_compat.c | 197 ++++++++-------- src/nng_compat.h | 72 +++--- src/platform/posix/posix_aio.h | 15 +- src/platform/posix/posix_alloc.c | 2 - src/platform/posix/posix_clock.c | 26 +-- src/platform/posix/posix_config.h | 14 +- src/platform/posix/posix_debug.c | 15 +- src/platform/posix/posix_epdesc.c | 117 ++++------ src/platform/posix/posix_impl.h | 23 +- src/platform/posix/posix_ipc.c | 56 ++--- src/platform/posix/posix_net.c | 62 +++--- src/platform/posix/posix_pipe.c | 24 +- src/platform/posix/posix_pipedesc.c | 79 +++---- src/platform/posix/posix_pollq.h | 30 +-- src/platform/posix/posix_pollq_poll.c | 104 ++++----- src/platform/posix/posix_rand.c | 25 +-- src/platform/posix/posix_resolv_gai.c | 84 +++---- src/platform/posix/posix_thread.c | 55 ++--- src/platform/windows/win_clock.c | 7 +- src/platform/windows/win_debug.c | 40 ++-- src/platform/windows/win_impl.h | 48 ++-- src/platform/windows/win_iocp.c | 57 ++--- src/platform/windows/win_ipc.c | 211 ++++++++---------- src/platform/windows/win_net.c | 34 ++- src/platform/windows/win_pipe.c | 27 +-- src/platform/windows/win_rand.c | 11 +- src/platform/windows/win_resolv.c | 75 +++---- src/platform/windows/win_thread.c | 42 +--- src/protocol/bus/bus.c | 100 ++++----- src/protocol/pair/pair.c | 84 +++---- src/protocol/pipeline/pull.c | 73 +++--- src/protocol/pipeline/push.c | 83 +++---- src/protocol/pubsub/pub.c | 95 ++++---- src/protocol/pubsub/sub.c | 97 ++++---- src/protocol/reqrep/rep.c | 178 +++++++-------- src/protocol/reqrep/req.c | 161 ++++++-------- src/protocol/survey/respond.c | 161 ++++++-------- src/protocol/survey/survey.c | 138 +++++------- src/transport/inproc/inproc.c | 168 ++++++-------- src/transport/ipc/ipc.c | 218 ++++++++---------- src/transport/tcp/tcp.c | 221 ++++++++---------- 91 files changed, 2469 insertions(+), 3579 deletions(-) create mode 100644 .clang-format create mode 100755 etc/format-check.sh delete mode 100644 etc/uncrustify.cfg delete mode 100755 etc/uncrustify_check.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..ff7c2f08 --- /dev/null +++ b/.clang-format @@ -0,0 +1,14 @@ +BasedOnStyle: WebKit +UseTab: ForIndentation +IndentWidth: 8 +ColumnLimit: 79 +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true +AlignTrailingComments: true +AlignEscapedNewlinesLeft: true +PointerAlignment: Right +ForEachMacros: ['NNI_LIST_FOREACH'] +AlwaysBreakAfterReturnType: TopLevelDefinitions +SpaceAfterCStyleCast: true +AllowShortFunctionsOnASingleLine: Inline +BreakBeforeBinaryOperators: None diff --git a/.travis.yml b/.travis.yml index caed76e4..3c0e53a0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,4 +30,4 @@ script: - cmake -DCMAKE_BUILD_TYPE=Debug .. - cmake --build . -- -j4 - ctest --output-on-failure -C Debug -j4 - - ../etc/uncrustify_check.sh + - ../etc/format-check.sh diff --git a/etc/README.adoc b/etc/README.adoc index 90faf708..b6470a3b 100644 --- a/etc/README.adoc +++ b/etc/README.adoc @@ -5,39 +5,34 @@ This directory contains support files that I use for developing this project. I recommend others consider doing the same. -Coding Style & Uncrustify +Coding Style ~~~~~~~~~~~~~~~~~~~~~~~~~ I'm using Sublime Text as my development IDE, and so I've set things up -for that. +for that. But, I think you should be able to use any reasonable editor. -The project coding style is automatically checked by the Uncrustify -configuration, and there is a shell script here that will validate it. -Uncrustify is not (for now) enforced outside of src/ (in particular it -will probably choke on the stuff in ./tests). I may fix that one day. +A clang-format configuration file is included in the project, you should +use that. (clang format requires that the style file live at the top of +of the project tree, not somewhere else, such as this etc directory.) The +format rules are probably inappropriate for the test/ subdirectory, as +the Convey framework has a style all it's own. -I am running this using Uncrustify version 0.60 -- and I know Trusty Tahr -has 0.59 which also seems to work. Older versions seem to generate different -ugly output, and I have not tested the new versions. Until updated otherwise, -the coding style is as enforced by this config with Uncrustify 0.60. +The style is based loosely on WebKit, but is really modified -- someday +if clang-format ever learns about BSD KNF I'll probably switch to that. + +Once upon a time this used uncrustify. However, the frequent breaking changes +in uncrustify versions, and the fact that the latest (at this time 0.65) does +not actually support one of the most common language constructs (extern "C" +opening braces) has driven me towards clang-format. The good news is that +this is probably actually easier for most folks to use. Sublime Text ~~~~~~~~~~~~ -I have a custom version of the Sublime Uncrustify package, which understands -project-specific settings. (I also have submitted an upstream PR for those -chaanges here: https://github.com/obxyann/Sublime-Uncrustify/pulls so -hopefully this functionality will be available to others.) The Sublime -project file here takes care of seting this up (nng.sublime-project) - -I have the arranged in my Uncrustify Sublime package to configure both -the location of the binary, and the location of the configuration, so that -Alt-U will autoformat my buffer. I've also arranged for Sublime text to -understand that .h is C, not C++ (this is important!) +I've also arranged for Sublime text to understand that .h is C, not C++ (this +is important!) -Configuring Uncrustify without these enhancements is a little tricker, and -will be specific to your setup. ISO Standard C ~~~~~~~~~~~~~~ diff --git a/etc/format-check.sh b/etc/format-check.sh new file mode 100755 index 00000000..ea9bd2f2 --- /dev/null +++ b/etc/format-check.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# +# Copyright 2016 Garrett D'Amore +# +# This software is supplied under the terms of the MIT License, a +# copy of which should be located in the distribution where this +# file was obtained (LICENSE.txt). A copy of the license may also be +# found online at https://opensource.org/licenses/MIT. +# + +# +# This script is used to run uncrustify and report files that don't match. +# It looks for .c and .h files, located in ../src, and uses the config file +# uncrustify.cfg located in the same directory as this script. It only handles +# C language at this point. +# +mydir=`dirname $0` +srcdir=${mydir}/../src +failed= + +clang-format -version > /dev/null +if [ $? -ne 0 ]; then + echo "clang-format not found. Skipping checks." + exit 0 +fi + +mytmpdir=`mktemp -d` + +for file in `find ${srcdir} -name '*.[ch]' -print` +do + ext=${file##*.} + oldf=${file} + newf=${mytmpdir}/new.${ext} + clang-format -style=file ${oldf} > ${newf} + cmp -s ${oldf} ${newf} + if [ $? -ne 0 ] + then + echo "${file} style changes" + if [ -t 1 ]; then + colordiff -u $oldf $newf + else + diff -u $oldf $newf + fi + failed=1 + fi +done +rm -rf $mytmpdir +if [ -n "$failed" ] +then + echo "Format differences found!" 1>&2 + # Sadly, there are different versions of Uncrustify, and they don't + # seem to universally agree. So let's not trigger a build error on + # this -- but instead just emit it to standard output. + exit 0 +fi diff --git a/etc/nng.sublime-project b/etc/nng.sublime-project index 3de94b35..8bb2ea31 100644 --- a/etc/nng.sublime-project +++ b/etc/nng.sublime-project @@ -9,6 +9,9 @@ { "tab_size": 8, "translate_tabs_to_spaces": false, - "uncrustify_config": "${project_dir}/uncrustify.cfg" + "ClangFormat": { + "style": "File", + "format_on_save": true + } } } diff --git a/etc/uncrustify.cfg b/etc/uncrustify.cfg deleted file mode 100644 index 2fb5ed69..00000000 --- a/etc/uncrustify.cfg +++ /dev/null @@ -1,408 +0,0 @@ -# Uncrustify 0.55 -# -# This is based on the FreeBSD uncrustify, except that we have -# made the following changes: -# -# * NNI_LIST_FOREACH is treated like a FOR instead of the FreeBSD list macro. -# -# * sp_after_cast is set to true, because we like spaces after casts (like -# illumos does. -# -# * sp_sizeof_paren is set to add. sizeof is a keyword, and we want keywords -# to get that extra space. -# -# * align_var_struct_span is set to 99, because struct member align is nice -# -# This differs from illumos in a few key regard: -# -# * NNI_LIST_FOREACH gets a space before the opening brace. I'm not -# sure cstyle understands this syntax at all. We know how to fix that, -# but it seems to me that this is a for() block, and should use the -# spacing rules. -# -# * Comments are aligned using pure spaces. This is actually better than -# the illumos cstyle thing, because tabs should never be used for alignment, -# only indentation. The illumos style is dead wrong, and fubar for anyone -# who wants to use an editor with "indents" other than 8. -# -# * #define is not followed by tab but rather by space. This is one it -# might be nice to fix, but frankly its just too much work to make this -# right. -# -# make #define turn into #define -# -set FOR NNI_LIST_FOREACH -newlines = lf -input_tab_size = 8 -output_tab_size = 8 -string_escape_char = 92 -string_escape_char2 = 0 -indent_columns = 8 -indent_continue = 4 -indent_with_tabs = 2 -indent_align_string = false -indent_xml_string = 0 -indent_brace = 0 -indent_braces = false -indent_braces_no_func = false -indent_brace_parent = false -indent_namespace = false -indent_namespace_level = 0 -indent_namespace_limit = 0 -indent_extern = false -indent_class = true -indent_class_colon = true -indent_else_if = false -indent_var_def_blk = 0 -indent_func_call_param = false -indent_func_def_param = false -indent_func_proto_param = false -indent_func_class_param = false -indent_func_ctor_var_param = false -indent_template_param = false -indent_func_param_double = false -indent_func_const = 0 -indent_func_throw = 0 -indent_member = 0 -indent_sing_line_comments = 0 -indent_relative_single_line_comments = false -indent_switch_case = 0 -indent_case_shift = 0 -indent_case_brace = 0 -indent_col1_comment = false -indent_label = 1 -indent_access_spec = 1 -indent_access_spec_body = false -indent_paren_nl = false -indent_paren_close = 0 -indent_comma_paren = false -indent_bool_paren = false -indent_square_nl = false -indent_preserve_sql = false -indent_align_assign = true -sp_arith = ignore -sp_assign = force -sp_before_assign = ignore -sp_after_assign = ignore -sp_enum_assign = ignore -sp_enum_before_assign = ignore -sp_enum_after_assign = ignore -sp_pp_concat = add -sp_pp_stringify = add -sp_bool = force -sp_compare = force -sp_inside_paren = remove -sp_paren_paren = remove -sp_balance_nested_parens = false -sp_paren_brace = ignore -sp_before_ptr_star = force -sp_before_unnamed_ptr_star = ignore -sp_between_ptr_star = remove -sp_after_ptr_star = remove -sp_after_ptr_star_func = ignore -sp_before_ptr_star_func = ignore -sp_before_byref = remove -sp_before_unnamed_byref = ignore -sp_after_byref = force -sp_after_byref_func = ignore -sp_before_byref_func = ignore -sp_after_type = force -sp_template_angle = ignore -sp_before_angle = remove -sp_inside_angle = remove -sp_after_angle = force -sp_angle_paren = ignore -sp_angle_word = ignore -sp_before_sparen = force -sp_inside_sparen = remove -sp_inside_sparen_close = ignore -sp_after_sparen = force -sp_sparen_brace = add -sp_invariant_paren = ignore -sp_after_invariant_paren = ignore -sp_special_semi = ignore -sp_before_semi = remove -sp_before_semi_for = ignore -sp_before_semi_for_empty = remove -sp_after_semi = add -sp_after_semi_for = force -sp_after_semi_for_empty = remove -sp_before_square = ignore -sp_before_squares = ignore -sp_inside_square = remove -sp_after_comma = force -sp_before_comma = remove -sp_before_ellipsis = ignore -sp_after_class_colon = ignore -sp_before_class_colon = ignore -sp_before_case_colon = remove -sp_after_operator = ignore -sp_after_operator_sym = ignore -#sp_after_cast = remove -sp_after_cast = add -sp_inside_paren_cast = ignore -sp_cpp_cast_paren = ignore -#sp_sizeof_paren = remove -sp_sizeof_paren = add -sp_after_tag = ignore -sp_inside_braces_enum = force -sp_inside_braces_struct = force -sp_inside_braces = force -sp_inside_braces_empty = ignore -sp_type_func = force -sp_func_proto_paren = remove -sp_func_def_paren = remove -sp_inside_fparens = ignore -sp_inside_fparen = remove -sp_square_fparen = ignore -sp_fparen_brace = add -sp_func_call_paren = remove -sp_func_call_user_paren = ignore -sp_func_class_paren = remove -sp_return_paren = force -sp_attribute_paren = ignore -sp_defined_paren = ignore -sp_throw_paren = ignore -sp_macro = ignore -sp_macro_func = ignore -sp_else_brace = add -sp_brace_else = add -sp_brace_typedef = force -sp_catch_brace = ignore -sp_brace_catch = ignore -sp_finally_brace = ignore -sp_brace_finally = ignore -sp_try_brace = ignore -sp_getset_brace = ignore -sp_before_dc = remove -sp_after_dc = remove -sp_d_array_colon = ignore -sp_not = remove -sp_inv = remove -sp_addr = remove -sp_member = remove -sp_deref = remove -sp_sign = remove -sp_incdec = remove -sp_before_nl_cont = add -sp_after_oc_scope = ignore -sp_after_oc_colon = ignore -sp_before_oc_colon = ignore -sp_after_send_oc_colon = ignore -sp_before_send_oc_colon = ignore -sp_after_oc_type = ignore -sp_after_oc_return_type = ignore -sp_after_oc_at_sel = ignore -sp_before_oc_block_caret = ignore -sp_after_oc_block_caret = ignore -sp_cond_colon = ignore -sp_cond_question = ignore -sp_case_label = remove -sp_range = ignore -sp_cmt_cpp_start = ignore -sp_endif_cmt = ignore -align_keep_tabs = false -align_with_tabs = true -align_on_tabstop = true -align_number_left = true -align_func_params = false -align_same_func_call_params = false -align_var_def_span = 0 -align_var_def_star_style = 0 -align_var_def_amp_style = 0 -align_var_def_thresh = 0 -align_var_def_gap = 0 -align_var_def_colon = false -align_var_def_attribute = false -align_var_def_inline = false -align_assign_span = 0 -align_assign_thresh = 12 -align_enum_equ_span = 16 -align_enum_equ_thresh = 0 -align_var_struct_span = 99 -align_var_struct_thresh = 0 -align_var_struct_gap = 0 -align_struct_init_span = 3 -align_typedef_gap = 3 -align_typedef_span = 5 -align_typedef_func = 0 -align_typedef_star_style = 0 -align_typedef_amp_style = 0 -align_right_cmt_span = 3 -align_right_cmt_mix = false -align_right_cmt_gap = 0 -align_right_cmt_at_col = 0 -align_func_proto_span = 0 -align_func_proto_gap = 0 -align_on_operator = false -align_mix_var_proto = false -align_single_line_func = false -align_single_line_brace = false -align_single_line_brace_gap = 0 -align_oc_msg_spec_span = 0 -align_nl_cont = true -align_pp_define_gap = 4 -align_pp_define_span = 3 -align_left_shift = true -align_oc_msg_colon_span = 0 -nl_collapse_empty_body = false -nl_assign_leave_one_liners = true -nl_class_leave_one_liners = true -nl_enum_leave_one_liners = false -nl_getset_leave_one_liners = false -nl_func_leave_one_liners = false -nl_if_leave_one_liners = false -nl_start_of_file = remove -nl_start_of_file_min = 0 -nl_end_of_file = force -nl_end_of_file_min = 1 -nl_assign_brace = remove -nl_assign_square = ignore -nl_after_square_assign = ignore -nl_func_var_def_blk = 1 -nl_fcall_brace = add -nl_enum_brace = remove -nl_struct_brace = remove -nl_union_brace = remove -nl_if_brace = remove -nl_brace_else = remove -nl_elseif_brace = ignore -nl_else_brace = remove -nl_else_if = remove -nl_brace_finally = ignore -nl_finally_brace = ignore -nl_try_brace = ignore -nl_getset_brace = force -nl_for_brace = remove -nl_catch_brace = ignore -nl_brace_catch = ignore -nl_while_brace = remove -nl_brace_brace = ignore -nl_do_brace = remove -nl_brace_while = remove -nl_switch_brace = remove -nl_multi_line_cond = false -nl_multi_line_define = true -nl_before_case = false -nl_before_throw = ignore -nl_after_case = false -nl_namespace_brace = ignore -nl_template_class = ignore -nl_class_brace = ignore -nl_class_init_args = ignore -nl_func_type_name = ignore -nl_func_type_name_class = ignore -nl_func_scope_name = ignore -nl_func_proto_type_name = ignore -nl_func_paren = remove -nl_func_decl_start = ignore -nl_func_decl_start_single = ignore -nl_func_decl_args = ignore -nl_func_decl_end = ignore -nl_func_decl_end_single = ignore -nl_func_decl_empty = ignore -nl_fdef_brace = add -nl_after_return = true -nl_return_expr = ignore -nl_after_semicolon = true -nl_after_brace_open = true -nl_after_brace_open_cmt = false -nl_after_vbrace_open = false -nl_after_vbrace_open_empty = false -nl_after_brace_close = true -nl_define_macro = false -nl_squeeze_ifdef = false -nl_before_if = ignore -nl_after_if = ignore -nl_before_for = ignore -nl_after_for = ignore -nl_before_while = ignore -nl_after_while = ignore -nl_before_switch = ignore -nl_after_switch = ignore -nl_before_do = ignore -nl_after_do = ignore -nl_ds_struct_enum_cmt = false -nl_ds_struct_enum_close_brace = false -nl_class_colon = ignore -nl_create_if_one_liner = false -nl_create_for_one_liner = false -nl_create_while_one_liner = false -pos_arith = ignore -pos_assign = ignore -pos_bool = trail -pos_compare = ignore -pos_conditional = ignore -pos_comma = ignore -pos_class_comma = ignore -pos_class_colon = ignore -code_width = 80 -ls_for_split_full = false -ls_func_split_full = false -nl_max = 4 -nl_after_func_proto = 0 -nl_after_func_proto_group = 2 -nl_after_func_body = 3 -nl_after_func_body_one_liner = 0 -nl_before_block_comment = 2 -nl_before_c_comment = 0 -nl_before_cpp_comment = 0 -nl_after_multiline_comment = false -nl_before_access_spec = 0 -nl_after_access_spec = 0 -nl_comment_func_def = 1 -nl_after_try_catch_finally = 0 -nl_around_cs_property = 0 -nl_between_get_set = 0 -eat_blanks_after_open_brace = true -eat_blanks_before_close_brace = true -mod_full_brace_do = remove -mod_full_brace_for = add -mod_full_brace_function = ignore -mod_full_brace_if = add -mod_full_brace_if_chain = false -mod_full_brace_nl = 0 -mod_full_brace_while = add -mod_paren_on_return = add -mod_pawn_semicolon = false -mod_full_paren_if_bool = true -mod_remove_extra_semicolon = true -mod_add_long_function_closebrace_comment = 0 -mod_add_long_switch_closebrace_comment = 0 -mod_add_long_ifdef_endif_comment = 0 -mod_add_long_ifdef_else_comment = 0 -mod_sort_import = false -mod_sort_using = false -mod_sort_include = false -mod_move_case_break = false -mod_case_brace = remove -mod_remove_empty_return = true -cmt_width = 0 -cmt_reflow_mode = 0 -cmt_indent_multi = true -cmt_c_group = false -cmt_c_nl_start = false -cmt_c_nl_end = false -cmt_cpp_group = false -cmt_cpp_nl_start = false -cmt_cpp_nl_end = false -cmt_cpp_to_c = false -cmt_star_cont = true -cmt_sp_before_star_cont = 0 -cmt_sp_after_star_cont = 0 -cmt_multi_check_last = true -cmt_insert_file_header = "" -cmt_insert_file_footer = "" -cmt_insert_func_header = "" -cmt_insert_class_header = "" -cmt_insert_before_preproc = false -pp_indent = remove -pp_indent_at_level = false -pp_indent_count = 1 -pp_space = ignore -pp_space_count = 0 -pp_indent_region = 0 -pp_region_indent_code = false -pp_indent_if = 0 -pp_if_indent_code = false -pp_define_at_level = false diff --git a/etc/uncrustify_check.sh b/etc/uncrustify_check.sh deleted file mode 100755 index e59c8425..00000000 --- a/etc/uncrustify_check.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/sh -# -# Copyright 2016 Garrett D'Amore -# -# This software is supplied under the terms of the MIT License, a -# copy of which should be located in the distribution where this -# file was obtained (LICENSE.txt). A copy of the license may also be -# found online at https://opensource.org/licenses/MIT. -# - -# -# This script is used to run uncrustify and report files that don't match. -# It looks for .c and .h files, located in ../src, and uses the config file -# uncrustify.cfg located in the same directory as this script. It only handles -# C language at this point. -# -mydir=`dirname $0` -srcdir=${mydir}/../src -failed= - -uncrustify --version > /dev/null -if [ $? -ne 0 ]; then - echo "Uncrustify not found. Skipping checks." - exit 0 -fi - -for file in `find ${srcdir} -name '*.[ch]' -print` -do - uncrustify -c "${mydir}/uncrustify.cfg" -q -lC $file - if [ $? -ne 0 ]; then - echo "Cannot run uncrustify??" 1>&2 - exit 2 - fi - if [ -t 1 ]; then - colordiff -u $file $file.uncrustify - else - diff -u $file $file.uncrustify - fi - if [ $? -ne 0 ]; then - failed=1 - fi - rm ${file}.uncrustify -done -if [ -n "$failed" ] -then - echo "Uncrustify differences found!" 1>&2 - # Sadly, there are different versions of Uncrustify, and they don't - # seem to universally agree. So let's not trigger a build error on - # this -- but instead just emit it to standard output. - exit 0 -fi diff --git a/src/core/aio.c b/src/core/aio.c index c7d2b0b0..b69671b0 100644 --- a/src/core/aio.c +++ b/src/core/aio.c @@ -7,13 +7,13 @@ // found online at https://opensource.org/licenses/MIT. // -#include #include "core/nng_impl.h" +#include -#define NNI_AIO_WAKE (1<<0) -#define NNI_AIO_DONE (1<<1) -#define NNI_AIO_FINI (1<<2) -#define NNI_AIO_STOP (1<<3) +#define NNI_AIO_WAKE (1 << 0) +#define NNI_AIO_DONE (1 << 1) +#define NNI_AIO_FINI (1 << 2) +#define NNI_AIO_STOP (1 << 3) int nni_aio_init(nni_aio *aio, nni_cb cb, void *arg) @@ -21,10 +21,10 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg) int rv; if (cb == NULL) { - cb = (nni_cb) nni_aio_wake; + cb = (nni_cb) nni_aio_wake; arg = aio; } - memset(aio, 0, sizeof (*aio)); + memset(aio, 0, sizeof(*aio)); if ((rv = nni_mtx_init(&aio->a_lk)) != 0) { return (rv); } @@ -32,16 +32,15 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg) nni_mtx_fini(&aio->a_lk); return (rv); } - aio->a_cb = cb; - aio->a_cbarg = arg; + aio->a_cb = cb; + aio->a_cbarg = arg; aio->a_expire = NNI_TIME_NEVER; - aio->a_flags = 0; + aio->a_flags = 0; nni_taskq_ent_init(&aio->a_tqe, cb, arg); return (0); } - void nni_aio_fini(nni_aio *aio) { @@ -72,7 +71,6 @@ nni_aio_fini(nni_aio *aio) } } - int nni_aio_result(nni_aio *aio) { @@ -80,21 +78,19 @@ nni_aio_result(nni_aio *aio) nni_mtx_lock(&aio->a_lk); rv = aio->a_result; - if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) { + if (aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP)) { rv = NNG_ECANCELED; } nni_mtx_unlock(&aio->a_lk); return (rv); } - size_t nni_aio_count(nni_aio *aio) { return (aio->a_count); } - void nni_aio_wake(nni_aio *aio) { @@ -104,18 +100,16 @@ nni_aio_wake(nni_aio *aio) nni_mtx_unlock(&aio->a_lk); } - void nni_aio_wait(nni_aio *aio) { nni_mtx_lock(&aio->a_lk); - while ((aio->a_flags & (NNI_AIO_WAKE|NNI_AIO_FINI)) == 0) { + while ((aio->a_flags & (NNI_AIO_WAKE | NNI_AIO_FINI)) == 0) { nni_cv_wait(&aio->a_cv); } nni_mtx_unlock(&aio->a_lk); } - int nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data) { @@ -123,28 +117,27 @@ nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data) NNI_ASSERT(aio->a_prov_cancel == NULL); nni_mtx_lock(&aio->a_lk); - aio->a_flags &= ~(NNI_AIO_DONE|NNI_AIO_WAKE); - if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) { + aio->a_flags &= ~(NNI_AIO_DONE | NNI_AIO_WAKE); + if (aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP)) { // We should not reschedule anything at this point. nni_mtx_unlock(&aio->a_lk); return (NNG_ECANCELED); } - aio->a_result = 0; - aio->a_count = 0; + aio->a_result = 0; + aio->a_count = 0; aio->a_prov_cancel = cancel; - aio->a_prov_data = data; + aio->a_prov_data = data; nni_mtx_unlock(&aio->a_lk); return (0); } - void nni_aio_stop(nni_aio *aio) { void (*cancelfn)(nni_aio *); nni_mtx_lock(&aio->a_lk); - aio->a_flags |= NNI_AIO_DONE|NNI_AIO_STOP; + aio->a_flags |= NNI_AIO_DONE | NNI_AIO_STOP; cancelfn = aio->a_prov_cancel; nni_mtx_unlock(&aio->a_lk); @@ -154,7 +147,7 @@ nni_aio_stop(nni_aio *aio) } nni_mtx_lock(&aio->a_lk); - aio->a_prov_data = NULL; + aio->a_prov_data = NULL; aio->a_prov_cancel = NULL; nni_cv_wake(&aio->a_cv); nni_mtx_unlock(&aio->a_lk); @@ -164,7 +157,6 @@ nni_aio_stop(nni_aio *aio) nni_taskq_cancel(NULL, &aio->a_tqe); } - void nni_aio_cancel(nni_aio *aio) { @@ -179,7 +171,7 @@ nni_aio_cancel(nni_aio *aio) } aio->a_flags |= NNI_AIO_DONE; aio->a_result = NNG_ECANCELED; - cancelfn = aio->a_prov_cancel; + cancelfn = aio->a_prov_cancel; nni_mtx_unlock(&aio->a_lk); // This unregisters the AIO from the provider. @@ -189,16 +181,15 @@ nni_aio_cancel(nni_aio *aio) nni_mtx_lock(&aio->a_lk); // These should have already been cleared by the cancel function. - aio->a_prov_data = NULL; + aio->a_prov_data = NULL; aio->a_prov_cancel = NULL; - if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) { + if (!(aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP))) { nni_taskq_dispatch(NULL, &aio->a_tqe); } nni_mtx_unlock(&aio->a_lk); } - // I/O provider related functions. void @@ -211,25 +202,23 @@ nni_aio_finish(nni_aio *aio, int result, size_t count) return; } aio->a_flags |= NNI_AIO_DONE; - aio->a_result = result; - aio->a_count = count; + aio->a_result = result; + aio->a_count = count; aio->a_prov_cancel = NULL; - aio->a_prov_data = NULL; + aio->a_prov_data = NULL; - if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) { + if (!(aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP))) { nni_taskq_dispatch(NULL, &aio->a_tqe); } nni_mtx_unlock(&aio->a_lk); } - void nni_aio_list_init(nni_list *list) { NNI_LIST_INIT(list, nni_aio, a_prov_node); } - void nni_aio_list_append(nni_list *list, nni_aio *aio) { @@ -237,14 +226,12 @@ nni_aio_list_append(nni_list *list, nni_aio *aio) nni_list_append(list, aio); } - void nni_aio_list_remove(nni_aio *aio) { nni_list_node_remove(&aio->a_prov_node); } - int nni_aio_list_active(nni_aio *aio) { diff --git a/src/core/aio.h b/src/core/aio.h index ee2b084e..1b1f58e1 100644 --- a/src/core/aio.h +++ b/src/core/aio.h @@ -15,41 +15,41 @@ #include "core/taskq.h" #include "core/thread.h" -typedef struct nni_aio_ops nni_aio_ops; +typedef struct nni_aio_ops nni_aio_ops; // An nni_aio is an async I/O handle. struct nni_aio { - int a_result; // Result code (nng_errno) - size_t a_count; // Bytes transferred (I/O only) - nni_cb a_cb; // User specified callback. - void * a_cbarg; // Callback argument. - nni_time a_expire; + int a_result; // Result code (nng_errno) + size_t a_count; // Bytes transferred (I/O only) + nni_cb a_cb; // User specified callback. + void * a_cbarg; // Callback argument. + nni_time a_expire; // These fields are private to the aio framework. - nni_mtx a_lk; - nni_cv a_cv; - unsigned a_flags; - nni_taskq_ent a_tqe; + nni_mtx a_lk; + nni_cv a_cv; + unsigned a_flags; + nni_taskq_ent a_tqe; // Read/write operations. - nni_iov a_iov[4]; - int a_niov; + nni_iov a_iov[4]; + int a_niov; // Message operations. - nni_msg * a_msg; + nni_msg *a_msg; // Connect/accept operations. - void * a_endpt; // opaque endpoint handle - void * a_pipe; // opaque pipe handle + void *a_endpt; // opaque endpoint handle + void *a_pipe; // opaque pipe handle // Resolver operations. - nni_sockaddr * a_addrs; - int a_naddrs; + nni_sockaddr *a_addrs; + int a_naddrs; // Provider-use fields. - void (*a_prov_cancel)(nni_aio *); - void * a_prov_data; - nni_list_node a_prov_node; + void (*a_prov_cancel)(nni_aio *); + void * a_prov_data; + nni_list_node a_prov_node; }; // nni_aio_init initializes an aio object. The callback is called with @@ -95,14 +95,14 @@ extern void nni_aio_wait(nni_aio *); extern void nni_aio_list_init(nni_list *); extern void nni_aio_list_append(nni_list *, nni_aio *); extern void nni_aio_list_remove(nni_aio *); -extern int nni_aio_list_active(nni_aio *); +extern int nni_aio_list_active(nni_aio *); // nni_aio_finish is called by the provider when an operation is complete. // The provider gives the result code (0 for success, an NNG errno otherwise), // and the amount of data transferred (if any). extern void nni_aio_finish(nni_aio *, int, size_t); -extern int nni_aio_start(nni_aio *, void (*)(nni_aio *), void *); +extern int nni_aio_start(nni_aio *, void (*)(nni_aio *), void *); extern void nni_aio_stop(nni_aio *); #endif // CORE_AIO_H diff --git a/src/core/clock.c b/src/core/clock.c index 3e36358a..31678f67 100644 --- a/src/core/clock.c +++ b/src/core/clock.c @@ -15,7 +15,6 @@ nni_clock(void) return (nni_plat_clock()); } - void nni_usleep(nni_duration usec) { diff --git a/src/core/defs.h b/src/core/defs.h index 5db68c1c..98ff5b97 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -15,118 +15,115 @@ // C compilers may get unhappy when named arguments are not used. While // there are things like __attribute__((unused)) which are arguably // superior, support for such are not universal. -#define NNI_ARG_UNUSED(x) ((void) x); +#define NNI_ARG_UNUSED(x) ((void) x); #define NNI_ASSERT(x) \ if (!(x)) \ - nni_panic("%s: %d: assert err: %s", __FILE__, __LINE__, # x) + nni_panic("%s: %d: assert err: %s", __FILE__, __LINE__, #x) // These types are common but have names shared with user space. -typedef struct nng_msg nni_msg; -typedef struct nng_sockaddr nni_sockaddr; -typedef struct nng_event nni_event; -typedef struct nng_notify nni_notify; +typedef struct nng_msg nni_msg; +typedef struct nng_sockaddr nni_sockaddr; +typedef struct nng_event nni_event; +typedef struct nng_notify nni_notify; // These are our own names. -typedef struct nni_socket nni_sock; -typedef struct nni_ep nni_ep; -typedef struct nni_pipe nni_pipe; -typedef struct nni_tran nni_tran; -typedef struct nni_tran_ep nni_tran_ep; -typedef struct nni_tran_pipe nni_tran_pipe; - -typedef struct nni_proto_sock_ops nni_proto_sock_ops; -typedef struct nni_proto_pipe_ops nni_proto_pipe_ops; -typedef struct nni_proto nni_proto; - -typedef struct nni_mtx nni_mtx; -typedef struct nni_cv nni_cv; -typedef struct nni_idhash nni_idhash; -typedef struct nni_thr nni_thr; +typedef struct nni_socket nni_sock; +typedef struct nni_ep nni_ep; +typedef struct nni_pipe nni_pipe; +typedef struct nni_tran nni_tran; +typedef struct nni_tran_ep nni_tran_ep; +typedef struct nni_tran_pipe nni_tran_pipe; + +typedef struct nni_proto_sock_ops nni_proto_sock_ops; +typedef struct nni_proto_pipe_ops nni_proto_pipe_ops; +typedef struct nni_proto nni_proto; + +typedef struct nni_mtx nni_mtx; +typedef struct nni_cv nni_cv; +typedef struct nni_idhash nni_idhash; +typedef struct nni_thr nni_thr; typedef void (*nni_thr_func)(void *); -typedef int nni_signal; // Wakeup channel. -typedef uint64_t nni_time; // Abs. time (usec). -typedef int64_t nni_duration; // Rel. time (usec). +typedef int nni_signal; // Wakeup channel. +typedef uint64_t nni_time; // Abs. time (usec). +typedef int64_t nni_duration; // Rel. time (usec). -typedef struct nni_aio nni_aio; +typedef struct nni_aio nni_aio; typedef void (*nni_cb)(void *); // Used by transports for scatter gather I/O. typedef struct { - void * iov_buf; - size_t iov_len; + void * iov_buf; + size_t iov_len; } nni_iov; // Notify descriptor. typedef struct { - int sn_wfd; // written to in order to flag an event - int sn_rfd; // read from in order to clear an event - int sn_init; + int sn_wfd; // written to in order to flag an event + int sn_rfd; // read from in order to clear an event + int sn_init; } nni_notifyfd; // Some default timing things. -#define NNI_TIME_NEVER ((nni_time) -1) -#define NNI_TIME_ZERO ((nni_time) 0) -#define NNI_SECOND (1000000) +#define NNI_TIME_NEVER ((nni_time) -1) +#define NNI_TIME_ZERO ((nni_time) 0) +#define NNI_SECOND (1000000) // Structure allocation conveniences. -#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof (*s)) -#define NNI_FREE_STRUCT(s) nni_free((s), sizeof (*s)) -#define NNI_ALLOC_STRUCTS(s, n) nni_alloc(sizeof (*s) * n) -#define NNI_FREE_STRUCTS(s, n) nni_free(s, sizeof (*s) * n) - -#define NNI_PUT16(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint16_t) (u)) >> 8); \ - (ptr)[1] = (uint8_t) ((uint16_t) (u)); \ - } \ - while (0) - -#define NNI_PUT32(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint32_t) (u)) >> 24); \ - (ptr)[1] = (uint8_t) (((uint32_t) (u)) >> 16); \ - (ptr)[2] = (uint8_t) (((uint32_t) (u)) >> 8); \ - (ptr)[3] = (uint8_t) ((uint32_t) (u)); \ - } \ - while (0) - -#define NNI_PUT64(ptr, u) \ - do { \ - (ptr)[0] = (uint8_t) (((uint64_t) (u)) >> 56); \ - (ptr)[1] = (uint8_t) (((uint64_t) (u)) >> 48); \ - (ptr)[2] = (uint8_t) (((uint64_t) (u)) >> 40); \ - (ptr)[3] = (uint8_t) (((uint64_t) (u)) >> 32); \ - (ptr)[4] = (uint8_t) (((uint64_t) (u)) >> 24); \ - (ptr)[5] = (uint8_t) (((uint64_t) (u)) >> 16); \ - (ptr)[6] = (uint8_t) (((uint64_t) (u)) >> 8); \ - (ptr)[7] = (uint8_t) ((uint64_t) (u)); \ - } \ - while (0) - -#define NNI_GET16(ptr, v) \ - v = (((uint32_t) ((uint8_t) (ptr)[0])) << 8) + \ - (((uint32_t) (uint8_t) (ptr)[1])) - -#define NNI_GET32(ptr, v) \ - v = (((uint32_t) ((uint8_t) (ptr)[0])) << 24) + \ - (((uint32_t) ((uint8_t) (ptr)[1])) << 16) + \ - (((uint32_t) ((uint8_t) (ptr)[2])) << 8) + \ - (((uint32_t) (uint8_t) (ptr)[3])) - -#define NNI_GET64(ptr, v) \ - v = (((uint64_t) ((uint8_t) (ptr)[0])) << 56) + \ - (((uint64_t) ((uint8_t) (ptr)[1])) << 48) + \ - (((uint64_t) ((uint8_t) (ptr)[2])) << 40) + \ - (((uint64_t) ((uint8_t) (ptr)[3])) << 32) + \ - (((uint64_t) ((uint8_t) (ptr)[4])) << 24) + \ - (((uint64_t) ((uint8_t) (ptr)[5])) << 16) + \ - (((uint64_t) ((uint8_t) (ptr)[6])) << 8) + \ - (((uint64_t) (uint8_t) (ptr)[7])) +#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof(*s)) +#define NNI_FREE_STRUCT(s) nni_free((s), sizeof(*s)) +#define NNI_ALLOC_STRUCTS(s, n) nni_alloc(sizeof(*s) * n) +#define NNI_FREE_STRUCTS(s, n) nni_free(s, sizeof(*s) * n) + +#define NNI_PUT16(ptr, u) \ + do { \ + (ptr)[0] = (uint8_t)(((uint16_t)(u)) >> 8); \ + (ptr)[1] = (uint8_t)((uint16_t)(u)); \ + } while (0) + +#define NNI_PUT32(ptr, u) \ + do { \ + (ptr)[0] = (uint8_t)(((uint32_t)(u)) >> 24); \ + (ptr)[1] = (uint8_t)(((uint32_t)(u)) >> 16); \ + (ptr)[2] = (uint8_t)(((uint32_t)(u)) >> 8); \ + (ptr)[3] = (uint8_t)((uint32_t)(u)); \ + } while (0) + +#define NNI_PUT64(ptr, u) \ + do { \ + (ptr)[0] = (uint8_t)(((uint64_t)(u)) >> 56); \ + (ptr)[1] = (uint8_t)(((uint64_t)(u)) >> 48); \ + (ptr)[2] = (uint8_t)(((uint64_t)(u)) >> 40); \ + (ptr)[3] = (uint8_t)(((uint64_t)(u)) >> 32); \ + (ptr)[4] = (uint8_t)(((uint64_t)(u)) >> 24); \ + (ptr)[5] = (uint8_t)(((uint64_t)(u)) >> 16); \ + (ptr)[6] = (uint8_t)(((uint64_t)(u)) >> 8); \ + (ptr)[7] = (uint8_t)((uint64_t)(u)); \ + } while (0) + +#define NNI_GET16(ptr, v) \ + v = (((uint32_t)((uint8_t)(ptr)[0])) << 8) + \ + (((uint32_t)(uint8_t)(ptr)[1])) + +#define NNI_GET32(ptr, v) \ + v = (((uint32_t)((uint8_t)(ptr)[0])) << 24) + \ + (((uint32_t)((uint8_t)(ptr)[1])) << 16) + \ + (((uint32_t)((uint8_t)(ptr)[2])) << 8) + \ + (((uint32_t)(uint8_t)(ptr)[3])) + +#define NNI_GET64(ptr, v) \ + v = (((uint64_t)((uint8_t)(ptr)[0])) << 56) + \ + (((uint64_t)((uint8_t)(ptr)[1])) << 48) + \ + (((uint64_t)((uint8_t)(ptr)[2])) << 40) + \ + (((uint64_t)((uint8_t)(ptr)[3])) << 32) + \ + (((uint64_t)((uint8_t)(ptr)[4])) << 24) + \ + (((uint64_t)((uint8_t)(ptr)[5])) << 16) + \ + (((uint64_t)((uint8_t)(ptr)[6])) << 8) + \ + (((uint64_t)(uint8_t)(ptr)[7])) // A few assorted other items. -#define NNI_FLAG_IPV4ONLY 1 +#define NNI_FLAG_IPV4ONLY 1 -#endif // CORE_DEFS_H +#endif // CORE_DEFS_H diff --git a/src/core/device.c b/src/core/device.c index 7d0454bd..cd0c8dfc 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -12,18 +12,18 @@ #include struct nni_device_pair { - nni_thr thrs[2]; - nni_sock * socks[2]; - int err[2]; + nni_thr thrs[2]; + nni_sock *socks[2]; + int err[2]; }; -typedef struct nni_device_pair nni_device_pair; +typedef struct nni_device_pair nni_device_pair; static int nni_device_loop(nni_sock *from, nni_sock *to) { nni_msg *msg; - int rv = 0; + int rv = 0; for (;;) { // Take messages sock[0], and send to sock[1]. @@ -40,7 +40,6 @@ nni_device_loop(nni_sock *from, nni_sock *to) return (rv); } - static void nni_device_fwd(void *p) { @@ -51,7 +50,6 @@ nni_device_fwd(void *p) nni_sock_shutdown(pair->socks[1]); } - static void nni_device_rev(void *p) { @@ -62,14 +60,13 @@ nni_device_rev(void *p) nni_sock_shutdown(pair->socks[1]); } - int nni_device(nni_sock *sock1, nni_sock *sock2) { nni_device_pair pair; - int rv; + int rv; - memset(&pair, 0, sizeof (pair)); + memset(&pair, 0, sizeof(pair)); pair.socks[0] = sock1; pair.socks[1] = sock2; @@ -105,8 +102,7 @@ nni_device(nni_sock *sock1, nni_sock *sock2) } // If the sockets are the same, then its a simple one way forwarder, // and we don't need two workers (but would be harmless if we did it). - if ((sock1 != sock2) && - ((sock2->s_flags & NNI_PROTO_FLAG_RCV) != 0) && + if ((sock1 != sock2) && ((sock2->s_flags & NNI_PROTO_FLAG_RCV) != 0) && ((sock1->s_flags & NNI_PROTO_FLAG_SND) != 0)) { nni_thr_run(&pair.thrs[1]); } diff --git a/src/core/endpt.c b/src/core/endpt.c index ddfde49e..0582b92b 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -9,16 +9,15 @@ #include "core/nng_impl.h" -#include -#include #include +#include +#include // Functionality related to end points. static nni_objhash *nni_eps = NULL; -static void *nni_ep_ctor(uint32_t); -static void nni_ep_dtor(void *); - +static void * nni_ep_ctor(uint32_t); +static void nni_ep_dtor(void *); int nni_ep_sys_init(void) @@ -32,7 +31,6 @@ nni_ep_sys_init(void) return (rv); } - void nni_ep_sys_fini(void) { @@ -40,11 +38,10 @@ nni_ep_sys_fini(void) nni_eps = NULL; } - int nni_ep_find(nni_ep **epp, uint32_t id) { - int rv; + int rv; nni_ep *ep; if ((rv = nni_init()) != 0) { @@ -68,7 +65,6 @@ nni_ep_find(nni_ep **epp, uint32_t id) return (0); } - void nni_ep_hold(nni_ep *ep) { @@ -78,35 +74,32 @@ nni_ep_hold(nni_ep *ep) NNI_ASSERT(rv == 0); } - void nni_ep_rele(nni_ep *ep) { nni_objhash_unref(nni_eps, ep->ep_id); } - uint32_t nni_ep_id(nni_ep *ep) { return (ep->ep_id); } - static void * nni_ep_ctor(uint32_t id) { nni_ep *ep; - int rv; + int rv; if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NULL); } ep->ep_closed = 0; - ep->ep_bound = 0; - ep->ep_pipe = NULL; - ep->ep_id = id; - ep->ep_data = NULL; + ep->ep_bound = 0; + ep->ep_pipe = NULL; + ep->ep_id = id; + ep->ep_data = NULL; NNI_LIST_NODE_INIT(&ep->ep_node); @@ -126,7 +119,6 @@ nni_ep_ctor(uint32_t id) return (ep); } - static void nni_ep_dtor(void *ptr) { @@ -140,14 +132,13 @@ nni_ep_dtor(void *ptr) NNI_FREE_STRUCT(ep); } - int nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr, int mode) { nni_tran *tran; - nni_ep *ep; - int rv; - uint32_t id; + nni_ep * ep; + int rv; + uint32_t id; if ((tran = nni_tran_find(addr)) == NULL) { return (NNG_ENOTSUP); @@ -165,14 +156,13 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr, int mode) ep->ep_mode = mode; // Could safely use strcpy here, but this avoids discussion. - (void) snprintf(ep->ep_addr, sizeof (ep->ep_addr), "%s", addr); + (void) snprintf(ep->ep_addr, sizeof(ep->ep_addr), "%s", addr); // Make a copy of the endpoint operations. This allows us to // modify them (to override NULLs for example), and avoids an extra // dereference on hot paths. ep->ep_ops = *tran->tran_ep; - if ((rv = ep->ep_ops.ep_init(&ep->ep_data, addr, sock, mode)) != 0) { nni_objhash_unref(nni_eps, id); return (rv); @@ -187,7 +177,6 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr, int mode) return (0); } - void nni_ep_stop(nni_ep *ep) { @@ -200,14 +189,12 @@ nni_ep_stop(nni_ep *ep) nni_mtx_unlock(&ep->ep_mtx); } - void nni_ep_close(nni_ep *ep) { nni_ep_stop(ep); } - void nni_ep_remove(nni_ep *ep) { @@ -236,12 +223,11 @@ nni_ep_remove(nni_ep *ep) nni_objhash_unref(nni_eps, ep->ep_id); } - static int nni_ep_connect_aio(nni_ep *ep, void **pipep) { nni_aio aio; - int rv; + int rv; nni_aio_init(&aio, NULL, NULL); aio.a_endpt = ep->ep_data; @@ -255,12 +241,11 @@ nni_ep_connect_aio(nni_ep *ep, void **pipep) return (rv); } - static int nni_ep_connect_sync(nni_ep *ep) { nni_pipe *pipe; - int rv; + int rv; rv = nni_pipe_create(&pipe, ep, ep->ep_sock, ep->ep_tran); if (rv != 0) { @@ -281,7 +266,6 @@ nni_ep_connect_sync(nni_ep *ep) return (0); } - int nni_ep_pipe_add(nni_ep *ep, nni_pipe *pipe) { @@ -297,7 +281,6 @@ nni_ep_pipe_add(nni_ep *ep, nni_pipe *pipe) return (0); } - void nni_ep_pipe_remove(nni_ep *ep, nni_pipe *pipe) { @@ -316,14 +299,13 @@ nni_ep_pipe_remove(nni_ep *ep, nni_pipe *pipe) nni_mtx_unlock(&ep->ep_mtx); } - // nni_dialer is the thread worker that dials in the background. static void nni_dialer(void *arg) { - nni_ep *ep = arg; - int rv; - nni_time cooldown; + nni_ep * ep = arg; + int rv; + nni_time cooldown; nni_duration maxrtime = 0, nmaxrtime; nni_duration defrtime = 0, ndefrtime; nni_duration rtime; @@ -337,7 +319,7 @@ nni_dialer(void *arg) // Times changed, so reset them. defrtime = ndefrtime; maxrtime = nmaxrtime; - rtime = defrtime; + rtime = defrtime; } nni_mtx_lock(&ep->ep_mtx); @@ -380,7 +362,6 @@ nni_dialer(void *arg) } } - int nni_ep_dial(nni_ep *ep, int flags) { @@ -423,12 +404,11 @@ nni_ep_dial(nni_ep *ep, int flags) return (rv); } - static int nni_ep_accept_aio(nni_ep *ep, void **pipep) { nni_aio aio; - int rv; + int rv; nni_aio_init(&aio, NULL, NULL); aio.a_endpt = ep->ep_data; @@ -442,12 +422,11 @@ nni_ep_accept_aio(nni_ep *ep, void **pipep) return (rv); } - static int nni_ep_accept_sync(nni_ep *ep) { nni_pipe *pipe; - int rv; + int rv; if (ep->ep_closed) { return (NNG_ECLOSED); @@ -468,12 +447,11 @@ nni_ep_accept_sync(nni_ep *ep) return (0); } - static void nni_listener(void *arg) { nni_ep *ep = arg; - int rv; + int rv; for (;;) { nni_time cooldown; @@ -534,7 +512,7 @@ nni_listener(void *arg) default: // Other cases we sleep just a tiny bit to avoid // burning the cpu (e.g. out of files). - cooldown = 1000; // 1 msec + cooldown = 1000; // 1 msec break; } cooldown += nni_clock(); @@ -549,7 +527,6 @@ nni_listener(void *arg) } } - int nni_ep_listen(nni_ep *ep, int flags) { @@ -594,7 +571,6 @@ nni_ep_listen(nni_ep *ep, int flags) return (0); } - void nni_ep_list_init(nni_list *list) { diff --git a/src/core/endpt.h b/src/core/endpt.h index becaa3f4..6ad2172b 100644 --- a/src/core/endpt.h +++ b/src/core/endpt.h @@ -19,41 +19,41 @@ // OUSIDE of the core is STRICTLY VERBOTEN. NO DIRECT ACCESS BY PROTOCOLS // OR TRANSPORTS. struct nni_ep { - nni_tran_ep ep_ops; // transport ops - nni_tran * ep_tran; // transport pointer - void * ep_data; // transport private - uint32_t ep_id; // endpoint id - nni_list_node ep_node; // per socket list - nni_sock * ep_sock; - char ep_addr[NNG_MAXADDRLEN]; - nni_thr ep_thr; - int ep_mode; - int ep_started; - int ep_closed; // full shutdown - int ep_bound; // true if we bound locally - nni_mtx ep_mtx; - nni_cv ep_cv; - nni_pipe * ep_pipe; // Connected pipe (dialers only) - nni_list ep_pipes; + nni_tran_ep ep_ops; // transport ops + nni_tran * ep_tran; // transport pointer + void * ep_data; // transport private + uint32_t ep_id; // endpoint id + nni_list_node ep_node; // per socket list + nni_sock * ep_sock; + char ep_addr[NNG_MAXADDRLEN]; + nni_thr ep_thr; + int ep_mode; + int ep_started; + int ep_closed; // full shutdown + int ep_bound; // true if we bound locally + nni_mtx ep_mtx; + nni_cv ep_cv; + nni_pipe * ep_pipe; // Connected pipe (dialers only) + nni_list ep_pipes; }; -#define NNI_EP_MODE_DIAL 1 -#define NNI_EP_MODE_LISTEN 2 +#define NNI_EP_MODE_DIAL 1 +#define NNI_EP_MODE_LISTEN 2 -extern int nni_ep_sys_init(void); -extern void nni_ep_sys_fini(void); -extern int nni_ep_find(nni_ep **, uint32_t); -extern void nni_ep_hold(nni_ep *); -extern void nni_ep_rele(nni_ep *); +extern int nni_ep_sys_init(void); +extern void nni_ep_sys_fini(void); +extern int nni_ep_find(nni_ep **, uint32_t); +extern void nni_ep_hold(nni_ep *); +extern void nni_ep_rele(nni_ep *); extern uint32_t nni_ep_id(nni_ep *); -extern int nni_ep_create(nni_ep **, nni_sock *, const char *, int); -extern void nni_ep_stop(nni_ep *); -extern void nni_ep_close(nni_ep *); -extern void nni_ep_remove(nni_ep *); -extern int nni_ep_dial(nni_ep *, int); -extern int nni_ep_listen(nni_ep *, int); -extern void nni_ep_list_init(nni_list *); -extern int nni_ep_pipe_add(nni_ep *, nni_pipe *); -extern void nni_ep_pipe_remove(nni_ep *, nni_pipe *); +extern int nni_ep_create(nni_ep **, nni_sock *, const char *, int); +extern void nni_ep_stop(nni_ep *); +extern void nni_ep_close(nni_ep *); +extern void nni_ep_remove(nni_ep *); +extern int nni_ep_dial(nni_ep *, int); +extern int nni_ep_listen(nni_ep *, int); +extern void nni_ep_list_init(nni_list *); +extern int nni_ep_pipe_add(nni_ep *, nni_pipe *); +extern void nni_ep_pipe_remove(nni_ep *, nni_pipe *); #endif // CORE_ENDPT_H diff --git a/src/core/event.c b/src/core/event.c index df9618ca..79910d80 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -15,13 +15,12 @@ int nni_ev_init(nni_event *event, int type, nni_sock *sock) { - memset(event, 0, sizeof (*event)); + memset(event, 0, sizeof(*event)); event->e_type = type; event->e_sock = sock; return (0); } - void nni_ev_fini(nni_event *event) { diff --git a/src/core/event.h b/src/core/event.h index e43306c9..6d9a9394 100644 --- a/src/core/event.h +++ b/src/core/event.h @@ -14,21 +14,21 @@ #include "core/list.h" struct nng_event { - int e_type; - nni_sock * e_sock; - nni_ep * e_ep; - nni_pipe * e_pipe; + int e_type; + nni_sock *e_sock; + nni_ep * e_ep; + nni_pipe *e_pipe; }; struct nng_notify { nng_notify_func n_func; - void * n_arg; - int n_type; - nni_sock * n_sock; - nni_aio n_aio; + void * n_arg; + int n_type; + nni_sock * n_sock; + nni_aio n_aio; }; -extern int nni_ev_init(nni_event *, int, nni_sock *); +extern int nni_ev_init(nni_event *, int, nni_sock *); extern void nni_ev_fini(nni_event *); #endif // CORE_EVENT_H diff --git a/src/core/idhash.c b/src/core/idhash.c index 4af06b0e..9f3b4b03 100644 --- a/src/core/idhash.c +++ b/src/core/idhash.c @@ -12,59 +12,55 @@ #include struct nni_idhash_entry { - uint32_t ihe_key; - uint32_t ihe_skips; - void * ihe_val; + uint32_t ihe_key; + uint32_t ihe_skips; + void * ihe_val; }; - int nni_idhash_init(nni_idhash *h) { h->ih_entries = NULL; - h->ih_count = 0; - h->ih_load = 0; - h->ih_cap = 0; + h->ih_count = 0; + h->ih_load = 0; + h->ih_cap = 0; h->ih_maxload = 0; h->ih_minload = 0; // never shrink below this h->ih_walkers = 0; - h->ih_minval = 0; - h->ih_maxval = 0xffffffff; - h->ih_dynval = 0; + h->ih_minval = 0; + h->ih_maxval = 0xffffffff; + h->ih_dynval = 0; return (0); } - void nni_idhash_fini(nni_idhash *h) { NNI_ASSERT(h->ih_walkers == 0); if (h->ih_entries != NULL) { - nni_free(h->ih_entries, h->ih_cap * sizeof (nni_idhash_entry)); + nni_free(h->ih_entries, h->ih_cap * sizeof(nni_idhash_entry)); h->ih_entries = NULL; h->ih_cap = h->ih_count = 0; h->ih_load = h->ih_minload = h->ih_maxload = 0; } } - void nni_idhash_reclaim(nni_idhash *h) { // Reclaim the buffer if we want, but preserve the limits. if ((h->ih_count == 0) && (h->ih_cap != 0) && (h->ih_walkers == 0)) { - nni_free(h->ih_entries, h->ih_cap * sizeof (nni_idhash_entry)); - h->ih_cap = 0; + nni_free(h->ih_entries, h->ih_cap * sizeof(nni_idhash_entry)); + h->ih_cap = 0; h->ih_entries = NULL; h->ih_minload = 0; h->ih_maxload = 0; } } - void -nni_idhash_set_limits(nni_idhash *h, uint32_t minval, uint32_t maxval, - uint32_t start) +nni_idhash_set_limits( + nni_idhash *h, uint32_t minval, uint32_t maxval, uint32_t start) { h->ih_minval = minval; h->ih_maxval = maxval; @@ -74,11 +70,9 @@ nni_idhash_set_limits(nni_idhash *h, uint32_t minval, uint32_t maxval, NNI_ASSERT(start <= maxval); } - // Inspired by Python dict implementation. This probe will visit every // cell. We always hash consecutively assigned IDs. -#define NNI_IDHASH_NEXTPROBE(h, j) \ - ((((j) * 5) + 1) & (h->ih_cap - 1)) +#define NNI_IDHASH_NEXTPROBE(h, j) ((((j) *5) + 1) & (h->ih_cap - 1)) int nni_idhash_find(nni_idhash *h, uint32_t id, void **valp) @@ -102,21 +96,20 @@ nni_idhash_find(nni_idhash *h, uint32_t id, void **valp) } } - static int nni_hash_resize(nni_idhash *h) { - size_t newsize; - size_t oldsize; + size_t newsize; + size_t oldsize; nni_idhash_entry *newents; nni_idhash_entry *oldents; - uint32_t i; + uint32_t i; if ((h->ih_load < h->ih_maxload) && (h->ih_load >= h->ih_minload)) { // No resize needed. return (0); } - if (h->ih_walkers && (h->ih_load < (h->ih_cap-1))) { + if (h->ih_walkers && (h->ih_load < (h->ih_cap - 1))) { // Don't resize when walkers are running. This way // walk functions can remove hash nodes. return (0); @@ -131,14 +124,14 @@ nni_hash_resize(nni_idhash *h) } oldents = h->ih_entries; - newents = nni_alloc(sizeof (nni_idhash_entry) * newsize); + newents = nni_alloc(sizeof(nni_idhash_entry) * newsize); if (newents == NULL) { return (NNG_ENOMEM); } - memset(newents, 0, sizeof (nni_idhash_entry) * newsize); + memset(newents, 0, sizeof(nni_idhash_entry) * newsize); h->ih_entries = newents; - h->ih_cap = newsize; + h->ih_cap = newsize; if (newsize > 8) { h->ih_minload = newsize / 8; h->ih_maxload = newsize * 2 / 3; @@ -164,17 +157,16 @@ nni_hash_resize(nni_idhash *h) } } if (oldsize != 0) { - nni_free(oldents, sizeof (nni_idhash_entry) * oldsize); + nni_free(oldents, sizeof(nni_idhash_entry) * oldsize); } return (0); } - int nni_idhash_remove(nni_idhash *h, uint32_t id) { - int rv; - void *val; + int rv; + void * val; uint32_t index; // First check that it is in the table. This may double the @@ -212,7 +204,6 @@ nni_idhash_remove(nni_idhash *h, uint32_t id) return (0); } - int nni_idhash_insert(nni_idhash *h, uint32_t id, void *val) { @@ -253,13 +244,12 @@ nni_idhash_insert(nni_idhash *h, uint32_t id, void *val) } } - int nni_idhash_alloc(nni_idhash *h, uint32_t *idp, void *val) { uint32_t id; - void *scrap; - int rv; + void * scrap; + int rv; if (h->ih_count > (h->ih_maxval - h->ih_minval)) { // Really more like ENOSPC.. the table is filled to max. @@ -285,14 +275,12 @@ nni_idhash_alloc(nni_idhash *h, uint32_t *idp, void *val) return (rv); } - size_t nni_idhash_count(nni_idhash *h) { return (h->ih_count); } - int nni_idhash_walk(nni_idhash *h, nni_idhash_walkfn fn, void *arg) { diff --git a/src/core/idhash.h b/src/core/idhash.h index 088eac55..bb3b1581 100644 --- a/src/core/idhash.h +++ b/src/core/idhash.h @@ -22,22 +22,22 @@ // use table sizes that are powers of two. Note that hash items // must be non-NULL. The table is locked. -typedef struct nni_idhash nni_idhash; -typedef struct nni_idhash_entry nni_idhash_entry; +typedef struct nni_idhash nni_idhash; +typedef struct nni_idhash_entry nni_idhash_entry; // The details of the nni_idhash are "private". But they let us inline // this into structures. struct nni_idhash { - size_t ih_cap; - size_t ih_count; - size_t ih_load; - size_t ih_minload; // considers placeholders - size_t ih_maxload; - uint32_t ih_walkers; - uint32_t ih_minval; - uint32_t ih_maxval; - uint32_t ih_dynval; - nni_idhash_entry * ih_entries; + size_t ih_cap; + size_t ih_count; + size_t ih_load; + size_t ih_minload; // considers placeholders + size_t ih_maxload; + uint32_t ih_walkers; + uint32_t ih_minval; + uint32_t ih_maxval; + uint32_t ih_dynval; + nni_idhash_entry *ih_entries; }; // nni_idhash_walkfn is called when walking a hash table. If the @@ -48,17 +48,17 @@ struct nni_idhash { // Note that the walkfn must not attempt to change the hash table. // The user must provide any locking needed. typedef int (*nni_idhash_walkfn)(void *, uint32_t, void *); -extern int nni_idhash_init(nni_idhash *); +extern int nni_idhash_init(nni_idhash *); extern void nni_idhash_fini(nni_idhash *); extern void nni_idhash_reclaim(nni_idhash *); extern void nni_idhash_set_limits(nni_idhash *, uint32_t, uint32_t, uint32_t); -extern int nni_idhash_create(nni_idhash **); +extern int nni_idhash_create(nni_idhash **); extern void nni_idhash_destroy(nni_idhash *); -extern int nni_idhash_find(nni_idhash *, uint32_t, void **); -extern int nni_idhash_remove(nni_idhash *, uint32_t); -extern int nni_idhash_insert(nni_idhash *, uint32_t, void *); -extern int nni_idhash_alloc(nni_idhash *, uint32_t *, void *); +extern int nni_idhash_find(nni_idhash *, uint32_t, void **); +extern int nni_idhash_remove(nni_idhash *, uint32_t); +extern int nni_idhash_insert(nni_idhash *, uint32_t, void *); +extern int nni_idhash_alloc(nni_idhash *, uint32_t *, void *); extern size_t nni_idhash_count(nni_idhash *); -extern int nni_idhash_walk(nni_idhash *, nni_idhash_walkfn, void *); +extern int nni_idhash_walk(nni_idhash *, nni_idhash_walkfn, void *); -#endif // CORE_IDHASH_H +#endif // CORE_IDHASH_H diff --git a/src/core/init.c b/src/core/init.c index 98b187d8..4292146d 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -8,8 +8,8 @@ // #include "core/nng_impl.h" -#include #include +#include static int nni_init_helper(void) @@ -53,14 +53,12 @@ nni_init_helper(void) return (0); } - int nni_init(void) { return (nni_plat_init(nni_init_helper)); } - void nni_fini(void) { diff --git a/src/core/list.c b/src/core/list.c index 10ec4ef8..97fe5c30 100644 --- a/src/core/list.c +++ b/src/core/list.c @@ -16,19 +16,17 @@ // Using pointer arithmetic, we can operate as a list of "anything". #define NODE(list, item) \ - (nni_list_node *) (void *) (((char *) item) + list->ll_offset) -#define ITEM(list, node) \ - (void *) (((char *) node) - list->ll_offset) + (nni_list_node *) (void *)(((char *) item) + list->ll_offset) +#define ITEM(list, node) (void *) (((char *) node) - list->ll_offset) void nni_list_init_offset(nni_list *list, size_t offset) { - list->ll_offset = offset; + list->ll_offset = offset; list->ll_head.ln_next = &list->ll_head; list->ll_head.ln_prev = &list->ll_head; } - void * nni_list_first(const nni_list *list) { @@ -40,7 +38,6 @@ nni_list_first(const nni_list *list) return (ITEM(list, node)); } - void * nni_list_last(const nni_list *list) { @@ -52,7 +49,6 @@ nni_list_last(const nni_list *list) return (ITEM(list, node)); } - void nni_list_append(nni_list *list, void *item) { @@ -61,13 +57,12 @@ nni_list_append(nni_list *list, void *item) if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("appending node already on a list or not inited"); } - node->ln_prev = list->ll_head.ln_prev; - node->ln_next = &list->ll_head; + node->ln_prev = list->ll_head.ln_prev; + node->ln_next = &list->ll_head; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void nni_list_prepend(nni_list *list, void *item) { @@ -76,45 +71,42 @@ nni_list_prepend(nni_list *list, void *item) if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("prepending node already on a list or not inited"); } - node->ln_next = list->ll_head.ln_next; - node->ln_prev = &list->ll_head; + node->ln_next = list->ll_head.ln_next; + node->ln_prev = &list->ll_head; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void nni_list_insert_before(nni_list *list, void *item, void *before) { - nni_list_node *node = NODE(list, item); + nni_list_node *node = NODE(list, item); nni_list_node *where = NODE(list, before); if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("inserting node already on a list or not inited"); } - node->ln_next = where; - node->ln_prev = where->ln_prev; + node->ln_next = where; + node->ln_prev = where->ln_prev; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void nni_list_insert_after(nni_list *list, void *item, void *after) { - nni_list_node *node = NODE(list, item); + nni_list_node *node = NODE(list, item); nni_list_node *where = NODE(list, after); if ((node->ln_next != NULL) || (node->ln_prev != NULL)) { nni_panic("inserting node already on a list or not inited"); } - node->ln_prev = where; - node->ln_next = where->ln_next; + node->ln_prev = where; + node->ln_next = where->ln_next; node->ln_next->ln_prev = node; node->ln_prev->ln_next = node; } - void * nni_list_next(const nni_list *list, void *item) { @@ -126,7 +118,6 @@ nni_list_next(const nni_list *list, void *item) return (ITEM(list, node)); } - void * nni_list_prev(const nni_list *list, void *item) { @@ -138,7 +129,6 @@ nni_list_prev(const nni_list *list, void *item) return (ITEM(list, node)); } - void nni_list_remove(nni_list *list, void *item) { @@ -146,11 +136,10 @@ nni_list_remove(nni_list *list, void *item) node->ln_prev->ln_next = node->ln_next; node->ln_next->ln_prev = node->ln_prev; - node->ln_next = NULL; - node->ln_prev = NULL; + node->ln_next = NULL; + node->ln_prev = NULL; } - int nni_list_active(nni_list *list, void *item) { @@ -159,28 +148,25 @@ nni_list_active(nni_list *list, void *item) return (node->ln_next == NULL ? 0 : 1); } - int nni_list_empty(nni_list *list) { return (list->ll_head.ln_next == &list->ll_head); } - int nni_list_node_active(nni_list_node *node) { return (node->ln_next == NULL ? 0 : 1); } - void nni_list_node_remove(nni_list_node *node) { if (node->ln_next != NULL) { node->ln_prev->ln_next = node->ln_next; node->ln_next->ln_prev = node->ln_prev; - node->ln_next = NULL; - node->ln_prev = NULL; + node->ln_next = NULL; + node->ln_prev = NULL; } } diff --git a/src/core/list.h b/src/core/list.h index 9701431e..b0007ec0 100644 --- a/src/core/list.h +++ b/src/core/list.h @@ -15,13 +15,13 @@ // In order to make life easy, we just define the list structures // directly, and let consumers directly inline structures. typedef struct nni_list_node { - struct nni_list_node * ln_next; - struct nni_list_node * ln_prev; + struct nni_list_node *ln_next; + struct nni_list_node *ln_prev; } nni_list_node; typedef struct nni_list { - struct nni_list_node ll_head; - size_t ll_offset; + struct nni_list_node ll_head; + size_t ll_offset; } nni_list; extern void nni_list_init_offset(nni_list *list, size_t offset); @@ -29,24 +29,26 @@ extern void nni_list_init_offset(nni_list *list, size_t offset); #define NNI_LIST_INIT(list, type, field) \ nni_list_init_offset(list, offsetof(type, field)) -#define NNI_LIST_NODE_INIT(node) \ - { (node)->ln_prev = (node)->ln_next = 0; } +#define NNI_LIST_NODE_INIT(node) \ + { \ + (node)->ln_prev = (node)->ln_next = 0; \ + } extern void *nni_list_first(const nni_list *); extern void *nni_list_last(const nni_list *); -extern void nni_list_append(nni_list *, void *); -extern void nni_list_prepend(nni_list *, void *); -extern void nni_list_insert_before(nni_list *, void *, void *); -extern void nni_list_insert_after(nni_list *, void *, void *); +extern void nni_list_append(nni_list *, void *); +extern void nni_list_prepend(nni_list *, void *); +extern void nni_list_insert_before(nni_list *, void *, void *); +extern void nni_list_insert_after(nni_list *, void *, void *); extern void *nni_list_next(const nni_list *, void *); extern void *nni_list_prev(const nni_list *, void *); -extern void nni_list_remove(nni_list *, void *); -extern int nni_list_active(nni_list *, void *); -extern int nni_list_empty(nni_list *); -extern int nni_list_node_active(nni_list_node *); -extern void nni_list_node_remove(nni_list_node *); +extern void nni_list_remove(nni_list *, void *); +extern int nni_list_active(nni_list *, void *); +extern int nni_list_empty(nni_list *); +extern int nni_list_node_active(nni_list_node *); +extern void nni_list_node_remove(nni_list_node *); -#define NNI_LIST_FOREACH(l, it) \ +#define NNI_LIST_FOREACH(l, it) \ for (it = nni_list_first(l); it != NULL; it = nni_list_next(l, it)) -#endif // CORE_LIST_H +#endif // CORE_LIST_H diff --git a/src/core/message.c b/src/core/message.c index 346570c9..4b563ce2 100644 --- a/src/core/message.c +++ b/src/core/message.c @@ -7,9 +7,9 @@ // found online at https://opensource.org/licenses/MIT. // +#include #include #include -#include #include "core/nng_impl.h" @@ -17,35 +17,35 @@ // Message chunk, internal to the message implementation. typedef struct { - size_t ch_cap; // allocated size - size_t ch_len; // length in use - uint8_t * ch_buf; // underlying buffer - uint8_t * ch_ptr; // pointer to actual data + size_t ch_cap; // allocated size + size_t ch_len; // length in use + uint8_t *ch_buf; // underlying buffer + uint8_t *ch_ptr; // pointer to actual data } nni_chunk; // Underlying message structure. struct nng_msg { - nni_chunk m_header; - nni_chunk m_body; - nni_time m_expire; // usec - nni_list m_options; + nni_chunk m_header; + nni_chunk m_body; + nni_time m_expire; // usec + nni_list m_options; }; typedef struct { - int mo_num; - size_t mo_sz; - void * mo_val; - nni_list_node mo_node; + int mo_num; + size_t mo_sz; + void * mo_val; + nni_list_node mo_node; } nni_msgopt; static void nni_chunk_dump(const nni_chunk *chunk, char *prefix) { - size_t i, j; + size_t i, j; uint8_t x; - char buf[128]; + char buf[128]; - (void) snprintf(buf, sizeof (buf), + (void) snprintf(buf, sizeof(buf), " %s (cap %d, len %d, offset %d ptr %p):", prefix, (int) chunk->ch_cap, (int) chunk->ch_len, (int) (chunk->ch_ptr - chunk->ch_buf), chunk->ch_ptr); @@ -59,13 +59,13 @@ nni_chunk_dump(const nni_chunk *chunk, char *prefix) nni_println(buf); j = 0; } - snprintf(buf, sizeof (buf), " %4x: ", (unsigned) i); + snprintf(buf, sizeof(buf), " %4x: ", (unsigned) i); j += strlen(buf); } buf[j++] = ' '; - x = (chunk->ch_ptr[i] >> 4); + x = (chunk->ch_ptr[i] >> 4); buf[j++] = x > 9 ? ('A' + (x - 10)) : '0' + x; - x = (chunk->ch_ptr[i] & 0x0f); + x = (chunk->ch_ptr[i] & 0x0f); buf[j++] = x > 9 ? ('A' + (x - 10)) : '0' + x; } if (j > 0) { @@ -74,20 +74,18 @@ nni_chunk_dump(const nni_chunk *chunk, char *prefix) } } - void nni_msg_dump(const char *banner, const nni_msg *msg) { char buf[128]; - (void) snprintf(buf, sizeof (buf), "--- %s BEGIN ---", banner); + (void) snprintf(buf, sizeof(buf), "--- %s BEGIN ---", banner); nni_println(buf); nni_chunk_dump(&msg->m_header, "HEADER"); nni_chunk_dump(&msg->m_body, "BODY"); nni_println("--- END ---"); } - // nni_chunk_grow increases the underlying space for a chunk. It ensures // that the desired amount of trailing space (including the length) // and headroom (excluding the length) are available. It also copies @@ -101,7 +99,7 @@ nni_msg_dump(const char *banner, const nni_msg *msg) static int nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) { - size_t headroom = 0; + size_t headroom = 0; uint8_t *newbuf; // We assume that if the pointer is a valid pointer, and inside @@ -115,7 +113,7 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) if ((ch->ch_ptr >= ch->ch_buf) && (ch->ch_ptr < (ch->ch_buf + ch->ch_cap))) { - headroom = (size_t) (ch->ch_ptr - ch->ch_buf); + headroom = (size_t)(ch->ch_ptr - ch->ch_buf); if (headwanted < headroom) { headwanted = headroom; // Never shrink this. } @@ -152,7 +150,6 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) return (0); } - static void nni_chunk_free(nni_chunk *ch) { @@ -165,7 +162,6 @@ nni_chunk_free(nni_chunk *ch) ch->ch_cap = 0; } - // nni_chunk_trunc truncates bytes from the end of the chunk. static int nni_chunk_trunc(nni_chunk *ch, size_t len) @@ -177,7 +173,6 @@ nni_chunk_trunc(nni_chunk *ch, size_t len) return (0); } - // nni_chunk_trim removes bytes from the beginning of the chunk. static int nni_chunk_trim(nni_chunk *ch, size_t len) @@ -193,7 +188,6 @@ nni_chunk_trim(nni_chunk *ch, size_t len) return (0); } - // nni_chunk_dup allocates storage for a new chunk, and copies // the contents of the source to the destination. The new chunk will // have the same size, headroom, and capacity as the original. @@ -210,7 +204,6 @@ nni_chunk_dup(nni_chunk *dst, const nni_chunk *src) return (0); } - // nni_chunk_append appends the data to the chunk, growing as necessary. // If the data pointer is NULL, then the chunk data region is allocated, // but uninitialized. @@ -235,7 +228,6 @@ nni_chunk_append(nni_chunk *ch, const void *data, size_t len) return (0); } - // nni_chunk_prepend prepends data to the chunk, as efficiently as possible. // If the data pointer is NULL, then no data is actually copied, but the // data region will have "grown" in the beginning, with uninitialized data. @@ -250,7 +242,7 @@ nni_chunk_prepend(nni_chunk *ch, const void *data, size_t len) if ((ch->ch_ptr >= ch->ch_buf) && (ch->ch_ptr < (ch->ch_buf + ch->ch_cap)) && - (len <= (size_t) (ch->ch_ptr - ch->ch_buf))) { + (len <= (size_t)(ch->ch_ptr - ch->ch_buf))) { // There is already enough room at the beginning. ch->ch_ptr -= len; } else if ((ch->ch_len + len) <= ch->ch_cap) { @@ -272,12 +264,11 @@ nni_chunk_prepend(nni_chunk *ch, const void *data, size_t len) return (0); } - int nni_msg_alloc(nni_msg **mp, size_t sz) { nni_msg *m; - int rv; + int rv; if ((m = NNI_ALLOC_STRUCT(m)) == NULL) { return (NNG_ENOMEM); @@ -295,7 +286,7 @@ nni_msg_alloc(nni_msg **mp, size_t sz) // to allow for inlining backtraces, etc. We also allow the // amount of space at the end for the same reason. Large aligned // allocations are unmolested to avoid excessive overallocation. - if ((sz < 1024) || ((sz & (sz-1)) != 0)) { + if ((sz < 1024) || ((sz & (sz - 1)) != 0)) { rv = nni_chunk_grow(&m->m_body, sz + 32, 32); } else { rv = nni_chunk_grow(&m->m_body, sz, 0); @@ -314,22 +305,20 @@ nni_msg_alloc(nni_msg **mp, size_t sz) return (0); } - int nni_msg_dup(nni_msg **dup, const nni_msg *src) { - nni_msg *m; + nni_msg * m; nni_msgopt *mo; nni_msgopt *newmo; - int rv; + int rv; if ((m = NNI_ALLOC_STRUCT(m)) == NULL) { return (NNG_ENOMEM); } - memset(m, 0, sizeof (*m)); + memset(m, 0, sizeof(*m)); NNI_LIST_INIT(&m->m_options, nni_msgopt, mo_node); - if ((rv = nni_chunk_dup(&m->m_header, &src->m_header)) != 0) { NNI_FREE_STRUCT(m); return (rv); @@ -341,13 +330,13 @@ nni_msg_dup(nni_msg **dup, const nni_msg *src) } NNI_LIST_FOREACH (&src->m_options, mo) { - newmo = nni_alloc(sizeof (*newmo) + mo->mo_sz); + newmo = nni_alloc(sizeof(*newmo) + mo->mo_sz); if (newmo == NULL) { nni_msg_free(m); return (NNG_ENOMEM); } - newmo->mo_val = ((char *) newmo + sizeof (*newmo)); - newmo->mo_sz = mo->mo_sz; + newmo->mo_val = ((char *) newmo + sizeof(*newmo)); + newmo->mo_sz = mo->mo_sz; newmo->mo_num = mo->mo_num; memcpy(newmo->mo_val, mo->mo_val, mo->mo_sz); nni_list_append(&m->m_options, newmo); @@ -357,7 +346,6 @@ nni_msg_dup(nni_msg **dup, const nni_msg *src) return (0); } - void nni_msg_free(nni_msg *m) { @@ -368,13 +356,12 @@ nni_msg_free(nni_msg *m) nni_chunk_free(&m->m_body); while ((mo = nni_list_first(&m->m_options)) != NULL) { nni_list_remove(&m->m_options, mo); - nni_free(mo, sizeof (*mo) + mo->mo_sz); + nni_free(mo, sizeof(*mo) + mo->mo_sz); } NNI_FREE_STRUCT(m); } } - int nni_msg_setopt(nni_msg *m, int opt, const void *val, size_t sz) { @@ -392,22 +379,21 @@ nni_msg_setopt(nni_msg *m, int opt, const void *val, size_t sz) break; } } - if ((newmo = nni_alloc(sizeof (*newmo) + sz)) == NULL) { + if ((newmo = nni_alloc(sizeof(*newmo) + sz)) == NULL) { return (NNG_ENOMEM); } - newmo->mo_val = ((char *) newmo + sizeof (*newmo)); - newmo->mo_sz = sz; + newmo->mo_val = ((char *) newmo + sizeof(*newmo)); + newmo->mo_sz = sz; newmo->mo_num = opt; memcpy(newmo->mo_val, val, sz); if (oldmo != NULL) { nni_list_remove(&m->m_options, oldmo); - nni_free(oldmo, sizeof (*oldmo) + oldmo->mo_sz); + nni_free(oldmo, sizeof(*oldmo) + oldmo->mo_sz); } nni_list_append(&m->m_options, newmo); return (0); } - int nni_msg_getopt(nni_msg *m, int opt, void *val, size_t *szp) { @@ -427,7 +413,6 @@ nni_msg_getopt(nni_msg *m, int opt, void *val, size_t *szp) return (NNG_ENOTSUP); } - int nni_msg_realloc(nni_msg *m, size_t sz) { @@ -445,84 +430,72 @@ nni_msg_realloc(nni_msg *m, size_t sz) return (0); } - void * nni_msg_header(nni_msg *m) { return (m->m_header.ch_ptr); } - size_t nni_msg_header_len(nni_msg *m) { return (m->m_header.ch_len); } - void * nni_msg_body(nni_msg *m) { return (m->m_body.ch_ptr); } - size_t nni_msg_len(nni_msg *m) { return (m->m_body.ch_len); } - int nni_msg_append(nni_msg *m, const void *data, size_t len) { return (nni_chunk_append(&m->m_body, data, len)); } - int nni_msg_prepend(nni_msg *m, const void *data, size_t len) { return (nni_chunk_prepend(&m->m_body, data, len)); } - int nni_msg_trim(nni_msg *m, size_t len) { return (nni_chunk_trim(&m->m_body, len)); } - int nni_msg_trunc(nni_msg *m, size_t len) { return (nni_chunk_trunc(&m->m_body, len)); } - int nni_msg_append_header(nni_msg *m, const void *data, size_t len) { return (nni_chunk_append(&m->m_header, data, len)); } - int nni_msg_prepend_header(nni_msg *m, const void *data, size_t len) { return (nni_chunk_prepend(&m->m_header, data, len)); } - int nni_msg_trim_header(nni_msg *m, size_t len) { return (nni_chunk_trim(&m->m_header, len)); } - int nni_msg_trunc_header(nni_msg *m, size_t len) { diff --git a/src/core/message.h b/src/core/message.h index 057c7539..7b71bd5c 100644 --- a/src/core/message.h +++ b/src/core/message.h @@ -12,24 +12,24 @@ // Internally used message API. Again, this is not part of our public API. -extern int nni_msg_alloc(nni_msg **, size_t); -extern void nni_msg_free(nni_msg *); -extern int nni_msg_realloc(nni_msg *, size_t); -extern int nni_msg_dup(nni_msg **, const nni_msg *); -extern void *nni_msg_header(nni_msg *); +extern int nni_msg_alloc(nni_msg **, size_t); +extern void nni_msg_free(nni_msg *); +extern int nni_msg_realloc(nni_msg *, size_t); +extern int nni_msg_dup(nni_msg **, const nni_msg *); +extern void * nni_msg_header(nni_msg *); extern size_t nni_msg_header_len(nni_msg *); -extern void *nni_msg_body(nni_msg *); +extern void * nni_msg_body(nni_msg *); extern size_t nni_msg_len(nni_msg *); -extern int nni_msg_append(nni_msg *, const void *, size_t); -extern int nni_msg_prepend(nni_msg *, const void *, size_t); -extern int nni_msg_append_header(nni_msg *, const void *, size_t); -extern int nni_msg_prepend_header(nni_msg *, const void *, size_t); -extern int nni_msg_trim(nni_msg *, size_t); -extern int nni_msg_trunc(nni_msg *, size_t); -extern int nni_msg_trim_header(nni_msg *, size_t); -extern int nni_msg_trunc_header(nni_msg *, size_t); -extern int nni_msg_setopt(nni_msg *, int, const void *, size_t); -extern int nni_msg_getopt(nni_msg *, int, void *, size_t *); -extern void nni_msg_dump(const char *, const nni_msg *); +extern int nni_msg_append(nni_msg *, const void *, size_t); +extern int nni_msg_prepend(nni_msg *, const void *, size_t); +extern int nni_msg_append_header(nni_msg *, const void *, size_t); +extern int nni_msg_prepend_header(nni_msg *, const void *, size_t); +extern int nni_msg_trim(nni_msg *, size_t); +extern int nni_msg_trunc(nni_msg *, size_t); +extern int nni_msg_trim_header(nni_msg *, size_t); +extern int nni_msg_trunc_header(nni_msg *, size_t); +extern int nni_msg_setopt(nni_msg *, int, const void *, size_t); +extern int nni_msg_getopt(nni_msg *, int, void *, size_t *); +extern void nni_msg_dump(const char *, const nni_msg *); -#endif // CORE_SOCKET_H +#endif // CORE_SOCKET_H diff --git a/src/core/msgqueue.c b/src/core/msgqueue.c index a69af47c..9f7ff7fd 100644 --- a/src/core/msgqueue.c +++ b/src/core/msgqueue.c @@ -15,37 +15,36 @@ // side can close, and they may be closed more than once. struct nni_msgq { - nni_mtx mq_lock; - nni_cv mq_drained; - int mq_cap; - int mq_alloc; // alloc is cap + 2... - int mq_len; - int mq_get; - int mq_put; - int mq_closed; - int mq_puterr; - int mq_geterr; - int mq_draining; - nni_msg ** mq_msgs; - - nni_list mq_aio_putq; - nni_list mq_aio_getq; - nni_list mq_aio_notify_get; - nni_list mq_aio_notify_put; - - nni_timer_node mq_timer; - nni_time mq_expire; + nni_mtx mq_lock; + nni_cv mq_drained; + int mq_cap; + int mq_alloc; // alloc is cap + 2... + int mq_len; + int mq_get; + int mq_put; + int mq_closed; + int mq_puterr; + int mq_geterr; + int mq_draining; + nni_msg **mq_msgs; + + nni_list mq_aio_putq; + nni_list mq_aio_getq; + nni_list mq_aio_notify_get; + nni_list mq_aio_notify_put; + + nni_timer_node mq_timer; + nni_time mq_expire; }; - static void nni_msgq_run_timeout(void *); int nni_msgq_init(nni_msgq **mqp, int cap) { struct nni_msgq *mq; - int rv; - int alloc; + int rv; + int alloc; if (cap < 0) { return (NNG_EINVAL); @@ -72,24 +71,24 @@ nni_msgq_init(nni_msgq **mqp, int cap) if ((rv = nni_cv_init(&mq->mq_drained, &mq->mq_lock)) != 0) { goto fail; } - if ((mq->mq_msgs = nni_alloc(sizeof (nng_msg *) * alloc)) == NULL) { + if ((mq->mq_msgs = nni_alloc(sizeof(nng_msg *) * alloc)) == NULL) { rv = NNG_ENOMEM; goto fail; } nni_timer_init(&mq->mq_timer, nni_msgq_run_timeout, mq); - mq->mq_cap = cap; - mq->mq_alloc = alloc; - mq->mq_len = 0; - mq->mq_get = 0; - mq->mq_put = 0; - mq->mq_closed = 0; - mq->mq_puterr = 0; - mq->mq_geterr = 0; - mq->mq_expire = NNI_TIME_NEVER; + mq->mq_cap = cap; + mq->mq_alloc = alloc; + mq->mq_len = 0; + mq->mq_get = 0; + mq->mq_put = 0; + mq->mq_closed = 0; + mq->mq_puterr = 0; + mq->mq_geterr = 0; + mq->mq_expire = NNI_TIME_NEVER; mq->mq_draining = 0; - *mqp = mq; + *mqp = mq; return (0); @@ -97,13 +96,12 @@ fail: nni_cv_fini(&mq->mq_drained); nni_mtx_fini(&mq->mq_lock); if (mq->mq_msgs != NULL) { - nni_free(mq->mq_msgs, sizeof (nng_msg *) * alloc); + nni_free(mq->mq_msgs, sizeof(nng_msg *) * alloc); } NNI_FREE_STRUCT(mq); return (rv); } - void nni_msgq_fini(nni_msgq *mq) { @@ -127,11 +125,10 @@ nni_msgq_fini(nni_msgq *mq) nni_msg_free(msg); } - nni_free(mq->mq_msgs, mq->mq_alloc * sizeof (nng_msg *)); + nni_free(mq->mq_msgs, mq->mq_alloc * sizeof(nng_msg *)); NNI_FREE_STRUCT(mq); } - static void nni_msgq_finish(nni_aio *aio, int rv) { @@ -139,7 +136,6 @@ nni_msgq_finish(nni_aio *aio, int rv) nni_aio_finish(aio, rv, 0); } - void nni_msgq_set_get_error(nni_msgq *mq, int error) { @@ -159,7 +155,6 @@ nni_msgq_set_get_error(nni_msgq *mq, int error) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_set_put_error(nni_msgq *mq, int error) { @@ -179,7 +174,6 @@ nni_msgq_set_put_error(nni_msgq *mq, int error) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_set_error(nni_msgq *mq, int error) { @@ -205,14 +199,13 @@ nni_msgq_set_error(nni_msgq *mq, int error) nni_mtx_unlock(&mq->mq_lock); } - static void nni_msgq_run_putq(nni_msgq *mq) { nni_aio *waio; nni_aio *raio; nni_msg *msg; - size_t len; + size_t len; while ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) { msg = waio->a_msg; @@ -248,7 +241,6 @@ nni_msgq_run_putq(nni_msgq *mq) } } - static void nni_msgq_run_getq(nni_msgq *mq) { @@ -272,7 +264,7 @@ nni_msgq_run_getq(nni_msgq *mq) // Nothing queued (unbuffered?), maybe a writer is waiting. if ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) { - msg = waio->a_msg; + msg = waio->a_msg; waio->a_msg = NULL; raio->a_msg = msg; nni_msgq_finish(raio, 0); @@ -286,7 +278,6 @@ nni_msgq_run_getq(nni_msgq *mq) } } - static void nni_msgq_run_notify(nni_msgq *mq) { @@ -316,7 +307,6 @@ nni_msgq_run_notify(nni_msgq *mq) } } - static void nni_msgq_cancel(nni_aio *aio) { @@ -331,7 +321,6 @@ nni_msgq_cancel(nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_notify_put(nni_msgq *mq, nni_aio *aio) { @@ -344,7 +333,6 @@ nni_msgq_aio_notify_put(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_notify_get(nni_msgq *mq, nni_aio *aio) { @@ -357,7 +345,6 @@ nni_msgq_aio_notify_get(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_put(nni_msgq *mq, nni_aio *aio) { @@ -391,7 +378,6 @@ nni_msgq_aio_put(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_aio_get(nni_msgq *mq, nni_aio *aio) { @@ -425,7 +411,6 @@ nni_msgq_aio_get(nni_msgq *mq, nni_aio *aio) nni_mtx_unlock(&mq->mq_lock); } - int nni_msgq_canput(nni_msgq *mq) { @@ -442,7 +427,6 @@ nni_msgq_canput(nni_msgq *mq) return (0); } - int nni_msgq_canget(nni_msgq *mq) { @@ -459,12 +443,11 @@ nni_msgq_canget(nni_msgq *mq) return (0); } - int nni_msgq_tryput(nni_msgq *mq, nni_msg *msg) { nni_aio *raio; - size_t len = nni_msg_len(msg); + size_t len = nni_msg_len(msg); nni_mtx_lock(&mq->mq_lock); if (mq->mq_closed) { @@ -500,16 +483,15 @@ nni_msgq_tryput(nni_msgq *mq, nni_msg *msg) return (NNG_EAGAIN); } - // XXX: Move this to generic AIO timeout... void nni_msgq_run_timeout(void *arg) { nni_msgq *mq = arg; - nni_time now; - nni_time exp; - nni_aio *aio; - nni_aio *naio; + nni_time now; + nni_time exp; + nni_aio * aio; + nni_aio * naio; now = nni_clock(); exp = NNI_TIME_NEVER; @@ -550,12 +532,11 @@ nni_msgq_run_timeout(void *arg) nni_mtx_unlock(&mq->mq_lock); } - int nni_msgq_get_until(nni_msgq *mq, nni_msg **msgp, nni_time expire) { nni_aio aio; - int rv; + int rv; if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) { return (rv); @@ -564,25 +545,24 @@ nni_msgq_get_until(nni_msgq *mq, nni_msg **msgp, nni_time expire) nni_msgq_aio_get(mq, &aio); nni_aio_wait(&aio); if ((rv = nni_aio_result(&aio)) == 0) { - *msgp = aio.a_msg; + *msgp = aio.a_msg; aio.a_msg = NULL; } nni_aio_fini(&aio); return (rv); } - int nni_msgq_put_until(nni_msgq *mq, nni_msg *msg, nni_time expire) { nni_aio aio; - int rv; + int rv; if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) { return (rv); } aio.a_expire = expire; - aio.a_msg = msg; + aio.a_msg = msg; nni_msgq_aio_put(mq, &aio); nni_aio_wait(&aio); rv = nni_aio_result(&aio); @@ -590,14 +570,13 @@ nni_msgq_put_until(nni_msgq *mq, nni_msg *msg, nni_time expire) return (rv); } - void nni_msgq_drain(nni_msgq *mq, nni_time expire) { nni_aio *aio; nni_mtx_lock(&mq->mq_lock); - mq->mq_closed = 1; + mq->mq_closed = 1; mq->mq_draining = 1; while ((mq->mq_len > 0) || !nni_list_empty(&mq->mq_aio_putq)) { if (nni_cv_until(&mq->mq_drained, expire) != 0) { @@ -622,7 +601,6 @@ nni_msgq_drain(nni_msgq *mq, nni_time expire) nni_mtx_unlock(&mq->mq_lock); } - void nni_msgq_close(nni_msgq *mq) { @@ -660,7 +638,6 @@ nni_msgq_close(nni_msgq *mq) nni_mtx_unlock(&mq->mq_lock); } - int nni_msgq_len(nni_msgq *mq) { @@ -672,7 +649,6 @@ nni_msgq_len(nni_msgq *mq) return (rv); } - int nni_msgq_cap(nni_msgq *mq) { @@ -684,23 +660,22 @@ nni_msgq_cap(nni_msgq *mq) return (rv); } - int nni_msgq_resize(nni_msgq *mq, int cap) { - int alloc; - nni_msg *msg; + int alloc; + nni_msg * msg; nni_msg **newq, **oldq; - int oldget; - int oldput; - int oldcap; - int oldlen; - int oldalloc; + int oldget; + int oldput; + int oldcap; + int oldlen; + int oldalloc; alloc = cap + 2; if (alloc > mq->mq_alloc) { - newq = nni_alloc(sizeof (nni_msg *) * alloc); + newq = nni_alloc(sizeof(nni_msg *) * alloc); if (newq == NULL) { return (NNG_ENOMEM); } @@ -726,17 +701,17 @@ nni_msgq_resize(nni_msgq *mq, int cap) goto out; } - oldq = mq->mq_msgs; - oldget = mq->mq_get; - oldput = mq->mq_put; - oldcap = mq->mq_cap; + oldq = mq->mq_msgs; + oldget = mq->mq_get; + oldput = mq->mq_put; + oldcap = mq->mq_cap; oldalloc = mq->mq_alloc; - oldlen = mq->mq_len; + oldlen = mq->mq_len; mq->mq_msgs = newq; mq->mq_len = mq->mq_get = mq->mq_put = 0; - mq->mq_cap = cap; - mq->mq_alloc = alloc; + mq->mq_cap = cap; + mq->mq_alloc = alloc; while (oldlen) { mq->mq_msgs[mq->mq_put++] = oldq[oldget++]; @@ -749,7 +724,7 @@ nni_msgq_resize(nni_msgq *mq, int cap) mq->mq_len++; oldlen--; } - nni_free(oldq, sizeof (nni_msg *) * oldalloc); + nni_free(oldq, sizeof(nni_msg *) * oldalloc); out: // Wake everyone up -- we changed everything. diff --git a/src/core/msgqueue.h b/src/core/msgqueue.h index 485eb9e7..794ac5cd 100644 --- a/src/core/msgqueue.h +++ b/src/core/msgqueue.h @@ -24,7 +24,7 @@ // // Readers & writers in a message queue can be woken either by a timeout // or by a specific signal (arranged by the caller). -typedef struct nni_msgq nni_msgq; +typedef struct nni_msgq nni_msgq; // nni_msgq_init creates a message queue with the given capacity, // which must be a positive number. It returns NNG_EINVAL if the capacity @@ -35,8 +35,8 @@ extern int nni_msgq_init(nni_msgq **, int); // messages that may be in the queue. extern void nni_msgq_fini(nni_msgq *); -extern int nni_msgq_canget(nni_msgq *); -extern int nni_msgq_canput(nni_msgq *); +extern int nni_msgq_canget(nni_msgq *); +extern int nni_msgq_canput(nni_msgq *); extern void nni_msgq_aio_put(nni_msgq *, nni_aio *); extern void nni_msgq_aio_get(nni_msgq *, nni_aio *); extern void nni_msgq_aio_notify_get(nni_msgq *, nni_aio *); @@ -108,4 +108,4 @@ extern int nni_msgq_cap(nni_msgq *mq); // nni_msgq_len returns the number of messages currently in the queue. extern int nni_msgq_len(nni_msgq *mq); -#endif // CORE_MSQUEUE_H +#endif // CORE_MSQUEUE_H diff --git a/src/core/nng_impl.h b/src/core/nng_impl.h index 291b5e28..8b76b036 100644 --- a/src/core/nng_impl.h +++ b/src/core/nng_impl.h @@ -44,9 +44,9 @@ #include "core/transport.h" // These have to come after the others - particularly transport.h +#include "core/endpt.h" #include "core/event.h" #include "core/pipe.h" #include "core/socket.h" -#include "core/endpt.h" -#endif // CORE_NNG_IMPL_H +#endif // CORE_NNG_IMPL_H diff --git a/src/core/objhash.c b/src/core/objhash.c index da4c6012..18c55f83 100644 --- a/src/core/objhash.c +++ b/src/core/objhash.c @@ -7,41 +7,41 @@ // found online at https://opensource.org/licenses/MIT. // -#include "core/nng_impl.h" #include "core/objhash.h" +#include "core/nng_impl.h" #include // The details of the nni_objhash are "private". struct nni_objhash { - size_t oh_cap; - size_t oh_count; - size_t oh_load; - size_t oh_minload; // considers placeholders - size_t oh_maxload; - uint32_t oh_minval; - uint32_t oh_maxval; - uint32_t oh_dynval; - nni_mtx oh_lock; - nni_cv oh_cv; - nni_objhash_node * oh_nodes; - nni_objhash_ctor oh_ctor; - nni_objhash_dtor oh_dtor; + size_t oh_cap; + size_t oh_count; + size_t oh_load; + size_t oh_minload; // considers placeholders + size_t oh_maxload; + uint32_t oh_minval; + uint32_t oh_maxval; + uint32_t oh_dynval; + nni_mtx oh_lock; + nni_cv oh_cv; + nni_objhash_node *oh_nodes; + nni_objhash_ctor oh_ctor; + nni_objhash_dtor oh_dtor; }; struct nni_objhash_node { - uint32_t on_id; // the key - uint32_t on_skips; // indicates - uint32_t on_refcnt; // reference count - void * on_val; // pointer to user data + uint32_t on_id; // the key + uint32_t on_skips; // indicates + uint32_t on_refcnt; // reference count + void * on_val; // pointer to user data }; int -nni_objhash_init(nni_objhash **ohp, nni_objhash_ctor ctor, - nni_objhash_dtor dtor) +nni_objhash_init( + nni_objhash **ohp, nni_objhash_ctor ctor, nni_objhash_dtor dtor) { nni_objhash *oh; - int rv; + int rv; if ((ctor == NULL) || (dtor == NULL)) { return (NNG_EINVAL); @@ -62,24 +62,23 @@ nni_objhash_init(nni_objhash **ohp, nni_objhash_ctor ctor, return (rv); } - oh->oh_nodes = NULL; - oh->oh_count = 0; - oh->oh_load = 0; - oh->oh_cap = 0; + oh->oh_nodes = NULL; + oh->oh_count = 0; + oh->oh_load = 0; + oh->oh_cap = 0; oh->oh_maxload = 0; oh->oh_minload = 0; // never shrink below this - oh->oh_minval = 1; - oh->oh_maxval = 0x7fffffff; - oh->oh_dynval = nni_random() % - (oh->oh_maxval - oh->oh_minval) + oh->oh_minval; + oh->oh_minval = 1; + oh->oh_maxval = 0x7fffffff; + oh->oh_dynval = + nni_random() % (oh->oh_maxval - oh->oh_minval) + oh->oh_minval; oh->oh_ctor = ctor; oh->oh_dtor = dtor; - *ohp = oh; + *ohp = oh; return (0); } - void nni_objhash_fini(nni_objhash *oh) { @@ -87,7 +86,7 @@ nni_objhash_fini(nni_objhash *oh) return; } if (oh->oh_nodes != NULL) { - nni_free(oh->oh_nodes, oh->oh_cap * sizeof (nni_objhash_node)); + nni_free(oh->oh_nodes, oh->oh_cap * sizeof(nni_objhash_node)); oh->oh_nodes = NULL; oh->oh_cap = oh->oh_count = 0; oh->oh_load = oh->oh_minload = oh->oh_maxload = 0; @@ -97,19 +96,16 @@ nni_objhash_fini(nni_objhash *oh) NNI_FREE_STRUCT(oh); } - // Inspired by Python dict implementation. This probe will visit every // cell. We always hash consecutively assigned IDs. -#define NNI_OBJHASH_NEXTPROBE(h, j) \ - ((((j) * 5) + 1)& (h->oh_cap - 1)) - +#define NNI_OBJHASH_NEXTPROBE(h, j) ((((j) *5) + 1) & (h->oh_cap - 1)) // nni_objhash_find_node finds the object hash node associated with a given id. // The object hash lock must be held by the caller. static nni_objhash_node * nni_objhash_find_node(nni_objhash *oh, uint32_t id) { - uint32_t index; + uint32_t index; nni_objhash_node *node; if (oh->oh_count == 0) { @@ -131,14 +127,13 @@ nni_objhash_find_node(nni_objhash *oh, uint32_t id) } } - // nni_objhash_find looks up the object, and bumps the reference on it. // The caller should drop the reference when done by calling nni_objhash_unref. int nni_objhash_find(nni_objhash *oh, uint32_t id, void **valp) { nni_objhash_node *node; - int rv; + int rv; nni_mtx_lock(&oh->oh_lock); node = nni_objhash_find_node(oh, id); @@ -156,18 +151,17 @@ nni_objhash_find(nni_objhash *oh, uint32_t id, void **valp) return (rv); } - // Resize the object hash. This is called internally with the lock // for the object hash held. Grow indicates that this is being called // from a function that intends to add data, so extra space is needed. static int nni_objhash_resize(nni_objhash *oh, int grow) { - size_t newsize; - size_t oldsize; + size_t newsize; + size_t oldsize; nni_objhash_node *newnodes; nni_objhash_node *oldnodes; - uint32_t i; + uint32_t i; if ((!grow) && (oh->oh_count == 0) && (oh->oh_cap != 0)) { // Table is empty, and we are unrefing. Lets reclaim the @@ -175,15 +169,16 @@ nni_objhash_resize(nni_objhash *oh, int grow) // fluctuate between one and zero are going to bang on the // allocator a bit. Since such cases should not be very // performance sensitive, this is probably okay. - nni_free(oh->oh_nodes, oh->oh_cap * sizeof (nni_objhash_node)); - oh->oh_cap = 0; - oh->oh_nodes = NULL; + nni_free(oh->oh_nodes, oh->oh_cap * sizeof(nni_objhash_node)); + oh->oh_cap = 0; + oh->oh_nodes = NULL; oh->oh_minload = 0; oh->oh_maxload = 0; return (0); } - if ((oh->oh_load < oh->oh_maxload) && (oh->oh_load >= oh->oh_minload)) { + if ((oh->oh_load < oh->oh_maxload) && + (oh->oh_load >= oh->oh_minload)) { // No resize needed. return (0); } @@ -197,14 +192,14 @@ nni_objhash_resize(nni_objhash *oh, int grow) } oldnodes = oh->oh_nodes; - newnodes = nni_alloc(sizeof (nni_objhash_node) * newsize); + newnodes = nni_alloc(sizeof(nni_objhash_node) * newsize); if (newnodes == NULL) { return (NNG_ENOMEM); } - memset(newnodes, 0, sizeof (nni_objhash_node) * newsize); + memset(newnodes, 0, sizeof(nni_objhash_node) * newsize); oh->oh_nodes = newnodes; - oh->oh_cap = newsize; + oh->oh_cap = newsize; if (newsize > 8) { oh->oh_minload = newsize / 8; oh->oh_maxload = newsize * 2 / 3; @@ -222,7 +217,7 @@ nni_objhash_resize(nni_objhash *oh, int grow) if (newnodes[index].on_val == NULL) { oh->oh_load++; newnodes[index].on_val = oldnodes[i].on_val; - newnodes[index].on_id = oldnodes[i].on_id; + newnodes[index].on_id = oldnodes[i].on_id; newnodes[index].on_refcnt = oldnodes[i].on_refcnt; break; @@ -232,19 +227,18 @@ nni_objhash_resize(nni_objhash *oh, int grow) } } if (oldsize != 0) { - nni_free(oldnodes, sizeof (nni_objhash_node) * oldsize); + nni_free(oldnodes, sizeof(nni_objhash_node) * oldsize); } return (0); } - void nni_objhash_unref(nni_objhash *oh, uint32_t id) { - void *val; - uint32_t index; + void * val; + uint32_t index; nni_objhash_node *node; - nni_objhash_dtor dtor; + nni_objhash_dtor dtor; nni_mtx_lock(&oh->oh_lock); @@ -303,14 +297,13 @@ nni_objhash_unref(nni_objhash *oh, uint32_t id) dtor(val); } - void nni_objhash_unref_wait(nni_objhash *oh, uint32_t id) { - void *val; - uint32_t index; + void * val; + uint32_t index; nni_objhash_node *node; - nni_objhash_dtor dtor; + nni_objhash_dtor dtor; nni_mtx_lock(&oh->oh_lock); @@ -360,7 +353,6 @@ nni_objhash_unref_wait(nni_objhash *oh, uint32_t id) dtor(val); } - // Allocate a new object hash entry. Note that this will execute the // constructor with the object hash lock held. Consequently, code that // runs the constructor must not run for long periods of time, since that @@ -368,8 +360,8 @@ nni_objhash_unref_wait(nni_objhash *oh, uint32_t id) int nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) { - uint32_t id; - uint32_t index; + uint32_t id; + uint32_t index; nni_objhash_node *node; nni_mtx_lock(&oh->oh_lock); @@ -418,7 +410,8 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) node->on_val = oh->oh_ctor(id); if (node->on_val == NULL) { - // Constructor failed; walk *again* to undo the skip increments. + // Constructor failed; walk *again* to undo the skip + // increments. node->on_refcnt--; index = id & (oh->oh_cap - 1); for (;;) { @@ -433,7 +426,7 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) } nni_mtx_unlock(&oh->oh_lock); - return (NNG_ENOMEM); // no other return from ctor + return (NNG_ENOMEM); // no other return from ctor } oh->oh_count++; @@ -441,7 +434,7 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) oh->oh_load++; } *valp = node->on_val; - *idp = id; + *idp = id; NNI_ASSERT(node->on_refcnt == 1); @@ -449,7 +442,6 @@ nni_objhash_alloc(nni_objhash *oh, uint32_t *idp, void **valp) return (0); } - size_t nni_objhash_count(nni_objhash *oh) { diff --git a/src/core/objhash.h b/src/core/objhash.h index e234ca3d..97f666b8 100644 --- a/src/core/objhash.h +++ b/src/core/objhash.h @@ -29,8 +29,8 @@ // are powers of two to make the modulo dirt cheap. // -typedef struct nni_objhash nni_objhash; -typedef struct nni_objhash_node nni_objhash_node; +typedef struct nni_objhash nni_objhash; +typedef struct nni_objhash_node nni_objhash_node; // Object constructor function. This is expected to allocate an object. // It takes the generated object ID as an argument, which it can store on @@ -44,14 +44,15 @@ typedef void (*nni_objhash_dtor)(void *); // nni_objhash_init initializes the object hash; the constructor and and // destructor functions are supplied. -extern int nni_objhash_init(nni_objhash **, nni_objhash_ctor, nni_objhash_dtor); +extern int nni_objhash_init( + nni_objhash **, nni_objhash_ctor, nni_objhash_dtor); extern void nni_objhash_fini(nni_objhash *); -extern int nni_objhash_find(nni_objhash *, uint32_t, void **); -extern void nni_objhash_unref(nni_objhash *, uint32_t); -extern void nni_objhash_unref_wait(nni_objhash *, uint32_t); -extern int nni_objhash_alloc(nni_objhash *, uint32_t *, void **); +extern int nni_objhash_find(nni_objhash *, uint32_t, void **); +extern void nni_objhash_unref(nni_objhash *, uint32_t); +extern void nni_objhash_unref_wait(nni_objhash *, uint32_t); +extern int nni_objhash_alloc(nni_objhash *, uint32_t *, void **); extern size_t nni_objhash_count(nni_objhash *); -#endif // CORE_OBJHASH_H +#endif // CORE_OBJHASH_H diff --git a/src/core/options.c b/src/core/options.c index d975c585..6d4afdb4 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -16,10 +16,10 @@ nni_setopt_duration(nni_duration *ptr, const void *val, size_t size) { nni_duration dur; - if (size != sizeof (*ptr)) { + if (size != sizeof(*ptr)) { return (NNG_EINVAL); } - memcpy(&dur, val, sizeof (dur)); + memcpy(&dur, val, sizeof(dur)); if (dur < -1) { return (NNG_EINVAL); } @@ -27,16 +27,15 @@ nni_setopt_duration(nni_duration *ptr, const void *val, size_t size) return (0); } - int nni_setopt_int(int *ptr, const void *val, size_t size, int minval, int maxval) { int v; - if (size != sizeof (v)) { + if (size != sizeof(v)) { return (NNG_EINVAL); } - memcpy(&v, val, sizeof (v)); + memcpy(&v, val, sizeof(v)); if (v > maxval) { return (NNG_EINVAL); } @@ -47,17 +46,16 @@ nni_setopt_int(int *ptr, const void *val, size_t size, int minval, int maxval) return (0); } - int -nni_setopt_size(size_t *ptr, const void *val, size_t size, size_t minval, - size_t maxval) +nni_setopt_size( + size_t *ptr, const void *val, size_t size, size_t minval, size_t maxval) { size_t v; - if (size != sizeof (v)) { + if (size != sizeof(v)) { return (NNG_EINVAL); } - memcpy(&v, val, sizeof (v)); + memcpy(&v, val, sizeof(v)); if (v > maxval) { return (NNG_EINVAL); } @@ -68,58 +66,54 @@ nni_setopt_size(size_t *ptr, const void *val, size_t size, size_t minval, return (0); } - int nni_getopt_duration(nni_duration *ptr, void *val, size_t *sizep) { - size_t sz = sizeof (*ptr); + size_t sz = sizeof(*ptr); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof (*ptr); + *sizep = sizeof(*ptr); memcpy(val, ptr, sz); return (0); } - int nni_getopt_int(int *ptr, void *val, size_t *sizep) { - size_t sz = sizeof (*ptr); + size_t sz = sizeof(*ptr); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof (*ptr); + *sizep = sizeof(*ptr); memcpy(val, ptr, sz); return (0); } - int nni_getopt_size(size_t *ptr, void *val, size_t *sizep) { - size_t sz = sizeof (*ptr); + size_t sz = sizeof(*ptr); if (sz > *sizep) { sz = *sizep; } - *sizep = sizeof (*ptr); + *sizep = sizeof(*ptr); memcpy(val, ptr, sz); return (0); } - int nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) { int len; - if (sz < sizeof (len)) { + if (sz < sizeof(len)) { return (NNG_EINVAL); } - memcpy(&len, val, sizeof (len)); + memcpy(&len, val, sizeof(len)); if (len < 0) { return (NNG_EINVAL); } @@ -132,7 +126,6 @@ nni_setopt_buf(nni_msgq *mq, const void *val, size_t sz) return (nni_msgq_resize(mq, len)); } - int nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep) { @@ -140,15 +133,14 @@ nni_getopt_buf(nni_msgq *mq, void *val, size_t *sizep) size_t sz = *sizep; - if (sz > sizeof (len)) { - sz = sizeof (len); + if (sz > sizeof(len)) { + sz = sizeof(len); } memcpy(val, &len, sz); - *sizep = sizeof (len); + *sizep = sizeof(len); return (0); } - static void nni_notifyfd_push(struct nng_event *ev, void *arg) { @@ -159,13 +151,12 @@ nni_notifyfd_push(struct nng_event *ev, void *arg) nni_plat_pipe_raise(fd->sn_wfd); } - int nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) { int rv; - if ((*szp < sizeof (int))) { + if ((*szp < sizeof(int))) { return (NNG_EINVAL); } @@ -186,8 +177,8 @@ nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) // If we already inited this, just give back the same file descriptor. if (fd->sn_init) { - memcpy(val, &fd->sn_rfd, sizeof (int)); - *szp = sizeof (int); + memcpy(val, &fd->sn_rfd, sizeof(int)); + *szp = sizeof(int); return (0); } @@ -200,7 +191,7 @@ nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp) return (NNG_ENOMEM); } - *szp = sizeof (int); - memcpy(val, &fd->sn_rfd, sizeof (int)); + *szp = sizeof(int); + memcpy(val, &fd->sn_rfd, sizeof(int)); return (0); } diff --git a/src/core/options.h b/src/core/options.h index ec5cce90..4f958e55 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -31,8 +31,8 @@ extern int nni_getopt_duration(nni_duration *, void *, size_t *); // maximum values (inclusive). extern int nni_setopt_int(int *, const void *, size_t, int, int); -#define NNI_MAXINT ((int) 2147483647) -#define NNI_MININT ((int) -2147483648) +#define NNI_MAXINT ((int) 2147483647) +#define NNI_MININT ((int) -2147483648) // nni_getopt_int gets an integer. extern int nni_getopt_int(int *, void *, size_t *); @@ -42,8 +42,8 @@ extern int nni_setopt_size(size_t *, const void *, size_t, size_t, size_t); // We limit the maximum size to 4GB. That's intentional because some of the // underlying protocols cannot cope with anything bigger than 32-bits. -#define NNI_MINSZ (0) -#define NNI_MAXSZ ((size_t) 0xffffffff) +#define NNI_MINSZ (0) +#define NNI_MAXSZ ((size_t) 0xffffffff) // nni_getopt_size obtains a size_t option. extern int nni_getopt_size(size_t *, void *, size_t *); @@ -51,4 +51,4 @@ extern int nni_getopt_size(size_t *, void *, size_t *); // nni_getopt_fd obtains a notification file descriptor. extern int nni_getopt_fd(nni_sock *, nni_notifyfd *, int, void *, size_t *); -#endif // CORE_OPTIONS_H +#endif // CORE_OPTIONS_H diff --git a/src/core/panic.c b/src/core/panic.c index b3d64dc3..f3c7cb26 100644 --- a/src/core/panic.c +++ b/src/core/panic.c @@ -7,12 +7,12 @@ // found online at https://opensource.org/licenses/MIT. // -#include -#include #include #include +#include +#include -#ifdef NNG_HAVE_BACKTRACE +#ifdef NNG_HAVE_BACKTRACE #include #endif @@ -23,12 +23,12 @@ void nni_show_backtrace(void) { #if NNG_HAVE_BACKTRACE - void *frames[50]; - int nframes; + void * frames[50]; + int nframes; char **lines; - int i; + int i; - nframes = backtrace(frames, sizeof (frames) / sizeof (frames[0])); + nframes = backtrace(frames, sizeof(frames) / sizeof(frames[0])); if (nframes > 1) { lines = backtrace_symbols(frames, nframes); if (lines == NULL) { @@ -41,7 +41,6 @@ nni_show_backtrace(void) #endif } - // nni_panic shows a panic message, a possible stack bracktrace, then aborts // the process/program. This should only be called when a condition arises // that should not be possible, e.g. a programming assertion failure. It should @@ -50,16 +49,15 @@ nni_show_backtrace(void) void nni_panic(const char *fmt, ...) { - char buf[128]; - char fbuf[128]; + char buf[128]; + char fbuf[128]; va_list va; va_start(va, fmt); - (void) vsnprintf(fbuf, sizeof (fbuf), fmt, va); + (void) vsnprintf(fbuf, sizeof(fbuf), fmt, va); va_end(va); - - (void) snprintf(buf, sizeof (buf), "panic: %s", fbuf); + (void) snprintf(buf, sizeof(buf), "panic: %s", fbuf); nni_println(buf); nni_println("This message is indicative of a BUG."); @@ -69,7 +67,6 @@ nni_panic(const char *fmt, ...) nni_plat_abort(); } - void nni_println(const char *msg) { diff --git a/src/core/panic.h b/src/core/panic.h index 323d64a1..6af14581 100644 --- a/src/core/panic.h +++ b/src/core/panic.h @@ -26,4 +26,4 @@ extern void nni_panic(const char *, ...); // include one. extern void nni_println(const char *); -#endif // CORE_PANIC_H +#endif // CORE_PANIC_H diff --git a/src/core/pipe.c b/src/core/pipe.c index 9151ede0..702e8eb1 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -29,10 +29,10 @@ nni_pipe_ctor(uint32_t id) return (NULL); } - p->p_tran_data = NULL; + p->p_tran_data = NULL; p->p_proto_data = NULL; p->p_proto_dtor = NULL; - p->p_id = id; + p->p_id = id; NNI_LIST_NODE_INIT(&p->p_sock_node); NNI_LIST_NODE_INIT(&p->p_ep_node); @@ -40,7 +40,6 @@ nni_pipe_ctor(uint32_t id) return (p); } - static void nni_pipe_dtor(void *ptr) { @@ -58,7 +57,6 @@ nni_pipe_dtor(void *ptr) NNI_FREE_STRUCT(p); } - int nni_pipe_sys_init(void) { @@ -73,7 +71,6 @@ nni_pipe_sys_init(void) return (0); } - void nni_pipe_sys_fini(void) { @@ -81,7 +78,6 @@ nni_pipe_sys_fini(void) nni_pipes = NULL; } - // nni_pipe_id returns the 32-bit pipe id, which can be used in backtraces. uint32_t nni_pipe_id(nni_pipe *p) @@ -89,21 +85,18 @@ nni_pipe_id(nni_pipe *p) return (p->p_id); } - void nni_pipe_recv(nni_pipe *p, nni_aio *aio) { p->p_tran_ops.p_recv(p->p_tran_data, aio); } - void nni_pipe_send(nni_pipe *p, nni_aio *aio) { p->p_tran_ops.p_send(p->p_tran_data, aio); } - // nni_pipe_close closes the underlying connection. It is expected that // subsequent attempts receive or send (including any waiting receive) will // simply return NNG_ECLOSED. @@ -128,7 +121,6 @@ nni_pipe_close(nni_pipe *p) nni_mtx_unlock(&p->p_mtx); } - // We have to stop asynchronously using a task, because otherwise we can // wind up having a callback from an AIO trying to cancel itself. That // simply will not work. @@ -147,7 +139,6 @@ nni_pipe_remove(nni_pipe *p) nni_objhash_unref(nni_pipes, p->p_id); } - void nni_pipe_stop(nni_pipe *p) { @@ -163,20 +154,18 @@ nni_pipe_stop(nni_pipe *p) nni_taskq_dispatch(NULL, &p->p_reap_tqe); } - uint16_t nni_pipe_peer(nni_pipe *p) { return (p->p_tran_ops.p_peer(p->p_tran_data)); } - static void nni_pipe_start_cb(void *arg) { - nni_pipe *p = arg; - nni_aio *aio = &p->p_start_aio; - int rv; + nni_pipe *p = arg; + nni_aio * aio = &p->p_start_aio; + int rv; nni_mtx_lock(&p->p_mtx); if ((rv = nni_aio_result(aio)) != 0) { @@ -192,13 +181,12 @@ nni_pipe_start_cb(void *arg) } } - int nni_pipe_create(nni_pipe **pp, nni_ep *ep, nni_sock *sock, nni_tran *tran) { nni_pipe *p; - int rv; - uint32_t id; + int rv; + uint32_t id; rv = nni_objhash_alloc(nni_pipes, &id, (void **) &p); if (rv != 0) { @@ -209,7 +197,7 @@ nni_pipe_create(nni_pipe **pp, nni_ep *ep, nni_sock *sock, nni_tran *tran) return (rv); } p->p_sock = sock; - p->p_ep = ep; + p->p_ep = ep; // Make a copy of the transport ops. We can override entry points // and we avoid an extra dereference on hot code paths. @@ -234,7 +222,6 @@ nni_pipe_create(nni_pipe **pp, nni_ep *ep, nni_sock *sock, nni_tran *tran) return (0); } - int nni_pipe_getopt(nni_pipe *p, int opt, void *val, size_t *szp) { @@ -245,7 +232,6 @@ nni_pipe_getopt(nni_pipe *p, int opt, void *val, size_t *szp) return (p->p_tran_ops.p_getopt(p->p_tran_data, opt, val, szp)); } - int nni_pipe_start(nni_pipe *p) { @@ -262,21 +248,18 @@ nni_pipe_start(nni_pipe *p) return (0); } - void * nni_pipe_get_proto_data(nni_pipe *p) { return (p->p_proto_data); } - void nni_pipe_sock_list_init(nni_list *list) { NNI_LIST_INIT(list, nni_pipe, p_sock_node); } - void nni_pipe_ep_list_init(nni_list *list) { diff --git a/src/core/pipe.h b/src/core/pipe.h index f7df8232..3107ffb3 100644 --- a/src/core/pipe.h +++ b/src/core/pipe.h @@ -19,23 +19,23 @@ #include "core/transport.h" struct nni_pipe { - uint32_t p_id; - nni_tran_pipe p_tran_ops; - void * p_tran_data; - void * p_proto_data; - nni_cb p_proto_dtor; - nni_list_node p_sock_node; - nni_list_node p_ep_node; - nni_sock * p_sock; - nni_ep * p_ep; - int p_reap; - int p_stop; - nni_mtx p_mtx; - nni_taskq_ent p_reap_tqe; - nni_aio p_start_aio; + uint32_t p_id; + nni_tran_pipe p_tran_ops; + void * p_tran_data; + void * p_proto_data; + nni_cb p_proto_dtor; + nni_list_node p_sock_node; + nni_list_node p_ep_node; + nni_sock * p_sock; + nni_ep * p_ep; + int p_reap; + int p_stop; + nni_mtx p_mtx; + nni_taskq_ent p_reap_tqe; + nni_aio p_start_aio; }; -extern int nni_pipe_sys_init(void); +extern int nni_pipe_sys_init(void); extern void nni_pipe_sys_fini(void); // AIO @@ -62,7 +62,7 @@ extern int nni_pipe_create(nni_pipe **, nni_ep *, nni_sock *, nni_tran *); extern uint16_t nni_pipe_proto(nni_pipe *); extern uint16_t nni_pipe_peer(nni_pipe *); -extern int nni_pipe_start(nni_pipe *); +extern int nni_pipe_start(nni_pipe *); extern int nni_pipe_getopt(nni_pipe *, int, void *, size_t *sizep); // nni_pipe_get_proto_data gets the protocol private data set with the diff --git a/src/core/platform.h b/src/core/platform.h index 107b180e..b3fc7572 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -76,9 +76,9 @@ extern void *nni_alloc(size_t); // Most implementations can just call free() here. extern void nni_free(void *, size_t); -typedef struct nni_plat_mtx nni_plat_mtx; -typedef struct nni_plat_cv nni_plat_cv; -typedef struct nni_plat_thr nni_plat_thr; +typedef struct nni_plat_mtx nni_plat_mtx; +typedef struct nni_plat_cv nni_plat_cv; +typedef struct nni_plat_thr nni_plat_thr; // // Threading & Synchronization Support @@ -187,7 +187,6 @@ extern int nni_plat_init(int (*)(void)); // will be called until nni_platform_init is called. extern void nni_plat_fini(void); - // nni_plat_lookup_host looks up a hostname in DNS, or the local hosts // file, or whatever. If your platform lacks support for naming, it must // at least cope with converting IP addresses in string form. The final @@ -199,8 +198,8 @@ extern int nni_plat_lookup_host(const char *, nni_sockaddr *, int); // TCP Support. // -typedef struct nni_plat_tcp_ep nni_plat_tcp_ep; -typedef struct nni_plat_tcp_pipe nni_plat_tcp_pipe; +typedef struct nni_plat_tcp_ep nni_plat_tcp_ep; +typedef struct nni_plat_tcp_pipe nni_plat_tcp_pipe; // nni_plat_tcp_ep_init creates a new endpoint associated with the url. extern int nni_plat_tcp_ep_init(nni_plat_tcp_ep **, const char *, int); @@ -247,15 +246,15 @@ extern void nni_plat_tcp_pipe_recv(nni_plat_tcp_pipe *, nni_aio *); // return names of either family. The passive flag indicates that the // name will be used for bind(), otherwise the name will be used with // connect(). The host part may be NULL only if passive is true. -extern void nni_plat_tcp_resolv(const char *, const char *, int, int, - nni_aio *); +extern void nni_plat_tcp_resolv( + const char *, const char *, int, int, nni_aio *); // // IPC (UNIX Domain Sockets & Named Pipes) Support. // -typedef struct nni_plat_ipc_ep nni_plat_ipc_ep; -typedef struct nni_plat_ipc_pipe nni_plat_ipc_pipe; +typedef struct nni_plat_ipc_ep nni_plat_ipc_ep; +typedef struct nni_plat_ipc_pipe nni_plat_ipc_pipe; // nni_plat_ipc_ep_init creates a new endpoint associated with the url. extern int nni_plat_ipc_ep_init(nni_plat_ipc_ep **, const char *, int); diff --git a/src/core/protocol.c b/src/core/protocol.c index 6aa59e84..a60454de 100644 --- a/src/core/protocol.c +++ b/src/core/protocol.c @@ -28,6 +28,7 @@ extern nni_proto nni_surveyor_proto; extern nni_proto nni_respondent_proto; static nni_proto *protocols[] = { + // clang-format off &nni_bus_proto, &nni_pair_proto, &nni_rep_proto, @@ -39,12 +40,13 @@ static nni_proto *protocols[] = { &nni_surveyor_proto, &nni_respondent_proto, NULL + // clang-format on }; nni_proto * nni_proto_find(uint16_t num) { - int i; + int i; nni_proto *p; for (i = 0; (p = protocols[i]) != NULL; i++) { @@ -55,7 +57,6 @@ nni_proto_find(uint16_t num) return (p); } - const char * nni_proto_name(uint16_t num) { @@ -67,12 +68,11 @@ nni_proto_name(uint16_t num) return (p->proto_name); } - uint16_t nni_proto_number(const char *name) { nni_proto *p; - int i; + int i; for (i = 0; (p = protocols[i]) != NULL; i++) { if (strcmp(p->proto_name, name) == 0) { @@ -82,7 +82,6 @@ nni_proto_number(const char *name) return (NNG_PROTO_NONE); } - uint16_t nni_proto_peer(uint16_t num) { diff --git a/src/core/protocol.h b/src/core/protocol.h index 745f5986..3a133469 100644 --- a/src/core/protocol.h +++ b/src/core/protocol.h @@ -24,18 +24,18 @@ struct nni_proto_pipe_ops { // pipe_init creates the protocol-specific per pipe data structure. // The last argument is the per-socket protocol private data. - int (*pipe_init)(void **, nni_pipe *, void *); + int (*pipe_init)(void **, nni_pipe *, void *); // pipe_fini releases any pipe data structures. This is called after // the pipe has been removed from the protocol, and the generic // pipe threads have been stopped. - void (*pipe_fini)(void *); + void (*pipe_fini)(void *); // pipe_start is called to register a pipe with the protocol. The // protocol can reject this, for example if another pipe is already // active on a 1:1 protocol. The protocol may not block during this, // as the socket lock is held. - int (*pipe_start)(void *); + int (*pipe_start)(void *); // pipe_stop is called to unregister a pipe from the protocol. // Threads may still acccess data structures, so the protocol @@ -43,66 +43,66 @@ struct nni_proto_pipe_ops { // lock held, so the protocol may not call back into the socket, and // must not block. This operation must be idempotent, and may // be called even if pipe_start was not. - void (*pipe_stop)(void *); + void (*pipe_stop)(void *); }; struct nni_proto_sock_ops { // sock_initf creates the protocol instance, which will be stored on // the socket. This is run without the sock lock held, and allocates // storage or other resources for the socket. - int (*sock_init)(void **, nni_sock *); + int (*sock_init)(void **, nni_sock *); // sock_fini destroys the protocol instance. This is run without the // socket lock held, and is intended to release resources. It may // block as needed. - void (*sock_fini)(void *); + void (*sock_fini)(void *); // Open the protocol instance. This is run with the lock held, // and intended to allow the protocol to start any asynchronous // processing. - void (*sock_open)(void *); + void (*sock_open)(void *); // Close the protocol instance. This is run with the lock held, // and intended to initiate closure of the socket. For example, // it can signal the socket worker threads to exit. - void (*sock_close)(void *); + void (*sock_close)(void *); // Option manipulation. These may be NULL. - int (*sock_setopt)(void *, int, const void *, size_t); - int (*sock_getopt)(void *, int, void *, size_t *); + int (*sock_setopt)(void *, int, const void *, size_t); + int (*sock_getopt)(void *, int, void *, size_t *); // Receive filter. This may be NULL, but if it isn't, then // messages coming into the system are routed here just before being // delivered to the application. To drop the message, the prtocol // should return NULL, otherwise the message (possibly modified). - nni_msg * (*sock_rfilter)(void *, nni_msg *); + nni_msg *(*sock_rfilter)(void *, nni_msg *); // Send filter. This may be NULL, but if it isn't, then messages // here are filtered just after they come from the application. - nni_msg * (*sock_sfilter)(void *, nni_msg *); + nni_msg *(*sock_sfilter)(void *, nni_msg *); }; struct nni_proto { - uint16_t proto_self; // our 16-bit D - uint16_t proto_peer; // who we peer with (ID) - const char * proto_name; // Our name - uint32_t proto_flags; // Protocol flags - const nni_proto_sock_ops * proto_sock_ops; // Per-socket opeations - const nni_proto_pipe_ops * proto_pipe_ops; // Per-pipe operations. + uint16_t proto_self; // our 16-bit D + uint16_t proto_peer; // who we peer with (ID) + const char * proto_name; // Our name + uint32_t proto_flags; // Protocol flags + const nni_proto_sock_ops *proto_sock_ops; // Per-socket opeations + const nni_proto_pipe_ops *proto_pipe_ops; // Per-pipe operations. }; // These flags determine which operations make sense. We use them so that // we can reject attempts to create notification fds for operations that make // no sense. -#define NNI_PROTO_FLAG_RCV 1 // Protocol can receive -#define NNI_PROTO_FLAG_SND 2 // Protocol can send -#define NNI_PROTO_FLAG_SNDRCV 3 // Protocol can both send & recv +#define NNI_PROTO_FLAG_RCV 1 // Protocol can receive +#define NNI_PROTO_FLAG_SND 2 // Protocol can send +#define NNI_PROTO_FLAG_SNDRCV 3 // Protocol can both send & recv // These functions are not used by protocols, but rather by the socket // core implementation. The lookups can be used by transports as well. -extern nni_proto *nni_proto_find(uint16_t); +extern nni_proto * nni_proto_find(uint16_t); extern const char *nni_proto_name(uint16_t); -extern uint16_t nni_proto_number(const char *); -extern uint16_t nni_proto_peer(uint16_t); +extern uint16_t nni_proto_number(const char *); +extern uint16_t nni_proto_peer(uint16_t); #endif // CORE_PROTOCOL_H diff --git a/src/core/random.c b/src/core/random.c index 00c0bcf7..eb0b4fc2 100644 --- a/src/core/random.c +++ b/src/core/random.c @@ -21,47 +21,46 @@ typedef struct { // the rsl is the actual results, and the randcnt is the length // of the results. - uint32_t randrsl[256]; - uint32_t randcnt; + uint32_t randrsl[256]; + uint32_t randcnt; // lock to protect concurrent access - nni_mtx mx; + nni_mtx mx; // more or less internal state - uint32_t mm[256]; - uint32_t aa; - uint32_t bb; - uint32_t cc; + uint32_t mm[256]; + uint32_t aa; + uint32_t bb; + uint32_t cc; } nni_isaac_ctx; - static void nni_isaac(nni_isaac_ctx *ctx) { register uint32_t i, x, y; - ctx->cc++; // cc incremented once per 256 results - ctx->bb += ctx->cc; // then combined with bb + ctx->cc++; // cc incremented once per 256 results + ctx->bb += ctx->cc; // then combined with bb for (i = 0; i < 256; ++i) { x = ctx->mm[i]; - switch (i%4) { + switch (i % 4) { case 0: - ctx->aa ^= (ctx->aa<<13); + ctx->aa ^= (ctx->aa << 13); break; case 1: - ctx->aa ^= (ctx->aa>>6); + ctx->aa ^= (ctx->aa >> 6); break; case 2: - ctx->aa ^= (ctx->aa<<2); + ctx->aa ^= (ctx->aa << 2); break; case 3: - ctx->aa ^= (ctx->aa>>16); + ctx->aa ^= (ctx->aa >> 16); break; } - ctx->aa += ctx->mm[(i+128)%256]; - ctx->mm[i] = y = ctx->mm[(x>>2)%256] + ctx->aa + ctx->bb; - ctx->randrsl[i] = ctx->bb = ctx->mm[(y>>10)%256] + x; + ctx->aa += ctx->mm[(i + 128) % 256]; + ctx->mm[i] = y = ctx->mm[(x >> 2) % 256] + ctx->aa + ctx->bb; + ctx->randrsl[i] = ctx->bb = ctx->mm[(y >> 10) % 256] + x; // Note that bits 2..9 are chosen from x but 10..17 are chosen // from y. The only important thing here is that 2..9 and @@ -73,83 +72,97 @@ nni_isaac(nni_isaac_ctx *ctx) } } - // if (flag!=0), then use the contents of randrsl[] to initialize mm[]. #define nni_isaac_mix(a, b, c, d, e, f, g, h) \ - { \ - a ^= b<<11; d += a; b += c; \ - b ^= c>>2; e += b; c += d; \ - c ^= d<<8; f += c; d += e; \ - d ^= e>>16; g += d; e += f; \ - e ^= f<<10; h += e; f += g; \ - f ^= g>>4; a += f; g += h; \ - g ^= h<<8; b += g; h += a; \ - h ^= a>>9; c += h; a += b; \ + { \ + a ^= b << 11; \ + d += a; \ + b += c; \ + b ^= c >> 2; \ + e += b; \ + c += d; \ + c ^= d << 8; \ + f += c; \ + d += e; \ + d ^= e >> 16; \ + g += d; \ + e += f; \ + e ^= f << 10; \ + h += e; \ + f += g; \ + f ^= g >> 4; \ + a += f; \ + g += h; \ + g ^= h << 8; \ + b += g; \ + h += a; \ + h ^= a >> 9; \ + c += h; \ + a += b; \ } static void nni_isaac_randinit(nni_isaac_ctx *ctx, int flag) { - int i; + int i; uint32_t a, b, c, d, e, f, g, h; ctx->aa = ctx->bb = ctx->cc = 0; - a = b = c = d = e = f = g = h = 0x9e3779b9; // the golden ratio + a = b = c = d = e = f = g = h = 0x9e3779b9; // the golden ratio - for (i = 0; i < 4; ++i) { // scramble it + for (i = 0; i < 4; ++i) { // scramble it nni_isaac_mix(a, b, c, d, e, f, g, h); } - for (i = 0; i < 256; i += 8) { // fill in mm[] with messy stuff - if (flag) { // use all the information in the seed + for (i = 0; i < 256; i += 8) { // fill in mm[] with messy stuff + if (flag) { // use all the information in the seed a += ctx->randrsl[i]; - b += ctx->randrsl[i+1]; - c += ctx->randrsl[i+2]; - d += ctx->randrsl[i+3]; - e += ctx->randrsl[i+4]; - f += ctx->randrsl[i+5]; - g += ctx->randrsl[i+6]; - h += ctx->randrsl[i+7]; + b += ctx->randrsl[i + 1]; + c += ctx->randrsl[i + 2]; + d += ctx->randrsl[i + 3]; + e += ctx->randrsl[i + 4]; + f += ctx->randrsl[i + 5]; + g += ctx->randrsl[i + 6]; + h += ctx->randrsl[i + 7]; } nni_isaac_mix(a, b, c, d, e, f, g, h); - ctx->mm[i] = a; - ctx->mm[i+1] = b; - ctx->mm[i+2] = c; - ctx->mm[i+3] = d; - ctx->mm[i+4] = e; - ctx->mm[i+5] = f; - ctx->mm[i+6] = g; - ctx->mm[i+7] = h; + ctx->mm[i] = a; + ctx->mm[i + 1] = b; + ctx->mm[i + 2] = c; + ctx->mm[i + 3] = d; + ctx->mm[i + 4] = e; + ctx->mm[i + 5] = f; + ctx->mm[i + 6] = g; + ctx->mm[i + 7] = h; } if (flag) { // do a second pass to make all of the seed affect all of mm for (i = 0; i < 256; i += 8) { a += ctx->mm[i]; - b += ctx->mm[i+1]; - c += ctx->mm[i+2]; - d += ctx->mm[i+3]; - e += ctx->mm[i+4]; - f += ctx->mm[i+5]; - g += ctx->mm[i+6]; - h += ctx->mm[i+7]; + b += ctx->mm[i + 1]; + c += ctx->mm[i + 2]; + d += ctx->mm[i + 3]; + e += ctx->mm[i + 4]; + f += ctx->mm[i + 5]; + g += ctx->mm[i + 6]; + h += ctx->mm[i + 7]; nni_isaac_mix(a, b, c, d, e, f, g, h); - ctx->mm[i] = a; - ctx->mm[i+1] = b; - ctx->mm[i+2] = c; - ctx->mm[i+3] = d; - ctx->mm[i+4] = e; - ctx->mm[i+5] = f; - ctx->mm[i+6] = g; - ctx->mm[i+7] = h; + ctx->mm[i] = a; + ctx->mm[i + 1] = b; + ctx->mm[i + 2] = c; + ctx->mm[i + 3] = d; + ctx->mm[i + 4] = e; + ctx->mm[i + 5] = f; + ctx->mm[i + 6] = g; + ctx->mm[i + 7] = h; } } - nni_isaac(ctx); // fill in the first set of results - ctx->randcnt = 256; // prepare to use the first set of results + nni_isaac(ctx); // fill in the first set of results + ctx->randcnt = 256; // prepare to use the first set of results } - static nni_isaac_ctx nni_random_ctx; int @@ -157,22 +170,21 @@ nni_random_sys_init(void) { // minimally, grab the system clock nni_isaac_ctx *ctx = &nni_random_ctx; - int rv; + int rv; if ((rv = nni_mtx_init(&ctx->mx)) != 0) { return (rv); } - nni_plat_seed_prng(ctx->randrsl, sizeof (ctx->randrsl)); + nni_plat_seed_prng(ctx->randrsl, sizeof(ctx->randrsl)); nni_isaac_randinit(ctx, 1); return (0); } - uint32_t nni_random(void) { - uint32_t rv; + uint32_t rv; nni_isaac_ctx *ctx = &nni_random_ctx; nni_mtx_lock(&ctx->mx); @@ -187,7 +199,6 @@ nni_random(void) return (rv); } - void nni_random_sys_fini(void) { diff --git a/src/core/socket.c b/src/core/socket.c index f7712aa3..c561b719 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -21,7 +21,6 @@ nni_sock_id(nni_sock *s) return (s->s_id); } - // nni_sock_sendq and nni_sock_recvq are called by the protocol to obtain // the upper read and write queues. nni_msgq * @@ -30,18 +29,16 @@ nni_sock_sendq(nni_sock *s) return (s->s_uwq); } - nni_msgq * nni_sock_recvq(nni_sock *s) { return (s->s_urq); } - int nni_sock_find(nni_sock **sockp, uint32_t id) { - int rv; + int rv; nni_sock *sock; if ((rv = nni_init()) != 0) { @@ -64,7 +61,6 @@ nni_sock_find(nni_sock **sockp, uint32_t id) return (0); } - void nni_sock_hold(nni_sock *sock) { @@ -74,18 +70,16 @@ nni_sock_hold(nni_sock *sock) NNI_ASSERT(rv == 0); } - void nni_sock_rele(nni_sock *sock) { nni_objhash_unref(nni_socks, sock->s_id); } - int nni_sock_pipe_ready(nni_sock *sock, nni_pipe *pipe) { - int rv; + int rv; void *pdata = nni_pipe_get_proto_data(pipe); nni_mtx_lock(&sock->s_mx); @@ -112,7 +106,6 @@ nni_sock_pipe_ready(nni_sock *sock, nni_pipe *pipe) return (0); } - void nni_sock_pipe_stop(nni_sock *sock, nni_pipe *pipe) { @@ -136,26 +129,23 @@ nni_sock_pipe_stop(nni_sock *sock, nni_pipe *pipe) nni_mtx_unlock(&sock->s_mx); } - void nni_sock_lock(nni_sock *sock) { nni_mtx_lock(&sock->s_mx); } - void nni_sock_unlock(nni_sock *sock) { nni_mtx_unlock(&sock->s_mx); } - static void nni_sock_cansend_cb(void *arg) { nni_notify *notify = arg; - nni_sock *sock = notify->n_sock; + nni_sock * sock = notify->n_sock; if (nni_aio_result(¬ify->n_aio) != 0) { return; @@ -164,12 +154,11 @@ nni_sock_cansend_cb(void *arg) notify->n_func(&sock->s_send_ev, notify->n_arg); } - static void nni_sock_canrecv_cb(void *arg) { nni_notify *notify = arg; - nni_sock *sock = notify->n_sock; + nni_sock * sock = notify->n_sock; if (nni_aio_result(¬ify->n_aio) != 0) { return; @@ -178,19 +167,18 @@ nni_sock_canrecv_cb(void *arg) notify->n_func(&sock->s_recv_ev, notify->n_arg); } - nni_notify * nni_sock_notify(nni_sock *sock, int type, nng_notify_func fn, void *arg) { nni_notify *notify; - int rv; + int rv; if ((notify = NNI_ALLOC_STRUCT(notify)) == NULL) { return (NULL); } notify->n_func = fn; - notify->n_arg = arg; + notify->n_arg = arg; notify->n_type = type; notify->n_sock = sock; @@ -223,7 +211,6 @@ fail: return (NULL); } - void nni_sock_unnotify(nni_sock *sock, nni_notify *notify) { @@ -231,14 +218,12 @@ nni_sock_unnotify(nni_sock *sock, nni_notify *notify) NNI_FREE_STRUCT(notify); } - nni_mtx * nni_sock_mtx(nni_sock *sock) { return (&sock->s_mx); } - static nni_msg * nni_sock_nullfilter(void *arg, nni_msg *mp) { @@ -246,7 +231,6 @@ nni_sock_nullfilter(void *arg, nni_msg *mp) return (mp); } - static int nni_sock_nullgetopt(void *arg, int num, void *data, size_t *szp) { @@ -257,7 +241,6 @@ nni_sock_nullgetopt(void *arg, int num, void *data, size_t *szp) return (NNG_ENOTSUP); } - static int nni_sock_nullsetopt(void *arg, int num, const void *data, size_t sz) { @@ -268,14 +251,12 @@ nni_sock_nullsetopt(void *arg, int num, const void *data, size_t sz) return (NNG_ENOTSUP); } - static void nni_sock_nullop(void *arg) { NNI_ARG_UNUSED(arg); } - static int nni_sock_nullstartpipe(void *arg) { @@ -284,25 +265,24 @@ nni_sock_nullstartpipe(void *arg) return (0); } - static void * nni_sock_ctor(uint32_t id) { - int rv; + int rv; nni_sock *sock; if ((sock = NNI_ALLOC_STRUCT(sock)) == NULL) { return (NULL); } // s_protocol, s_peer, and s_flags undefined as yet. - sock->s_linger = 0; - sock->s_sndtimeo = -1; - sock->s_rcvtimeo = -1; - sock->s_closing = 0; - sock->s_reconn = NNI_SECOND; + sock->s_linger = 0; + sock->s_sndtimeo = -1; + sock->s_rcvtimeo = -1; + sock->s_closing = 0; + sock->s_reconn = NNI_SECOND; sock->s_reconnmax = 0; - sock->s_rcvmaxsz = 1024 * 1024; // 1 MB by default - sock->s_id = id; + sock->s_rcvmaxsz = 1024 * 1024; // 1 MB by default + sock->s_id = id; nni_pipe_sock_list_init(&sock->s_pipes); @@ -343,7 +323,6 @@ fail: return (NULL); } - static void nni_sock_dtor(void *ptr) { @@ -351,12 +330,12 @@ nni_sock_dtor(void *ptr) // Close any open notification pipes. if (sock->s_recv_fd.sn_init) { - nni_plat_pipe_close(sock->s_recv_fd.sn_wfd, - sock->s_recv_fd.sn_rfd); + nni_plat_pipe_close( + sock->s_recv_fd.sn_wfd, sock->s_recv_fd.sn_rfd); } if (sock->s_send_fd.sn_init) { - nni_plat_pipe_close(sock->s_send_fd.sn_wfd, - sock->s_send_fd.sn_rfd); + nni_plat_pipe_close( + sock->s_send_fd.sn_wfd, sock->s_send_fd.sn_rfd); } // The protocol needs to clean up its state. @@ -373,7 +352,6 @@ nni_sock_dtor(void *ptr) NNI_FREE_STRUCT(sock); } - int nni_sock_sys_init(void) { @@ -384,7 +362,6 @@ nni_sock_sys_init(void) return (rv); } - void nni_sock_sys_fini(void) { @@ -392,17 +369,16 @@ nni_sock_sys_fini(void) nni_socks = NULL; } - // nn_sock_open creates the underlying socket. int nni_sock_open(nni_sock **sockp, uint16_t pnum) { - nni_sock *sock; - nni_proto *proto; - int rv; + nni_sock * sock; + nni_proto * proto; + int rv; nni_proto_sock_ops *sops; nni_proto_pipe_ops *pops; - uint32_t sockid; + uint32_t sockid; if ((rv = nni_init()) != 0) { return (rv); @@ -418,8 +394,8 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum) // We make a copy of the protocol operations. sock->s_protocol = proto->proto_self; - sock->s_peer = proto->proto_peer; - sock->s_flags = proto->proto_flags; + sock->s_peer = proto->proto_peer; + sock->s_flags = proto->proto_flags; sock->s_sock_ops = *proto->proto_sock_ops; sops = &sock->s_sock_ops; @@ -442,7 +418,7 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum) sops->sock_open = nni_sock_nullop; } sock->s_pipe_ops = *proto->proto_pipe_ops; - pops = &sock->s_pipe_ops; + pops = &sock->s_pipe_ops; if (pops->pipe_start == NULL) { pops->pipe_start = nni_sock_nullstartpipe; } @@ -461,7 +437,6 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum) return (0); } - // nni_sock_shutdown shuts down the socket; after this point no further // access to the socket will function, and any threads blocked in entry // points will be woken (and the functions they are blocked in will return @@ -470,8 +445,8 @@ int nni_sock_shutdown(nni_sock *sock) { nni_pipe *pipe; - nni_ep *ep; - nni_time linger; + nni_ep * ep; + nni_time linger; nni_mtx_lock(&sock->s_mx); if (sock->s_closing) { @@ -566,7 +541,6 @@ nni_sock_shutdown(nni_sock *sock) return (0); } - // nni_sock_ep_add adds a newly created endpoint to the socket. The // caller must hold references on the sock and the ep, and not be holding // the socket lock. The ep acquires a reference against the sock, @@ -586,7 +560,6 @@ nni_sock_ep_add(nni_sock *sock, nni_ep *ep) return (0); } - void nni_sock_ep_remove(nni_sock *sock, nni_ep *ep) { @@ -602,7 +575,6 @@ nni_sock_ep_remove(nni_sock *sock, nni_ep *ep) nni_mtx_unlock(&sock->s_mx); } - // nni_sock_close shuts down the socket, then releases any resources // associated with it. It is a programmer error to reference the socket // after this function is called, as the pointer may reference invalid @@ -637,7 +609,6 @@ nni_sock_close(nni_sock *sock) nni_objhash_unref_wait(nni_socks, sock->s_id); } - int nni_sock_sendmsg(nni_sock *sock, nni_msg *msg, nni_time expire) { @@ -682,11 +653,10 @@ nni_sock_sendmsg(nni_sock *sock, nni_msg *msg, nni_time expire) return (rv); } - int nni_sock_recvmsg(nni_sock *sock, nni_msg **msgp, nni_time expire) { - int rv; + int rv; nni_msg *msg; nni_mtx_lock(&sock->s_mx); @@ -722,7 +692,6 @@ nni_sock_recvmsg(nni_sock *sock, nni_msg **msgp, nni_time expire) return (0); } - // nni_sock_protocol returns the socket's 16-bit protocol number. uint16_t nni_sock_proto(nni_sock *sock) @@ -730,28 +699,24 @@ nni_sock_proto(nni_sock *sock) return (sock->s_protocol); } - uint16_t nni_sock_peer(nni_sock *sock) { return (sock->s_peer); } - nni_duration nni_sock_linger(nni_sock *sock) { return (sock->s_linger); } - size_t nni_sock_rcvmaxsz(nni_sock *sock) { return (sock->s_rcvmaxsz); } - void nni_sock_reconntimes(nni_sock *sock, nni_duration *rcur, nni_duration *rmax) { @@ -762,12 +727,11 @@ nni_sock_reconntimes(nni_sock *sock, nni_duration *rcur, nni_duration *rmax) nni_mtx_unlock(&sock->s_mx); } - int nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags) { nni_ep *ep; - int rv; + int rv; if ((rv = nni_ep_create(&ep, sock, addr, NNI_EP_MODE_DIAL)) != 0) { return (rv); @@ -782,12 +746,11 @@ nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags) return (rv); } - int nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags) { nni_ep *ep; - int rv; + int rv; if ((rv = nni_ep_create(&ep, sock, addr, NNI_EP_MODE_LISTEN)) != 0) { return (rv); @@ -802,21 +765,18 @@ nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags) return (rv); } - void nni_sock_recverr(nni_sock *sock, int err) { sock->s_recverr = err; } - void nni_sock_senderr(nni_sock *sock, int err) { sock->s_senderr = err; } - int nni_sock_setopt(nni_sock *sock, int opt, const void *val, size_t size) { @@ -855,15 +815,14 @@ nni_sock_setopt(nni_sock *sock, int opt, const void *val, size_t size) rv = nni_setopt_buf(sock->s_urq, val, size); break; case NNG_OPT_RCVMAXSZ: - rv = nni_setopt_size(&sock->s_rcvmaxsz, val, size, 0, - NNI_MAXSZ); + rv = nni_setopt_size( + &sock->s_rcvmaxsz, val, size, 0, NNI_MAXSZ); break; } nni_mtx_unlock(&sock->s_mx); return (rv); } - int nni_sock_getopt(nni_sock *sock, int opt, void *val, size_t *sizep) { @@ -905,12 +864,12 @@ nni_sock_getopt(nni_sock *sock, int opt, void *val, size_t *sizep) rv = nni_getopt_size(&sock->s_rcvmaxsz, val, sizep); break; case NNG_OPT_SNDFD: - rv = nni_getopt_fd(sock, &sock->s_send_fd, NNG_EV_CAN_SND, - val, sizep); + rv = nni_getopt_fd( + sock, &sock->s_send_fd, NNG_EV_CAN_SND, val, sizep); break; case NNG_OPT_RCVFD: - rv = nni_getopt_fd(sock, &sock->s_recv_fd, NNG_EV_CAN_RCV, - val, sizep); + rv = nni_getopt_fd( + sock, &sock->s_recv_fd, NNG_EV_CAN_RCV, val, sizep); break; } nni_mtx_unlock(&sock->s_mx); diff --git a/src/core/socket.h b/src/core/socket.h index 928264d9..c4619b42 100644 --- a/src/core/socket.h +++ b/src/core/socket.h @@ -10,82 +10,81 @@ #ifndef CORE_SOCKET_H #define CORE_SOCKET_H - // NB: This structure is supplied here for use by the CORE. Use of this library // OUSIDE of the core is STRICTLY VERBOTEN. NO DIRECT ACCESS BY PROTOCOLS OR // TRANSPORTS. struct nni_socket { - nni_mtx s_mx; - nni_cv s_cv; + nni_mtx s_mx; + nni_cv s_cv; - uint32_t s_id; + uint32_t s_id; - nni_msgq * s_uwq; // Upper write queue - nni_msgq * s_urq; // Upper read queue + nni_msgq *s_uwq; // Upper write queue + nni_msgq *s_urq; // Upper read queue - uint16_t s_protocol; - uint16_t s_peer; - uint32_t s_flags; + uint16_t s_protocol; + uint16_t s_peer; + uint32_t s_flags; - nni_proto_pipe_ops s_pipe_ops; - nni_proto_sock_ops s_sock_ops; + nni_proto_pipe_ops s_pipe_ops; + nni_proto_sock_ops s_sock_ops; - void * s_data; // Protocol private + void *s_data; // Protocol private // XXX: options - nni_duration s_linger; // linger time - nni_duration s_sndtimeo; // send timeout - nni_duration s_rcvtimeo; // receive timeout - nni_duration s_reconn; // reconnect time - nni_duration s_reconnmax; // max reconnect time + nni_duration s_linger; // linger time + nni_duration s_sndtimeo; // send timeout + nni_duration s_rcvtimeo; // receive timeout + nni_duration s_reconn; // reconnect time + nni_duration s_reconnmax; // max reconnect time - nni_list s_eps; // active endpoints - nni_list s_pipes; // active pipes + nni_list s_eps; // active endpoints + nni_list s_pipes; // active pipes - size_t s_rcvmaxsz; // maximum receive size + size_t s_rcvmaxsz; // maximum receive size - int s_ep_pend; // EP dial/listen in progress - int s_closing; // Socket is closing - int s_closed; // Socket closed - int s_besteffort; // Best effort mode delivery - int s_senderr; // Protocol state machine use - int s_recverr; // Protocol state machine use + int s_ep_pend; // EP dial/listen in progress + int s_closing; // Socket is closing + int s_closed; // Socket closed + int s_besteffort; // Best effort mode delivery + int s_senderr; // Protocol state machine use + int s_recverr; // Protocol state machine use - nni_event s_recv_ev; // Event for readability - nni_event s_send_ev; // Event for sendability + nni_event s_recv_ev; // Event for readability + nni_event s_send_ev; // Event for sendability - nni_notifyfd s_send_fd; - nni_notifyfd s_recv_fd; + nni_notifyfd s_send_fd; + nni_notifyfd s_recv_fd; - uint32_t s_nextid; // Next Pipe ID. + uint32_t s_nextid; // Next Pipe ID. }; -extern int nni_sock_sys_init(void); +extern int nni_sock_sys_init(void); extern void nni_sock_sys_fini(void); -extern int nni_sock_find(nni_sock **, uint32_t); -extern void nni_sock_hold(nni_sock *); -extern void nni_sock_rele(nni_sock *); -extern int nni_sock_open(nni_sock **, uint16_t); -extern void nni_sock_close(nni_sock *); -extern int nni_sock_shutdown(nni_sock *); +extern int nni_sock_find(nni_sock **, uint32_t); +extern void nni_sock_hold(nni_sock *); +extern void nni_sock_rele(nni_sock *); +extern int nni_sock_open(nni_sock **, uint16_t); +extern void nni_sock_close(nni_sock *); +extern int nni_sock_shutdown(nni_sock *); extern uint16_t nni_sock_proto(nni_sock *); extern uint16_t nni_sock_peer(nni_sock *); -extern int nni_sock_setopt(nni_sock *, int, const void *, size_t); -extern int nni_sock_getopt(nni_sock *, int, void *, size_t *); -extern int nni_sock_recvmsg(nni_sock *, nni_msg **, nni_time); -extern int nni_sock_sendmsg(nni_sock *, nni_msg *, nni_time); -extern int nni_sock_dial(nni_sock *, const char *, nni_ep **, int); -extern int nni_sock_listen(nni_sock *, const char *, nni_ep **, int); +extern int nni_sock_setopt(nni_sock *, int, const void *, size_t); +extern int nni_sock_getopt(nni_sock *, int, void *, size_t *); +extern int nni_sock_recvmsg(nni_sock *, nni_msg **, nni_time); +extern int nni_sock_sendmsg(nni_sock *, nni_msg *, nni_time); +extern int nni_sock_dial(nni_sock *, const char *, nni_ep **, int); +extern int nni_sock_listen(nni_sock *, const char *, nni_ep **, int); extern uint32_t nni_sock_id(nni_sock *); extern void nni_sock_lock(nni_sock *); extern void nni_sock_unlock(nni_sock *); extern nni_notify *nni_sock_notify(nni_sock *, int, nng_notify_func, void *); -extern void nni_sock_unnotify(nni_sock *, nni_notify *); +extern void nni_sock_unnotify(nni_sock *, nni_notify *); -extern int nni_sock_ep_add(nni_sock *, nni_ep *); +extern int nni_sock_ep_add(nni_sock *, nni_ep *); extern void nni_sock_ep_remove(nni_sock *, nni_ep *); extern void nni_sock_pipe_stop(nni_sock *, nni_pipe *); @@ -123,7 +122,7 @@ extern nni_msgq *nni_sock_recvq(nni_sock *); extern nni_mtx *nni_sock_mtx(nni_sock *); extern nni_duration nni_sock_linger(nni_sock *); -extern size_t nni_sock_rcvmaxsz(nni_sock *); +extern size_t nni_sock_rcvmaxsz(nni_sock *); extern void nni_sock_reconntimes(nni_sock *, nni_duration *, nni_duration *); -#endif // CORE_SOCKET_H +#endif // CORE_SOCKET_H diff --git a/src/core/taskq.c b/src/core/taskq.c index f4a0beee..e45819b5 100644 --- a/src/core/taskq.c +++ b/src/core/taskq.c @@ -9,20 +9,20 @@ #include "core/nng_impl.h" -typedef struct nni_taskq_thr nni_taskq_thr; +typedef struct nni_taskq_thr nni_taskq_thr; struct nni_taskq_thr { - nni_taskq * tqt_tq; - nni_thr tqt_thread; - nni_taskq_ent * tqt_running; - int tqt_wait; + nni_taskq * tqt_tq; + nni_thr tqt_thread; + nni_taskq_ent *tqt_running; + int tqt_wait; }; struct nni_taskq { - nni_list tq_ents; - nni_mtx tq_mtx; - nni_cv tq_cv; - nni_taskq_thr * tq_threads; - int tq_nthreads; - int tq_close; + nni_list tq_ents; + nni_mtx tq_mtx; + nni_cv tq_cv; + nni_taskq_thr *tq_threads; + int tq_nthreads; + int tq_close; }; static nni_taskq *nni_taskq_systq = NULL; @@ -31,14 +31,14 @@ static void nni_taskq_thread(void *self) { nni_taskq_thr *thr = self; - nni_taskq *tq = thr->tqt_tq; + nni_taskq * tq = thr->tqt_tq; nni_taskq_ent *ent; nni_mtx_lock(&tq->tq_mtx); for (;;) { if ((ent = nni_list_first(&tq->tq_ents)) != NULL) { nni_list_remove(&tq->tq_ents, ent); - ent->tqe_tq = NULL; + ent->tqe_tq = NULL; thr->tqt_running = ent; nni_mtx_unlock(&tq->tq_mtx); ent->tqe_cb(ent->tqe_arg); @@ -60,13 +60,12 @@ nni_taskq_thread(void *self) nni_mtx_unlock(&tq->tq_mtx); } - int nni_taskq_init(nni_taskq **tqp, int nthr) { - int rv; + int rv; nni_taskq *tq; - int i; + int i; if ((tq = NNI_ALLOC_STRUCT(tq)) == NULL) { return (NNG_ENOMEM); @@ -83,7 +82,7 @@ nni_taskq_init(nni_taskq **tqp, int nthr) tq->tq_close = 0; NNI_LIST_INIT(&tq->tq_ents, nni_taskq_ent, tqe_node); - tq->tq_threads = nni_alloc(sizeof (nni_taskq_thr) * nthr); + tq->tq_threads = nni_alloc(sizeof(nni_taskq_thr) * nthr); if (tq->tq_threads == NULL) { nni_cv_fini(&tq->tq_cv); nni_mtx_fini(&tq->tq_mtx); @@ -92,10 +91,10 @@ nni_taskq_init(nni_taskq **tqp, int nthr) } tq->tq_nthreads = nthr; for (i = 0; i < nthr; i++) { - tq->tq_threads[i].tqt_tq = tq; + tq->tq_threads[i].tqt_tq = tq; tq->tq_threads[i].tqt_running = NULL; rv = nni_thr_init(&tq->tq_threads[i].tqt_thread, - nni_taskq_thread, &tq->tq_threads[i]); + nni_taskq_thread, &tq->tq_threads[i]); if (rv != 0) { goto fail; } @@ -113,7 +112,6 @@ fail: return (rv); } - void nni_taskq_fini(nni_taskq *tq) { @@ -126,13 +124,12 @@ nni_taskq_fini(nni_taskq *tq) for (i = 0; i < tq->tq_nthreads; i++) { nni_thr_fini(&tq->tq_threads[i].tqt_thread); } - nni_free(tq->tq_threads, tq->tq_nthreads * sizeof (nni_taskq_thr)); + nni_free(tq->tq_threads, tq->tq_nthreads * sizeof(nni_taskq_thr)); nni_cv_fini(&tq->tq_cv); nni_mtx_fini(&tq->tq_mtx); NNI_FREE_STRUCT(tq); } - int nni_taskq_dispatch(nni_taskq *tq, nni_taskq_ent *ent) { @@ -155,7 +152,6 @@ nni_taskq_dispatch(nni_taskq *tq, nni_taskq_ent *ent) return (0); } - int nni_taskq_cancel(nni_taskq *tq, nni_taskq_ent *ent) { @@ -191,17 +187,15 @@ nni_taskq_cancel(nni_taskq *tq, nni_taskq_ent *ent) return (0); } - void nni_taskq_ent_init(nni_taskq_ent *ent, nni_cb cb, void *arg) { NNI_LIST_NODE_INIT(&ent->tqe_node); - ent->tqe_cb = cb; + ent->tqe_cb = cb; ent->tqe_arg = arg; - ent->tqe_tq = NULL; + ent->tqe_tq = NULL; } - int nni_taskq_sys_init(void) { @@ -212,7 +206,6 @@ nni_taskq_sys_init(void) return (rv); } - void nni_taskq_sys_fini(void) { diff --git a/src/core/taskq.h b/src/core/taskq.h index 7f717c3c..f1e29a34 100644 --- a/src/core/taskq.h +++ b/src/core/taskq.h @@ -13,24 +13,24 @@ #include "core/defs.h" #include "core/list.h" -typedef struct nni_taskq nni_taskq; -typedef struct nni_taskq_ent nni_taskq_ent; +typedef struct nni_taskq nni_taskq; +typedef struct nni_taskq_ent nni_taskq_ent; struct nni_taskq_ent { - nni_list_node tqe_node; - void * tqe_arg; - nni_cb tqe_cb; - nni_taskq * tqe_tq; + nni_list_node tqe_node; + void * tqe_arg; + nni_cb tqe_cb; + nni_taskq * tqe_tq; }; -extern int nni_taskq_init(nni_taskq **, int); +extern int nni_taskq_init(nni_taskq **, int); extern void nni_taskq_fini(nni_taskq *); -extern int nni_taskq_dispatch(nni_taskq *, nni_taskq_ent *); -extern int nni_taskq_cancel(nni_taskq *, nni_taskq_ent *); +extern int nni_taskq_dispatch(nni_taskq *, nni_taskq_ent *); +extern int nni_taskq_cancel(nni_taskq *, nni_taskq_ent *); extern void nni_taskq_ent_init(nni_taskq_ent *, nni_cb, void *); -extern int nni_taskq_sys_init(void); +extern int nni_taskq_sys_init(void); extern void nni_taskq_sys_fini(void); #endif // CORE_TASKQ_H diff --git a/src/core/thread.c b/src/core/thread.c index bccb4ff3..e603e32b 100644 --- a/src/core/thread.c +++ b/src/core/thread.c @@ -15,49 +15,42 @@ nni_mtx_init(nni_mtx *mtx) return (nni_plat_mtx_init(&mtx->mtx)); } - void nni_mtx_fini(nni_mtx *mtx) { nni_plat_mtx_fini(&mtx->mtx); } - void nni_mtx_lock(nni_mtx *mtx) { nni_plat_mtx_lock(&mtx->mtx); } - void nni_mtx_unlock(nni_mtx *mtx) { nni_plat_mtx_unlock(&mtx->mtx); } - int nni_cv_init(nni_cv *cv, nni_mtx *mtx) { return (nni_plat_cv_init(&cv->cv, &mtx->mtx)); } - void nni_cv_fini(nni_cv *cv) { nni_plat_cv_fini(&cv->cv); } - void nni_cv_wait(nni_cv *cv) { nni_plat_cv_wait(&cv->cv); } - int nni_cv_until(nni_cv *cv, nni_time until) { @@ -74,19 +67,17 @@ nni_cv_until(nni_cv *cv, nni_time until) return (nni_plat_cv_until(&cv->cv, until)); } - void nni_cv_wake(nni_cv *cv) { nni_plat_cv_wake(&cv->cv); } - static void nni_thr_wrap(void *arg) { nni_thr *thr = arg; - int start; + int start; nni_plat_mtx_lock(&thr->mtx); while (((start = thr->start) == 0) && (thr->stop == 0)) { @@ -102,17 +93,16 @@ nni_thr_wrap(void *arg) nni_plat_mtx_unlock(&thr->mtx); } - int nni_thr_init(nni_thr *thr, nni_thr_func fn, void *arg) { int rv; - thr->done = 0; + thr->done = 0; thr->start = 0; - thr->stop = 0; - thr->fn = fn; - thr->arg = arg; + thr->stop = 0; + thr->fn = fn; + thr->arg = arg; if ((rv = nni_plat_mtx_init(&thr->mtx)) != 0) { thr->done = 1; @@ -138,7 +128,6 @@ nni_thr_init(nni_thr *thr, nni_thr_func fn, void *arg) return (0); } - void nni_thr_run(nni_thr *thr) { @@ -148,7 +137,6 @@ nni_thr_run(nni_thr *thr) nni_plat_mtx_unlock(&thr->mtx); } - void nni_thr_wait(nni_thr *thr) { @@ -164,7 +152,6 @@ nni_thr_wait(nni_thr *thr) nni_plat_mtx_unlock(&thr->mtx); } - void nni_thr_fini(nni_thr *thr) { diff --git a/src/core/thread.h b/src/core/thread.h index b99c8ffb..ebd8db83 100644 --- a/src/core/thread.h +++ b/src/core/thread.h @@ -22,15 +22,15 @@ struct nni_cv { }; struct nni_thr { - nni_plat_thr thr; - nni_plat_mtx mtx; - nni_plat_cv cv; - nni_thr_func fn; - void * arg; - int start; - int stop; - int done; - int init; + nni_plat_thr thr; + nni_plat_mtx mtx; + nni_plat_cv cv; + nni_thr_func fn; + void * arg; + int start; + int stop; + int done; + int init; }; // nni_mtx_init initializes the mutex. (Win32 programmers take note; diff --git a/src/core/timer.c b/src/core/timer.c index b6be0b52..6058b067 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -16,27 +16,26 @@ static void nni_timer_loop(void *); // XXX: replace this timer list with a minHeap based priority queue. struct nni_timer { - nni_mtx t_mx; - nni_cv t_cv; - nni_list t_entries; - nni_thr t_thr; - int t_close; - int t_waiting; - nni_timer_node *t_active; // Must never ever be dereferenced! + nni_mtx t_mx; + nni_cv t_cv; + nni_list t_entries; + nni_thr t_thr; + int t_close; + int t_waiting; + nni_timer_node *t_active; // Must never ever be dereferenced! }; -typedef struct nni_timer nni_timer; +typedef struct nni_timer nni_timer; static nni_timer nni_global_timer; - int nni_timer_sys_init(void) { - int rv; + int rv; nni_timer *timer = &nni_global_timer; - memset(timer, 0, sizeof (*timer)); + memset(timer, 0, sizeof(*timer)); NNI_LIST_INIT(&timer->t_entries, nni_timer_node, t_node); timer->t_close = 0; @@ -50,7 +49,6 @@ nni_timer_sys_init(void) return (0); } - void nni_timer_sys_fini(void) { @@ -66,22 +64,19 @@ nni_timer_sys_fini(void) nni_mtx_fini(&timer->t_mx); } - void nni_timer_init(nni_timer_node *node, nni_cb cb, void *arg) { - node->t_cb = cb; + node->t_cb = cb; node->t_arg = arg; } - void nni_timer_fini(nni_timer_node *node) { NNI_ARG_UNUSED(node); } - void nni_timer_cancel(nni_timer_node *node) { @@ -98,13 +93,12 @@ nni_timer_cancel(nni_timer_node *node) nni_mtx_unlock(&timer->t_mx); } - void nni_timer_schedule(nni_timer_node *node, nni_time when) { - nni_timer *timer = &nni_global_timer; + nni_timer * timer = &nni_global_timer; nni_timer_node *srch; - int wake = 1; + int wake = 1; nni_mtx_lock(&timer->t_mx); node->t_expire = when; @@ -129,12 +123,11 @@ nni_timer_schedule(nni_timer_node *node, nni_time when) nni_mtx_unlock(&timer->t_mx); } - static void nni_timer_loop(void *arg) { - nni_timer *timer = arg; - nni_time now; + nni_timer * timer = arg; + nni_time now; nni_timer_node *node; for (;;) { diff --git a/src/core/timer.h b/src/core/timer.h index 09883e0b..a8108d5f 100644 --- a/src/core/timer.h +++ b/src/core/timer.h @@ -16,19 +16,19 @@ // For the sake of simplicity, we just maintain a single global timer thread. struct nni_timer_node { - nni_time t_expire; - nni_cb t_cb; - void * t_arg; - nni_list_node t_node; + nni_time t_expire; + nni_cb t_cb; + void * t_arg; + nni_list_node t_node; }; -typedef struct nni_timer_node nni_timer_node; +typedef struct nni_timer_node nni_timer_node; extern void nni_timer_init(nni_timer_node *, nni_cb, void *); extern void nni_timer_fini(nni_timer_node *); extern void nni_timer_schedule(nni_timer_node *, nni_time); extern void nni_timer_cancel(nni_timer_node *); -extern int nni_timer_sys_init(void); +extern int nni_timer_sys_init(void); extern void nni_timer_sys_fini(void); #endif // CORE_TIMER_H diff --git a/src/core/transport.c b/src/core/transport.c index ce61f722..61b1a0c3 100644 --- a/src/core/transport.c +++ b/src/core/transport.c @@ -18,21 +18,22 @@ extern nni_tran nni_tcp_tran; extern nni_tran nni_ipc_tran; static nni_tran *transports[] = { + // clang-format off &nni_inproc_tran, &nni_tcp_tran, &nni_ipc_tran, NULL + // clang-format on }; - nni_tran * nni_tran_find(const char *addr) { // address is of the form "://blah..." const char *end; - int len; - int i; - nni_tran *tran; + int len; + int i; + nni_tran * tran; if ((end = strstr(addr, "://")) == NULL) { return (NULL); @@ -47,13 +48,12 @@ nni_tran_find(const char *addr) return (NULL); } - // nni_tran_sys_init initializes the entire transport subsystem, including // each individual transport. void nni_tran_sys_init(void) { - int i; + int i; nni_tran *tran; for (i = 0; (tran = transports[i]) != NULL; i++) { @@ -61,13 +61,12 @@ nni_tran_sys_init(void) } } - // nni_tran_sys_fini finalizes the entire transport system, including all // transports. void nni_tran_sys_fini(void) { - int i; + int i; nni_tran *tran; for (i = 0; (tran = transports[i]) != NULL; i++) { diff --git a/src/core/transport.h b/src/core/transport.h index 50da473d..72f4cc61 100644 --- a/src/core/transport.h +++ b/src/core/transport.h @@ -15,24 +15,23 @@ struct nni_tran { // tran_scheme is the transport scheme, such as "tcp" or "inproc". - const char * tran_scheme; + const char *tran_scheme; // tran_ep links our endpoint-specific operations. - const nni_tran_ep * tran_ep; + const nni_tran_ep *tran_ep; // tran_pipe links our pipe-specific operations. - const nni_tran_pipe * tran_pipe; + const nni_tran_pipe *tran_pipe; // tran_init, if not NULL, is called once during library // initialization. - int (*tran_init)(void); + int (*tran_init)(void); // tran_fini, if not NULL, is called during library deinitialization. // It should release any global resources, close any open files, etc. - void (*tran_fini)(void); + void (*tran_fini)(void); }; - // Endpoint operations are called by the socket in a protocol-independent // fashion. The socket makes individual calls, which are expected to block // if appropriate (except for destroy). Endpoints are unable to call back @@ -40,36 +39,36 @@ struct nni_tran { struct nni_tran_ep { // ep_init creates a vanilla endpoint. The value created is // used for the first argument for all other endpoint functions. - int (*ep_init)(void **, const char *, nni_sock *, int); + int (*ep_init)(void **, const char *, nni_sock *, int); // ep_fini frees the resources associated with the endpoint. // The endpoint will already have been closed. - void (*ep_fini)(void *); + void (*ep_fini)(void *); // ep_connect establishes a connection. It can return errors // NNG_EACCESS, NNG_ECONNREFUSED, NNG_EBADADDR, NNG_ECONNFAILED, // NNG_ETIMEDOUT, and NNG_EPROTO. - void (*ep_connect)(void *, nni_aio *); + void (*ep_connect)(void *, nni_aio *); // ep_bind just does the bind() and listen() work, // reserving the address but not creating any connections. // It should return NNG_EADDRINUSE if the address is already // taken. It can also return NNG_EBADADDR for an unsuitable // address, or NNG_EACCESS for permission problems. - int (*ep_bind)(void *); + int (*ep_bind)(void *); // ep_accept accepts an inbound connection. - void (*ep_accept)(void *, nni_aio *); + void (*ep_accept)(void *, nni_aio *); // ep_close stops the endpoint from operating altogether. It does // not affect pipes that have already been created. - void (*ep_close)(void *); + void (*ep_close)(void *); // ep_setopt sets an endpoint (transport-specific) option. - int (*ep_setopt)(void *, int, const void *, size_t); + int (*ep_setopt)(void *, int, const void *, size_t); // ep_getopt gets an endpoint (transport-specific) option. - int (*ep_getopt)(void *, int, void *, size_t *); + int (*ep_getopt)(void *, int, void *, size_t *); }; // Pipe operations are entry points called by the socket. These may be called @@ -81,14 +80,14 @@ struct nni_tran_pipe { // resources, including closing files and freeing memory, used by // the pipe. After this call returns, the system will not make // further calls on the same pipe. - void (*p_fini)(void *); + void (*p_fini)(void *); // p_start starts the pipe running. This gives the transport a // chance to hook into any transport specific negotiation phase. // The pipe will not have its p_send or p_recv calls started, and // will not be access by the "socket" until the pipe has indicated // its readiness by finishing the aio. - void (*p_start)(void *, nni_aio *); + void (*p_start)(void *, nni_aio *); // p_aio_send queues the message for transmit. If this fails, then // the caller may try again with the same message (or free it). If @@ -96,30 +95,30 @@ struct nni_tran_pipe { // message, and the caller may not use it again. The transport will // have the responsibility to free the message (nng_msg_free()) when // it is finished with it. - void (*p_send)(void *, nni_aio *); + void (*p_send)(void *, nni_aio *); // p_recv schedules a message receive. This will be performed even for // cases where no data is expected, to allow detection of a remote // disconnect. - void (*p_recv)(void *, nni_aio *); + void (*p_recv)(void *, nni_aio *); // p_close closes the pipe. Further recv or send operations should // return back NNG_ECLOSED. - void (*p_close)(void *); + void (*p_close)(void *); // p_peer returns the peer protocol. This may arrive in whatever // transport specific manner is appropriate. - uint16_t (*p_peer)(void *); + uint16_t (*p_peer)(void *); // p_getopt gets an pipe (transport-specific) property. These values // may not be changed once the pipe is created. - int (*p_getopt)(void *, int, void *, size_t *); + int (*p_getopt)(void *, int, void *, size_t *); }; // These APIs are used by the framework internally, and not for use by // transport implementations. extern nni_tran *nni_tran_find(const char *); -extern void nni_tran_sys_init(void); -extern void nni_tran_sys_fini(void); +extern void nni_tran_sys_init(void); +extern void nni_tran_sys_fini(void); #endif // CORE_TRANSPORT_H diff --git a/src/nng.c b/src/nng.c index 59c003a7..e66a3ddc 100644 --- a/src/nng.c +++ b/src/nng.c @@ -23,7 +23,7 @@ int nng_open(nng_socket *sidp, uint16_t proto) { - int rv; + int rv; nni_sock *sock; if ((rv = nni_sock_open(&sock, proto)) != 0) { @@ -36,18 +36,16 @@ nng_open(nng_socket *sidp, uint16_t proto) return (0); } - void nng_fini(void) { nni_fini(); } - int nng_shutdown(nng_socket sid) { - int rv; + int rv; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -58,11 +56,10 @@ nng_shutdown(nng_socket sid) return (rv); } - int nng_close(nng_socket sid) { - int rv; + int rv; nni_sock *sock; // Close is special, because we still want to be able to get @@ -75,12 +72,11 @@ nng_close(nng_socket sid) return (rv); } - uint16_t nng_protocol(nng_socket sid) { - int rv; - uint16_t pnum; + int rv; + uint16_t pnum; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -91,12 +87,11 @@ nng_protocol(nng_socket sid) return (pnum); } - uint16_t nng_peer(nng_socket sid) { - int rv; - uint16_t pnum; + int rv; + uint16_t pnum; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -107,12 +102,11 @@ nng_peer(nng_socket sid) return (pnum); } - int nng_recv(nng_socket sid, void *buf, size_t *szp, int flags) { nng_msg *msg; - int rv; + int rv; // Note that while it would be nice to make this a zero copy operation, // its not normally possible if a size was specified. @@ -144,12 +138,11 @@ nng_recv(nng_socket sid, void *buf, size_t *szp, int flags) return (0); } - int nng_recvmsg(nng_socket sid, nng_msg **msgp, int flags) { - nni_time expire; - int rv; + nni_time expire; + int rv; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -169,12 +162,11 @@ nng_recvmsg(nng_socket sid, nng_msg **msgp, int flags) return (rv); } - int nng_send(nng_socket sid, void *buf, size_t len, int flags) { nng_msg *msg; - int rv; + int rv; if ((rv = nng_msg_alloc(&msg, len)) != 0) { return (rv); @@ -188,26 +180,23 @@ nng_send(nng_socket sid, void *buf, size_t len, int flags) return (rv); } - void * nng_alloc(size_t sz) { return (nni_alloc(sz)); } - void nng_free(void *buf, size_t sz) { nni_free(buf, sz); } - int nng_sendmsg(nng_socket sid, nng_msg *msg, int flags) { - nni_time expire; - int rv; + nni_time expire; + int rv; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -227,12 +216,11 @@ nng_sendmsg(nng_socket sid, nng_msg *msg, int flags) return (rv); } - int nng_dial(nng_socket sid, const char *addr, nng_endpoint *epp, int flags) { - nni_ep *ep; - int rv; + nni_ep * ep; + int rv; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -247,12 +235,11 @@ nng_dial(nng_socket sid, const char *addr, nng_endpoint *epp, int flags) return (rv); } - int nng_listen(nng_socket sid, const char *addr, nng_endpoint *epp, int flags) { - nni_ep *ep; - int rv; + nni_ep * ep; + int rv; nni_sock *sock; if ((rv = nni_sock_find(&sock, sid)) != 0) { @@ -267,11 +254,10 @@ nng_listen(nng_socket sid, const char *addr, nng_endpoint *epp, int flags) return (rv); } - int nng_endpoint_close(nng_endpoint eid) { - int rv; + int rv; nni_ep *ep; if ((rv = nni_ep_find(&ep, eid)) != 0) { @@ -281,12 +267,11 @@ nng_endpoint_close(nng_endpoint eid) return (0); } - int nng_setopt(nng_socket sid, int opt, const void *val, size_t sz) { nni_sock *sock; - int rv; + int rv; if ((rv = nni_sock_find(&sock, sid)) != 0) { return (rv); @@ -296,12 +281,11 @@ nng_setopt(nng_socket sid, int opt, const void *val, size_t sz) return (rv); } - int nng_getopt(nng_socket sid, int opt, void *val, size_t *szp) { nni_sock *sock; - int rv; + int rv; if ((rv = nni_sock_find(&sock, sid)) != 0) { return (rv); @@ -311,13 +295,12 @@ nng_getopt(nng_socket sid, int opt, void *val, size_t *szp) return (rv); } - nng_notify * nng_setnotify(nng_socket sid, int mask, nng_notify_func fn, void *arg) { - nni_sock *sock; + nni_sock * sock; nng_notify *notify; - int rv; + int rv; if ((rv = nni_sock_find(&sock, sid)) != 0) { return (NULL); @@ -327,12 +310,11 @@ nng_setnotify(nng_socket sid, int mask, nng_notify_func fn, void *arg) return (notify); } - void nng_unsetnotify(nng_socket sid, nng_notify *notify) { nni_sock *sock; - int rv; + int rv; if ((rv = nni_sock_find(&sock, sid)) != 0) { return; @@ -341,7 +323,6 @@ nng_unsetnotify(nng_socket sid, nng_notify *notify) nni_sock_rele(sock); } - nng_socket nng_event_socket(nng_event *ev) { @@ -349,27 +330,25 @@ nng_event_socket(nng_event *ev) return (nni_sock_id(ev->e_sock)); } - int nng_event_type(nng_event *ev) { return (ev->e_type); } - int nng_device(nng_socket s1, nng_socket s2) { - int rv; + int rv; nni_sock *sock1 = NULL; nni_sock *sock2 = NULL; - if ((s1 > 0) && (s1 != (nng_socket)-1)) { + if ((s1 > 0) && (s1 != (nng_socket) -1)) { if ((rv = nni_sock_find(&sock1, s1)) != 0) { return (rv); } } - if (((s2 > 0) && (s2 != (nng_socket)-1)) && (s2 != s1)) { + if (((s2 > 0) && (s2 != (nng_socket) -1)) && (s2 != s1)) { if ((rv = nni_sock_find(&sock2, s2)) != 0) { nni_sock_rele(sock1); return (rv); @@ -380,14 +359,13 @@ nng_device(nng_socket s1, nng_socket s2) return (rv); } - // Misc. const char * nng_strerror(int num) { switch (num) { case 0: - return ("Hunky dory"); // What did you expect? + return ("Hunky dory"); // What did you expect? case NNG_EINTR: return ("Interrupted"); @@ -457,7 +435,6 @@ nng_strerror(int num) return ("Unknown error"); } - #if 0 int nng_pipe_getopt(nng_pipe *pipe, int opt, void *val, size_t *sizep) @@ -480,10 +457,8 @@ nng_pipe_close(nng_pipe *pipe) return (0); } - #endif - // Message handling. int nng_msg_alloc(nng_msg **msgp, size_t size) @@ -491,112 +466,96 @@ nng_msg_alloc(nng_msg **msgp, size_t size) return (nni_msg_alloc(msgp, size)); } - int nng_msg_realloc(nng_msg *msg, size_t sz) { return (nni_msg_realloc(msg, sz)); } - void nng_msg_free(nng_msg *msg) { nni_msg_free(msg); } - void * nng_msg_body(nng_msg *msg) { return (nni_msg_body(msg)); } - size_t nng_msg_len(nng_msg *msg) { return (nni_msg_len(msg)); } - void * nng_msg_header(nng_msg *msg) { return (nni_msg_header(msg)); } - size_t nng_msg_header_len(nng_msg *msg) { return (nni_msg_header_len(msg)); } - int nng_msg_append(nng_msg *msg, const void *data, size_t sz) { return (nni_msg_append(msg, data, sz)); } - int nng_msg_prepend(nng_msg *msg, const void *data, size_t sz) { return (nni_msg_prepend(msg, data, sz)); } - int nng_msg_append_header(nng_msg *msg, const void *data, size_t sz) { return (nni_msg_append_header(msg, data, sz)); } - int nng_msg_prepend_header(nng_msg *msg, const void *data, size_t sz) { return (nni_msg_prepend_header(msg, data, sz)); } - int nng_msg_trim(nng_msg *msg, size_t sz) { return (nni_msg_trim(msg, sz)); } - int nng_msg_trunc(nng_msg *msg, size_t sz) { return (nni_msg_trunc(msg, sz)); } - int nng_msg_trim_header(nng_msg *msg, size_t sz) { return (nni_msg_trim_header(msg, sz)); } - int nng_msg_trunc_header(nng_msg *msg, size_t sz) { return (nni_msg_trunc_header(msg, sz)); } - int nng_msg_getopt(nng_msg *msg, int opt, void *ptr, size_t *szp) { return (nni_msg_getopt(msg, opt, ptr, szp)); } - int nng_snapshot_create(nng_socket sock, nng_snapshot **snapp) { @@ -604,14 +563,12 @@ nng_snapshot_create(nng_socket sock, nng_snapshot **snapp) return (NNG_ENOTSUP); } - void nng_snapshot_destroy(nng_snapshot *snap) { // Stats TBD. } - int nng_snapshot_update(nng_snapshot *snap) { @@ -619,7 +576,6 @@ nng_snapshot_update(nng_snapshot *snap) return (NNG_ENOTSUP); } - int nng_snapshot_next(nng_snapshot *snap, nng_stat **statp) { @@ -628,7 +584,6 @@ nng_snapshot_next(nng_snapshot *snap, nng_stat **statp) return (NNG_ENOTSUP); } - const char * nng_stat_name(nng_stat *stat) { @@ -636,7 +591,6 @@ nng_stat_name(nng_stat *stat) return (NULL); } - int nng_stat_type(nng_stat *stat) { @@ -644,7 +598,6 @@ nng_stat_type(nng_stat *stat) return (0); } - int64_t nng_stat_value(nng_stat *stat) { @@ -652,7 +605,6 @@ nng_stat_value(nng_stat *stat) return (0); } - // These routines exist as utility functions, exposing some of our "guts" // to the external world for the purposes of test code and bundled utilities. // They should not be considered part of our public API, and applications @@ -664,14 +616,12 @@ nng_usleep(uint64_t usec) nni_usleep(usec); } - uint64_t nng_clock(void) { return ((uint64_t) nni_clock()); } - // nng_thread_create creates a thread structure, and starts it running. // Unlike the internals, this allocates stuff dynamically, and does not // wait to start. @@ -679,14 +629,14 @@ int nng_thread_create(void **thrp, void (*func)(void *), void *arg) { nni_thr *thr; - int rv; + int rv; nni_init(); if ((thr = NNI_ALLOC_STRUCT(thr)) == NULL) { return (NNG_ENOMEM); } - memset(thr, 0, sizeof (*thr)); + memset(thr, 0, sizeof(*thr)); *thrp = thr; if ((rv = nni_thr_init(thr, func, arg)) != 0) { return (rv); @@ -695,7 +645,6 @@ nng_thread_create(void **thrp, void (*func)(void *), void *arg) return (0); } - void nng_thread_destroy(void *arg) { diff --git a/src/nng.h b/src/nng.h index e9c5e32e..feaa3fda 100644 --- a/src/nng.h +++ b/src/nng.h @@ -31,26 +31,26 @@ extern "C" { #ifndef NNG_DECL #if defined(_WIN32) && !defined(NNG_STATIC_LIB) #if defined(NNG_SHARED_LIB) -#define NNG_DECL __declspec(dllexport) +#define NNG_DECL __declspec(dllexport) #else -#define NNG_DECL __declspec(dllimport) +#define NNG_DECL __declspec(dllimport) #endif // NNG_SHARED_LIB #else -#define NNG_DECL extern -#endif // _WIN32 && !NNG_STATIC_LIB -#endif // NNG_DECL +#define NNG_DECL extern +#endif // _WIN32 && !NNG_STATIC_LIB +#endif // NNG_DECL // Types common to nng. -typedef uint32_t nng_socket; -typedef uint32_t nng_dialer; -typedef uint32_t nng_listener; -typedef uint32_t nng_pipe; -typedef struct nng_msg nng_msg; -typedef struct nng_event nng_event; -typedef struct nng_notify nng_notify; -typedef struct nng_snapshot nng_snapshot; -typedef struct nng_stat nng_stat; -typedef uint32_t nng_endpoint; // XXX: REMOVE ME. +typedef uint32_t nng_socket; +typedef uint32_t nng_dialer; +typedef uint32_t nng_listener; +typedef uint32_t nng_pipe; +typedef struct nng_msg nng_msg; +typedef struct nng_event nng_event; +typedef struct nng_notify nng_notify; +typedef struct nng_snapshot nng_snapshot; +typedef struct nng_stat nng_stat; +typedef uint32_t nng_endpoint; // XXX: REMOVE ME. // nng_open simply creates a socket of the given class. It returns an // error code on failure, or zero on success. The socket starts in cooked @@ -123,6 +123,7 @@ NNG_DECL void nng_unsetnotify(nng_socket, nng_notify *); // NNG_EV_PIPE_REM - A pipe (connection) is removed from the socket. // NNG_EV_ENDPT_ADD - An endpoint is added to the socket. // NNG_EV_ENDPT_REM - An endpoint is removed from the socket. +// clang-format off #define NNG_EV_BIT(x) (1U << (x)) #define NNG_EV_CAN_RCV NNG_EV_BIT(0) #define NNG_EV_CAN_SND NNG_EV_BIT(1) @@ -136,6 +137,7 @@ NNG_DECL void nng_unsetnotify(nng_socket, nng_notify *); // XXX: Remove these. #define NNG_EV_ENDPT_ADD NNG_EV_DIALER_ADD #define NNG_EV_ENDPT_REM NNG_EV_DIALER_REM +// clang-format on // The following functions return more detailed information about the event. // Some of the values will not make sense for some event types, in which case @@ -143,7 +145,7 @@ NNG_DECL void nng_unsetnotify(nng_socket, nng_notify *); NNG_DECL int nng_event_type(nng_event *); NNG_DECL nng_socket nng_event_socket(nng_event *); NNG_DECL nng_endpoint nng_event_endpoint(nng_event *); -NNG_DECL nng_pipe nng_event_pipe(nng_event *); +NNG_DECL nng_pipe nng_event_pipe(nng_event *); NNG_DECL const char *nng_event_reason(nng_event *); // nng_listen creates a listening endpoint with no special options, @@ -241,22 +243,22 @@ NNG_DECL void *nng_alloc(size_t); NNG_DECL void nng_free(void *, size_t); // Message API. -NNG_DECL int nng_msg_alloc(nng_msg **, size_t); -NNG_DECL void nng_msg_free(nng_msg *); -NNG_DECL int nng_msg_realloc(nng_msg *, size_t); +NNG_DECL int nng_msg_alloc(nng_msg **, size_t); +NNG_DECL void nng_msg_free(nng_msg *); +NNG_DECL int nng_msg_realloc(nng_msg *, size_t); NNG_DECL void *nng_msg_header(nng_msg *); NNG_DECL size_t nng_msg_header_len(nng_msg *); -NNG_DECL void *nng_msg_body(nng_msg *); +NNG_DECL void * nng_msg_body(nng_msg *); NNG_DECL size_t nng_msg_len(nng_msg *); -NNG_DECL int nng_msg_append(nng_msg *, const void *, size_t); -NNG_DECL int nng_msg_prepend(nng_msg *, const void *, size_t); -NNG_DECL int nng_msg_trim(nng_msg *, size_t); -NNG_DECL int nng_msg_trunc(nng_msg *, size_t); -NNG_DECL int nng_msg_append_header(nng_msg *, const void *, size_t); -NNG_DECL int nng_msg_prepend_header(nng_msg *, const void *, size_t); -NNG_DECL int nng_msg_trim_header(nng_msg *, size_t); -NNG_DECL int nng_msg_trunc_header(nng_msg *, size_t); -NNG_DECL int nng_msg_getopt(nng_msg *, int, void *, size_t *); +NNG_DECL int nng_msg_append(nng_msg *, const void *, size_t); +NNG_DECL int nng_msg_prepend(nng_msg *, const void *, size_t); +NNG_DECL int nng_msg_trim(nng_msg *, size_t); +NNG_DECL int nng_msg_trunc(nng_msg *, size_t); +NNG_DECL int nng_msg_append_header(nng_msg *, const void *, size_t); +NNG_DECL int nng_msg_prepend_header(nng_msg *, const void *, size_t); +NNG_DECL int nng_msg_trim_header(nng_msg *, size_t); +NNG_DECL int nng_msg_trunc_header(nng_msg *, size_t); +NNG_DECL int nng_msg_getopt(nng_msg *, int, void *, size_t *); // Pipe API. Generally pipes are only "observable" to applications, but // we do permit an application to close a pipe. This can be useful, for @@ -266,9 +268,9 @@ NNG_DECL int nng_pipe_getopt(nng_pipe, int, void *, size_t *); NNG_DECL int nng_pipe_close(nng_pipe); // Flags. -#define NNG_FLAG_ALLOC 1 // Recv to allocate receive buffer. -#define NNG_FLAG_NONBLOCK 2 // Non-block send/recv. -#define NNG_FLAG_SYNCH 4 // Synchronous dial / listen +#define NNG_FLAG_ALLOC 1 // Recv to allocate receive buffer. +#define NNG_FLAG_NONBLOCK 2 // Non-block send/recv. +#define NNG_FLAG_SYNCH 4 // Synchronous dial / listen // Protocol numbers. These are to be used with nng_socket_create(). // These values are used on the wire, so must not be changed. The major @@ -278,6 +280,7 @@ NNG_DECL int nng_pipe_close(nng_pipe); // There are gaps in the list, which are obsolete or unsupported protocols. // Protocol numbers are never more than 16 bits. Also, there will never be // a valid protocol numbered 0 (NNG_PROTO_NONE). +// clang-format off #define NNG_PROTO(major, minor) (((major) * 16) + (minor)) #define NNG_PROTO_NONE NNG_PROTO(0, 0) #define NNG_PROTO_PAIR NNG_PROTO(1, 0) @@ -291,12 +294,14 @@ NNG_DECL int nng_pipe_close(nng_pipe); #define NNG_PROTO_RESPONDENT NNG_PROTO(6, 3) #define NNG_PROTO_BUS NNG_PROTO(7, 0) #define NNG_PROTO_STAR NNG_PROTO(100, 0) +// clang-format on // Options. We encode option numbers as follows: // // - 0: socket, 1: transport // - zero (socket), or transport (8 bits) // - specific value (16 bits) +// clang-format off #define NNG_OPT_SOCKET(c) (c) #define NNG_OPT_TRANSPORT_OPT(t, c) (0x10000 | ((t) << 16) | (c)) @@ -320,6 +325,7 @@ NNG_DECL int nng_pipe_close(nng_pipe); #define NNG_OPT_REMOTEADDR NNG_OPT_SOCKET(17) #define NNG_OPT_RCVFD NNG_OPT_SOCKET(18) #define NNG_OPT_SNDFD NNG_OPT_SOCKET(19) +// clang-format on // XXX: TBD: priorities, socket names, ipv4only @@ -367,8 +373,8 @@ NNG_DECL const char *nng_stat_name(nng_stat *); // user as is. NNG_DECL int nng_stat_type(nng_stat *); -#define NNG_STAT_LEVEL 0 -#define NNG_STAT_COUNTER 1 +#define NNG_STAT_LEVEL 0 +#define NNG_STAT_COUNTER 1 // nng_stat_unit provides information about the unit for the statistic, // such as NNG_UNIT_BYTES or NNG_UNIT_BYTES. If no specific unit is @@ -376,12 +382,14 @@ NNG_DECL int nng_stat_type(nng_stat *); // returned. NNG_DECL int nng_stat_unit(nng_stat *); +// clang-format off #define NNG_UNIT_NONE 0 #define NNG_UNIT_BYTES 1 #define NNG_UNIT_MESSAGES 2 #define NNG_UNIT_BOOLEAN 3 #define NNG_UNIT_MILLIS 4 #define NNG_UNIT_EVENTS 5 +// clang-format on // nng_stat_value returns returns the actual value of the statistic. // Statistic values reflect their value at the time that the corresponding @@ -425,6 +433,7 @@ NNG_DECL void nng_thread_destroy(void *); // Error codes. These may happen to align to errnos used on your platform, // but do not count on this. +// clang-format off #define NNG_EINTR (1) #define NNG_ENOMEM (2) #define NNG_EINVAL (3) @@ -445,6 +454,7 @@ NNG_DECL void nng_thread_destroy(void *); #define NNG_ECONNABORTED (18) #define NNG_ECONNRESET (19) #define NNG_ECANCELED (20) +// clang-format on // NNG_SYSERR is a special code, which allows us to wrap errors from the // underlyuing operating system. We generally prefer to map errors to one @@ -457,56 +467,58 @@ NNG_DECL void nng_thread_destroy(void *); // we treat it the same as memory. But for files, etc. that's OS-specific, // and we use the generic below. Some of the above error codes we use // internally, and the application should never see (e.g. NNG_EINTR). -#define NNG_ESYSERR (0x10000000) +#define NNG_ESYSERR (0x10000000) // Maximum length of a socket address. This includes the terminating NUL. // This limit is built into other implementations, so do not change it. -#define NNG_MAXADDRLEN (128) +#define NNG_MAXADDRLEN (128) // Some address details. This is in some ways like a traditional sockets // sockaddr, but we have our own to cope with our unique families, etc. // The details of this structure are directly exposed to applications. // These structures can be obtained via property lookups, etc. struct nng_sockaddr_path { - uint16_t sa_family; - char sa_path[NNG_MAXADDRLEN]; + uint16_t sa_family; + char sa_path[NNG_MAXADDRLEN]; }; -typedef struct nng_sockaddr_path nng_sockaddr_path; -typedef struct nng_sockaddr_path nng_sockaddr_ipc; -typedef struct nng_sockaddr_path nng_sockaddr_inproc; +typedef struct nng_sockaddr_path nng_sockaddr_path; +typedef struct nng_sockaddr_path nng_sockaddr_ipc; +typedef struct nng_sockaddr_path nng_sockaddr_inproc; struct nng_sockaddr_in6 { - uint16_t sa_family; - uint16_t sa_port; - uint8_t sa_addr[16]; + uint16_t sa_family; + uint16_t sa_port; + uint8_t sa_addr[16]; }; -typedef struct nng_sockaddr_in6 nng_sockaddr_in6; -typedef struct nng_sockaddr_in6 nng_sockaddr_udp6; -typedef struct nng_sockaddr_in6 nng_sockaddr_tcp6; +typedef struct nng_sockaddr_in6 nng_sockaddr_in6; +typedef struct nng_sockaddr_in6 nng_sockaddr_udp6; +typedef struct nng_sockaddr_in6 nng_sockaddr_tcp6; struct nng_sockaddr_in { - uint16_t sa_family; - uint16_t sa_port; - uint32_t sa_addr; + uint16_t sa_family; + uint16_t sa_port; + uint32_t sa_addr; }; -typedef struct nng_sockaddr_in nng_sockaddr_in; -typedef struct nng_sockaddr_in nng_sockaddr_udp; -typedef struct nng_sockaddr_in nng_sockaddr_tcp; +typedef struct nng_sockaddr_in nng_sockaddr_in; +typedef struct nng_sockaddr_in nng_sockaddr_udp; +typedef struct nng_sockaddr_in nng_sockaddr_tcp; typedef struct nng_sockaddr { union { - uint16_t s_family; - nng_sockaddr_path s_path; - nng_sockaddr_in6 s_in6; - nng_sockaddr_in s_in; + uint16_t s_family; + nng_sockaddr_path s_path; + nng_sockaddr_in6 s_in6; + nng_sockaddr_in s_in; } s_un; } nng_sockaddr; +// clang-format off #define NNG_AF_UNSPEC 0 #define NNG_AF_INPROC 1 #define NNG_AF_IPC 2 #define NNG_AF_INET 3 #define NNG_AF_INET6 4 +// clang-format on #ifdef __cplusplus } diff --git a/src/nng_compat.c b/src/nng_compat.c index 41995dd9..523358f3 100644 --- a/src/nng_compat.c +++ b/src/nng_compat.c @@ -7,20 +7,20 @@ // found online at https://opensource.org/licenses/MIT. // -#include "nng.h" #include "nng_compat.h" +#include "nng.h" -#include #include +#include // This file supplies the legacy compatibility API. Applications should // avoid using these if at all possible, and instead use the new style APIs. static struct { - int nerr; - int perr; -} -nn_errnos[] = { + int nerr; + int perr; +} nn_errnos[] = { + // clang-format off { NNG_EINTR, EINTR }, { NNG_ENOMEM, ENOMEM }, { NNG_EINVAL, EINVAL }, @@ -41,12 +41,13 @@ nn_errnos[] = { { NNG_ECONNABORTED, ECONNABORTED }, { NNG_ECONNRESET, ECONNRESET }, { 0, 0 }, + // clang-format on }; const char * nn_strerror(int err) { - int i; + int i; static char msgbuf[32]; for (i = 0; nn_errnos[i].perr != 0; i++) { @@ -60,11 +61,10 @@ nn_strerror(int err) // Arguablye we could use strerror() here, but we should only // be getting errnos we understand at this point. - (void) snprintf(msgbuf, sizeof (msgbuf), "Unknown error %d", err); + (void) snprintf(msgbuf, sizeof(msgbuf), "Unknown error %d", err); return (msgbuf); } - static void nn_seterror(int err) { @@ -80,19 +80,17 @@ nn_seterror(int err) errno = EIO; } - int nn_errno(void) { return (errno); } - int nn_socket(int domain, int protocol) { nng_socket sock; - int rv; + int rv; if ((domain != AF_SP) && (domain != AF_SP_RAW)) { errno = EAFNOSUPPORT; @@ -104,7 +102,7 @@ nn_socket(int domain, int protocol) } if (domain == AF_SP_RAW) { int raw = 1; - rv = nng_setopt(sock, NNG_OPT_RAW, &raw, sizeof (raw)); + rv = nng_setopt(sock, NNG_OPT_RAW, &raw, sizeof(raw)); if (rv != 0) { nn_seterror(rv); nng_close(sock); @@ -114,7 +112,6 @@ nn_socket(int domain, int protocol) return ((int) sock); } - int nn_close(int s) { @@ -127,25 +124,24 @@ nn_close(int s) return (0); } - int nn_bind(int s, const char *addr) { - int rv; + int rv; nng_endpoint ep; - if ((rv = nng_listen((nng_socket) s, addr, &ep, NNG_FLAG_SYNCH)) != 0) { + if ((rv = nng_listen((nng_socket) s, addr, &ep, NNG_FLAG_SYNCH)) != + 0) { nn_seterror(rv); return (-1); } return ((int) ep); } - int nn_connect(int s, const char *addr) { - int rv; + int rv; nng_endpoint ep; if ((rv = nng_dial((nng_socket) s, addr, &ep, 0)) != 0) { @@ -155,7 +151,6 @@ nn_connect(int s, const char *addr) return ((int) ep); } - int nn_shutdown(int s, int ep) { @@ -173,15 +168,14 @@ nn_shutdown(int s, int ep) return (0); } - void * nn_allocmsg(size_t size, int type) { nng_msg *msg; - int rv; + int rv; // Validate type and non-zero size. This also checks for overflow. - if ((type != 0) || (size < 1) || ((size + sizeof (msg) < size))) { + if ((type != 0) || (size < 1) || ((size + sizeof(msg) < size))) { nn_seterror(NNG_EINVAL); return (NULL); } @@ -189,7 +183,7 @@ nn_allocmsg(size_t size, int type) // So our "messages" from nn are really going to be nng messages // but to make this work, we use a bit of headroom in the message // to stash the message header. - if ((rv = nng_msg_alloc(&msg, size + (sizeof (msg)))) != 0) { + if ((rv = nng_msg_alloc(&msg, size + (sizeof(msg)))) != 0) { nn_seterror(rv); return (NULL); } @@ -199,40 +193,38 @@ nn_allocmsg(size_t size, int type) // We are counting on the implementation of nn_msg_trim to not // reallocate the message but just to leave the prefix inplace. - (void) nng_msg_trim(msg, sizeof (msg)); + (void) nng_msg_trim(msg, sizeof(msg)); return (nng_msg_body(msg)); } - int nn_freemsg(void *ptr) { nng_msg *msg; - msg = *(nng_msg **) (((char *) ptr) - sizeof (msg)); + msg = *(nng_msg **) (((char *) ptr) - sizeof(msg)); nng_msg_free(msg); return (0); } - void * nn_reallocmsg(void *ptr, size_t len) { nng_msg *msg; - int rv; + int rv; - if ((len + sizeof (msg)) < len) { + if ((len + sizeof(msg)) < len) { // overflowed! nn_seterror(NNG_EINVAL); return (NULL); } // This counts on message bodies being aligned sensibly. - msg = *(nng_msg **) (((char *) ptr) - sizeof (msg)); + msg = *(nng_msg **) (((char *) ptr) - sizeof(msg)); // We need to realloc the requested len, plus size for our header. - if ((rv = nng_msg_realloc(msg, len + sizeof (msg))) != 0) { + if ((rv = nng_msg_realloc(msg, len + sizeof(msg))) != 0) { // We don't free the old message. Code is free to cope // as it sees fit. nn_seterror(rv); @@ -240,11 +232,10 @@ nn_reallocmsg(void *ptr, size_t len) } // Stash the msg header pointer *(nng_msg **) (nng_msg_body(msg)) = msg; - nng_msg_trim(msg, sizeof (msg)); + nng_msg_trim(msg, sizeof(msg)); return (nng_msg_body(msg)); } - static int nn_flags(int flags) { @@ -261,7 +252,6 @@ nn_flags(int flags) } } - int nn_send(int s, const void *buf, size_t len, int flags) { @@ -272,9 +262,9 @@ nn_send(int s, const void *buf, size_t len, int flags) } if (len == NN_MSG) { nng_msg *msg; - memcpy(&msg, ((char *) buf) - sizeof (msg), sizeof (msg)); + memcpy(&msg, ((char *) buf) - sizeof(msg), sizeof(msg)); len = nng_msg_len(msg); - rv = nng_sendmsg((nng_socket) s, msg, flags); + rv = nng_sendmsg((nng_socket) s, msg, flags); } else { rv = nng_send((nng_socket) s, (void *) buf, len, flags); } @@ -285,7 +275,6 @@ nn_send(int s, const void *buf, size_t len, int flags) return ((int) len); } - int nn_recv(int s, void *buf, size_t len, int flags) { @@ -307,7 +296,7 @@ nn_recv(int s, void *buf, size_t len, int flags) // Note that this *can* alter the message, // although for performance reasons it ought not. // (There should be sufficient headroom.) - if ((rv = nng_msg_prepend(msg, &msg, sizeof (msg))) != 0) { + if ((rv = nng_msg_prepend(msg, &msg, sizeof(msg))) != 0) { nng_msg_free(msg); nn_seterror(rv); return (-1); @@ -316,7 +305,7 @@ nn_recv(int s, void *buf, size_t len, int flags) // now "trim" it off... the value is still there, but the // contents are unreferenced. We rely on legacy nanomsg's // ignorance of nng msgs to preserve this. - nng_msg_trim(msg, sizeof (msg)); + nng_msg_trim(msg, sizeof(msg)); *(void **) buf = nng_msg_body(msg); return ((int) nng_msg_len(msg)); @@ -329,14 +318,13 @@ nn_recv(int s, void *buf, size_t len, int flags) return ((int) len); } - int nn_recvmsg(int s, struct nn_msghdr *mh, int flags) { - int rv; + int rv; nng_msg *msg; - size_t len; - int keep = 0; + size_t len; + int keep = 0; if ((flags = nn_flags(flags)) == -1) { return (-1); @@ -357,19 +345,19 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) if ((mh->msg_iovlen == 1) && (mh->msg_iov[0].iov_len == NN_MSG)) { // Receiver wants to have a dynamically allocated message. // There can only be one of these. - if ((rv = nng_msg_prepend(msg, &msg, sizeof (msg))) != 0) { + if ((rv = nng_msg_prepend(msg, &msg, sizeof(msg))) != 0) { nng_msg_free(msg); nn_seterror(rv); return (-1); } - nng_msg_trim(msg, sizeof (msg)); + nng_msg_trim(msg, sizeof(msg)); *(void **) (mh->msg_iov[0].iov_base) = nng_msg_body(msg); - len = nng_msg_len(msg); + len = nng_msg_len(msg); keep = 1; // Do not discard message! } else { // copyout to multiple iovecs. - char *ptr = nng_msg_body(msg); - int i; + char * ptr = nng_msg_body(msg); + int i; size_t n; len = nng_msg_len(msg); @@ -398,15 +386,15 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) // If the caller has requested control information (header details), // we grab it. if (mh->msg_control != NULL) { - char *cdata; - size_t clen; - size_t tlen; - size_t spsz; + char * cdata; + size_t clen; + size_t tlen; + size_t spsz; struct nn_cmsghdr *hdr; - unsigned char *ptr; + unsigned char * ptr; spsz = nng_msg_header_len(msg); - clen = NN_CMSG_SPACE(sizeof (spsz) + spsz); + clen = NN_CMSG_SPACE(sizeof(spsz) + spsz); if ((tlen = mh->msg_controllen) == NN_MSG) { // Ideally we'd use the same msg, but we would need @@ -414,32 +402,32 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) // instead we just make a new message. nng_msg *nmsg; - rv = nng_msg_alloc(&nmsg, clen + sizeof (nmsg)); + rv = nng_msg_alloc(&nmsg, clen + sizeof(nmsg)); if (rv != 0) { nng_msg_free(msg); nn_seterror(rv); return (-1); } - memcpy(nng_msg_body(nmsg), &nmsg, sizeof (nmsg)); - nng_msg_trim(nmsg, sizeof (nmsg)); - cdata = nng_msg_body(nmsg); + memcpy(nng_msg_body(nmsg), &nmsg, sizeof(nmsg)); + nng_msg_trim(nmsg, sizeof(nmsg)); + cdata = nng_msg_body(nmsg); *(void **) mh->msg_control = cdata; - tlen = clen; + tlen = clen; } else { cdata = mh->msg_control; memset(cdata, 0, - tlen > sizeof (*hdr) ? sizeof (*hdr) : tlen); + tlen > sizeof(*hdr) ? sizeof(*hdr) : tlen); } if (clen <= tlen) { - ptr = NN_CMSG_DATA(cdata); - hdr = (void *) cdata; - hdr->cmsg_len = clen; + ptr = NN_CMSG_DATA(cdata); + hdr = (void *) cdata; + hdr->cmsg_len = clen; hdr->cmsg_level = PROTO_SP; - hdr->cmsg_type = SP_HDR; + hdr->cmsg_type = SP_HDR; - memcpy(ptr, &spsz, sizeof (spsz)); - ptr += sizeof (spsz); + memcpy(ptr, &spsz, sizeof(spsz)); + ptr += sizeof(spsz); memcpy(ptr, nng_msg_header(msg), spsz); } } @@ -450,16 +438,15 @@ nn_recvmsg(int s, struct nn_msghdr *mh, int flags) return ((int) len); } - int nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) { - nng_msg *msg = NULL; + nng_msg *msg = NULL; nng_msg *cmsg = NULL; - char *cdata; - int keep = 0; - size_t sz; - int rv; + char * cdata; + int keep = 0; + size_t sz; + int rv; if ((flags = nn_flags(flags)) == -1) { return (-1); @@ -476,12 +463,12 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) } if ((mh->msg_iovlen == 1) && (mh->msg_iov[0].iov_len == NN_MSG)) { - msg = *(nng_msg **) - (((char *) mh->msg_iov[0].iov_base) - sizeof (msg)); + msg = *(nng_msg **) (((char *) mh->msg_iov[0].iov_base) - + sizeof(msg)); keep = 1; // keep the message on error } else { char *ptr; - int i; + int i; sz = 0; // Get the total message size. @@ -506,11 +493,11 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) // usability we've ever seen. cmsg = NULL; if ((cdata = mh->msg_control) != NULL) { - size_t clen; - size_t offs; - size_t spsz; + size_t clen; + size_t offs; + size_t spsz; struct nn_cmsghdr *chdr; - unsigned char *data; + unsigned char * data; if ((clen = mh->msg_controllen) == NN_MSG) { // Underlying data is a message. This is awkward, @@ -518,14 +505,14 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) // only free this message on success. So we save the // message now. cdata = *(void **) cdata; - cmsg = *(nng_msg **) (cdata - sizeof (cmsg)); - clen = nng_msg_len(cmsg); + cmsg = *(nng_msg **) (cdata - sizeof(cmsg)); + clen = nng_msg_len(cmsg); } else { clen = mh->msg_controllen; } offs = 0; - while ((offs + sizeof (NN_CMSG_LEN(0))) < clen) { + while ((offs + sizeof(NN_CMSG_LEN(0))) < clen) { chdr = (void *) (cdata + offs); if ((chdr->cmsg_level != PROTO_SP) || (chdr->cmsg_type != SP_HDR)) { @@ -534,18 +521,18 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) // SP header in theory. Starts with size, then // any backtrace details. - if (chdr->cmsg_len < sizeof (size_t)) { + if (chdr->cmsg_len < sizeof(size_t)) { offs += chdr->cmsg_len; continue; } data = NN_CMSG_DATA(chdr); - memcpy(&spsz, data, sizeof (spsz)); - if ((spsz + sizeof (spsz)) > chdr->cmsg_len) { + memcpy(&spsz, data, sizeof(spsz)); + if ((spsz + sizeof(spsz)) > chdr->cmsg_len) { // Truncated header? Ignore it. offs += chdr->cmsg_len; continue; } - data += sizeof (spsz); + data += sizeof(spsz); rv = nng_msg_append_header(msg, data, spsz); if (rv != 0) { if (!keep) { @@ -575,14 +562,13 @@ nn_sendmsg(int s, const struct nn_msghdr *mh, int flags) return ((int) sz); } - int nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) { - int opt = 0; - int mscvt = 0; + int opt = 0; + int mscvt = 0; uint64_t usec; - int rv; + int rv; switch (nnlevel) { case NN_SOL_SOCKET: @@ -597,11 +583,11 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) opt = NNG_OPT_RCVBUF; break; case NN_RECONNECT_IVL: - opt = NNG_OPT_RECONN_TIME; + opt = NNG_OPT_RECONN_TIME; mscvt = 1; break; case NN_RECONNECT_IVL_MAX: - opt = NNG_OPT_RECONN_MAXTIME; + opt = NNG_OPT_RECONN_MAXTIME; mscvt = 1; break; case NN_SNDFD: @@ -617,11 +603,11 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) opt = NNG_OPT_MAXTTL; break; case NN_RCVTIMEO: - opt = NNG_OPT_RCVTIMEO; + opt = NNG_OPT_RCVTIMEO; mscvt = 1; break; case NN_SNDTIMEO: - opt = NNG_OPT_SNDTIMEO; + opt = NNG_OPT_SNDTIMEO; mscvt = 1; break; case NN_DOMAIN: @@ -640,7 +626,7 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) case NN_REQ: switch (nnopt) { case NN_REQ_RESEND_IVL: - opt = NNG_OPT_RESENDTIME; + opt = NNG_OPT_RESENDTIME; mscvt = 1; break; default: @@ -664,7 +650,7 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) case NN_SURVEYOR: switch (nnopt) { case NN_SURVEY_DEADLINE: - opt = NNG_OPT_SURVEYTIME; + opt = NNG_OPT_SURVEYTIME; mscvt = 1; break; default: @@ -679,14 +665,14 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) if (mscvt) { // We have to convert value to ms... - if (sz != sizeof (int)) { + if (sz != sizeof(int)) { errno = EINVAL; return (-1); } usec = *(int *) valp; usec *= 1000; valp = &usec; - sz = sizeof (usec); + sz = sizeof(usec); } if ((rv = nng_setopt((nng_socket) s, opt, valp, sz)) != 0) { @@ -696,12 +682,11 @@ nn_setsockopt(int s, int nnlevel, int nnopt, const void *valp, size_t sz) return (0); } - struct nn_cmsghdr * nn_cmsg_next(struct nn_msghdr *mh, struct nn_cmsghdr *first) { size_t clen; - char *data; + char * data; // We only support SP headers, so there can be at most one header. if (first != NULL) { @@ -710,7 +695,7 @@ nn_cmsg_next(struct nn_msghdr *mh, struct nn_cmsghdr *first) if ((clen = mh->msg_controllen) == NN_MSG) { nng_msg *msg; data = *((void **) (mh->msg_control)); - msg = *(nng_msg **) (data - sizeof (msg)); + msg = *(nng_msg **) (data - sizeof(msg)); clen = nng_msg_len(msg); } else { data = mh->msg_control; @@ -722,13 +707,12 @@ nn_cmsg_next(struct nn_msghdr *mh, struct nn_cmsghdr *first) first = first + first->cmsg_len; } - if (((char *) first + sizeof (*first)) > (data + clen)) { + if (((char *) first + sizeof(*first)) > (data + clen)) { return (NULL); } return (first); } - int nn_device(int s1, int s2) { @@ -740,7 +724,6 @@ nn_device(int s1, int s2) return (-1); } - // nn_term is suitable only for shutting down the entire library, // and is not thread-safe with other functions. void @@ -753,7 +736,6 @@ nn_term(void) // in use by libraries, etc. } - // Internal test support routines. void @@ -762,14 +744,12 @@ nn_sleep(uint64_t msec) nng_usleep(msec * 1000); } - uint64_t nn_clock(void) { return (nng_clock()); } - extern void nni_panic(const char *, ...); int @@ -784,7 +764,6 @@ nn_thread_init(struct nn_thread *thr, void (*func)(void *), void *arg) return (rv); } - void nn_thread_term(struct nn_thread *thr) { diff --git a/src/nng_compat.h b/src/nng_compat.h index 1896f6f7..cd189b2d 100644 --- a/src/nng_compat.h +++ b/src/nng_compat.h @@ -37,6 +37,11 @@ extern "C" { #include #include +// clang-format gets in the way of most of this file. +// We turn it off, at least until it gets smarter about aligning +// macro definitions or we adopt enums or somesuch. +// clang-format off + // NNG_DECL is used on declarations to deal with scope. // For building Windows DLLs, it should be the appropriate // __declspec(). (We recommend *not* building this library @@ -228,57 +233,57 @@ extern "C" { #define NN_WS_MSG_TYPE_TEXT 1 #define NN_WS_MSG_TYPE_BINARY 2 +// from this point on formatting is fine +// clang-format on + // Poll stuff -#define NN_POLLIN 1 -#define NN_POLLOUT 2 +#define NN_POLLIN 1 +#define NN_POLLOUT 2 struct nn_pollfd { - int fd; - uint16_t events; - uint16_t revents; + int fd; + uint16_t events; + uint16_t revents; }; // Magical size for allocation -#define NN_MSG ((size_t) -1) +#define NN_MSG ((size_t) -1) struct nn_iovec { - void * iov_base; - size_t iov_len; + void * iov_base; + size_t iov_len; }; struct nn_msghdr { - struct nn_iovec * msg_iov; - int msg_iovlen; - void * msg_control; - size_t msg_controllen; + struct nn_iovec *msg_iov; + int msg_iovlen; + void * msg_control; + size_t msg_controllen; }; struct nn_cmsghdr { - size_t cmsg_len; - int cmsg_level; - int cmsg_type; + size_t cmsg_len; + int cmsg_level; + int cmsg_type; }; #define NN_CMSG_ALIGN(len) \ - (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((len) + sizeof(size_t) - 1) & (size_t) ~(sizeof(size_t) - 1)) // Unlike old nanomsg, we explicitly only support the SP header as attached // cmsg data. It turns out that old nanomsg didn't really store anything // useful otherwise anyway. (One specific exception was that it stored the // message type of text or binary for the websocket transport. We don't think // anyone used that in practice though.) -#define NN_CMSG_FIRSTHDR(mh) \ - nn_cmsg_next((struct nn_msghdr *) (mh), NULL) +#define NN_CMSG_FIRSTHDR(mh) nn_cmsg_next((struct nn_msghdr *) (mh), NULL) #define NN_CMSG_NXTHDR(mh, ch) \ nn_cmsg_next((struct nn_msghdr *) (mh), (struct nn_cmsghdr *) ch) -#define NN_CMSG_DATA(ch) \ - ((unsigned char *) (((struct nn_cmsghdr *) (ch)) + 1)) +#define NN_CMSG_DATA(ch) ((unsigned char *) (((struct nn_cmsghdr *) (ch)) + 1)) #define NN_CMSG_SPACE(len) \ - (NN_CMSG_ALIGN(len) + NN_CMSG_ALIGN(sizeof (struct nn_cmsghdr))) -#define NN_CMSG_LEN(len) \ - (NN_CMSG_ALIGN(sizeof (struct nn_cmsghdr)) + (len)) + (NN_CMSG_ALIGN(len) + NN_CMSG_ALIGN(sizeof(struct nn_cmsghdr))) +#define NN_CMSG_LEN(len) (NN_CMSG_ALIGN(sizeof(struct nn_cmsghdr)) + (len)) -NN_DECL struct nn_cmsghdr *nn_cmsg_next(struct nn_msghdr *, - struct nn_cmsghdr *); +NN_DECL struct nn_cmsghdr *nn_cmsg_next( + struct nn_msghdr *, struct nn_cmsghdr *); NN_DECL int nn_socket(int, int); NN_DECL int nn_setsockopt(int, int, int, const void *, size_t); NN_DECL int nn_getsockopt(int, int, int, void *, size_t *); @@ -292,14 +297,13 @@ NN_DECL int nn_recvmsg(int, struct nn_msghdr *, int); NN_DECL int nn_close(int); NN_DECL int nn_poll(struct nn_pollfd *, int, int); NN_DECL int nn_device(int, int); -NN_DECL uint64_t nn_get_statistic(int, int); -NN_DECL void *nn_allocmsg(size_t, int); -NN_DECL void *nn_reallocmsg(void *, size_t); -NN_DECL int nn_freemsg(void *); -NN_DECL int nn_errno(void); +NN_DECL uint64_t nn_get_statistic(int, int); +NN_DECL void * nn_allocmsg(size_t, int); +NN_DECL void * nn_reallocmsg(void *, size_t); +NN_DECL int nn_freemsg(void *); +NN_DECL int nn_errno(void); NN_DECL const char *nn_strerror(int); -NN_DECL void nn_term(void); - +NN_DECL void nn_term(void); // This stuff is intended to be exposed only for test programs and our // own utilities. Do not use in your own programs. @@ -311,8 +315,8 @@ struct nn_thread { NN_DECL void nn_sleep(uint64_t); NN_DECL uint64_t nn_clock(void); -NN_DECL int nn_thread_init(struct nn_thread *, void (*)(void *), void *); -NN_DECL void nn_thread_term(struct nn_thread *); +NN_DECL int nn_thread_init(struct nn_thread *, void (*)(void *), void *); +NN_DECL void nn_thread_term(struct nn_thread *); #endif // NNG_PRIVATE diff --git a/src/platform/posix/posix_aio.h b/src/platform/posix/posix_aio.h index 13559c08..60594152 100644 --- a/src/platform/posix/posix_aio.h +++ b/src/platform/posix/posix_aio.h @@ -18,26 +18,25 @@ #include "core/nng_impl.h" -typedef struct nni_posix_pollq nni_posix_pollq; +typedef struct nni_posix_pollq nni_posix_pollq; +typedef struct nni_posix_pipedesc nni_posix_pipedesc; +typedef struct nni_posix_epdesc nni_posix_epdesc; -typedef struct nni_posix_pipedesc nni_posix_pipedesc; -typedef struct nni_posix_epdesc nni_posix_epdesc; - -extern int nni_posix_pipedesc_init(nni_posix_pipedesc **, int); +extern int nni_posix_pipedesc_init(nni_posix_pipedesc **, int); extern void nni_posix_pipedesc_fini(nni_posix_pipedesc *); extern void nni_posix_pipedesc_recv(nni_posix_pipedesc *, nni_aio *); extern void nni_posix_pipedesc_send(nni_posix_pipedesc *, nni_aio *); extern void nni_posix_pipedesc_close(nni_posix_pipedesc *); -extern int nni_posix_epdesc_init(nni_posix_epdesc **, const char *); +extern int nni_posix_epdesc_init(nni_posix_epdesc **, const char *); extern const char *nni_posix_epdesc_url(nni_posix_epdesc *); -extern void nni_posix_epdesc_set_local(nni_posix_epdesc *, void *, int); +extern void nni_posix_epdesc_set_local(nni_posix_epdesc *, void *, int); extern void nni_posix_epdesc_set_remote(nni_posix_epdesc *, void *, int); extern void nni_posix_epdesc_fini(nni_posix_epdesc *); extern void nni_posix_epdesc_close(nni_posix_epdesc *); extern void nni_posix_epdesc_connect(nni_posix_epdesc *, nni_aio *); -extern int nni_posix_epdesc_listen(nni_posix_epdesc *); +extern int nni_posix_epdesc_listen(nni_posix_epdesc *); extern void nni_posix_epdesc_accept(nni_posix_epdesc *, nni_aio *); #endif // PLATFORM_POSIX_AIO_H diff --git a/src/platform/posix/posix_alloc.c b/src/platform/posix/posix_alloc.c index aef5bbfc..a3cd29b9 100644 --- a/src/platform/posix/posix_alloc.c +++ b/src/platform/posix/posix_alloc.c @@ -20,7 +20,6 @@ nni_alloc(size_t sz) return (calloc(1, sz)); } - void nni_free(void *ptr, size_t size) { @@ -28,7 +27,6 @@ nni_free(void *ptr, size_t size) free(ptr); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_clock.c b/src/platform/posix/posix_clock.c index c87fb890..93e0978c 100644 --- a/src/platform/posix/posix_clock.c +++ b/src/platform/posix/posix_clock.c @@ -12,9 +12,9 @@ #ifdef PLATFORM_POSIX_CLOCK -#include #include #include +#include #ifndef NNG_USE_GETTIMEOFDAY @@ -23,7 +23,7 @@ nni_time nni_plat_clock(void) { struct timespec ts; - nni_time usec; + nni_time usec; if (clock_gettime(NNG_USE_CLOCKID, &ts) != 0) { /* This should never ever occur. */ @@ -36,13 +36,12 @@ nni_plat_clock(void) return (usec); } - void nni_plat_usleep(nni_duration usec) { struct timespec ts; - ts.tv_sec = usec / 1000000; + ts.tv_sec = usec / 1000000; ts.tv_nsec = (usec % 1000000) * 1000; /* Do this in a loop, so that interrupts don't actually wake us. */ @@ -53,8 +52,7 @@ nni_plat_usleep(nni_duration usec) } } - -#else // NNG_USE_GETTIMEOFDAY +#else // NNG_USE_GETTIMEOFDAY // If you're here, its because you don't have a modern clock_gettime with // monotonic clocks, or the necessary pthread_condattr_settclock(). In @@ -65,9 +63,9 @@ nni_plat_usleep(nni_duration usec) // but if you change the clock by a large amount you might wonder what the // heck is happening until it does.) +#include #include #include -#include nni_time nni_plat_clock(void) @@ -86,7 +84,6 @@ nni_plat_clock(void) return (usec); } - void nni_plat_usleep(nni_duration usec) { @@ -99,16 +96,16 @@ nni_plat_usleep(nni_duration usec) // So we can use poll() instead, which is rather coarse, but // pretty much guaranteed to work. struct pollfd pfd; - nni_time now; - nni_time expire; + nni_time now; + nni_time expire; // Possibly we could pass NULL instead of pfd, but passing a valid // pointer ensures that if the system dereferences the pointer it // won't come back with EFAULT. - pfd.fd = -1; + pfd.fd = -1; pfd.events = 0; - now = nni_clock(); + now = nni_clock(); expire = now + usec; while (now < expire) { @@ -122,12 +119,11 @@ nni_plat_usleep(nni_duration usec) } } - -#endif // NNG_USE_GETTIMEOFDAY +#endif // NNG_USE_GETTIMEOFDAY #else // Suppress empty symbols warnings in ranlib. int nni_posix_clock_not_used = 0; -#endif // PLATFORM_POSIX_CLOCK +#endif // PLATFORM_POSIX_CLOCK diff --git a/src/platform/posix/posix_config.h b/src/platform/posix/posix_config.h index 7e82a050..8ed6387c 100644 --- a/src/platform/posix/posix_config.h +++ b/src/platform/posix/posix_config.h @@ -49,18 +49,18 @@ // exists but has somehow very very different semantics. We don't know // of any such concerns.) This won't be used if any of the other options // are defined and work. -#define NNG_USE_DEVURANDOM 1 +#define NNG_USE_DEVURANDOM 1 -#define NNG_USE_CLOCKID CLOCK_REALTIME +#define NNG_USE_CLOCKID CLOCK_REALTIME #ifndef CLOCK_REALTIME #define NNG_USE_GETTIMEOFDAY #elif !defined(NNG_USE_CLOCKID) -#define NNG_USE_CLOCKID CLOCK_MONOTONIC +#define NNG_USE_CLOCKID CLOCK_MONOTONIC #else -#define NNG_USE_CLOCKID CLOCK_REALTIME -#endif // CLOCK_REALTIME +#define NNG_USE_CLOCKID CLOCK_REALTIME +#endif // CLOCK_REALTIME -#define NNG_USE_POSIX_POLLQ_POLL 1 -#define NNG_USE_POSIX_RESOLV_GAI 1 +#define NNG_USE_POSIX_POLLQ_POLL 1 +#define NNG_USE_POSIX_RESOLV_GAI 1 #endif // PLATFORM_POSIX diff --git a/src/platform/posix/posix_debug.c b/src/platform/posix/posix_debug.c index b9321ea5..647d93ff 100644 --- a/src/platform/posix/posix_debug.c +++ b/src/platform/posix/posix_debug.c @@ -12,8 +12,8 @@ #ifdef PLATFORM_POSIX_DEBUG #include -#include #include +#include #include void @@ -22,7 +22,6 @@ nni_plat_abort(void) abort(); } - void nni_plat_println(const char *message) { @@ -30,7 +29,6 @@ nni_plat_println(const char *message) fputc('\n', stderr); } - const char * nni_plat_strerror(int errnum) { @@ -40,7 +38,6 @@ nni_plat_strerror(int errnum) return (strerror(errnum)); } - // There are of course other errors than these, but these are the ones // that we might reasonably expect and want to handle "cleanly". Most of // the others should be handled by the system error code. Note that EFAULT @@ -50,10 +47,10 @@ nni_plat_strerror(int errnum) // error codes should exist, and be distinct positive numbers. (EWOULDBLOCK // and EAGAIN are permitted to have the same value.) static struct { - int posix_err; - int nng_err; -} -nni_plat_errnos[] = { + int posix_err; + int nng_err; +} nni_plat_errnos[] = { + // clang-format off { EINTR, NNG_EINTR }, { EINVAL, NNG_EINVAL }, { ENOMEM, NNG_ENOMEM }, @@ -86,6 +83,7 @@ nni_plat_errnos[] = { { EWOULDBLOCK, NNG_EAGAIN }, // must be last { 0, 0 }, + // clang-format on }; int @@ -108,7 +106,6 @@ nni_plat_errno(int errnum) return (NNG_ESYSERR + errnum); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 77662ddd..a6de29e1 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -14,38 +14,37 @@ #include "platform/posix/posix_pollq.h" #include +#include +#include +#include #include #include -#include #include +#include #include #include -#include #include -#include -#include -#ifdef SOCK_CLOEXEC -#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) +#ifdef SOCK_CLOEXEC +#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) #else -#define NNI_STREAM_SOCKTYPE SOCK_STREAM +#define NNI_STREAM_SOCKTYPE SOCK_STREAM #endif struct nni_posix_epdesc { - int fd; - nni_list connectq; - nni_list acceptq; - nni_posix_pollq_node node; - nni_posix_pollq * pq; + int fd; + nni_list connectq; + nni_list acceptq; + nni_posix_pollq_node node; + nni_posix_pollq * pq; struct sockaddr_storage locaddr; struct sockaddr_storage remaddr; - socklen_t loclen; - socklen_t remlen; - const char * url; - nni_mtx mtx; + socklen_t loclen; + socklen_t remlen; + const char * url; + nni_mtx mtx; }; - static void nni_posix_epdesc_cancel(nni_aio *aio) { @@ -56,11 +55,10 @@ nni_posix_epdesc_cancel(nni_aio *aio) nni_mtx_unlock(&ed->mtx); } - static void nni_posix_epdesc_finish(nni_aio *aio, int rv, int newfd) { - nni_posix_epdesc *ed = aio->a_prov_data; + nni_posix_epdesc * ed = aio->a_prov_data; nni_posix_pipedesc *pd; // acceptq or connectq. @@ -78,13 +76,12 @@ nni_posix_epdesc_finish(nni_aio *aio, int rv, int newfd) nni_aio_finish(aio, rv, 0); } - static void nni_posix_epdesc_doconnect(nni_posix_epdesc *ed) { - nni_aio *aio; + nni_aio * aio; socklen_t sz; - int rv; + int rv; // Note that normally there will only be a single connect AIO... // A socket that is here will have *initiated* with a connect() @@ -94,7 +91,7 @@ nni_posix_epdesc_doconnect(nni_posix_epdesc *ed) // status of the connection attempt... while ((aio = nni_list_first(&ed->connectq)) != NULL) { rv = -1; - sz = sizeof (rv); + sz = sizeof(rv); if (getsockopt(ed->fd, SOL_SOCKET, SO_ERROR, &rv, &sz) < 0) { rv = errno; } @@ -121,24 +118,22 @@ nni_posix_epdesc_doconnect(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_doaccept(nni_posix_epdesc *ed) { - nni_aio *aio; - int newfd; + nni_aio * aio; + int newfd; struct sockaddr_storage ss; - socklen_t slen; + socklen_t slen; while ((aio = nni_list_first(&ed->acceptq)) != NULL) { - // We could argue that knowing the remote peer address would - // be nice. But frankly if someone wants it, they can just - // do getpeername(). +// We could argue that knowing the remote peer address would +// be nice. But frankly if someone wants it, they can just +// do getpeername(). #ifdef NNG_USE_ACCEPT4 newfd = accept4(ed->fd, NULL, NULL, SOCK_CLOEXEC); - if ((newfd < 0) && - ((errno == ENOSYS) || (errno == ENOTSUP))) { + if ((newfd < 0) && ((errno == ENOSYS) || (errno == ENOTSUP))) { newfd = accept(ed->fd, NULL, NULL); } #else @@ -172,13 +167,12 @@ nni_posix_epdesc_doaccept(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_doerror(nni_posix_epdesc *ed) { - nni_aio *aio; - int rv = 1; - socklen_t sz = sizeof (rv); + nni_aio * aio; + int rv = 1; + socklen_t sz = sizeof(rv); if (getsockopt(ed->fd, SOL_SOCKET, SO_ERROR, &rv, &sz) < 0) { rv = errno; @@ -196,11 +190,10 @@ nni_posix_epdesc_doerror(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_doclose(nni_posix_epdesc *ed) { - nni_aio *aio; + nni_aio * aio; struct sockaddr_un *sun; if (ed->fd != -1) { @@ -220,7 +213,6 @@ nni_posix_epdesc_doclose(nni_posix_epdesc *ed) } } - static void nni_posix_epdesc_cb(void *arg) { @@ -234,14 +226,14 @@ nni_posix_epdesc_cb(void *arg) if (ed->node.revents & POLLOUT) { nni_posix_epdesc_doconnect(ed); } - if (ed->node.revents & (POLLERR|POLLHUP)) { + if (ed->node.revents & (POLLERR | POLLHUP)) { nni_posix_epdesc_doerror(ed); } if (ed->node.revents & POLLNVAL) { nni_posix_epdesc_doclose(ed); } ed->node.revents = 0; - ed->node.events = 0; + ed->node.events = 0; if (!nni_list_empty(&ed->connectq)) { ed->node.events |= POLLOUT; @@ -252,7 +244,6 @@ nni_posix_epdesc_cb(void *arg) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_close(nni_posix_epdesc *ed) { @@ -263,16 +254,15 @@ nni_posix_epdesc_close(nni_posix_epdesc *ed) nni_mtx_unlock(&ed->mtx); } - static int nni_posix_epdesc_parseaddr(char *pair, char **hostp, uint16_t *portp) { char *host, *port, *end; - char c; - int val; + char c; + int val; if (pair[0] == '[') { - host = pair+1; + host = pair + 1; // IP address enclosed ... for IPv6 usually. if ((end = strchr(host, ']')) == NULL) { return (NNG_EADDRINVAL); @@ -315,18 +305,17 @@ nni_posix_epdesc_parseaddr(char *pair, char **hostp, uint16_t *portp) return (0); } - int nni_posix_epdesc_listen(nni_posix_epdesc *ed) { - int len; + int len; struct sockaddr_storage *ss; - int rv; - int fd; + int rv; + int fd; nni_mtx_lock(&ed->mtx); - ss = &ed->locaddr; + ss = &ed->locaddr; len = ed->loclen; if ((fd = socket(ss->ss_family, NNI_STREAM_SOCKTYPE, 0)) < 0) { @@ -352,13 +341,12 @@ nni_posix_epdesc_listen(nni_posix_epdesc *ed) (void) fcntl(fd, F_SETFL, O_NONBLOCK); - ed->fd = fd; + ed->fd = fd; ed->node.fd = fd; nni_mtx_unlock(&ed->mtx); return (0); } - void nni_posix_epdesc_accept(nni_posix_epdesc *ed, nni_aio *aio) { @@ -394,7 +382,6 @@ nni_posix_epdesc_accept(nni_posix_epdesc *ed, nni_aio *aio) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_connect(nni_posix_epdesc *ed, nni_aio *aio) { @@ -469,12 +456,11 @@ nni_posix_epdesc_connect(nni_posix_epdesc *ed, nni_aio *aio) nni_mtx_unlock(&ed->mtx); } - int nni_posix_epdesc_init(nni_posix_epdesc **edp, const char *url) { nni_posix_epdesc *ed; - int rv; + int rv; if ((ed = NNI_ALLOC_STRUCT(ed)) == NULL) { return (NNG_ENOMEM); @@ -490,12 +476,12 @@ nni_posix_epdesc_init(nni_posix_epdesc **edp, const char *url) // one. For now we just have a global pollq. Note that by tying // the ed to a single pollq we may get some kind of cache warmth. - ed->pq = nni_posix_pollq_get((int) nni_random()); - ed->fd = -1; + ed->pq = nni_posix_pollq_get((int) nni_random()); + ed->fd = -1; ed->node.index = 0; - ed->node.cb = nni_posix_epdesc_cb; - ed->node.data = ed; - ed->url = url; + ed->node.cb = nni_posix_epdesc_cb; + ed->node.data = ed; + ed->url = url; nni_aio_list_init(&ed->connectq); nni_aio_list_init(&ed->acceptq); @@ -504,18 +490,16 @@ nni_posix_epdesc_init(nni_posix_epdesc **edp, const char *url) return (0); } - const char * nni_posix_epdesc_url(nni_posix_epdesc *ed) { return (ed->url); } - void nni_posix_epdesc_set_local(nni_posix_epdesc *ed, void *sa, int len) { - if ((len < 0) || (len > sizeof (struct sockaddr_storage))) { + if ((len < 0) || (len > sizeof(struct sockaddr_storage))) { return; } nni_mtx_lock(&ed->mtx); @@ -524,11 +508,10 @@ nni_posix_epdesc_set_local(nni_posix_epdesc *ed, void *sa, int len) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_set_remote(nni_posix_epdesc *ed, void *sa, int len) { - if ((len < 0) || (len > sizeof (struct sockaddr_storage))) { + if ((len < 0) || (len > sizeof(struct sockaddr_storage))) { return; } nni_mtx_lock(&ed->mtx); @@ -537,7 +520,6 @@ nni_posix_epdesc_set_remote(nni_posix_epdesc *ed, void *sa, int len) nni_mtx_unlock(&ed->mtx); } - void nni_posix_epdesc_fini(nni_posix_epdesc *ed) { @@ -548,7 +530,6 @@ nni_posix_epdesc_fini(nni_posix_epdesc *ed) NNI_FREE_STRUCT(ed); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 407adf74..72842076 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -16,7 +16,7 @@ // when using PLATFORM_POSIX_CLOCK, your condition variable timeouts need // to use the same base clock values. Normally these should be used // together. Almost everything depends on PLATFORM_POSIX_DEBUG. -#ifdef PLATFORM_POSIX +#ifdef PLATFORM_POSIX #define PLATFORM_POSIX_ALLOC #define PLATFORM_POSIX_DEBUG #define PLATFORM_POSIX_CLOCK @@ -40,7 +40,7 @@ extern int nni_plat_errno(int); // Define types that this platform uses. #ifdef PLATFORM_POSIX_THREAD -extern int nni_plat_devnull; // open descriptor on /dev/null +extern int nni_plat_devnull; // open descriptor on /dev/null #include @@ -48,28 +48,27 @@ extern int nni_plat_devnull; // open descriptor on /dev/null // elsewhere. struct nni_plat_mtx { - int init; - pthread_t owner; + int init; + pthread_t owner; pthread_mutex_t mtx; }; struct nni_plat_thr { - pthread_t tid; - void (*func)(void *); - void * arg; + pthread_t tid; + void (*func)(void *); + void *arg; }; struct nni_plat_cv { - pthread_cond_t cv; - nni_plat_mtx * mtx; + pthread_cond_t cv; + nni_plat_mtx * mtx; }; #endif - -extern int nni_posix_pollq_sysinit(void); +extern int nni_posix_pollq_sysinit(void); extern void nni_posix_pollq_sysfini(void); -extern int nni_posix_resolv_sysinit(void); +extern int nni_posix_resolv_sysinit(void); extern void nni_posix_resolv_sysfini(void); #endif // PLATFORM_POSIX_IMPL_H diff --git a/src/platform/posix/posix_ipc.c b/src/platform/posix/posix_ipc.c index 706ef15f..a362de6d 100644 --- a/src/platform/posix/posix_ipc.c +++ b/src/platform/posix/posix_ipc.c @@ -13,24 +13,23 @@ #include "platform/posix/posix_aio.h" #include -#include +#include +#include #include +#include #include -#include #include +#include #include #include -#include #include -#include -#ifdef SOCK_CLOEXEC -#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) +#ifdef SOCK_CLOEXEC +#define NNI_STREAM_SOCKTYPE (SOCK_STREAM | SOCK_CLOEXEC) #else -#define NNI_STREAM_SOCKTYPE SOCK_STREAM +#define NNI_STREAM_SOCKTYPE SOCK_STREAM #endif - // Solaris/SunOS systems define this, which collides with our symbol // names. Just undefine it now. #ifdef sun @@ -43,32 +42,32 @@ int nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) { - nni_posix_epdesc *ed; - int rv; + nni_posix_epdesc * ed; + int rv; struct sockaddr_un sun; - const char *path; + const char * path; if (strncmp(url, "ipc://", strlen("ipc://")) != 0) { return (NNG_EADDRINVAL); } - path = url + strlen("ipc://"); // skip the prefix. + path = url + strlen("ipc://"); // skip the prefix. // prepare the sockaddr_un sun.sun_family = AF_UNIX; - if (strlen(url) >= sizeof (sun.sun_path)) { + if (strlen(url) >= sizeof(sun.sun_path)) { return (NNG_EADDRINVAL); } - snprintf(sun.sun_path, sizeof (sun.sun_path), "%s", path); + snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path); if ((rv = nni_posix_epdesc_init(&ed, url)) != 0) { return (rv); } switch (mode) { case NNI_EP_MODE_DIAL: - nni_posix_epdesc_set_remote(ed, &sun, sizeof (sun)); + nni_posix_epdesc_set_remote(ed, &sun, sizeof(sun)); break; case NNI_EP_MODE_LISTEN: - nni_posix_epdesc_set_local(ed, &sun, sizeof (sun)); + nni_posix_epdesc_set_local(ed, &sun, sizeof(sun)); break; default: nni_posix_epdesc_fini(ed); @@ -79,21 +78,18 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) return (0); } - void nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) { nni_posix_epdesc_fini((void *) ep); } - void nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) { nni_posix_epdesc_close((void *) ep); } - // UNIX DOMAIN SOCKETS -- these have names in the file namespace. // We are going to check to see if there was a name already there. // If there was, and nothing is listening (ECONNREFUSED), then we @@ -102,12 +98,12 @@ nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) static int nni_plat_ipc_remove_stale(const char *path) { - int fd; - int rv; + int fd; + int rv; struct sockaddr_un sun; sun.sun_family = AF_UNIX; - snprintf(sun.sun_path, sizeof (sun.sun_path), "%s", path); + snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path); if ((fd = socket(AF_UNIX, NNI_STREAM_SOCKTYPE, 0)) < 0) { return (nni_plat_errno(errno)); @@ -119,7 +115,7 @@ nni_plat_ipc_remove_stale(const char *path) // then the cleanup will fail. As this is supposed to be an // exceptional case, don't worry. (void) fcntl(fd, F_SETFL, O_NONBLOCK); - if (connect(fd, (void *) &sun, sizeof (sun)) < 0) { + if (connect(fd, (void *) &sun, sizeof(sun)) < 0) { if (errno == ECONNREFUSED) { (void) unlink(path); } @@ -128,14 +124,13 @@ nni_plat_ipc_remove_stale(const char *path) return (0); } - int nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) { - const char *path; - nni_posix_epdesc *ed = (void *) ep; + const char * path; + nni_posix_epdesc * ed = (void *) ep; struct sockaddr_un sun; - int rv; + int rv; path = nni_posix_epdesc_url(ed); path += strlen("ipc://"); @@ -146,49 +141,42 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) return (nni_posix_epdesc_listen(ed)); } - void nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) { nni_posix_epdesc_connect((void *) ep, aio); } - void nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) { nni_posix_epdesc_accept((void *) ep, aio); } - void nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *p) { nni_posix_pipedesc_fini((void *) p); } - void nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *p) { nni_posix_pipedesc_close((void *) p); } - void nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *p, nni_aio *aio) { nni_posix_pipedesc_send((void *) p, aio); } - void nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *p, nni_aio *aio) { nni_posix_pipedesc_recv((void *) p, aio); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_net.c b/src/platform/posix/posix_net.c index 26ec3632..78e1e6ee 100644 --- a/src/platform/posix/posix_net.c +++ b/src/platform/posix/posix_net.c @@ -12,72 +12,70 @@ #ifdef PLATFORM_POSIX_NET #include "platform/posix/posix_aio.h" +#include #include +#include +#include +#include #include #include #include -#include #include +#include #include -#include -#include -#include -#include #include static int nni_posix_tcp_addr(struct sockaddr_storage *ss, const nni_sockaddr *sa) { - struct sockaddr_in *sin; + struct sockaddr_in * sin; struct sockaddr_in6 *sin6; switch (sa->s_un.s_family) { case NNG_AF_INET: sin = (void *) ss; - memset(sin, 0, sizeof (*sin)); - sin->sin_family = PF_INET; - sin->sin_port = sa->s_un.s_in.sa_port; + memset(sin, 0, sizeof(*sin)); + sin->sin_family = PF_INET; + sin->sin_port = sa->s_un.s_in.sa_port; sin->sin_addr.s_addr = sa->s_un.s_in.sa_addr; - return (sizeof (*sin)); - + return (sizeof(*sin)); case NNG_AF_INET6: sin6 = (void *) ss; - memset(sin6, 0, sizeof (*sin6)); -#ifdef SIN6_LEN - sin6->sin6_len = sizeof (*sin6); + memset(sin6, 0, sizeof(*sin6)); +#ifdef SIN6_LEN + sin6->sin6_len = sizeof(*sin6); #endif sin6->sin6_family = PF_INET6; - sin6->sin6_port = sa->s_un.s_in6.sa_port; + sin6->sin6_port = sa->s_un.s_in6.sa_port; memcpy(sin6->sin6_addr.s6_addr, sa->s_un.s_in6.sa_addr, 16); - return (sizeof (*sin6)); + return (sizeof(*sin6)); } return (-1); } - extern int nni_tcp_parse_url(char *, char **, char **, char **, char **); int nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const char *url, int mode) { - nni_posix_epdesc *ed; - char buf[NNG_MAXADDRLEN]; - int rv; - char *lhost, *rhost; - char *lserv, *rserv; - char *sep; + nni_posix_epdesc * ed; + char buf[NNG_MAXADDRLEN]; + int rv; + char * lhost, *rhost; + char * lserv, *rserv; + char * sep; struct sockaddr_storage ss; - int len; - int passive; - nni_aio aio; + int len; + int passive; + nni_aio aio; if ((rv = nni_posix_epdesc_init(&ed, url)) != 0) { return (rv); } // Make a local copy. - snprintf(buf, sizeof (buf), "%s", url); + snprintf(buf, sizeof(buf), "%s", url); nni_aio_init(&aio, NULL, NULL); if (mode == NNI_EP_MODE_DIAL) { @@ -138,70 +136,60 @@ done: return (rv); } - void nni_plat_tcp_ep_fini(nni_plat_tcp_ep *ep) { nni_posix_epdesc_fini((void *) ep); } - void nni_plat_tcp_ep_close(nni_plat_tcp_ep *ep) { nni_posix_epdesc_close((void *) ep); } - int nni_plat_tcp_ep_listen(nni_plat_tcp_ep *ep) { return (nni_posix_epdesc_listen((void *) ep)); } - void nni_plat_tcp_ep_connect(nni_plat_tcp_ep *ep, nni_aio *aio) { return (nni_posix_epdesc_connect((void *) ep, aio)); } - void nni_plat_tcp_ep_accept(nni_plat_tcp_ep *ep, nni_aio *aio) { return (nni_posix_epdesc_accept((void *) ep, aio)); } - void nni_plat_tcp_pipe_fini(nni_plat_tcp_pipe *p) { nni_posix_pipedesc_fini((void *) p); } - void nni_plat_tcp_pipe_close(nni_plat_tcp_pipe *p) { nni_posix_pipedesc_close((void *) p); } - void nni_plat_tcp_pipe_send(nni_plat_tcp_pipe *p, nni_aio *aio) { nni_posix_pipedesc_send((void *) p, aio); } - void nni_plat_tcp_pipe_recv(nni_plat_tcp_pipe *p, nni_aio *aio) { nni_posix_pipedesc_recv((void *) p, aio); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_pipe.c b/src/platform/posix/posix_pipe.c index c86022ef..78415d26 100644 --- a/src/platform/posix/posix_pipe.c +++ b/src/platform/posix/posix_pipe.c @@ -21,14 +21,14 @@ // Linux eventfd. This is lighter weight than pipes, and has better semantics // to boot. This is far better than say epoll(). -#include #include +#include #include #ifdef EFD_CLOEXEC -#define NNI_EVENTFD_FLAGS EFD_CLOEXEC +#define NNI_EVENTFD_FLAGS EFD_CLOEXEC #else -#define NNI_EVENTFD_FLAGS 0 +#define NNI_EVENTFD_FLAGS 0 #endif int @@ -46,25 +46,22 @@ nni_plat_pipe_open(int *wfd, int *rfd) return (0); } - void nni_plat_pipe_raise(int wfd) { uint64_t one = 1; - (void) write(wfd, &one, sizeof (one)); + (void) write(wfd, &one, sizeof(one)); } - void nni_plat_pipe_clear(int rfd) { uint64_t val; - (void) read(rfd, &val, sizeof (val)); + (void) read(rfd, &val, sizeof(val)); } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -72,11 +69,10 @@ nni_plat_pipe_close(int wfd, int rfd) (void) close(wfd); } - #else // NNG_USE_EVENTFD -#include #include +#include int nni_plat_pipe_open(int *wfd, int *rfd) @@ -97,7 +93,6 @@ nni_plat_pipe_open(int *wfd, int *rfd) return (0); } - void nni_plat_pipe_raise(int wfd) { @@ -106,23 +101,21 @@ nni_plat_pipe_raise(int wfd) (void) write(wfd, &c, 1); } - void nni_plat_pipe_clear(int rfd) { char buf[32]; - int rv; + int rv; for (;;) { // Completely drain the pipe, but don't wait. This coalesces // events somewhat. - if (read(rfd, buf, sizeof (buf)) <= 0) { + if (read(rfd, buf, sizeof(buf)) <= 0) { return; } } } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -130,7 +123,6 @@ nni_plat_pipe_close(int wfd, int rfd) close(rfd); } - #endif // NNG_USE_EVENTFD #else diff --git a/src/platform/posix/posix_pipedesc.c b/src/platform/posix/posix_pipedesc.c index c509643e..faab91ad 100644 --- a/src/platform/posix/posix_pipedesc.c +++ b/src/platform/posix/posix_pipedesc.c @@ -14,29 +14,27 @@ #include "platform/posix/posix_pollq.h" #include +#include +#include #include #include -#include #include +#include #include -#include #include -#include - // nni_posix_pipedesc is a descriptor kept one per transport pipe (i.e. open // file descriptor for TCP socket, etc.) This contains the list of pending // aios for that underlying socket, as well as the socket itself. struct nni_posix_pipedesc { - nni_posix_pollq * pq; - int fd; - nni_list readq; - nni_list writeq; - nni_posix_pollq_node node; - nni_mtx mtx; + nni_posix_pollq * pq; + int fd; + nni_list readq; + nni_list writeq; + nni_posix_pollq_node node; + nni_mtx mtx; }; - static void nni_posix_pipedesc_finish(nni_aio *aio, int rv) { @@ -44,24 +42,23 @@ nni_posix_pipedesc_finish(nni_aio *aio, int rv) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd) { - int n; - int rv; - int i; - struct iovec iovec[4]; + int n; + int rv; + int i; + struct iovec iovec[4]; struct iovec *iovp; - nni_aio *aio; + nni_aio * aio; while ((aio = nni_list_first(&pd->writeq)) != NULL) { for (i = 0; i < aio->a_niov; i++) { - iovec[i].iov_len = aio->a_iov[i].iov_len; + iovec[i].iov_len = aio->a_iov[i].iov_len; iovec[i].iov_base = aio->a_iov[i].iov_buf; } iovp = &iovec[0]; - rv = 0; + rv = 0; n = writev(pd->fd, iovp, aio->a_niov); if (n < 0) { @@ -92,7 +89,7 @@ nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd) // remaininng ones up, and decrement count handled. n -= aio->a_iov[0].iov_len; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } NNI_ASSERT(aio->a_niov > 0); aio->a_niov--; @@ -106,24 +103,23 @@ nni_posix_pipedesc_dowrite(nni_posix_pipedesc *pd) } } - static void nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) { - int n; - int rv; - int i; - struct iovec iovec[4]; + int n; + int rv; + int i; + struct iovec iovec[4]; struct iovec *iovp; - nni_aio *aio; + nni_aio * aio; while ((aio = nni_list_first(&pd->readq)) != NULL) { for (i = 0; i < aio->a_niov; i++) { - iovec[i].iov_len = aio->a_iov[i].iov_len; + iovec[i].iov_len = aio->a_iov[i].iov_len; iovec[i].iov_base = aio->a_iov[i].iov_buf; } iovp = &iovec[0]; - rv = 0; + rv = 0; n = readv(pd->fd, iovp, aio->a_niov); if (n < 0) { @@ -160,7 +156,7 @@ nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) // remaininng ones up, and decrement count handled. n -= aio->a_iov[0].iov_len; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } NNI_ASSERT(aio->a_niov > 0); aio->a_niov--; @@ -174,7 +170,6 @@ nni_posix_pipedesc_doread(nni_posix_pipedesc *pd) } } - static void nni_posix_pipedesc_doclose(nni_posix_pipedesc *pd) { @@ -194,7 +189,6 @@ nni_posix_pipedesc_doclose(nni_posix_pipedesc *pd) } } - static void nni_posix_pipedesc_cb(void *arg) { @@ -207,12 +201,12 @@ nni_posix_pipedesc_cb(void *arg) if (pd->node.revents & POLLOUT) { nni_posix_pipedesc_dowrite(pd); } - if (pd->node.revents & (POLLHUP|POLLERR|POLLNVAL)) { + if (pd->node.revents & (POLLHUP | POLLERR | POLLNVAL)) { nni_posix_pipedesc_doclose(pd); } pd->node.revents = 0; - pd->node.events = 0; + pd->node.events = 0; if (!nni_list_empty(&pd->writeq)) { pd->node.events |= POLLOUT; @@ -228,7 +222,6 @@ nni_posix_pipedesc_cb(void *arg) nni_mtx_unlock(&pd->mtx); } - void nni_posix_pipedesc_close(nni_posix_pipedesc *pd) { @@ -239,7 +232,6 @@ nni_posix_pipedesc_close(nni_posix_pipedesc *pd) nni_mtx_unlock(&pd->mtx); } - static void nni_posix_pipedesc_cancel(nni_aio *aio) { @@ -250,7 +242,6 @@ nni_posix_pipedesc_cancel(nni_aio *aio) nni_mtx_unlock(&pd->mtx); } - void nni_posix_pipedesc_recv(nni_posix_pipedesc *pd, nni_aio *aio) { @@ -280,7 +271,6 @@ nni_posix_pipedesc_recv(nni_posix_pipedesc *pd, nni_aio *aio) nni_mtx_unlock(&pd->mtx); } - void nni_posix_pipedesc_send(nni_posix_pipedesc *pd, nni_aio *aio) { @@ -310,17 +300,16 @@ nni_posix_pipedesc_send(nni_posix_pipedesc *pd, nni_aio *aio) nni_mtx_unlock(&pd->mtx); } - int nni_posix_pipedesc_init(nni_posix_pipedesc **pdp, int fd) { nni_posix_pipedesc *pd; - int rv; + int rv; if ((pd = NNI_ALLOC_STRUCT(pd)) == NULL) { return (NNG_ENOMEM); } - memset(pd, 0, sizeof (*pd)); + memset(pd, 0, sizeof(*pd)); // We could randomly choose a different pollq, or for efficiencies // sake we could take a modulo of the file desc number to choose @@ -331,10 +320,10 @@ nni_posix_pipedesc_init(nni_posix_pipedesc **pdp, int fd) NNI_FREE_STRUCT(pd); return (rv); } - pd->pq = nni_posix_pollq_get(fd); - pd->fd = fd; - pd->node.fd = fd; - pd->node.cb = nni_posix_pipedesc_cb; + pd->pq = nni_posix_pollq_get(fd); + pd->fd = fd; + pd->node.fd = fd; + pd->node.cb = nni_posix_pipedesc_cb; pd->node.data = pd; (void) fcntl(pd->fd, F_SETFL, O_NONBLOCK); @@ -346,7 +335,6 @@ nni_posix_pipedesc_init(nni_posix_pipedesc **pdp, int fd) return (0); } - void nni_posix_pipedesc_fini(nni_posix_pipedesc *pd) { @@ -358,7 +346,6 @@ nni_posix_pipedesc_fini(nni_posix_pipedesc *pd) NNI_FREE_STRUCT(pd); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_pollq.h b/src/platform/posix/posix_pollq.h index 224bf765..e42b120d 100644 --- a/src/platform/posix/posix_pollq.h +++ b/src/platform/posix/posix_pollq.h @@ -21,27 +21,27 @@ #include "core/nng_impl.h" #include -typedef struct nni_posix_pollq_node nni_posix_pollq_node; -typedef struct nni_posix_pollq nni_posix_pollq; +typedef struct nni_posix_pollq_node nni_posix_pollq_node; +typedef struct nni_posix_pollq nni_posix_pollq; struct nni_posix_pollq_node { - nni_posix_pollq * pq; // associated pollq - nni_list_node node; // linkage into the pollq list - int index; // used by the poller impl - int armed; // used by the poller impl - int fd; // file descriptor to poll - int events; // events to watch for - int revents; // events received - void * data; // user data - nni_cb cb; // user callback on event + nni_posix_pollq *pq; // associated pollq + nni_list_node node; // linkage into the pollq list + int index; // used by the poller impl + int armed; // used by the poller impl + int fd; // file descriptor to poll + int events; // events to watch for + int revents; // events received + void * data; // user data + nni_cb cb; // user callback on event }; extern nni_posix_pollq *nni_posix_pollq_get(int); -extern int nni_posix_pollq_submit(nni_posix_pollq *, nni_posix_pollq_node *); +extern int nni_posix_pollq_submit(nni_posix_pollq *, nni_posix_pollq_node *); extern void nni_posix_pollq_cancel(nni_posix_pollq *, nni_posix_pollq_node *); -extern int nni_posix_pollq_sysinit(void); +extern int nni_posix_pollq_sysinit(void); extern void nni_posix_pollq_sysfini(void); -#endif // PLATFORM_POSIX +#endif // PLATFORM_POSIX -#endif // PLATFORM_POSIX_POLLQ_H +#endif // PLATFORM_POSIX_POLLQ_H diff --git a/src/platform/posix/posix_pollq_poll.c b/src/platform/posix/posix_pollq_poll.c index cd2eea7b..fe1359ec 100644 --- a/src/platform/posix/posix_pollq_poll.c +++ b/src/platform/posix/posix_pollq_poll.c @@ -13,15 +13,14 @@ #ifdef NNG_USE_POSIX_POLLQ_POLL #include +#include +#include #include #include -#include #include +#include #include -#include #include -#include - // POSIX AIO using poll(). We use a single poll thread to perform // I/O operations for the entire system. This isn't entirely scalable, @@ -33,33 +32,31 @@ // nni_posix_pollq is a work structure used by the poller thread, that keeps // track of all the underlying pipe handles and so forth being used by poll(). struct nni_posix_pollq { - nni_mtx mtx; - nni_cv cv; - struct pollfd * fds; - struct pollfd * newfds; - int nfds; - int nnewfds; - int wakewfd; // write side of waker pipe - int wakerfd; // read side of waker pipe - int close; // request for worker to exit - int started; - nni_thr thr; // worker thread - nni_list nodes; // poll list - nni_list notify; // notify list - int nnodes; // num of nodes in nodes list - int cancel; // waiters for cancellation - int inpoll; // poller asleep in poll - - nni_posix_pollq_node * active; // active node (in callback) + nni_mtx mtx; + nni_cv cv; + struct pollfd *fds; + struct pollfd *newfds; + int nfds; + int nnewfds; + int wakewfd; // write side of waker pipe + int wakerfd; // read side of waker pipe + int close; // request for worker to exit + int started; + nni_thr thr; // worker thread + nni_list nodes; // poll list + nni_list notify; // notify list + int nnodes; // num of nodes in nodes list + int cancel; // waiters for cancellation + int inpoll; // poller asleep in poll + + nni_posix_pollq_node *active; // active node (in callback) }; - - static int nni_posix_pollq_poll_grow(nni_posix_pollq *pq) { - int grow = pq->nnodes + 2; // one for us, one for waker - int noldfds; + int grow = pq->nnodes + 2; // one for us, one for waker + int noldfds; struct pollfd *oldfds; struct pollfd *newfds; @@ -72,34 +69,32 @@ nni_posix_pollq_poll_grow(nni_posix_pollq *pq) // Maybe we are adding a *lot* of pipes at once, and have to grow // multiple times before the poller gets scheduled. In that case // toss the old array before we finish. - oldfds = pq->newfds; + oldfds = pq->newfds; noldfds = pq->nnewfds; - if ((newfds = nni_alloc(grow * sizeof (struct pollfd))) == NULL) { + if ((newfds = nni_alloc(grow * sizeof(struct pollfd))) == NULL) { return (NNG_ENOMEM); } - - pq->newfds = newfds; + pq->newfds = newfds; pq->nnewfds = grow; if (noldfds != 0) { - nni_free(oldfds, noldfds * sizeof (struct pollfd)); + nni_free(oldfds, noldfds * sizeof(struct pollfd)); } return (0); } - static void nni_posix_poll_thr(void *arg) { - nni_posix_pollq *pollq = arg; + nni_posix_pollq * pollq = arg; nni_posix_pollq_node *node, *nextnode; nni_mtx_lock(&pollq->mtx); for (;;) { - int rv; - int nfds; + int rv; + int nfds; struct pollfd *fds; if (pollq->close) { @@ -109,28 +104,28 @@ nni_posix_poll_thr(void *arg) if (pollq->newfds != NULL) { // We have "grown" by the caller. Free up the old // space, and start using the new. - nni_free(pollq->fds, - pollq->nfds * sizeof (struct pollfd)); - pollq->fds = pollq->newfds; - pollq->nfds = pollq->nnewfds; + nni_free( + pollq->fds, pollq->nfds * sizeof(struct pollfd)); + pollq->fds = pollq->newfds; + pollq->nfds = pollq->nnewfds; pollq->newfds = NULL; } - fds = pollq->fds; + fds = pollq->fds; nfds = 0; // The waker pipe is set up so that we will be woken // when it is written (this allows us to be signaled). - fds[nfds].fd = pollq->wakerfd; - fds[nfds].events = POLLIN; + fds[nfds].fd = pollq->wakerfd; + fds[nfds].events = POLLIN; fds[nfds].revents = 0; nfds++; // Set up the poll list. NNI_LIST_FOREACH (&pollq->nodes, node) { - fds[nfds].fd = node->fd; - fds[nfds].events = node->armed; + fds[nfds].fd = node->fd; + fds[nfds].events = node->armed; fds[nfds].revents = 0; - node->index = nfds; + node->index = nfds; nfds++; } @@ -150,7 +145,6 @@ nni_posix_poll_thr(void *arg) continue; } - // If the waker pipe was signaled, read from it. if (fds[0].revents & POLLIN) { NNI_ASSERT(fds[0].fd == pollq->wakerfd); @@ -183,7 +177,7 @@ nni_posix_poll_thr(void *arg) } // Clear the index for the next time around. - node->index = 0; + node->index = 0; node->revents = fds[index].revents; // Now we move this node to the callback list. @@ -216,7 +210,6 @@ nni_posix_poll_thr(void *arg) nni_mtx_unlock(&pollq->mtx); } - void nni_posix_pollq_cancel(nni_posix_pollq *pq, nni_posix_pollq_node *node) { @@ -238,7 +231,6 @@ nni_posix_pollq_cancel(nni_posix_pollq *pq, nni_posix_pollq_node *node) nni_mtx_unlock(&pq->mtx); } - int nni_posix_pollq_submit(nni_posix_pollq *pq, nni_posix_pollq_node *node) { @@ -278,13 +270,12 @@ nni_posix_pollq_submit(nni_posix_pollq *pq, nni_posix_pollq_node *node) return (0); } - static void nni_posix_pollq_fini(nni_posix_pollq *pq) { if (pq->started) { nni_mtx_lock(&pq->mtx); - pq->close = 1; + pq->close = 1; pq->started = 0; nni_plat_pipe_raise(pq->wakewfd); @@ -298,12 +289,11 @@ nni_posix_pollq_fini(nni_posix_pollq *pq) nni_plat_pipe_close(pq->wakewfd, pq->wakerfd); pq->wakewfd = pq->wakerfd = -1; } - nni_free(pq->newfds, pq->nnewfds * sizeof (struct pollfd)); - nni_free(pq->fds, pq->nfds * sizeof (struct pollfd)); + nni_free(pq->newfds, pq->nnewfds * sizeof(struct pollfd)); + nni_free(pq->fds, pq->nfds * sizeof(struct pollfd)); nni_mtx_fini(&pq->mtx); } - static int nni_posix_pollq_init(nni_posix_pollq *pq) { @@ -313,7 +303,7 @@ nni_posix_pollq_init(nni_posix_pollq *pq) NNI_LIST_INIT(&pq->notify, nni_posix_pollq_node, node); pq->wakewfd = -1; pq->wakerfd = -1; - pq->close = 0; + pq->close = 0; if (((rv = nni_mtx_init(&pq->mtx)) != 0) || ((rv = nni_cv_init(&pq->cv, &pq->mtx)) != 0) || @@ -328,7 +318,6 @@ nni_posix_pollq_init(nni_posix_pollq *pq) return (0); } - // We use a single pollq for the entire system, which means only a single // thread is polling. This may be somewhat less than optimally efficient, // and it may be worth investigating having multiple threads to improve @@ -344,7 +333,6 @@ nni_posix_pollq_get(int fd) return (&nni_posix_global_pollq); } - int nni_posix_pollq_sysinit(void) { @@ -354,14 +342,12 @@ nni_posix_pollq_sysinit(void) return (rv); } - void nni_posix_pollq_sysfini(void) { nni_posix_pollq_fini(&nni_posix_global_pollq); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_rand.c b/src/platform/posix/posix_rand.c index 11d166bd..4322e491 100644 --- a/src/platform/posix/posix_rand.c +++ b/src/platform/posix/posix_rand.c @@ -12,14 +12,14 @@ #ifdef PLATFORM_POSIX_RANDOM -#include #include +#include #include +#include #include -#include #include -#include -#include +#include +#include #ifdef NNG_USE_GETRANDOM #include @@ -34,17 +34,17 @@ // really really you want to have more data than this available, especially // for cryptographic applications. struct nni_plat_prng_x { - nni_time now; - pid_t pid; - uid_t uid; - struct utsname uts; + nni_time now; + pid_t pid; + uid_t uid; + struct utsname uts; }; void nni_plat_seed_prng(void *buf, size_t bufsz) { struct nni_plat_prng_x x; - int i; + int i; memset(buf, 0, bufsz); @@ -75,21 +75,20 @@ nni_plat_seed_prng(void *buf, size_t bufsz) // limited of systems, we have at least *some* level of randomness. // The mixing is done in a way to avoid diminishing entropy we may // have already collected. - memset(&x, 0, sizeof (x)); // satisfy valgrind + memset(&x, 0, sizeof(x)); // satisfy valgrind x.now = nni_clock(); x.pid = getpid(); x.uid = getuid(); uname(&x.uts); - for (i = 0; (i < bufsz) && (i < sizeof (x)); i++) { + for (i = 0; (i < bufsz) && (i < sizeof(x)); i++) { ((uint8_t *) buf)[i] ^= ((uint8_t *) &x)[i]; } } - #else // Suppress empty symbols warnings in ranlib. int nni_posix_rand_not_used = 0; -#endif // PLATFORM_POSIX_RANDOM +#endif // PLATFORM_POSIX_RANDOM diff --git a/src/platform/posix/posix_resolv_gai.c b/src/platform/posix/posix_resolv_gai.c index 4283e9f6..cc27b213 100644 --- a/src/platform/posix/posix_resolv_gai.c +++ b/src/platform/posix/posix_resolv_gai.c @@ -7,24 +7,22 @@ // found online at https://opensource.org/licenses/MIT. // - #include "core/nng_impl.h" #ifdef NNG_USE_POSIX_RESOLV_GAI #include "platform/posix/posix_aio.h" #include +#include +#include #include #include #include -#include #include +#include #include #include #include -#include -#include - // We use a single resolver taskq - but we allocate a few threads // for it to ensure that names can be looked up concurrently. This isn't @@ -35,25 +33,23 @@ // thread-safe getaddrinfo(). In that case they should set this to 1. #ifndef NNG_POSIX_RESOLV_CONCURRENCY -#define NNG_POSIX_RESOLV_CONCURRENCY 4 +#define NNG_POSIX_RESOLV_CONCURRENCY 4 #endif - static nni_taskq *nni_posix_resolv_tq = NULL; -static nni_mtx nni_posix_resolv_mtx; +static nni_mtx nni_posix_resolv_mtx; -typedef struct nni_posix_resolv_item nni_posix_resolv_item; +typedef struct nni_posix_resolv_item nni_posix_resolv_item; struct nni_posix_resolv_item { - int family; - int passive; - const char * name; - const char * serv; - int proto; - nni_aio * aio; - nni_taskq_ent tqe; + int family; + int passive; + const char * name; + const char * serv; + int proto; + nni_aio * aio; + nni_taskq_ent tqe; }; - static void nni_posix_resolv_finish(nni_posix_resolv_item *item, int rv) { @@ -64,7 +60,6 @@ nni_posix_resolv_finish(nni_posix_resolv_item *item, int rv) NNI_FREE_STRUCT(item); } - static void nni_posix_resolv_cancel(nni_aio *aio) { @@ -81,7 +76,6 @@ nni_posix_resolv_cancel(nni_aio *aio) NNI_FREE_STRUCT(item); } - static int nni_posix_gai_errno(int rv) { @@ -110,16 +104,15 @@ nni_posix_gai_errno(int rv) } } - static void nni_posix_resolv_task(void *arg) { nni_posix_resolv_item *item = arg; - nni_aio *aio = item->aio; - struct addrinfo hints; - struct addrinfo *results; - struct addrinfo *probe; - int i, rv; + nni_aio * aio = item->aio; + struct addrinfo hints; + struct addrinfo * results; + struct addrinfo * probe; + int i, rv; results = NULL; @@ -129,7 +122,7 @@ nni_posix_resolv_task(void *arg) case AF_UNSPEC: // We treat these all as IP addresses. The service and the // host part are split. - memset(&hints, 0, sizeof (hints)); + memset(&hints, 0, sizeof(hints)); if (item->passive) { hints.ai_flags |= AI_PASSIVE; } @@ -137,11 +130,11 @@ nni_posix_resolv_task(void *arg) hints.ai_flags |= AI_ADDRCONFIG; #endif hints.ai_protocol = item->proto; - hints.ai_family = item->family; + hints.ai_family = item->family; if (item->family == AF_INET6) { - // We prefer to have v4mapped addresses if a remote - // v4 address isn't avaiable. And we prefer to only - // do this if we actually support v6. +// We prefer to have v4mapped addresses if a remote +// v4 address isn't avaiable. And we prefer to only +// do this if we actually support v6. #if defined(AI_V4MAPPED_CFG) hints.ai_flags |= AI_V4MAPPED_CFG; #elif defined(AI_V4MAPPED) @@ -174,27 +167,27 @@ nni_posix_resolv_task(void *arg) aio->a_addrs = NNI_ALLOC_STRUCTS(aio->a_addrs, aio->a_naddrs); if (aio->a_addrs == NULL) { aio->a_naddrs = 0; - rv = NNG_ENOMEM; + rv = NNG_ENOMEM; break; } i = 0; for (probe = results; probe != NULL; probe = probe->ai_next) { - struct sockaddr_in *sin; + struct sockaddr_in * sin; struct sockaddr_in6 *sin6; - nng_sockaddr *sa = &aio->a_addrs[i]; + nng_sockaddr * sa = &aio->a_addrs[i]; switch (probe->ai_addr->sa_family) { case AF_INET: sin = (void *) probe->ai_addr; sa->s_un.s_in.sa_family = NNG_AF_INET; - sa->s_un.s_in.sa_port = sin->sin_port; - sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; + sa->s_un.s_in.sa_port = sin->sin_port; + sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; i++; break; case AF_INET6: sin6 = (void *) probe->ai_addr; sa->s_un.s_in6.sa_family = NNG_AF_INET6; - sa->s_un.s_in6.sa_port = sin6->sin6_port; + sa->s_un.s_in6.sa_port = sin6->sin6_port; memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16); i++; @@ -222,13 +215,12 @@ nni_posix_resolv_task(void *arg) nni_mtx_unlock(&nni_posix_resolv_mtx); } - static void nni_posix_resolv_ip(const char *host, const char *serv, int passive, int family, int proto, nni_aio *aio) { nni_posix_resolv_item *item; - int rv; + int rv; if ((aio->a_naddrs != 0) && (aio->a_addrs != NULL)) { NNI_FREE_STRUCTS(aio->a_addrs, aio->a_naddrs); @@ -253,10 +245,10 @@ nni_posix_resolv_ip(const char *host, const char *serv, int passive, } // NB: host and serv must remain valid until this is completed. item->passive = passive; - item->name = host; - item->serv = serv; - item->proto = proto; - item->aio = aio; + item->name = host; + item->serv = serv; + item->proto = proto; + item->aio = aio; nni_mtx_lock(&nni_posix_resolv_mtx); // If we were stopped, we're done... @@ -273,15 +265,13 @@ nni_posix_resolv_ip(const char *host, const char *serv, int passive, nni_mtx_unlock(&nni_posix_resolv_mtx); } - void -nni_plat_tcp_resolv(const char *host, const char *serv, int family, - int passive, nni_aio *aio) +nni_plat_tcp_resolv( + const char *host, const char *serv, int family, int passive, nni_aio *aio) { nni_posix_resolv_ip(host, serv, passive, family, IPPROTO_TCP, aio); } - int nni_posix_resolv_sysinit(void) { @@ -297,7 +287,6 @@ nni_posix_resolv_sysinit(void) return (0); } - void nni_posix_resolv_sysfini(void) { @@ -308,7 +297,6 @@ nni_posix_resolv_sysfini(void) nni_mtx_fini(&nni_posix_resolv_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index a2f24f8a..45bbfcd3 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -14,21 +14,21 @@ #ifdef PLATFORM_POSIX_THREAD #include +#include #include -#include -#include +#include #include +#include #include +#include #include -#include -#include -static pthread_mutex_t nni_plat_lock = PTHREAD_MUTEX_INITIALIZER; -static int nni_plat_inited = 0; -static int nni_plat_forked = 0; +static pthread_mutex_t nni_plat_lock = PTHREAD_MUTEX_INITIALIZER; +static int nni_plat_inited = 0; +static int nni_plat_forked = 0; -pthread_condattr_t nni_cvattr; -pthread_mutexattr_t nni_mxattr; +pthread_condattr_t nni_cvattr; +pthread_mutexattr_t nni_mxattr; static pthread_attr_t nni_pthread_attr; // We open a /dev/null file descriptor so that we can dup2() it to @@ -55,7 +55,6 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) return (0); } - void nni_plat_mtx_fini(nni_plat_mtx *mtx) { @@ -72,7 +71,6 @@ nni_plat_mtx_fini(nni_plat_mtx *mtx) mtx->init = 0; } - void nni_plat_mtx_lock(nni_plat_mtx *mtx) { @@ -84,7 +82,6 @@ nni_plat_mtx_lock(nni_plat_mtx *mtx) mtx->owner = pthread_self(); } - void nni_plat_mtx_unlock(nni_plat_mtx *mtx) { @@ -97,7 +94,6 @@ nni_plat_mtx_unlock(nni_plat_mtx *mtx) } } - int nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) { @@ -117,7 +113,6 @@ nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) return (0); } - void nni_plat_cv_wake(nni_plat_cv *cv) { @@ -128,7 +123,6 @@ nni_plat_cv_wake(nni_plat_cv *cv) } } - void nni_plat_cv_wait(nni_plat_cv *cv) { @@ -141,20 +135,19 @@ nni_plat_cv_wait(nni_plat_cv *cv) cv->mtx->owner = pthread_self(); } - int nni_plat_cv_until(nni_plat_cv *cv, nni_time until) { struct timespec ts; - int rv; + int rv; NNI_ASSERT(cv->mtx->owner == pthread_self()); // Our caller has already guaranteed a sane value for until. - ts.tv_sec = until / 1000000; + ts.tv_sec = until / 1000000; ts.tv_nsec = (until % 1000000) * 1000; - rv = pthread_cond_timedwait(&cv->cv, &cv->mtx->mtx, &ts); + rv = pthread_cond_timedwait(&cv->cv, &cv->mtx->mtx, &ts); cv->mtx->owner = pthread_self(); if (rv == ETIMEDOUT) { return (NNG_ETIMEDOUT); @@ -164,7 +157,6 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until) return (0); } - void nni_plat_cv_fini(nni_plat_cv *cv) { @@ -179,12 +171,11 @@ nni_plat_cv_fini(nni_plat_cv *cv) cv->mtx = NULL; } - static void * nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; - sigset_t set; + sigset_t set; // Suppress (block) SIGPIPE for this thread. sigemptyset(&set); @@ -195,26 +186,24 @@ nni_plat_thr_main(void *arg) return (NULL); } - int nni_plat_thr_init(nni_plat_thr *thr, void (*fn)(void *), void *arg) { int rv; thr->func = fn; - thr->arg = arg; + thr->arg = arg; // POSIX wants functions to return a void *, but we don't care. - rv = pthread_create(&thr->tid, &nni_pthread_attr, - nni_plat_thr_main, thr); + rv = pthread_create( + &thr->tid, &nni_pthread_attr, nni_plat_thr_main, thr); if (rv != 0) { - //nni_printf("pthread_create: %s", strerror(rv)); + // nni_printf("pthread_create: %s", strerror(rv)); return (NNG_ENOMEM); } return (0); } - void nni_plat_thr_fini(nni_plat_thr *thr) { @@ -225,14 +214,12 @@ nni_plat_thr_fini(nni_plat_thr *thr) } } - void nni_atfork_child(void) { nni_plat_forked = 1; } - int nni_plat_init(int (*helper)(void)) { @@ -243,14 +230,14 @@ nni_plat_init(int (*helper)(void)) nni_panic("nng is not fork-reentrant safe"); } if (nni_plat_inited) { - return (0); // fast path + return (0); // fast path } if ((devnull = open("/dev/null", O_RDONLY)) < 0) { return (nni_plat_errno(errno)); } pthread_mutex_lock(&nni_plat_lock); - if (nni_plat_inited) { // check again under the lock to be sure + if (nni_plat_inited) { // check again under the lock to be sure pthread_mutex_unlock(&nni_plat_lock); close(devnull); return (0); @@ -337,7 +324,6 @@ nni_plat_init(int (*helper)(void)) return (rv); } - void nni_plat_fini(void) { @@ -349,12 +335,11 @@ nni_plat_fini(void) pthread_attr_destroy(&nni_pthread_attr); (void) close(nni_plat_devnull); nni_plat_devnull = -1; - nni_plat_inited = 0; + nni_plat_inited = 0; } pthread_mutex_unlock(&nni_plat_lock); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_clock.c b/src/platform/windows/win_clock.c index 9064b5e2..5a874a71 100644 --- a/src/platform/windows/win_clock.c +++ b/src/platform/windows/win_clock.c @@ -15,16 +15,14 @@ nni_time nni_plat_clock(void) { // We are limited by the system clock, but that is ok. - return (GetTickCount64()*1000); + return (GetTickCount64() * 1000); } - void nni_plat_usleep(nni_duration dur) { uint64_t exp; - // Convert duration to msec, rounding up. dur += 999; dur /= 1000; @@ -43,10 +41,9 @@ nni_plat_usleep(nni_duration dur) } } - #else // Suppress empty symbols warnings in ranlib. int nni_win_clock_not_used = 0; -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS diff --git a/src/platform/windows/win_debug.c b/src/platform/windows/win_debug.c index 43db0505..bc3d5bfa 100644 --- a/src/platform/windows/win_debug.c +++ b/src/platform/windows/win_debug.c @@ -12,8 +12,8 @@ #ifdef PLATFORM_WINDOWS #include -#include #include +#include #include void @@ -22,14 +22,12 @@ nni_plat_abort(void) abort(); } - void nni_plat_println(const char *message) { (void) fprintf(stderr, "%s\n", message); } - const char * nni_plat_strerror(int errnum) { @@ -39,25 +37,16 @@ nni_plat_strerror(int errnum) return (strerror(errnum)); } - // Win32 has its own error codes, but these ones it shares with POSIX. static struct { - int sys_err; - int nng_err; -} -nni_plat_errnos[] = { - { ENOENT, NNG_ENOENT }, - { EINTR, NNG_EINTR }, - { EINVAL, NNG_EINVAL }, - { ENOMEM, NNG_ENOMEM }, - { EACCES, NNG_EPERM }, - { EAGAIN, NNG_EAGAIN }, - { EBADF, NNG_ECLOSED }, - { EBUSY, NNG_EBUSY }, - { ENAMETOOLONG, NNG_EINVAL }, - { EPERM, NNG_EPERM }, - { EPIPE, NNG_ECLOSED }, - { 0, 0 } // must be last + int sys_err; + int nng_err; +} nni_plat_errnos[] = { + { ENOENT, NNG_ENOENT }, { EINTR, NNG_EINTR }, { EINVAL, NNG_EINVAL }, + { ENOMEM, NNG_ENOMEM }, { EACCES, NNG_EPERM }, { EAGAIN, NNG_EAGAIN }, + { EBADF, NNG_ECLOSED }, { EBUSY, NNG_EBUSY }, + { ENAMETOOLONG, NNG_EINVAL }, { EPERM, NNG_EPERM }, + { EPIPE, NNG_ECLOSED }, { 0, 0 } // must be last }; int @@ -77,15 +66,14 @@ nni_plat_errno(int errnum) return (NNG_ESYSERR + errnum); } - // Windows has infinite numbers of error codes it seems. We only bother // with the ones that are relevant to us (we think). Note that there is // no overlap between errnos and GetLastError values. static struct { - int win_err; - int nng_err; -} -nni_win_errnos[] = { + int win_err; + int nng_err; +} nni_win_errnos[] = { + // clang-format off { ERROR_FILE_NOT_FOUND, NNG_ENOENT }, { ERROR_ACCESS_DENIED, NNG_EPERM }, { ERROR_INVALID_HANDLE, NNG_ECLOSED }, @@ -106,6 +94,7 @@ nni_win_errnos[] = { { WAIT_TIMEOUT, NNG_ETIMEDOUT }, // Must be Last!! { 0, 0 }, + // clang-format on }; // This converts a Windows API error (from GetLastError()) to an @@ -127,7 +116,6 @@ nni_win_error(int errnum) return (NNG_ESYSERR + errnum); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index 6ce38250..234acc35 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -16,10 +16,10 @@ #define WIN32_LEAN_AND_MEAN #endif -#include -#include #include #include +#include +#include #include #include "core/list.h" @@ -27,7 +27,7 @@ // These types are provided for here, to permit them to be directly inlined // elsewhere. -typedef struct nni_win_event nni_win_event; +typedef struct nni_win_event nni_win_event; // nni_win_event is used with io completion ports. This allows us to get // to a specific completion callback without requiring the poller (in the @@ -35,50 +35,50 @@ typedef struct nni_win_event nni_win_event; // this to pass back status and counts to the routine, which may not be // conveyed in the OVERLAPPED directly. struct nni_win_event { - OVERLAPPED olpd; - HANDLE h; - void * ptr; - nni_cb cb; - nni_list aios; + OVERLAPPED olpd; + HANDLE h; + void * ptr; + nni_cb cb; + nni_list aios; }; struct nni_plat_thr { - void (*func)(void *); - void * arg; - HANDLE handle; + void (*func)(void *); + void * arg; + HANDLE handle; }; struct nni_plat_mtx { SRWLOCK srl; - DWORD owner; - int init; + DWORD owner; + int init; }; struct nni_plat_cv { - CONDITION_VARIABLE cv; - PSRWLOCK srl; + CONDITION_VARIABLE cv; + PSRWLOCK srl; }; extern int nni_win_error(int); extern int nni_winsock_error(int); -extern int nni_win_event_init(nni_win_event *, nni_cb, void *, HANDLE); -extern void nni_win_event_fini(nni_win_event *); -extern int nni_win_event_reset(nni_win_event *); +extern int nni_win_event_init(nni_win_event *, nni_cb, void *, HANDLE); +extern void nni_win_event_fini(nni_win_event *); +extern int nni_win_event_reset(nni_win_event *); extern OVERLAPPED *nni_win_event_overlapped(nni_win_event *); -extern void nni_win_event_cancel(nni_win_event *); +extern void nni_win_event_cancel(nni_win_event *); extern int nni_win_iocp_register(HANDLE); -extern int nni_win_iocp_sysinit(void); +extern int nni_win_iocp_sysinit(void); extern void nni_win_iocp_sysfini(void); -extern int nni_win_ipc_sysinit(void); +extern int nni_win_ipc_sysinit(void); extern void nni_win_ipc_sysfini(void); -extern int nni_win_resolv_sysinit(void); +extern int nni_win_resolv_sysinit(void); extern void nni_win_resolv_sysfini(void); -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS -#endif // PLATFORM_WIN_IMPL_H +#endif // PLATFORM_WIN_IMPL_H diff --git a/src/platform/windows/win_iocp.c b/src/platform/windows/win_iocp.c index 381da64f..7244b360 100644 --- a/src/platform/windows/win_iocp.c +++ b/src/platform/windows/win_iocp.c @@ -11,7 +11,7 @@ #ifdef PLATFORM_WINDOWS -#define NNI_WIN_IOCP_NTHREADS 4 +#define NNI_WIN_IOCP_NTHREADS 4 #include // Windows IO Completion Port support. We basically creaet a single @@ -19,33 +19,33 @@ // to the port on an as needed basis. We use a single IO completion // port for pretty much everything. -static HANDLE nni_win_global_iocp = NULL; +static HANDLE nni_win_global_iocp = NULL; static nni_thr nni_win_iocp_thrs[NNI_WIN_IOCP_NTHREADS]; static nni_mtx nni_win_iocp_mtx; static void nni_win_iocp_handler(void *arg) { - HANDLE iocp; - DWORD cnt; - ULONG_PTR key; - OVERLAPPED *olpd; + HANDLE iocp; + DWORD cnt; + ULONG_PTR key; + OVERLAPPED * olpd; nni_win_event *evt; - int status; - BOOL rv; + int status; + BOOL rv; NNI_ARG_UNUSED(arg); iocp = nni_win_global_iocp; for (;;) { - key = 0; - olpd = NULL; + key = 0; + olpd = NULL; status = 0; - cnt = 0; + cnt = 0; - rv = GetQueuedCompletionStatus(iocp, &cnt, &key, &olpd, - INFINITE); + rv = GetQueuedCompletionStatus( + iocp, &cnt, &key, &olpd, INFINITE); if (rv == FALSE) { if (olpd == NULL) { @@ -62,7 +62,6 @@ nni_win_iocp_handler(void *arg) } } - int nni_win_iocp_register(HANDLE h) { @@ -72,23 +71,21 @@ nni_win_iocp_register(HANDLE h) return (0); } - int nni_win_event_init(nni_win_event *evt, nni_cb cb, void *ptr, HANDLE h) { - ZeroMemory(&evt->olpd, sizeof (evt->olpd)); + ZeroMemory(&evt->olpd, sizeof(evt->olpd)); evt->olpd.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (evt->olpd.hEvent == NULL) { return (nni_win_error(GetLastError())); } nni_aio_list_init(&evt->aios); evt->ptr = ptr; - evt->cb = cb; - evt->h = h; + evt->cb = cb; + evt->h = h; return (0); } - void nni_win_event_fini(nni_win_event *evt) { @@ -98,7 +95,6 @@ nni_win_event_fini(nni_win_event *evt) } } - int nni_win_event_reset(nni_win_event *evt) { @@ -108,18 +104,16 @@ nni_win_event_reset(nni_win_event *evt) return (0); } - OVERLAPPED * nni_win_event_overlapped(nni_win_event *evt) { return (&evt->olpd); } - void nni_win_event_cancel(nni_win_event *evt) { - int rv; + int rv; DWORD cnt; // Try to cancel the event... @@ -146,23 +140,22 @@ nni_win_event_cancel(nni_win_event *evt) GetOverlappedResult(evt->h, &evt->olpd, &cnt, TRUE); } - int nni_win_iocp_sysinit(void) { HANDLE h; - int i; - int rv; + int i; + int rv; - h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, - NNI_WIN_IOCP_NTHREADS); + h = CreateIoCompletionPort( + INVALID_HANDLE_VALUE, NULL, 0, NNI_WIN_IOCP_NTHREADS); if (h == NULL) { return (nni_win_error(GetLastError())); } nni_win_global_iocp = h; for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) { - rv = nni_thr_init(&nni_win_iocp_thrs[i], nni_win_iocp_handler, - NULL); + rv = nni_thr_init( + &nni_win_iocp_thrs[i], nni_win_iocp_handler, NULL); if (rv != 0) { goto fail; } @@ -186,11 +179,10 @@ fail: return (rv); } - void nni_win_iocp_sysfini(void) { - int i; + int i; HANDLE h; if ((h = nni_win_global_iocp) != NULL) { @@ -203,7 +195,6 @@ nni_win_iocp_sysfini(void) nni_mtx_fini(&nni_win_iocp_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_ipc.c b/src/platform/windows/win_ipc.c index f6601f93..237ded78 100644 --- a/src/platform/windows/win_ipc.c +++ b/src/platform/windows/win_ipc.c @@ -20,31 +20,30 @@ static void nni_win_ipc_send_start(nni_plat_ipc_pipe *); static void nni_win_ipc_recv_start(nni_plat_ipc_pipe *); struct nni_plat_ipc_pipe { - HANDLE p; - nni_win_event recv_evt; - nni_win_event send_evt; - nni_mtx mtx; - nni_list readq; - nni_list writeq; + HANDLE p; + nni_win_event recv_evt; + nni_win_event send_evt; + nni_mtx mtx; + nni_list readq; + nni_list writeq; }; struct nni_plat_ipc_ep { - char path[256]; - int mode; - int started; - nni_list aios; - HANDLE p; // accept side only - nni_win_event acc_evt; // accept side only - nni_mtx mtx; // accept side only - nni_list_node node; // conn side uses this + char path[256]; + int mode; + int started; + nni_list aios; + HANDLE p; // accept side only + nni_win_event acc_evt; // accept side only + nni_mtx mtx; // accept side only + nni_list_node node; // conn side uses this }; - static int nni_win_ipc_pipe_init(nni_plat_ipc_pipe **pipep, HANDLE p) { nni_plat_ipc_pipe *pipe; - int rv; + int rv; if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) { return (NNG_ENOMEM); @@ -71,7 +70,6 @@ nni_win_ipc_pipe_init(nni_plat_ipc_pipe **pipep, HANDLE p) return (0); } - static void nni_win_ipc_send_cancel(nni_aio *aio) { @@ -83,15 +81,14 @@ nni_win_ipc_send_cancel(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) { - nni_win_event *evt = &pipe->send_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - int rv = 0; - nni_aio *aio; - DWORD cnt; + nni_win_event *evt = &pipe->send_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + int rv = 0; + nni_aio * aio; + DWORD cnt; if (GetOverlappedResult(pipe->p, olpd, &cnt, TRUE) == FALSE) { rv = nni_win_error(GetLastError()); @@ -110,7 +107,7 @@ nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) if (aio->a_iov[0].iov_len == 0) { int i; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } aio->a_niov--; } @@ -127,16 +124,15 @@ nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_send_start(nni_plat_ipc_pipe *pipe) { - void *buf; - DWORD len; - int rv; - nni_win_event *evt = &pipe->send_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - nni_aio *aio = nni_list_first(&pipe->writeq); + void * buf; + DWORD len; + int rv; + nni_win_event *evt = &pipe->send_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + nni_aio * aio = nni_list_first(&pipe->writeq); NNI_ASSERT(aio != NULL); NNI_ASSERT(aio->a_niov > 0); @@ -157,8 +153,8 @@ nni_win_ipc_send_start(nni_plat_ipc_pipe *pipe) // scrambling the data anyway. Note that Windows named pipes do // not appear to support scatter/gather, so we have to process // each element in turn. - buf = aio->a_iov[0].iov_buf; - len = (DWORD) aio->a_iov[0].iov_len; + buf = aio->a_iov[0].iov_buf; + len = (DWORD) aio->a_iov[0].iov_len; olpd = nni_win_event_overlapped(evt); // We limit ourselves to writing 16MB at a time. Named Pipes @@ -190,7 +186,6 @@ fail: nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_send_cb(void *arg) { @@ -201,12 +196,11 @@ nni_win_ipc_send_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *pipe, nni_aio *aio) { nni_win_event *evt = &pipe->send_evt; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_start(aio, nni_win_ipc_send_cancel, pipe)) != 0) { @@ -229,7 +223,6 @@ nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *pipe, nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_recv_cancel(nni_aio *aio) { @@ -241,15 +234,14 @@ nni_win_ipc_recv_cancel(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) { - nni_win_event *evt = &pipe->recv_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - int rv = 0; - nni_aio *aio; - DWORD cnt; + nni_win_event *evt = &pipe->recv_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + int rv = 0; + nni_aio * aio; + DWORD cnt; if (GetOverlappedResult(pipe->p, olpd, &cnt, TRUE) == FALSE) { rv = nni_win_error(GetLastError()); @@ -268,7 +260,7 @@ nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) if (aio->a_iov[0].iov_len == 0) { int i; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } aio->a_niov--; } @@ -285,16 +277,15 @@ nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_recv_start(nni_plat_ipc_pipe *pipe) { - void *buf; - DWORD len; - int rv; - nni_win_event *evt = &pipe->recv_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - nni_aio *aio = nni_list_first(&pipe->readq); + void * buf; + DWORD len; + int rv; + nni_win_event *evt = &pipe->recv_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + nni_aio * aio = nni_list_first(&pipe->readq); NNI_ASSERT(aio != NULL); NNI_ASSERT(aio->a_niov > 0); @@ -315,8 +306,8 @@ nni_win_ipc_recv_start(nni_plat_ipc_pipe *pipe) // scrambling the data anyway. Note that Windows named pipes do // not appear to support scatter/gather, so we have to process // each element in turn. - buf = aio->a_iov[0].iov_buf; - len = (DWORD) aio->a_iov[0].iov_len; + buf = aio->a_iov[0].iov_buf; + len = (DWORD) aio->a_iov[0].iov_len; olpd = nni_win_event_overlapped(evt); // We limit ourselves to writing 16MB at a time. Named Pipes @@ -348,7 +339,6 @@ fail: nni_aio_finish(aio, rv, 0); } - static void nni_win_ipc_recv_cb(void *arg) { @@ -359,12 +349,11 @@ nni_win_ipc_recv_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *pipe, nni_aio *aio) { nni_win_event *evt = &pipe->send_evt; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_start(aio, nni_win_ipc_recv_cancel, pipe)) != 0) { @@ -386,7 +375,6 @@ nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *pipe, nni_aio *aio) nni_win_ipc_recv_start(pipe); } - void nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *pipe) { @@ -400,7 +388,6 @@ nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *pipe) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *pipe) { @@ -412,13 +399,12 @@ nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *pipe) NNI_FREE_STRUCT(pipe); } - int nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) { - const char *path; + const char * path; nni_plat_ipc_ep *ep; - int rv; + int rv; if (strncmp(url, "ipc://", strlen("ipc://")) != 0) { return (NNG_EADDRINVAL); @@ -427,7 +413,7 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NNG_ENOMEM); } - ZeroMemory(ep, sizeof (ep)); + ZeroMemory(ep, sizeof(ep)); if ((rv = nni_mtx_init(&ep->mtx)) != 0) { NNI_FREE_STRUCT(ep); return (rv); @@ -437,17 +423,16 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) NNI_LIST_NODE_INIT(&ep->node); nni_aio_list_init(&ep->aios); - (void) snprintf(ep->path, sizeof (ep->path), "\\\\.\\pipe\\%s", path); + (void) snprintf(ep->path, sizeof(ep->path), "\\\\.\\pipe\\%s", path); *epp = ep; return (0); } - int nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) { - int rv; + int rv; HANDLE p; nni_mtx_lock(&ep->mtx); @@ -463,12 +448,11 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) // We create the first named pipe, and we make sure that it is // properly ours. p = CreateNamedPipeA(ep->path, - PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | - FILE_FLAG_FIRST_PIPE_INSTANCE, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | - PIPE_REJECT_REMOTE_CLIENTS, - PIPE_UNLIMITED_INSTANCES, - 4096, 4096, 0, NULL); + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | + FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL); if (p == INVALID_HANDLE_VALUE) { if ((rv = GetLastError()) == ERROR_ACCESS_DENIED) { rv = NNG_EADDRINUSE; @@ -486,7 +470,7 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) goto failed; } - ep->p = p; + ep->p = p; ep->started = 1; nni_mtx_unlock(&ep->mtx); return (0); @@ -501,16 +485,15 @@ failed: return (rv); } - static void nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) { - nni_win_event *evt = &ep->acc_evt; - DWORD nbytes; - int rv; + nni_win_event * evt = &ep->acc_evt; + DWORD nbytes; + int rv; nni_plat_ipc_pipe *pipe; - nni_aio *aio; - HANDLE newp, oldp; + nni_aio * aio; + HANDLE newp, oldp; // Note: This should be called with the ep lock held, and only when // the ConnectNamedPipe has finished. @@ -541,17 +524,16 @@ nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) } newp = CreateNamedPipeA(ep->path, - PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | - PIPE_REJECT_REMOTE_CLIENTS, - PIPE_UNLIMITED_INSTANCES, - 4096, 4096, 0, NULL); + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL); if (newp == INVALID_HANDLE_VALUE) { rv = nni_win_error(GetLastError()); DisconnectNamedPipe(ep->p); return; } - oldp = ep->p; + oldp = ep->p; ep->p = newp; if ((rv = nni_win_ipc_pipe_init(&pipe, oldp)) != 0) { @@ -564,7 +546,6 @@ nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) nni_aio_finish(aio, 0, 0); } - static void nni_win_ipc_acc_cb(void *arg) { @@ -575,7 +556,6 @@ nni_win_ipc_acc_cb(void *arg) nni_mtx_unlock(&ep->mtx); } - static void nni_win_ipc_acc_cancel(nni_aio *aio) { @@ -587,11 +567,10 @@ nni_win_ipc_acc_cancel(nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - void nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) { - int rv; + int rv; nni_win_event *evt = &ep->acc_evt; nni_mtx_lock(&ep->mtx); @@ -628,34 +607,32 @@ nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - // So Windows IPC is a bit different on the client side. There is no // support for asynchronous connection, but we can fake it with a single // thread that runs to establish the connection. That thread will run // keep looping, sleeping for 10 ms between attempts. It performs non-blocking // attempts to connect. -typedef struct nni_win_ipc_conn_work nni_win_ipc_conn_work; +typedef struct nni_win_ipc_conn_work nni_win_ipc_conn_work; struct nni_win_ipc_conn_work { - nni_list waiters; - nni_list workers; - nni_mtx mtx; - nni_cv cv; - nni_thr thr; - int exit; + nni_list waiters; + nni_list workers; + nni_mtx mtx; + nni_cv cv; + nni_thr thr; + int exit; }; static nni_win_ipc_conn_work nni_win_ipc_connecter; - static void nni_win_ipc_conn_thr(void *arg) { nni_win_ipc_conn_work *w = arg; - nni_plat_ipc_ep *ep; - nni_plat_ipc_pipe *pipe; - nni_aio *aio; - HANDLE p; - int rv; + nni_plat_ipc_ep * ep; + nni_plat_ipc_pipe * pipe; + nni_aio * aio; + HANDLE p; + int rv; nni_mtx_lock(&w->mtx); for (;;) { @@ -674,10 +651,9 @@ nni_win_ipc_conn_thr(void *arg) continue; } nni_list_remove(&ep->aios, aio); - p = CreateFileA(ep->path, - GENERIC_READ | GENERIC_WRITE, - 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, - NULL); + p = CreateFileA(ep->path, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, + NULL); if (p == INVALID_HANDLE_VALUE) { switch ((rv = GetLastError())) { @@ -686,12 +662,12 @@ nni_win_ipc_conn_thr(void *arg) nni_list_prepend(&ep->aios, aio); break; case ERROR_FILE_NOT_FOUND: - nni_aio_finish(aio, NNG_ECONNREFUSED, - 0); + nni_aio_finish( + aio, NNG_ECONNREFUSED, 0); break; default: - nni_aio_finish(aio, nni_win_error(rv), - 0); + nni_aio_finish( + aio, nni_win_error(rv), 0); break; } } else { @@ -723,12 +699,11 @@ nni_win_ipc_conn_thr(void *arg) nni_mtx_unlock(&w->mtx); } - static void nni_win_ipc_conn_cancel(nni_aio *aio) { - nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; - nni_plat_ipc_ep *ep = aio->a_prov_data; + nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; + nni_plat_ipc_ep * ep = aio->a_prov_data; nni_mtx_lock(&w->mtx); nni_aio_list_remove(aio); @@ -738,7 +713,6 @@ nni_win_ipc_conn_cancel(nni_aio *aio) nni_mtx_unlock(&w->mtx); } - void nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) { @@ -762,7 +736,6 @@ nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) nni_mtx_unlock(&w->mtx); } - void nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) { @@ -776,12 +749,11 @@ nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) NNI_FREE_STRUCT(ep); } - void nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) { nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; - nni_aio *aio; + nni_aio * aio; switch (ep->mode) { case NNI_EP_MODE_DIAL: @@ -811,11 +783,10 @@ nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) } } - int nni_win_ipc_sysinit(void) { - int rv; + int rv; nni_win_ipc_conn_work *worker = &nni_win_ipc_connecter; NNI_LIST_INIT(&worker->workers, nni_plat_ipc_ep, node); @@ -835,7 +806,6 @@ nni_win_ipc_sysinit(void) return (0); } - void nni_win_ipc_sysfini(void) { @@ -850,7 +820,6 @@ nni_win_ipc_sysfini(void) nni_mtx_fini(&worker->mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_net.c b/src/platform/windows/win_net.c index 2a77d97f..19491d02 100644 --- a/src/platform/windows/win_net.c +++ b/src/platform/windows/win_net.c @@ -14,36 +14,33 @@ #include static LPFN_CONNECTEX nni_win_connectex; -static LPFN_ACCEPTEX nni_win_acceptex; - +static LPFN_ACCEPTEX nni_win_acceptex; struct nni_plat_tcp_pipe { - SOCKET s; - nni_win_event recv_evt; - nni_win_event send_evt; - WSAOVERLAPPED recv_olpd; - WSAOVERLAPPED send_olpd; + SOCKET s; + nni_win_event recv_evt; + nni_win_event send_evt; + WSAOVERLAPPED recv_olpd; + WSAOVERLAPPED send_olpd; }; - struct nni_plat_tcp_ep { - SOCKET s; - nni_win_event evt; - WSAOVERLAPPED olpd; + SOCKET s; + nni_win_event evt; + WSAOVERLAPPED olpd; // We have to lookup some function pointers using ioctls. Winsock, // gotta love it. - LPFN_CONNECTEX connectex; - LPFN_ACCEPTEX acceptex; + LPFN_CONNECTEX connectex; + LPFN_ACCEPTEX acceptex; }; - // Windows has infinite numbers of error codes it seems. static struct { - int wsa_err; - int nng_err; -} -nni_plat_wsa_errnos[] = { + int wsa_err; + int nng_err; +} nni_plat_wsa_errnos[] = { + // clang-format off { WSA_INVALID_HANDLE, NNG_ECLOSED }, { WSA_NOT_ENOUGH_MEMORY, NNG_ENOMEM }, { WSA_INVALID_PARAMETER, NNG_EINVAL }, @@ -106,6 +103,7 @@ nni_plat_wsa_errnos[] = { // Eliding all the QoS related errors. // Must be Last!! { 0, 0 }, + // clang-format on }; diff --git a/src/platform/windows/win_pipe.c b/src/platform/windows/win_pipe.c index 22886faa..ed6149c0 100644 --- a/src/platform/windows/win_pipe.c +++ b/src/platform/windows/win_pipe.c @@ -24,17 +24,17 @@ nni_plat_pipe_open(int *wfdp, int *rfdp) SOCKET wfd = INVALID_SOCKET; struct sockaddr_in addr; - socklen_t alen; - int one; - ULONG yes; - int rv; + socklen_t alen; + int one; + ULONG yes; + int rv; - ZeroMemory(&addr, sizeof (addr)); + ZeroMemory(&addr, sizeof(addr)); // Restrict our bind to the loopback address. We bind to an // ephemeral port. - addr.sin_family = AF_INET; - addr.sin_port = 0; + addr.sin_family = AF_INET; + addr.sin_port = 0; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); afd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -42,15 +42,14 @@ nni_plat_pipe_open(int *wfdp, int *rfdp) goto fail; } - // Make sure we have exclusive address use... one = 1; - if (setsockopt(afd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - (char *) (&one), sizeof (one)) != 0) { + if (setsockopt(afd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) (&one), + sizeof(one)) != 0) { goto fail; } - alen = sizeof (addr); + alen = sizeof(addr); if (bind(afd, (struct sockaddr *) &addr, alen) != 0) { goto fail; } @@ -110,7 +109,6 @@ fail: return (rv); } - void nni_plat_pipe_raise(int wfd) { @@ -119,7 +117,6 @@ nni_plat_pipe_raise(int wfd) send((SOCKET) wfd, &c, 1, 0); } - void nni_plat_pipe_clear(int rfd) { @@ -128,13 +125,12 @@ nni_plat_pipe_clear(int rfd) for (;;) { // Completely drain the pipe, but don't wait. This coalesces // events somewhat. - if (recv((SOCKET) rfd, buf, sizeof (buf), 0) <= 0) { + if (recv((SOCKET) rfd, buf, sizeof(buf), 0) <= 0) { return; } } } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -142,7 +138,6 @@ nni_plat_pipe_close(int wfd, int rfd) closesocket((SOCKET) rfd); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_rand.c b/src/platform/windows/win_rand.c index a1e381d8..38c86572 100644 --- a/src/platform/windows/win_rand.c +++ b/src/platform/windows/win_rand.c @@ -21,18 +21,17 @@ nni_plat_seed_prng(void *buf, size_t bufsz) // The rand_s routine uses RtlGenRandom to get high quality // pseudo random numbers (i.e. numbers that should be good enough // for use with crypto keying.) - while (bufsz > sizeof (val)) { + while (bufsz > sizeof(val)) { rand_s(&val); - memcpy(buf, &val, sizeof (val)); - buf = (((char *) buf) + sizeof (val)); - bufsz -= sizeof (val); + memcpy(buf, &val, sizeof(val)); + buf = (((char *) buf) + sizeof(val)); + bufsz -= sizeof(val); } } - #else // Suppress empty symbols warnings in ranlib. int nni_win_rand_not_used = 0; -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS diff --git a/src/platform/windows/win_resolv.c b/src/platform/windows/win_resolv.c index 3d37d546..44d00c34 100644 --- a/src/platform/windows/win_resolv.c +++ b/src/platform/windows/win_resolv.c @@ -7,7 +7,6 @@ // found online at https://opensource.org/licenses/MIT. // - #include "core/nng_impl.h" #ifdef PLATFORM_WINDOWS @@ -23,25 +22,23 @@ // changed with this define. #ifndef NNG_WIN_RESOLV_CONCURRENCY -#define NNG_WIN_RESOLV_CONCURRENCY 4 +#define NNG_WIN_RESOLV_CONCURRENCY 4 #endif - static nni_taskq *nni_win_resolv_tq = NULL; -static nni_mtx nni_win_resolv_mtx; +static nni_mtx nni_win_resolv_mtx; -typedef struct nni_win_resolv_item nni_win_resolv_item; +typedef struct nni_win_resolv_item nni_win_resolv_item; struct nni_win_resolv_item { - int family; - int passive; - const char * name; - const char * serv; - int proto; - nni_aio * aio; - nni_taskq_ent tqe; + int family; + int passive; + const char * name; + const char * serv; + int proto; + nni_aio * aio; + nni_taskq_ent tqe; }; - static void nni_win_resolv_finish(nni_win_resolv_item *item, int rv) { @@ -52,7 +49,6 @@ nni_win_resolv_finish(nni_win_resolv_item *item, int rv) NNI_FREE_STRUCT(item); } - static void nni_win_resolv_cancel(nni_aio *aio) { @@ -69,7 +65,6 @@ nni_win_resolv_cancel(nni_aio *aio) NNI_FREE_STRUCT(item); } - static int nni_win_gai_errno(int rv) { @@ -97,16 +92,15 @@ nni_win_gai_errno(int rv) } } - static void nni_win_resolv_task(void *arg) { nni_win_resolv_item *item = arg; - nni_aio *aio = item->aio; - struct addrinfo hints; - struct addrinfo *results; - struct addrinfo *probe; - int i, rv; + nni_aio * aio = item->aio; + struct addrinfo hints; + struct addrinfo * results; + struct addrinfo * probe; + int i, rv; results = NULL; @@ -116,13 +110,13 @@ nni_win_resolv_task(void *arg) case AF_UNSPEC: // We treat these all as IP addresses. The service and the // host part are split. - memset(&hints, 0, sizeof (hints)); + memset(&hints, 0, sizeof(hints)); if (item->passive) { hints.ai_flags |= AI_PASSIVE; } hints.ai_flags |= AI_ADDRCONFIG; hints.ai_protocol = item->proto; - hints.ai_family = item->family; + hints.ai_family = item->family; if (item->family == AF_INET6) { hints.ai_flags |= AI_V4MAPPED; } @@ -152,27 +146,27 @@ nni_win_resolv_task(void *arg) aio->a_addrs = NNI_ALLOC_STRUCTS(aio->a_addrs, aio->a_naddrs); if (aio->a_addrs == NULL) { aio->a_naddrs = 0; - rv = NNG_ENOMEM; + rv = NNG_ENOMEM; break; } i = 0; for (probe = results; probe != NULL; probe = probe->ai_next) { - struct sockaddr_in *sin; + struct sockaddr_in * sin; struct sockaddr_in6 *sin6; - nng_sockaddr *sa = &aio->a_addrs[i]; + nng_sockaddr * sa = &aio->a_addrs[i]; switch (probe->ai_addr->sa_family) { case AF_INET: sin = (void *) probe->ai_addr; sa->s_un.s_in.sa_family = NNG_AF_INET; - sa->s_un.s_in.sa_port = sin->sin_port; - sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; + sa->s_un.s_in.sa_port = sin->sin_port; + sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; i++; break; case AF_INET6: sin6 = (void *) probe->ai_addr; sa->s_un.s_in6.sa_family = NNG_AF_INET6; - sa->s_un.s_in6.sa_port = sin6->sin6_port; + sa->s_un.s_in6.sa_port = sin6->sin6_port; memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16); i++; @@ -200,13 +194,12 @@ nni_win_resolv_task(void *arg) nni_mtx_unlock(&nni_win_resolv_mtx); } - static void -nni_win_resolv_ip(const char *host, const char *serv, int passive, - int family, int proto, nni_aio *aio) +nni_win_resolv_ip(const char *host, const char *serv, int passive, int family, + int proto, nni_aio *aio) { nni_win_resolv_item *item; - int rv; + int rv; if ((aio->a_naddrs != 0) && (aio->a_addrs != NULL)) { NNI_FREE_STRUCTS(aio->a_addrs, aio->a_naddrs); @@ -231,10 +224,10 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, } // NB: host and serv must remain valid until this is completed. item->passive = passive; - item->name = host; - item->serv = serv; - item->proto = proto; - item->aio = aio; + item->name = host; + item->serv = serv; + item->proto = proto; + item->aio = aio; nni_mtx_lock(&nni_win_resolv_mtx); // If we were stopped, we're done... @@ -251,15 +244,13 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, nni_mtx_unlock(&nni_win_resolv_mtx); } - void -nni_plat_tcp_resolv(const char *host, const char *serv, int family, - int passive, nni_aio *aio) +nni_plat_tcp_resolv( + const char *host, const char *serv, int family, int passive, nni_aio *aio) { nni_win_resolv_ip(host, serv, passive, family, IPPROTO_TCP, aio); } - int nni_win_resolv_sysinit(void) { @@ -275,7 +266,6 @@ nni_win_resolv_sysinit(void) return (0); } - void nni_win_resolv_sysfini(void) { @@ -286,7 +276,6 @@ nni_win_resolv_sysfini(void) nni_mtx_fini(&nni_win_resolv_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index bc2d7a0c..693516bd 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -22,7 +22,6 @@ nni_alloc(size_t sz) return (v); } - void nni_free(void *b, size_t z) { @@ -30,7 +29,6 @@ nni_free(void *b, size_t z) HeapFree(GetProcessHeap(), 0, b); } - int nni_plat_mtx_init(nni_plat_mtx *mtx) { @@ -39,28 +37,24 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) return (0); } - void nni_plat_mtx_fini(nni_plat_mtx *mtx) { mtx->init = 0; } - void nni_plat_mtx_lock(nni_plat_mtx *mtx) { AcquireSRWLockExclusive(&mtx->srl); } - void nni_plat_mtx_unlock(nni_plat_mtx *mtx) { ReleaseSRWLockExclusive(&mtx->srl); } - int nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) { @@ -69,49 +63,43 @@ nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) return (0); } - void nni_plat_cv_wake(nni_plat_cv *cv) { WakeAllConditionVariable(&cv->cv); } - void nni_plat_cv_wait(nni_plat_cv *cv) { (void) SleepConditionVariableSRW(&cv->cv, cv->srl, INFINITE, 0); } - int nni_plat_cv_until(nni_plat_cv *cv, nni_time until) { nni_time now; - DWORD msec; - BOOL ok; + DWORD msec; + BOOL ok; now = nni_plat_clock(); if (now > until) { msec = 0; } else { // times are in usec, but win32 wants millis - msec = (DWORD) (((until - now) + 999)/1000); + msec = (DWORD)(((until - now) + 999) / 1000); } ok = SleepConditionVariableSRW(&cv->cv, cv->srl, msec, 0); return (ok ? 0 : NNG_ETIMEDOUT); } - void nni_plat_cv_fini(nni_plat_cv *cv) { } - -static unsigned int __stdcall -nni_plat_thr_main(void *arg) +static unsigned int __stdcall nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; @@ -119,25 +107,22 @@ nni_plat_thr_main(void *arg) return (0); } - int nni_plat_thr_init(nni_plat_thr *thr, void (*fn)(void *), void *arg) { thr->func = fn; - thr->arg = arg; + thr->arg = arg; // We could probably even go down to 8k... but crypto for some // protocols might get bigger than this. 1MB is waaay too big. - thr->handle = (HANDLE) _beginthreadex(NULL, 16384, - nni_plat_thr_main, thr, STACK_SIZE_PARAM_IS_A_RESERVATION, - NULL); + thr->handle = (HANDLE) _beginthreadex(NULL, 16384, nni_plat_thr_main, + thr, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL); if (thr->handle == NULL) { - return (NNG_ENOMEM); // Best guess... + return (NNG_ENOMEM); // Best guess... } return (0); } - void nni_plat_thr_fini(nni_plat_thr *thr) { @@ -149,23 +134,22 @@ nni_plat_thr_fini(nni_plat_thr *thr) } } - int nni_plat_init(int (*helper)(void)) { - static LONG inited = 0; - int rv; + static LONG inited = 0; + int rv; static SRWLOCK lock = SRWLOCK_INIT; if (inited) { - return (0); // fast path + return (0); // fast path } AcquireSRWLockExclusive(&lock); if (!inited) { WSADATA data; - WORD ver; + WORD ver; ver = MAKEWORD(2, 2); if (WSAStartup(MAKEWORD(2, 2), &data) != 0) { if ((LOBYTE(data.wVersion) != 2) || @@ -195,7 +179,6 @@ out: return (rv); } - void nni_plat_fini(void) { @@ -205,7 +188,6 @@ nni_plat_fini(void) WSACleanup(); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/protocol/bus/bus.c b/src/protocol/bus/bus.c index 5755d5a9..8c9ed83c 100644 --- a/src/protocol/bus/bus.c +++ b/src/protocol/bus/bus.c @@ -17,8 +17,8 @@ // for each participant to receive the message, each sender must be connected // to every other node in the network (full mesh). -typedef struct nni_bus_pipe nni_bus_pipe; -typedef struct nni_bus_sock nni_bus_sock; +typedef struct nni_bus_pipe nni_bus_pipe; +typedef struct nni_bus_sock nni_bus_sock; static void nni_bus_sock_getq(nni_bus_sock *); static void nni_bus_pipe_getq(nni_bus_pipe *); @@ -33,27 +33,26 @@ static void nni_bus_pipe_putq_cb(void *); // An nni_bus_sock is our per-socket protocol private structure. struct nni_bus_sock { - nni_sock * nsock; - int raw; - nni_aio aio_getq; - nni_list pipes; - nni_mtx mtx; + nni_sock *nsock; + int raw; + nni_aio aio_getq; + nni_list pipes; + nni_mtx mtx; }; // An nni_bus_pipe is our per-pipe protocol private structure. struct nni_bus_pipe { - nni_pipe * npipe; - nni_bus_sock * psock; - nni_msgq * sendq; - nni_list_node node; - nni_aio aio_getq; - nni_aio aio_recv; - nni_aio aio_send; - nni_aio aio_putq; - nni_mtx mtx; + nni_pipe * npipe; + nni_bus_sock *psock; + nni_msgq * sendq; + nni_list_node node; + nni_aio aio_getq; + nni_aio aio_recv; + nni_aio aio_send; + nni_aio aio_putq; + nni_mtx mtx; }; - static void nni_bus_sock_fini(void *arg) { @@ -66,12 +65,11 @@ nni_bus_sock_fini(void *arg) } } - static int nni_bus_sock_init(void **sp, nni_sock *nsock) { nni_bus_sock *psock; - int rv; + int rv; if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) { return (NNG_ENOMEM); @@ -85,7 +83,7 @@ nni_bus_sock_init(void **sp, nni_sock *nsock) goto fail; } psock->nsock = nsock; - psock->raw = 0; + psock->raw = 0; *sp = psock; return (0); @@ -95,7 +93,6 @@ fail: return (rv); } - static void nni_bus_sock_open(void *arg) { @@ -104,7 +101,6 @@ nni_bus_sock_open(void *arg) nni_bus_sock_getq(psock); } - static void nni_bus_pipe_fini(void *arg) { @@ -121,12 +117,11 @@ nni_bus_pipe_fini(void *arg) } } - static int nni_bus_pipe_init(void **pp, nni_pipe *npipe, void *psock) { nni_bus_pipe *ppipe; - int rv; + int rv; if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) { return (NNG_ENOMEM); @@ -155,7 +150,7 @@ nni_bus_pipe_init(void **pp, nni_pipe *npipe, void *psock) ppipe->npipe = npipe; ppipe->psock = psock; - *pp = ppipe; + *pp = ppipe; return (0); fail: @@ -163,7 +158,6 @@ fail: return (rv); } - static int nni_bus_pipe_start(void *arg) { @@ -180,7 +174,6 @@ nni_bus_pipe_start(void *arg) return (0); } - static void nni_bus_pipe_stop(void *arg) { @@ -201,7 +194,6 @@ nni_bus_pipe_stop(void *arg) nni_mtx_unlock(&ppipe->psock->mtx); } - static void nni_bus_pipe_getq_cb(void *arg) { @@ -218,7 +210,6 @@ nni_bus_pipe_getq_cb(void *arg) nni_pipe_send(ppipe->npipe, &ppipe->aio_send); } - static void nni_bus_pipe_send_cb(void *arg) { @@ -235,21 +226,20 @@ nni_bus_pipe_send_cb(void *arg) nni_bus_pipe_getq(ppipe); } - static void nni_bus_pipe_recv_cb(void *arg) { nni_bus_pipe *ppipe = arg; nni_bus_sock *psock = ppipe->psock; - nni_msg *msg; - uint32_t id; + nni_msg * msg; + uint32_t id; if (nni_aio_result(&ppipe->aio_recv) != 0) { nni_pipe_stop(ppipe->npipe); return; } msg = ppipe->aio_recv.a_msg; - id = nni_pipe_id(ppipe->npipe); + id = nni_pipe_id(ppipe->npipe); if (nni_msg_prepend_header(msg, &id, 4) != 0) { // XXX: bump a nomemory stat @@ -262,7 +252,6 @@ nni_bus_pipe_recv_cb(void *arg) nni_msgq_aio_put(nni_sock_recvq(psock->nsock), &ppipe->aio_putq); } - static void nni_bus_pipe_putq_cb(void *arg) { @@ -279,16 +268,15 @@ nni_bus_pipe_putq_cb(void *arg) nni_bus_pipe_recv(ppipe); } - static void nni_bus_sock_getq_cb(void *arg) { nni_bus_sock *psock = arg; nni_bus_pipe *ppipe; nni_bus_pipe *lpipe; - nni_msgq *uwq = nni_sock_sendq(psock->nsock); - nni_msg *msg, *dup; - uint32_t sender; + nni_msgq * uwq = nni_sock_sendq(psock->nsock); + nni_msg * msg, *dup; + uint32_t sender; if (nni_aio_result(&psock->aio_getq) != 0) { return; @@ -333,33 +321,29 @@ nni_bus_sock_getq_cb(void *arg) nni_bus_sock_getq(psock); } - static void nni_bus_sock_getq(nni_bus_sock *psock) { nni_msgq_aio_get(nni_sock_sendq(psock->nsock), &psock->aio_getq); } - static void nni_bus_pipe_getq(nni_bus_pipe *ppipe) { nni_msgq_aio_get(ppipe->sendq, &ppipe->aio_getq); } - static void nni_bus_pipe_recv(nni_bus_pipe *ppipe) { nni_pipe_recv(ppipe->npipe, &ppipe->aio_recv); } - static int nni_bus_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_bus_sock *psock = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -371,12 +355,11 @@ nni_bus_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_bus_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_bus_sock *psock = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -388,29 +371,28 @@ nni_bus_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - static nni_proto_pipe_ops nni_bus_pipe_ops = { - .pipe_init = nni_bus_pipe_init, - .pipe_fini = nni_bus_pipe_fini, - .pipe_start = nni_bus_pipe_start, - .pipe_stop = nni_bus_pipe_stop, + .pipe_init = nni_bus_pipe_init, + .pipe_fini = nni_bus_pipe_fini, + .pipe_start = nni_bus_pipe_start, + .pipe_stop = nni_bus_pipe_stop, }; static nni_proto_sock_ops nni_bus_sock_ops = { - .sock_init = nni_bus_sock_init, - .sock_fini = nni_bus_sock_fini, - .sock_open = nni_bus_sock_open, - .sock_setopt = nni_bus_sock_setopt, - .sock_getopt = nni_bus_sock_getopt, + .sock_init = nni_bus_sock_init, + .sock_fini = nni_bus_sock_fini, + .sock_open = nni_bus_sock_open, + .sock_setopt = nni_bus_sock_setopt, + .sock_getopt = nni_bus_sock_getopt, }; // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. nni_proto nni_bus_proto = { - .proto_self = NNG_PROTO_BUS, - .proto_peer = NNG_PROTO_BUS, - .proto_name = "bus", - .proto_flags = NNI_PROTO_FLAG_SNDRCV, + .proto_self = NNG_PROTO_BUS, + .proto_peer = NNG_PROTO_BUS, + .proto_name = "bus", + .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &nni_bus_sock_ops, .proto_pipe_ops = &nni_bus_pipe_ops, }; diff --git a/src/protocol/pair/pair.c b/src/protocol/pair/pair.c index b283377e..30302352 100644 --- a/src/protocol/pair/pair.c +++ b/src/protocol/pair/pair.c @@ -16,8 +16,8 @@ // While a peer is connected to the server, all other peer connection // attempts are discarded. -typedef struct nni_pair_pipe nni_pair_pipe; -typedef struct nni_pair_sock nni_pair_sock; +typedef struct nni_pair_pipe nni_pair_pipe; +typedef struct nni_pair_sock nni_pair_sock; static void nni_pair_send_cb(void *); static void nni_pair_recv_cb(void *); @@ -27,12 +27,12 @@ static void nni_pair_pipe_fini(void *); // An nni_pair_sock is our per-socket protocol private structure. struct nni_pair_sock { - nni_sock * nsock; - nni_pair_pipe * ppipe; - nni_msgq * uwq; - nni_msgq * urq; - int raw; - nni_mtx mtx; + nni_sock * nsock; + nni_pair_pipe *ppipe; + nni_msgq * uwq; + nni_msgq * urq; + int raw; + nni_mtx mtx; }; // An nni_pair_pipe is our per-pipe protocol private structure. We keep @@ -40,19 +40,19 @@ struct nni_pair_sock { // pipe. The separate data structure is more like other protocols that do // manage multiple pipes. struct nni_pair_pipe { - nni_pipe * npipe; - nni_pair_sock * psock; - nni_aio aio_send; - nni_aio aio_recv; - nni_aio aio_getq; - nni_aio aio_putq; + nni_pipe * npipe; + nni_pair_sock *psock; + nni_aio aio_send; + nni_aio aio_recv; + nni_aio aio_getq; + nni_aio aio_putq; }; static int nni_pair_sock_init(void **sp, nni_sock *nsock) { nni_pair_sock *psock; - int rv; + int rv; if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) { return (NNG_ENOMEM); @@ -63,14 +63,13 @@ nni_pair_sock_init(void **sp, nni_sock *nsock) } psock->nsock = nsock; psock->ppipe = NULL; - psock->raw = 0; - psock->uwq = nni_sock_sendq(nsock); - psock->urq = nni_sock_recvq(nsock); - *sp = psock; + psock->raw = 0; + psock->uwq = nni_sock_sendq(nsock); + psock->urq = nni_sock_recvq(nsock); + *sp = psock; return (0); } - static void nni_pair_sock_fini(void *arg) { @@ -83,12 +82,11 @@ nni_pair_sock_fini(void *arg) } } - static int nni_pair_pipe_init(void **pp, nni_pipe *npipe, void *psock) { nni_pair_pipe *ppipe; - int rv; + int rv; if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) { return (NNG_ENOMEM); @@ -111,7 +109,7 @@ nni_pair_pipe_init(void **pp, nni_pipe *npipe, void *psock) } ppipe->npipe = npipe; ppipe->psock = psock; - *pp = ppipe; + *pp = ppipe; return (0); fail: @@ -119,7 +117,6 @@ fail: return (rv); } - static void nni_pair_pipe_fini(void *arg) { @@ -132,7 +129,6 @@ nni_pair_pipe_fini(void *arg) NNI_FREE_STRUCT(ppipe); } - static int nni_pair_pipe_start(void *arg) { @@ -142,7 +138,7 @@ nni_pair_pipe_start(void *arg) nni_mtx_lock(&psock->mtx); if (psock->ppipe != NULL) { nni_mtx_unlock(&psock->mtx); - return (NNG_EBUSY); // Already have a peer, denied. + return (NNG_EBUSY); // Already have a peer, denied. } psock->ppipe = ppipe; nni_mtx_unlock(&psock->mtx); @@ -155,7 +151,6 @@ nni_pair_pipe_start(void *arg) return (0); } - static void nni_pair_pipe_stop(void *arg) { @@ -174,7 +169,6 @@ nni_pair_pipe_stop(void *arg) nni_mtx_unlock(&psock->mtx); } - static void nni_pair_recv_cb(void *arg) { @@ -191,7 +185,6 @@ nni_pair_recv_cb(void *arg) nni_msgq_aio_put(psock->urq, &ppipe->aio_putq); } - static void nni_pair_putq_cb(void *arg) { @@ -206,7 +199,6 @@ nni_pair_putq_cb(void *arg) nni_pipe_recv(ppipe->npipe, &ppipe->aio_recv); } - static void nni_pair_getq_cb(void *arg) { @@ -223,7 +215,6 @@ nni_pair_getq_cb(void *arg) nni_pipe_send(ppipe->npipe, &ppipe->aio_send); } - static void nni_pair_send_cb(void *arg) { @@ -240,12 +231,11 @@ nni_pair_send_cb(void *arg) nni_msgq_aio_get(psock->uwq, &ppipe->aio_getq); } - static int nni_pair_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_pair_sock *psock = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -257,12 +247,11 @@ nni_pair_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_pair_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_pair_sock *psock = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -274,29 +263,28 @@ nni_pair_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_pair_pipe_ops = { - .pipe_init = nni_pair_pipe_init, - .pipe_fini = nni_pair_pipe_fini, - .pipe_start = nni_pair_pipe_start, - .pipe_stop = nni_pair_pipe_stop, + .pipe_init = nni_pair_pipe_init, + .pipe_fini = nni_pair_pipe_fini, + .pipe_start = nni_pair_pipe_start, + .pipe_stop = nni_pair_pipe_stop, }; static nni_proto_sock_ops nni_pair_sock_ops = { - .sock_init = nni_pair_sock_init, - .sock_fini = nni_pair_sock_fini, - .sock_setopt = nni_pair_sock_setopt, - .sock_getopt = nni_pair_sock_getopt, + .sock_init = nni_pair_sock_init, + .sock_fini = nni_pair_sock_fini, + .sock_setopt = nni_pair_sock_setopt, + .sock_getopt = nni_pair_sock_getopt, }; nni_proto nni_pair_proto = { - .proto_self = NNG_PROTO_PAIR, - .proto_peer = NNG_PROTO_PAIR, - .proto_name = "pair", - .proto_flags = NNI_PROTO_FLAG_SNDRCV, + .proto_self = NNG_PROTO_PAIR, + .proto_peer = NNG_PROTO_PAIR, + .proto_name = "pair", + .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &nni_pair_sock_ops, .proto_pipe_ops = &nni_pair_pipe_ops, }; diff --git a/src/protocol/pipeline/pull.c b/src/protocol/pipeline/pull.c index 27fd5478..80ff4245 100644 --- a/src/protocol/pipeline/pull.c +++ b/src/protocol/pipeline/pull.c @@ -14,8 +14,8 @@ // Pull protocol. The PULL protocol is the "read" side of a pipeline. -typedef struct nni_pull_pipe nni_pull_pipe; -typedef struct nni_pull_sock nni_pull_sock; +typedef struct nni_pull_pipe nni_pull_pipe; +typedef struct nni_pull_sock nni_pull_sock; static void nni_pull_putq_cb(void *); static void nni_pull_recv_cb(void *); @@ -23,16 +23,16 @@ static void nni_pull_putq(nni_pull_pipe *, nni_msg *); // An nni_pull_sock is our per-socket protocol private structure. struct nni_pull_sock { - nni_msgq * urq; - int raw; + nni_msgq *urq; + int raw; }; // An nni_pull_pipe is our per-pipe protocol private structure. struct nni_pull_pipe { - nni_pipe * pipe; - nni_pull_sock * pull; - nni_aio putq_aio; - nni_aio recv_aio; + nni_pipe * pipe; + nni_pull_sock *pull; + nni_aio putq_aio; + nni_aio recv_aio; }; static int @@ -45,12 +45,11 @@ nni_pull_sock_init(void **pullp, nni_sock *sock) } pull->raw = 0; pull->urq = nni_sock_recvq(sock); - *pullp = pull; + *pullp = pull; nni_sock_senderr(sock, NNG_ENOTSUP); return (0); } - static void nni_pull_sock_fini(void *arg) { @@ -61,12 +60,11 @@ nni_pull_sock_fini(void *arg) } } - static int nni_pull_pipe_init(void **ppp, nni_pipe *pipe, void *psock) { nni_pull_pipe *pp; - int rv; + int rv; if ((pp = NNI_ALLOC_STRUCT(pp)) == NULL) { return (NNG_ENOMEM); @@ -82,11 +80,10 @@ nni_pull_pipe_init(void **ppp, nni_pipe *pipe, void *psock) } pp->pipe = pipe; pp->pull = psock; - *ppp = pp; + *ppp = pp; return (0); } - static void nni_pull_pipe_fini(void *arg) { @@ -99,7 +96,6 @@ nni_pull_pipe_fini(void *arg) } } - static int nni_pull_pipe_start(void *arg) { @@ -111,7 +107,6 @@ nni_pull_pipe_start(void *arg) return (0); } - static void nni_pull_pipe_stop(void *arg) { @@ -121,13 +116,12 @@ nni_pull_pipe_stop(void *arg) nni_aio_stop(&pp->recv_aio); } - static void nni_pull_recv_cb(void *arg) { - nni_pull_pipe *pp = arg; - nni_aio *aio = &pp->recv_aio; - nni_msg *msg; + nni_pull_pipe *pp = arg; + nni_aio * aio = &pp->recv_aio; + nni_msg * msg; if (nni_aio_result(aio) != 0) { // Failed to get a message, probably the pipe is closed. @@ -136,17 +130,16 @@ nni_pull_recv_cb(void *arg) } // Got a message... start the put to send it up to the application. - msg = aio->a_msg; + msg = aio->a_msg; aio->a_msg = NULL; nni_pull_putq(pp, msg); } - static void nni_pull_putq_cb(void *arg) { - nni_pull_pipe *pp = arg; - nni_aio *aio = &pp->putq_aio; + nni_pull_pipe *pp = arg; + nni_aio * aio = &pp->putq_aio; if (nni_aio_result(aio) != 0) { // If we failed to put, probably NNG_ECLOSED, nothing else @@ -160,7 +153,6 @@ nni_pull_putq_cb(void *arg) nni_pipe_recv(pp->pipe, &pp->recv_aio); } - // nni_pull_putq schedules a put operation to the user socket (sendup). static void nni_pull_putq(nni_pull_pipe *pp, nni_msg *msg) @@ -172,12 +164,11 @@ nni_pull_putq(nni_pull_pipe *pp, nni_msg *msg) nni_msgq_aio_put(pull->urq, &pp->putq_aio); } - static int nni_pull_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_pull_sock *pull = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -189,12 +180,11 @@ nni_pull_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_pull_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_pull_sock *pull = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -206,28 +196,27 @@ nni_pull_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_pull_pipe_ops = { - .pipe_init = nni_pull_pipe_init, - .pipe_fini = nni_pull_pipe_fini, - .pipe_start = nni_pull_pipe_start, - .pipe_stop = nni_pull_pipe_stop, + .pipe_init = nni_pull_pipe_init, + .pipe_fini = nni_pull_pipe_fini, + .pipe_start = nni_pull_pipe_start, + .pipe_stop = nni_pull_pipe_stop, }; static nni_proto_sock_ops nni_pull_sock_ops = { - .sock_init = nni_pull_sock_init, - .sock_fini = nni_pull_sock_fini, - .sock_setopt = nni_pull_sock_setopt, - .sock_getopt = nni_pull_sock_getopt, + .sock_init = nni_pull_sock_init, + .sock_fini = nni_pull_sock_fini, + .sock_setopt = nni_pull_sock_setopt, + .sock_getopt = nni_pull_sock_getopt, }; nni_proto nni_pull_proto = { - .proto_self = NNG_PROTO_PULL, - .proto_peer = NNG_PROTO_PUSH, - .proto_name = "pull", - .proto_flags = NNI_PROTO_FLAG_RCV, + .proto_self = NNG_PROTO_PULL, + .proto_peer = NNG_PROTO_PUSH, + .proto_name = "pull", + .proto_flags = NNI_PROTO_FLAG_RCV, .proto_pipe_ops = &nni_pull_pipe_ops, .proto_sock_ops = &nni_pull_sock_ops, }; diff --git a/src/protocol/pipeline/push.c b/src/protocol/pipeline/push.c index bbda91da..f1da0a9f 100644 --- a/src/protocol/pipeline/push.c +++ b/src/protocol/pipeline/push.c @@ -16,8 +16,8 @@ // Push distributes fairly, or tries to, by giving messages in round-robin // order. -typedef struct nni_push_pipe nni_push_pipe; -typedef struct nni_push_sock nni_push_sock; +typedef struct nni_push_pipe nni_push_pipe; +typedef struct nni_push_sock nni_push_sock; static void nni_push_send_cb(void *); static void nni_push_recv_cb(void *); @@ -25,20 +25,20 @@ static void nni_push_getq_cb(void *); // An nni_push_sock is our per-socket protocol private structure. struct nni_push_sock { - nni_msgq * uwq; - int raw; - nni_sock * sock; + nni_msgq *uwq; + int raw; + nni_sock *sock; }; // An nni_push_pipe is our per-pipe protocol private structure. struct nni_push_pipe { - nni_pipe * pipe; - nni_push_sock * push; - nni_list_node node; + nni_pipe * pipe; + nni_push_sock *push; + nni_list_node node; - nni_aio aio_recv; - nni_aio aio_send; - nni_aio aio_getq; + nni_aio aio_recv; + nni_aio aio_send; + nni_aio aio_getq; }; static int @@ -49,15 +49,14 @@ nni_push_sock_init(void **pushp, nni_sock *sock) if ((push = NNI_ALLOC_STRUCT(push)) == NULL) { return (NNG_ENOMEM); } - push->raw = 0; + push->raw = 0; push->sock = sock; - push->uwq = nni_sock_sendq(sock); - *pushp = push; + push->uwq = nni_sock_sendq(sock); + *pushp = push; nni_sock_recverr(sock, NNG_ENOTSUP); return (0); } - static void nni_push_sock_fini(void *arg) { @@ -68,7 +67,6 @@ nni_push_sock_fini(void *arg) } } - static void nni_push_pipe_fini(void *arg) { @@ -80,12 +78,11 @@ nni_push_pipe_fini(void *arg) NNI_FREE_STRUCT(pp); } - static int nni_push_pipe_init(void **ppp, nni_pipe *pipe, void *psock) { nni_push_pipe *pp; - int rv; + int rv; if ((pp = NNI_ALLOC_STRUCT(pp)) == NULL) { return (NNG_ENOMEM); @@ -103,7 +100,7 @@ nni_push_pipe_init(void **ppp, nni_pipe *pipe, void *psock) NNI_LIST_NODE_INIT(&pp->node); pp->pipe = pipe; pp->push = psock; - *ppp = pp; + *ppp = pp; return (0); fail: @@ -111,11 +108,10 @@ fail: return (rv); } - static int nni_push_pipe_start(void *arg) { - nni_push_pipe *pp = arg; + nni_push_pipe *pp = arg; nni_push_sock *push = pp->push; if (nni_pipe_peer(pp->pipe) != NNG_PROTO_PULL) { @@ -132,11 +128,10 @@ nni_push_pipe_start(void *arg) return (0); } - static void nni_push_pipe_stop(void *arg) { - nni_push_pipe *pp = arg; + nni_push_pipe *pp = arg; nni_push_sock *push = pp->push; nni_aio_stop(&pp->aio_recv); @@ -144,7 +139,6 @@ nni_push_pipe_stop(void *arg) nni_aio_stop(&pp->aio_getq); } - static void nni_push_recv_cb(void *arg) { @@ -161,11 +155,10 @@ nni_push_recv_cb(void *arg) nni_pipe_recv(pp->pipe, &pp->aio_recv); } - static void nni_push_send_cb(void *arg) { - nni_push_pipe *pp = arg; + nni_push_pipe *pp = arg; nni_push_sock *push = pp->push; if (nni_aio_result(&pp->aio_send) != 0) { @@ -178,12 +171,11 @@ nni_push_send_cb(void *arg) nni_msgq_aio_get(push->uwq, &pp->aio_getq); } - static void nni_push_getq_cb(void *arg) { - nni_push_pipe *pp = arg; - nni_aio *aio = &pp->aio_getq; + nni_push_pipe *pp = arg; + nni_aio * aio = &pp->aio_getq; if (nni_aio_result(aio) != 0) { // If the socket is closing, nothing else we can do. @@ -192,17 +184,16 @@ nni_push_getq_cb(void *arg) } pp->aio_send.a_msg = aio->a_msg; - aio->a_msg = NULL; + aio->a_msg = NULL; nni_pipe_send(pp->pipe, &pp->aio_send); } - static int nni_push_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_push_sock *push = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -214,12 +205,11 @@ nni_push_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_push_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_push_sock *push = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -231,28 +221,27 @@ nni_push_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_push_pipe_ops = { - .pipe_init = nni_push_pipe_init, - .pipe_fini = nni_push_pipe_fini, - .pipe_start = nni_push_pipe_start, - .pipe_stop = nni_push_pipe_stop, + .pipe_init = nni_push_pipe_init, + .pipe_fini = nni_push_pipe_fini, + .pipe_start = nni_push_pipe_start, + .pipe_stop = nni_push_pipe_stop, }; static nni_proto_sock_ops nni_push_sock_ops = { - .sock_init = nni_push_sock_init, - .sock_fini = nni_push_sock_fini, - .sock_setopt = nni_push_sock_setopt, - .sock_getopt = nni_push_sock_getopt, + .sock_init = nni_push_sock_init, + .sock_fini = nni_push_sock_fini, + .sock_setopt = nni_push_sock_setopt, + .sock_getopt = nni_push_sock_getopt, }; nni_proto nni_push_proto = { - .proto_self = NNG_PROTO_PUSH, - .proto_peer = NNG_PROTO_PULL, - .proto_name = "push", - .proto_flags = NNI_PROTO_FLAG_SND, + .proto_self = NNG_PROTO_PUSH, + .proto_peer = NNG_PROTO_PULL, + .proto_name = "push", + .proto_flags = NNI_PROTO_FLAG_SND, .proto_pipe_ops = &nni_push_pipe_ops, .proto_sock_ops = &nni_push_sock_ops, }; diff --git a/src/protocol/pubsub/pub.c b/src/protocol/pubsub/pub.c index e3b37f1a..64c2c59d 100644 --- a/src/protocol/pubsub/pub.c +++ b/src/protocol/pubsub/pub.c @@ -17,8 +17,8 @@ // perform sender-side filtering. Its best effort delivery, so anything // that can't receive the message won't get one. -typedef struct nni_pub_pipe nni_pub_pipe; -typedef struct nni_pub_sock nni_pub_sock; +typedef struct nni_pub_pipe nni_pub_pipe; +typedef struct nni_pub_sock nni_pub_sock; static void nni_pub_pipe_recv_cb(void *); static void nni_pub_pipe_send_cb(void *); @@ -29,30 +29,30 @@ static void nni_pub_pipe_fini(void *); // An nni_pub_sock is our per-socket protocol private structure. struct nni_pub_sock { - nni_sock * sock; - nni_msgq * uwq; - int raw; - nni_aio aio_getq; - nni_list pipes; - nni_mtx mtx; + nni_sock *sock; + nni_msgq *uwq; + int raw; + nni_aio aio_getq; + nni_list pipes; + nni_mtx mtx; }; // An nni_pub_pipe is our per-pipe protocol private structure. struct nni_pub_pipe { - nni_pipe * pipe; - nni_pub_sock * pub; - nni_msgq * sendq; - nni_aio aio_getq; - nni_aio aio_send; - nni_aio aio_recv; - nni_list_node node; + nni_pipe * pipe; + nni_pub_sock *pub; + nni_msgq * sendq; + nni_aio aio_getq; + nni_aio aio_send; + nni_aio aio_recv; + nni_list_node node; }; static int nni_pub_sock_init(void **pubp, nni_sock *sock) { nni_pub_sock *pub; - int rv; + int rv; if ((pub = NNI_ALLOC_STRUCT(pub)) == NULL) { return (NNG_ENOMEM); @@ -67,7 +67,7 @@ nni_pub_sock_init(void **pubp, nni_sock *sock) return (rv); } pub->sock = sock; - pub->raw = 0; + pub->raw = 0; NNI_LIST_INIT(&pub->pipes, nni_pub_pipe, node); pub->uwq = nni_sock_sendq(sock); @@ -77,7 +77,6 @@ nni_pub_sock_init(void **pubp, nni_sock *sock) return (0); } - static void nni_pub_sock_fini(void *arg) { @@ -88,7 +87,6 @@ nni_pub_sock_fini(void *arg) NNI_FREE_STRUCT(pub); } - static void nni_pub_sock_open(void *arg) { @@ -97,7 +95,6 @@ nni_pub_sock_open(void *arg) nni_msgq_aio_get(pub->uwq, &pub->aio_getq); } - static void nni_pub_pipe_fini(void *arg) { @@ -110,12 +107,11 @@ nni_pub_pipe_fini(void *arg) NNI_FREE_STRUCT(pp); } - static int nni_pub_pipe_init(void **ppp, nni_pipe *pipe, void *psock) { nni_pub_pipe *pp; - int rv; + int rv; if ((pp = NNI_ALLOC_STRUCT(pp)) == NULL) { return (NNG_ENOMEM); @@ -140,8 +136,8 @@ nni_pub_pipe_init(void **ppp, nni_pipe *pipe, void *psock) goto fail; } pp->pipe = pipe; - pp->pub = psock; - *ppp = pp; + pp->pub = psock; + *ppp = pp; return (0); fail: @@ -149,11 +145,10 @@ fail: return (rv); } - static int nni_pub_pipe_start(void *arg) { - nni_pub_pipe *pp = arg; + nni_pub_pipe *pp = arg; nni_pub_sock *pub = pp->pub; if (nni_pipe_peer(pp->pipe) != NNG_PROTO_SUB) { @@ -170,11 +165,10 @@ nni_pub_pipe_start(void *arg) return (0); } - static void nni_pub_pipe_stop(void *arg) { - nni_pub_pipe *pp = arg; + nni_pub_pipe *pp = arg; nni_pub_sock *pub = pp->pub; nni_aio_stop(&pp->aio_getq); @@ -189,23 +183,22 @@ nni_pub_pipe_stop(void *arg) nni_mtx_unlock(&pub->mtx); } - static void nni_pub_sock_getq_cb(void *arg) { nni_pub_sock *pub = arg; - nni_msgq *uwq = pub->uwq; - nni_msg *msg, *dup; + nni_msgq * uwq = pub->uwq; + nni_msg * msg, *dup; nni_pub_pipe *pp; nni_pub_pipe *last; - int rv; + int rv; if (nni_aio_result(&pub->aio_getq) != 0) { return; } - msg = pub->aio_getq.a_msg; + msg = pub->aio_getq.a_msg; pub->aio_getq.a_msg = NULL; nni_mtx_lock(&pub->mtx); @@ -232,7 +225,6 @@ nni_pub_sock_getq_cb(void *arg) nni_msgq_aio_get(uwq, &pub->aio_getq); } - static void nni_pub_pipe_recv_cb(void *arg) { @@ -248,7 +240,6 @@ nni_pub_pipe_recv_cb(void *arg) nni_pipe_recv(pp->pipe, &pp->aio_recv); } - static void nni_pub_pipe_getq_cb(void *arg) { @@ -265,7 +256,6 @@ nni_pub_pipe_getq_cb(void *arg) nni_pipe_send(pp->pipe, &pp->aio_send); } - static void nni_pub_pipe_send_cb(void *arg) { @@ -282,12 +272,11 @@ nni_pub_pipe_send_cb(void *arg) nni_msgq_aio_get(pp->sendq, &pp->aio_getq); } - static int nni_pub_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_pub_sock *pub = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -299,12 +288,11 @@ nni_pub_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_pub_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_pub_sock *pub = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -316,29 +304,28 @@ nni_pub_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_pub_pipe_ops = { - .pipe_init = nni_pub_pipe_init, - .pipe_fini = nni_pub_pipe_fini, - .pipe_start = nni_pub_pipe_start, - .pipe_stop = nni_pub_pipe_stop, + .pipe_init = nni_pub_pipe_init, + .pipe_fini = nni_pub_pipe_fini, + .pipe_start = nni_pub_pipe_start, + .pipe_stop = nni_pub_pipe_stop, }; nni_proto_sock_ops nni_pub_sock_ops = { - .sock_init = nni_pub_sock_init, - .sock_fini = nni_pub_sock_fini, - .sock_open = nni_pub_sock_open, - .sock_setopt = nni_pub_sock_setopt, - .sock_getopt = nni_pub_sock_getopt, + .sock_init = nni_pub_sock_init, + .sock_fini = nni_pub_sock_fini, + .sock_open = nni_pub_sock_open, + .sock_setopt = nni_pub_sock_setopt, + .sock_getopt = nni_pub_sock_getopt, }; nni_proto nni_pub_proto = { - .proto_self = NNG_PROTO_PUB, - .proto_peer = NNG_PROTO_SUB, - .proto_name = "pub", - .proto_flags = NNI_PROTO_FLAG_SND, + .proto_self = NNG_PROTO_PUB, + .proto_peer = NNG_PROTO_SUB, + .proto_name = "pub", + .proto_flags = NNI_PROTO_FLAG_SND, .proto_sock_ops = &nni_pub_sock_ops, .proto_pipe_ops = &nni_pub_pipe_ops, }; diff --git a/src/protocol/pubsub/sub.c b/src/protocol/pubsub/sub.c index 09a724e2..bc4de973 100644 --- a/src/protocol/pubsub/sub.c +++ b/src/protocol/pubsub/sub.c @@ -16,34 +16,34 @@ // it from publishers, and filters out those it is not interested in, // only passing up ones that match known subscriptions. -typedef struct nni_sub_pipe nni_sub_pipe; -typedef struct nni_sub_sock nni_sub_sock; -typedef struct nni_sub_topic nni_sub_topic; +typedef struct nni_sub_pipe nni_sub_pipe; +typedef struct nni_sub_sock nni_sub_sock; +typedef struct nni_sub_topic nni_sub_topic; static void nni_sub_recv_cb(void *); static void nni_sub_putq_cb(void *); static void nni_sub_pipe_fini(void *); struct nni_sub_topic { - nni_list_node node; - size_t len; - void * buf; + nni_list_node node; + size_t len; + void * buf; }; // An nni_rep_sock is our per-socket protocol private structure. struct nni_sub_sock { - nni_sock * sock; - nni_list topics; - nni_msgq * urq; - int raw; + nni_sock *sock; + nni_list topics; + nni_msgq *urq; + int raw; }; // An nni_rep_pipe is our per-pipe protocol private structure. struct nni_sub_pipe { - nni_pipe * pipe; - nni_sub_sock * sub; - nni_aio aio_recv; - nni_aio aio_putq; + nni_pipe * pipe; + nni_sub_sock *sub; + nni_aio aio_recv; + nni_aio aio_putq; }; static int @@ -56,7 +56,7 @@ nni_sub_sock_init(void **subp, nni_sock *sock) } NNI_LIST_INIT(&sub->topics, nni_sub_topic, node); sub->sock = sock; - sub->raw = 0; + sub->raw = 0; sub->urq = nni_sock_recvq(sock); nni_sock_senderr(sock, NNG_ENOTSUP); @@ -64,11 +64,10 @@ nni_sub_sock_init(void **subp, nni_sock *sock) return (0); } - static void nni_sub_sock_fini(void *arg) { - nni_sub_sock *sub = arg; + nni_sub_sock * sub = arg; nni_sub_topic *topic; while ((topic = nni_list_first(&sub->topics)) != NULL) { @@ -79,12 +78,11 @@ nni_sub_sock_fini(void *arg) NNI_FREE_STRUCT(sub); } - static int nni_sub_pipe_init(void **spp, nni_pipe *pipe, void *ssock) { nni_sub_pipe *sp; - int rv; + int rv; if ((sp = NNI_ALLOC_STRUCT(sp)) == NULL) { return (NNG_ENOMEM); @@ -95,12 +93,11 @@ nni_sub_pipe_init(void **spp, nni_pipe *pipe, void *ssock) return (rv); } sp->pipe = pipe; - sp->sub = ssock; - *spp = sp; + sp->sub = ssock; + *spp = sp; return (0); } - static void nni_sub_pipe_fini(void *arg) { @@ -111,7 +108,6 @@ nni_sub_pipe_fini(void *arg) NNI_FREE_STRUCT(sp); } - static int nni_sub_pipe_start(void *arg) { @@ -121,7 +117,6 @@ nni_sub_pipe_start(void *arg) return (0); } - static void nni_sub_pipe_stop(void *arg) { @@ -131,13 +126,12 @@ nni_sub_pipe_stop(void *arg) nni_aio_stop(&sp->aio_recv); } - static void nni_sub_recv_cb(void *arg) { - nni_sub_pipe *sp = arg; + nni_sub_pipe *sp = arg; nni_sub_sock *sub = sp->sub; - nni_msgq *urq = sub->urq; + nni_msgq * urq = sub->urq; if (nni_aio_result(&sp->aio_recv) != 0) { nni_pipe_stop(sp->pipe); @@ -149,7 +143,6 @@ nni_sub_recv_cb(void *arg) nni_msgq_aio_put(sub->urq, &sp->aio_putq); } - static void nni_sub_putq_cb(void *arg) { @@ -165,7 +158,6 @@ nni_sub_putq_cb(void *arg) nni_pipe_recv(sp->pipe, &sp->aio_recv); } - // For now we maintain subscriptions on a sorted linked list. As we do not // expect to have huge numbers of subscriptions, and as the operation is // really O(n), we think this is acceptable. In the future we might decide @@ -215,12 +207,11 @@ nni_sub_subscribe(nni_sub_sock *sub, const void *buf, size_t sz) return (0); } - static int nni_sub_unsubscribe(nni_sub_sock *sub, const void *buf, size_t sz) { nni_sub_topic *topic; - int rv; + int rv; NNI_LIST_FOREACH (&sub->topics, topic) { if (topic->len >= sz) { @@ -246,12 +237,11 @@ nni_sub_unsubscribe(nni_sub_sock *sub, const void *buf, size_t sz) return (NNG_ENOENT); } - static int nni_sub_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_sub_sock *sub = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -269,12 +259,11 @@ nni_sub_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_sub_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_sub_sock *sub = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RAW: @@ -286,22 +275,21 @@ nni_sub_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - static nni_msg * nni_sub_sock_rfilter(void *arg, nni_msg *msg) { - nni_sub_sock *sub = arg; + nni_sub_sock * sub = arg; nni_sub_topic *topic; - char *body; - size_t len; - int match; + char * body; + size_t len; + int match; if (sub->raw) { return (msg); } body = nni_msg_body(msg); - len = nni_msg_len(msg); + len = nni_msg_len(msg); match = 0; // Check to see if the message matches one of our subscriptions. @@ -329,29 +317,28 @@ nni_sub_sock_rfilter(void *arg, nni_msg *msg) return (msg); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_sub_pipe_ops = { - .pipe_init = nni_sub_pipe_init, - .pipe_fini = nni_sub_pipe_fini, - .pipe_start = nni_sub_pipe_start, - .pipe_stop = nni_sub_pipe_stop, + .pipe_init = nni_sub_pipe_init, + .pipe_fini = nni_sub_pipe_fini, + .pipe_start = nni_sub_pipe_start, + .pipe_stop = nni_sub_pipe_stop, }; static nni_proto_sock_ops nni_sub_sock_ops = { - .sock_init = nni_sub_sock_init, - .sock_fini = nni_sub_sock_fini, - .sock_setopt = nni_sub_sock_setopt, - .sock_getopt = nni_sub_sock_getopt, - .sock_rfilter = nni_sub_sock_rfilter, + .sock_init = nni_sub_sock_init, + .sock_fini = nni_sub_sock_fini, + .sock_setopt = nni_sub_sock_setopt, + .sock_getopt = nni_sub_sock_getopt, + .sock_rfilter = nni_sub_sock_rfilter, }; nni_proto nni_sub_proto = { - .proto_self = NNG_PROTO_SUB, - .proto_peer = NNG_PROTO_PUB, - .proto_name = "sub", - .proto_flags = NNI_PROTO_FLAG_RCV, + .proto_self = NNG_PROTO_SUB, + .proto_peer = NNG_PROTO_PUB, + .proto_name = "sub", + .proto_flags = NNI_PROTO_FLAG_RCV, .proto_sock_ops = &nni_sub_sock_ops, .proto_pipe_ops = &nni_sub_pipe_ops, }; diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 7d887b55..cfc83a5b 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -16,9 +16,8 @@ // request-reply pair. This is useful for building RPC servers, for // example. -typedef struct nni_rep_pipe nni_rep_pipe; -typedef struct nni_rep_sock nni_rep_sock; - +typedef struct nni_rep_pipe nni_rep_pipe; +typedef struct nni_rep_sock nni_rep_sock; static void nni_rep_sock_getq_cb(void *); static void nni_rep_pipe_getq_cb(void *); @@ -29,29 +28,29 @@ static void nni_rep_pipe_fini(void *); // An nni_rep_sock is our per-socket protocol private structure. struct nni_rep_sock { - nni_sock * sock; - nni_msgq * uwq; - nni_msgq * urq; - int raw; - int ttl; - nni_idhash pipes; - char * btrace; - size_t btrace_len; - nni_aio aio_getq; - nni_mtx mtx; + nni_sock * sock; + nni_msgq * uwq; + nni_msgq * urq; + int raw; + int ttl; + nni_idhash pipes; + char * btrace; + size_t btrace_len; + nni_aio aio_getq; + nni_mtx mtx; }; // An nni_rep_pipe is our per-pipe protocol private structure. struct nni_rep_pipe { - nni_pipe * pipe; - nni_rep_sock * rep; - nni_msgq * sendq; - uint32_t id; // we have to save it - nni_aio aio_getq; - nni_aio aio_send; - nni_aio aio_recv; - nni_aio aio_putq; - nni_mtx mtx; + nni_pipe * pipe; + nni_rep_sock *rep; + nni_msgq * sendq; + uint32_t id; // we have to save it + nni_aio aio_getq; + nni_aio aio_send; + nni_aio aio_recv; + nni_aio aio_putq; + nni_mtx mtx; }; static void @@ -68,20 +67,19 @@ nni_rep_sock_fini(void *arg) NNI_FREE_STRUCT(rep); } - static int nni_rep_sock_init(void **repp, nni_sock *sock) { nni_rep_sock *rep; - int rv; + int rv; if ((rep = NNI_ALLOC_STRUCT(rep)) == NULL) { return (NNG_ENOMEM); } - rep->ttl = 8; // Per RFC - rep->sock = sock; - rep->raw = 0; - rep->btrace = NULL; + rep->ttl = 8; // Per RFC + rep->sock = sock; + rep->raw = 0; + rep->btrace = NULL; rep->btrace_len = 0; if (((rv = nni_mtx_init(&rep->mtx)) != 0) || ((rv = nni_idhash_init(&rep->pipes)) != 0)) { @@ -106,7 +104,6 @@ fail: return (rv); } - static void nni_rep_sock_open(void *arg) { @@ -115,7 +112,6 @@ nni_rep_sock_open(void *arg) nni_msgq_aio_get(rep->uwq, &rep->aio_getq); } - static void nni_rep_sock_close(void *arg) { @@ -124,12 +120,11 @@ nni_rep_sock_close(void *arg) nni_aio_stop(&rep->aio_getq); } - static int nni_rep_pipe_init(void **rpp, nni_pipe *pipe, void *rsock) { nni_rep_pipe *rp; - int rv; + int rv; if ((rp = NNI_ALLOC_STRUCT(rp)) == NULL) { return (NNG_ENOMEM); @@ -138,21 +133,25 @@ nni_rep_pipe_init(void **rpp, nni_pipe *pipe, void *rsock) ((rv = nni_mtx_init(&rp->mtx)) != 0)) { goto fail; } - if ((rv = nni_aio_init(&rp->aio_getq, nni_rep_pipe_getq_cb, rp)) != 0) { + if ((rv = nni_aio_init(&rp->aio_getq, nni_rep_pipe_getq_cb, rp)) != + 0) { goto fail; } - if ((rv = nni_aio_init(&rp->aio_send, nni_rep_pipe_send_cb, rp)) != 0) { + if ((rv = nni_aio_init(&rp->aio_send, nni_rep_pipe_send_cb, rp)) != + 0) { goto fail; } - if ((rv = nni_aio_init(&rp->aio_recv, nni_rep_pipe_recv_cb, rp)) != 0) { + if ((rv = nni_aio_init(&rp->aio_recv, nni_rep_pipe_recv_cb, rp)) != + 0) { goto fail; } - if ((rv = nni_aio_init(&rp->aio_putq, nni_rep_pipe_putq_cb, rp)) != 0) { + if ((rv = nni_aio_init(&rp->aio_putq, nni_rep_pipe_putq_cb, rp)) != + 0) { goto fail; } rp->pipe = pipe; - rp->rep = rsock; - *rpp = rp; + rp->rep = rsock; + *rpp = rp; return (0); fail: @@ -160,7 +159,6 @@ fail: return (rv); } - static void nni_rep_pipe_fini(void *arg) { @@ -175,13 +173,12 @@ nni_rep_pipe_fini(void *arg) NNI_FREE_STRUCT(rp); } - static int nni_rep_pipe_start(void *arg) { - nni_rep_pipe *rp = arg; + nni_rep_pipe *rp = arg; nni_rep_sock *rep = rp->rep; - int rv; + int rv; rp->id = nni_pipe_id(rp->pipe); @@ -197,13 +194,12 @@ nni_rep_pipe_start(void *arg) return (0); } - static void nni_rep_pipe_stop(void *arg) { - nni_rep_pipe *rp = arg; + nni_rep_pipe *rp = arg; nni_rep_sock *rep = rp->rep; - uint32_t id; + uint32_t id; nni_aio_stop(&rp->aio_getq); nni_aio_stop(&rp->aio_putq); @@ -211,8 +207,8 @@ nni_rep_pipe_stop(void *arg) nni_aio_stop(&rp->aio_recv); nni_mtx_lock(&rp->mtx); - id = rp->id; - rp->id = 0; // makes this idempotent + id = rp->id; + rp->id = 0; // makes this idempotent nni_msgq_close(rp->sendq); nni_mtx_unlock(&rp->mtx); @@ -223,17 +219,16 @@ nni_rep_pipe_stop(void *arg) } } - static void nni_rep_sock_getq_cb(void *arg) { nni_rep_sock *rep = arg; - nni_msgq *uwq = rep->uwq; - nni_msg *msg; - uint8_t *header; - uint32_t id; + nni_msgq * uwq = rep->uwq; + nni_msg * msg; + uint8_t * header; + uint32_t id; nni_rep_pipe *rp; - int rv; + int rv; // This watches for messages from the upper write queue, // extracts the destination pipe, and forwards it to the appropriate @@ -245,7 +240,7 @@ nni_rep_sock_getq_cb(void *arg) return; } - msg = rep->aio_getq.a_msg; + msg = rep->aio_getq.a_msg; rep->aio_getq.a_msg = NULL; // We yank the outgoing pipe id from the header @@ -278,7 +273,6 @@ nni_rep_sock_getq_cb(void *arg) nni_msgq_aio_get(uwq, &rep->aio_getq); } - static void nni_rep_pipe_getq_cb(void *arg) { @@ -295,7 +289,6 @@ nni_rep_pipe_getq_cb(void *arg) nni_pipe_send(rp->pipe, &rp->aio_send); } - static void nni_rep_pipe_send_cb(void *arg) { @@ -311,17 +304,16 @@ nni_rep_pipe_send_cb(void *arg) nni_msgq_aio_get(rp->sendq, &rp->aio_getq); } - static void nni_rep_pipe_recv_cb(void *arg) { - nni_rep_pipe *rp = arg; + nni_rep_pipe *rp = arg; nni_rep_sock *rep = rp->rep; - nni_msg *msg; - int rv; - uint8_t idbuf[4]; - uint8_t *body; - int hops; + nni_msg * msg; + int rv; + uint8_t idbuf[4]; + uint8_t * body; + int hops; if (nni_aio_result(&rp->aio_recv) != 0) { nni_pipe_stop(rp->pipe); @@ -330,7 +322,7 @@ nni_rep_pipe_recv_cb(void *arg) NNI_PUT32(idbuf, rp->id); - msg = rp->aio_recv.a_msg; + msg = rp->aio_recv.a_msg; rp->aio_recv.a_msg = NULL; // Store the pipe id in the header, first thing. @@ -350,8 +342,8 @@ nni_rep_pipe_recv_cb(void *arg) goto malformed; } body = nni_msg_body(msg); - end = (body[0] & 0x80) ? 1 : 0; - rv = nni_msg_append_header(msg, body, 4); + end = (body[0] & 0x80) ? 1 : 0; + rv = nni_msg_append_header(msg, body, 4); if (rv != 0) { // Presumably this is due to out of memory. // We could just discard and try again, but we @@ -376,7 +368,6 @@ malformed: nni_pipe_stop(rp->pipe); } - static void nni_rep_pipe_putq_cb(void *arg) { @@ -392,12 +383,11 @@ nni_rep_pipe_putq_cb(void *arg) nni_pipe_recv(rp->pipe, &rp->aio_recv); } - static int nni_rep_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_rep_sock *rep = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_MAXTTL: @@ -413,12 +403,11 @@ nni_rep_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_rep_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_rep_sock *rep = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_MAXTTL: @@ -433,7 +422,6 @@ nni_rep_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - static nni_msg * nni_rep_sock_sfilter(void *arg, nni_msg *msg) { @@ -458,36 +446,35 @@ nni_rep_sock_sfilter(void *arg, nni_msg *msg) if (nni_msg_append_header(msg, rep->btrace, rep->btrace_len) != 0) { nni_free(rep->btrace, rep->btrace_len); - rep->btrace = NULL; + rep->btrace = NULL; rep->btrace_len = 0; nni_msg_free(msg); return (NULL); } nni_free(rep->btrace, rep->btrace_len); - rep->btrace = NULL; + rep->btrace = NULL; rep->btrace_len = 0; return (msg); } - static nni_msg * nni_rep_sock_rfilter(void *arg, nni_msg *msg) { nni_rep_sock *rep = arg; - char *header; - size_t len; + char * header; + size_t len; if (rep->raw) { return (msg); } nni_sock_senderr(rep->sock, 0); - len = nni_msg_header_len(msg); + len = nni_msg_header_len(msg); header = nni_msg_header(msg); if (rep->btrace != NULL) { nni_free(rep->btrace, rep->btrace_len); - rep->btrace = NULL; + rep->btrace = NULL; rep->btrace_len = 0; } if ((rep->btrace = nni_alloc(len)) == NULL) { @@ -500,32 +487,31 @@ nni_rep_sock_rfilter(void *arg, nni_msg *msg) return (msg); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_rep_pipe_ops = { - .pipe_init = nni_rep_pipe_init, - .pipe_fini = nni_rep_pipe_fini, - .pipe_start = nni_rep_pipe_start, - .pipe_stop = nni_rep_pipe_stop, + .pipe_init = nni_rep_pipe_init, + .pipe_fini = nni_rep_pipe_fini, + .pipe_start = nni_rep_pipe_start, + .pipe_stop = nni_rep_pipe_stop, }; static nni_proto_sock_ops nni_rep_sock_ops = { - .sock_init = nni_rep_sock_init, - .sock_fini = nni_rep_sock_fini, - .sock_open = nni_rep_sock_open, - .sock_close = nni_rep_sock_close, - .sock_setopt = nni_rep_sock_setopt, - .sock_getopt = nni_rep_sock_getopt, - .sock_rfilter = nni_rep_sock_rfilter, - .sock_sfilter = nni_rep_sock_sfilter, + .sock_init = nni_rep_sock_init, + .sock_fini = nni_rep_sock_fini, + .sock_open = nni_rep_sock_open, + .sock_close = nni_rep_sock_close, + .sock_setopt = nni_rep_sock_setopt, + .sock_getopt = nni_rep_sock_getopt, + .sock_rfilter = nni_rep_sock_rfilter, + .sock_sfilter = nni_rep_sock_sfilter, }; nni_proto nni_rep_proto = { - .proto_self = NNG_PROTO_REP, - .proto_peer = NNG_PROTO_REQ, - .proto_name = "rep", - .proto_flags = NNI_PROTO_FLAG_SNDRCV, + .proto_self = NNG_PROTO_REP, + .proto_peer = NNG_PROTO_REQ, + .proto_name = "rep", + .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &nni_rep_sock_ops, .proto_pipe_ops = &nni_rep_pipe_ops, }; diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index f32fd66f..f77700f5 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -7,9 +7,9 @@ // found online at https://opensource.org/licenses/MIT. // +#include #include #include -#include #include "core/nng_impl.h" @@ -17,8 +17,8 @@ // request-reply pair. This is useful for building RPC clients, for // example. -typedef struct nni_req_pipe nni_req_pipe; -typedef struct nni_req_sock nni_req_sock; +typedef struct nni_req_pipe nni_req_pipe; +typedef struct nni_req_sock nni_req_sock; static void nni_req_resend(nni_req_sock *); static void nni_req_timeout(void *); @@ -26,38 +26,38 @@ static void nni_req_pipe_fini(void *); // An nni_req_sock is our per-socket protocol private structure. struct nni_req_sock { - nni_sock * sock; - nni_msgq * uwq; - nni_msgq * urq; - nni_duration retry; - nni_time resend; - int raw; - int wantw; - nni_msg * reqmsg; + nni_sock * sock; + nni_msgq * uwq; + nni_msgq * urq; + nni_duration retry; + nni_time resend; + int raw; + int wantw; + nni_msg * reqmsg; - nni_req_pipe * pendpipe; + nni_req_pipe *pendpipe; - nni_list readypipes; - nni_list busypipes; + nni_list readypipes; + nni_list busypipes; - nni_timer_node timer; + nni_timer_node timer; - uint32_t nextid; // next id - uint8_t reqid[4]; // outstanding request ID (big endian) - nni_mtx mtx; + uint32_t nextid; // next id + uint8_t reqid[4]; // outstanding request ID (big endian) + nni_mtx mtx; }; // An nni_req_pipe is our per-pipe protocol private structure. struct nni_req_pipe { - nni_pipe * pipe; - nni_req_sock * req; - nni_list_node node; - nni_aio aio_getq; // raw mode only - nni_aio aio_sendraw; // raw mode only - nni_aio aio_sendcooked; // cooked mode only - nni_aio aio_recv; - nni_aio aio_putq; - nni_mtx mtx; + nni_pipe * pipe; + nni_req_sock *req; + nni_list_node node; + nni_aio aio_getq; // raw mode only + nni_aio aio_sendraw; // raw mode only + nni_aio aio_sendcooked; // cooked mode only + nni_aio aio_recv; + nni_aio aio_putq; + nni_mtx mtx; }; static void nni_req_resender(void *); @@ -71,7 +71,7 @@ static int nni_req_sock_init(void **reqp, nni_sock *sock) { nni_req_sock *req; - int rv; + int rv; if ((req = NNI_ALLOC_STRUCT(req)) == NULL) { return (NNG_ENOMEM); @@ -88,21 +88,20 @@ nni_req_sock_init(void **reqp, nni_sock *sock) // this is "semi random" start for request IDs. req->nextid = nni_random(); - req->retry = NNI_SECOND * 60; - req->sock = sock; + req->retry = NNI_SECOND * 60; + req->sock = sock; req->reqmsg = NULL; - req->raw = 0; - req->wantw = 0; + req->raw = 0; + req->wantw = 0; req->resend = NNI_TIME_ZERO; req->uwq = nni_sock_sendq(sock); req->urq = nni_sock_recvq(sock); - *reqp = req; + *reqp = req; nni_sock_recverr(sock, NNG_ESTATE); return (0); } - static void nni_req_sock_close(void *arg) { @@ -111,7 +110,6 @@ nni_req_sock_close(void *arg) nni_timer_cancel(&req->timer); } - static void nni_req_sock_fini(void *arg) { @@ -126,12 +124,11 @@ nni_req_sock_fini(void *arg) NNI_FREE_STRUCT(req); } - static int nni_req_pipe_init(void **rpp, nni_pipe *pipe, void *rsock) { nni_req_pipe *rp; - int rv; + int rv; if ((rp = NNI_ALLOC_STRUCT(rp)) == NULL) { return (NNG_ENOMEM); @@ -159,8 +156,8 @@ nni_req_pipe_init(void **rpp, nni_pipe *pipe, void *rsock) NNI_LIST_NODE_INIT(&rp->node); rp->pipe = pipe; - rp->req = rsock; - *rpp = rp; + rp->req = rsock; + *rpp = rp; return (0); failed: @@ -168,7 +165,6 @@ failed: return (rv); } - static void nni_req_pipe_fini(void *arg) { @@ -185,11 +181,10 @@ nni_req_pipe_fini(void *arg) } } - static int nni_req_pipe_start(void *arg) { - nni_req_pipe *rp = arg; + nni_req_pipe *rp = arg; nni_req_sock *req = rp->req; if (nni_pipe_peer(rp->pipe) != NNG_PROTO_REP) { @@ -203,17 +198,15 @@ nni_req_pipe_start(void *arg) } nni_mtx_unlock(&req->mtx); - nni_msgq_aio_get(req->uwq, &rp->aio_getq); nni_pipe_recv(rp->pipe, &rp->aio_recv); return (0); } - static void nni_req_pipe_stop(void *arg) { - nni_req_pipe *rp = arg; + nni_req_pipe *rp = arg; nni_req_sock *req = rp->req; nni_aio_stop(&rp->aio_getq); @@ -236,19 +229,18 @@ nni_req_pipe_stop(void *arg) // removing the pipe we sent the last request on... // schedule immediate resend. req->pendpipe = NULL; - req->resend = NNI_TIME_ZERO; - req->wantw = 1; + req->resend = NNI_TIME_ZERO; + req->wantw = 1; nni_req_resend(req); } nni_mtx_unlock(&req->mtx); } - static int nni_req_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_req_sock *req = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RESENDTIME: @@ -263,12 +255,11 @@ nni_req_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_req_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_req_sock *req = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_RESENDTIME: @@ -283,7 +274,6 @@ nni_req_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - // Raw and cooked mode differ in the way they send messages out. // // For cooked mdes, we have a getq callback on the upper write queue, which @@ -303,7 +293,7 @@ nni_req_sock_getopt(void *arg, int opt, void *buf, size_t *szp) static void nni_req_getq_cb(void *arg) { - nni_req_pipe *rp = arg; + nni_req_pipe *rp = arg; nni_req_sock *req = rp->req; // We should be in RAW mode. Cooked mode traffic bypasses @@ -318,13 +308,12 @@ nni_req_getq_cb(void *arg) } rp->aio_sendraw.a_msg = rp->aio_getq.a_msg; - rp->aio_getq.a_msg = NULL; + rp->aio_getq.a_msg = NULL; // Send the message, but use the raw mode aio. nni_pipe_send(rp->pipe, &rp->aio_sendraw); } - static void nni_req_sendraw_cb(void *arg) { @@ -341,11 +330,10 @@ nni_req_sendraw_cb(void *arg) nni_msgq_aio_get(rp->req->uwq, &rp->aio_getq); } - static void nni_req_sendcooked_cb(void *arg) { - nni_req_pipe *rp = arg; + nni_req_pipe *rp = arg; nni_req_sock *req = rp->req; if (nni_aio_result(&rp->aio_sendcooked) != 0) { @@ -377,7 +365,6 @@ nni_req_sendcooked_cb(void *arg) nni_mtx_unlock(&req->mtx); } - static void nni_req_putq_cb(void *arg) { @@ -393,19 +380,18 @@ nni_req_putq_cb(void *arg) nni_pipe_recv(rp->pipe, &rp->aio_recv); } - static void nni_req_recv_cb(void *arg) { nni_req_pipe *rp = arg; - nni_msg *msg; + nni_msg * msg; if (nni_aio_result(&rp->aio_recv) != 0) { nni_pipe_stop(rp->pipe); return; } - msg = rp->aio_recv.a_msg; + msg = rp->aio_recv.a_msg; rp->aio_recv.a_msg = NULL; // We yank 4 bytes of body, and move them to the header. @@ -434,7 +420,6 @@ malformed: nni_pipe_stop(rp->pipe); } - static void nni_req_timeout(void *arg) { @@ -448,12 +433,11 @@ nni_req_timeout(void *arg) nni_mtx_unlock(&req->mtx); } - static void nni_req_resend(nni_req_sock *req) { nni_req_pipe *rp; - nni_msg *msg; + nni_msg * msg; // Note: This routine should be called with the socket lock held. // Also, this should only be called while handling cooked mode @@ -470,8 +454,8 @@ nni_req_resend(nni_req_sock *req) // mark that we have a message we want to resend, // in case something comes available. req->wantw = 1; - nni_timer_schedule(&req->timer, - nni_clock() + req->retry); + nni_timer_schedule( + &req->timer, nni_clock() + req->retry); return; } @@ -489,8 +473,8 @@ nni_req_resend(nni_req_sock *req) nni_list_remove(&req->readypipes, rp); nni_list_append(&req->busypipes, rp); - req->pendpipe = rp; - req->resend = nni_clock() + req->retry; + req->pendpipe = rp; + req->resend = nni_clock() + req->retry; rp->aio_sendcooked.a_msg = msg; // Note that because we were ready rather than busy, we @@ -501,12 +485,11 @@ nni_req_resend(nni_req_sock *req) } } - static nni_msg * nni_req_sock_sfilter(void *arg, nni_msg *msg) { nni_req_sock *req = arg; - uint32_t id; + uint32_t id; if (req->raw) { // No automatic retry, and the request ID must @@ -542,7 +525,7 @@ nni_req_sock_sfilter(void *arg, nni_msg *msg) req->reqmsg = msg; // Schedule for immediate send req->resend = NNI_TIME_ZERO; - req->wantw = 1; + req->wantw = 1; nni_req_resend(req); nni_mtx_unlock(&req->mtx); @@ -553,12 +536,11 @@ nni_req_sock_sfilter(void *arg, nni_msg *msg) return (NULL); } - static nni_msg * nni_req_sock_rfilter(void *arg, nni_msg *msg) { nni_req_sock *req = arg; - nni_msg *rmsg; + nni_msg * rmsg; if (req->raw) { // Pass it unmolested @@ -585,7 +567,7 @@ nni_req_sock_rfilter(void *arg, nni_msg *msg) return (NULL); } - req->reqmsg = NULL; + req->reqmsg = NULL; req->pendpipe = NULL; nni_mtx_unlock(&req->mtx); @@ -595,31 +577,30 @@ nni_req_sock_rfilter(void *arg, nni_msg *msg) return (msg); } - // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. static nni_proto_pipe_ops nni_req_pipe_ops = { - .pipe_init = nni_req_pipe_init, - .pipe_fini = nni_req_pipe_fini, - .pipe_start = nni_req_pipe_start, - .pipe_stop = nni_req_pipe_stop, + .pipe_init = nni_req_pipe_init, + .pipe_fini = nni_req_pipe_fini, + .pipe_start = nni_req_pipe_start, + .pipe_stop = nni_req_pipe_stop, }; static nni_proto_sock_ops nni_req_sock_ops = { - .sock_init = nni_req_sock_init, - .sock_fini = nni_req_sock_fini, - .sock_close = nni_req_sock_close, - .sock_setopt = nni_req_sock_setopt, - .sock_getopt = nni_req_sock_getopt, - .sock_rfilter = nni_req_sock_rfilter, - .sock_sfilter = nni_req_sock_sfilter, + .sock_init = nni_req_sock_init, + .sock_fini = nni_req_sock_fini, + .sock_close = nni_req_sock_close, + .sock_setopt = nni_req_sock_setopt, + .sock_getopt = nni_req_sock_getopt, + .sock_rfilter = nni_req_sock_rfilter, + .sock_sfilter = nni_req_sock_sfilter, }; nni_proto nni_req_proto = { - .proto_self = NNG_PROTO_REQ, - .proto_peer = NNG_PROTO_REP, - .proto_name = "req", - .proto_flags = NNI_PROTO_FLAG_SNDRCV, + .proto_self = NNG_PROTO_REQ, + .proto_peer = NNG_PROTO_REP, + .proto_name = "req", + .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &nni_req_sock_ops, .proto_pipe_ops = &nni_req_pipe_ops, }; diff --git a/src/protocol/survey/respond.c b/src/protocol/survey/respond.c index 73cc4792..6a784738 100644 --- a/src/protocol/survey/respond.c +++ b/src/protocol/survey/respond.c @@ -16,8 +16,8 @@ // the surveyor pattern. This is useful for building service discovery, or // voting algorithsm, for example. -typedef struct nni_resp_pipe nni_resp_pipe; -typedef struct nni_resp_sock nni_resp_sock; +typedef struct nni_resp_pipe nni_resp_pipe; +typedef struct nni_resp_sock nni_resp_sock; static void nni_resp_recv_cb(void *); static void nni_resp_putq_cb(void *); @@ -28,31 +28,30 @@ static void nni_resp_pipe_fini(void *); // An nni_resp_sock is our per-socket protocol private structure. struct nni_resp_sock { - nni_sock * nsock; - nni_msgq * urq; - nni_msgq * uwq; - int raw; - int ttl; - nni_idhash pipes; - char * btrace; - size_t btrace_len; - nni_aio aio_getq; - nni_mtx mtx; + nni_sock * nsock; + nni_msgq * urq; + nni_msgq * uwq; + int raw; + int ttl; + nni_idhash pipes; + char * btrace; + size_t btrace_len; + nni_aio aio_getq; + nni_mtx mtx; }; // An nni_resp_pipe is our per-pipe protocol private structure. struct nni_resp_pipe { - nni_pipe * npipe; - nni_resp_sock * psock; - uint32_t id; - nni_msgq * sendq; - nni_aio aio_getq; - nni_aio aio_putq; - nni_aio aio_send; - nni_aio aio_recv; + nni_pipe * npipe; + nni_resp_sock *psock; + uint32_t id; + nni_msgq * sendq; + nni_aio aio_getq; + nni_aio aio_putq; + nni_aio aio_send; + nni_aio aio_recv; }; - static void nni_resp_sock_fini(void *arg) { @@ -69,23 +68,22 @@ nni_resp_sock_fini(void *arg) } } - static int nni_resp_sock_init(void **pp, nni_sock *nsock) { nni_resp_sock *psock; - int rv; + int rv; if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) { return (NNG_ENOMEM); } - psock->ttl = 8; // Per RFC - psock->nsock = nsock; - psock->raw = 0; - psock->btrace = NULL; + psock->ttl = 8; // Per RFC + psock->nsock = nsock; + psock->raw = 0; + psock->btrace = NULL; psock->btrace_len = 0; - psock->urq = nni_sock_recvq(nsock); - psock->uwq = nni_sock_sendq(nsock); + psock->urq = nni_sock_recvq(nsock); + psock->uwq = nni_sock_sendq(nsock); if (((rv = nni_idhash_init(&psock->pipes)) != 0) || ((rv = nni_mtx_init(&psock->mtx)) != 0)) { goto fail; @@ -104,7 +102,6 @@ fail: return (rv); } - static void nni_resp_sock_open(void *arg) { @@ -113,7 +110,6 @@ nni_resp_sock_open(void *arg) nni_msgq_aio_get(psock->uwq, &psock->aio_getq); } - static void nni_resp_sock_close(void *arg) { @@ -122,12 +118,11 @@ nni_resp_sock_close(void *arg) nni_aio_stop(&psock->aio_getq); } - static int nni_resp_pipe_init(void **pp, nni_pipe *npipe, void *psock) { nni_resp_pipe *ppipe; - int rv; + int rv; if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) { return (NNG_ENOMEM); @@ -154,7 +149,7 @@ nni_resp_pipe_init(void **pp, nni_pipe *npipe, void *psock) ppipe->npipe = npipe; ppipe->psock = psock; - *pp = ppipe; + *pp = ppipe; return (0); fail: @@ -162,7 +157,6 @@ fail: return (rv); } - static void nni_resp_pipe_fini(void *arg) { @@ -176,13 +170,12 @@ nni_resp_pipe_fini(void *arg) NNI_FREE_STRUCT(ppipe); } - static int nni_resp_pipe_start(void *arg) { nni_resp_pipe *ppipe = arg; nni_resp_sock *psock = ppipe->psock; - int rv; + int rv; ppipe->id = nni_pipe_id(ppipe->npipe); @@ -199,7 +192,6 @@ nni_resp_pipe_start(void *arg) return (rv); } - static void nni_resp_pipe_stop(void *arg) { @@ -220,7 +212,6 @@ nni_resp_pipe_stop(void *arg) nni_mtx_unlock(&psock->mtx); } - // nni_resp_sock_send watches for messages from the upper write queue, // extracts the destination pipe, and forwards it to the appropriate // destination pipe via a separate queue. This prevents a single bad @@ -230,16 +221,16 @@ void nni_resp_sock_getq_cb(void *arg) { nni_resp_sock *psock = arg; - nni_msg *msg; - uint8_t *header; - uint32_t id; + nni_msg * msg; + uint8_t * header; + uint32_t id; nni_resp_pipe *ppipe; - int rv; + int rv; if (nni_aio_result(&psock->aio_getq) != 0) { return; } - msg = psock->aio_getq.a_msg; + msg = psock->aio_getq.a_msg; psock->aio_getq.a_msg = NULL; // We yank the outgoing pipe id from the header @@ -269,7 +260,6 @@ nni_resp_sock_getq_cb(void *arg) nni_mtx_unlock(&psock->mtx); } - void nni_resp_getq_cb(void *arg) { @@ -286,7 +276,6 @@ nni_resp_getq_cb(void *arg) nni_pipe_send(ppipe->npipe, &ppipe->aio_send); } - void nni_resp_send_cb(void *arg) { @@ -302,17 +291,16 @@ nni_resp_send_cb(void *arg) nni_msgq_aio_get(ppipe->sendq, &ppipe->aio_getq); } - static void nni_resp_recv_cb(void *arg) { nni_resp_pipe *ppipe = arg; nni_resp_sock *psock = ppipe->psock; - nni_msgq *urq; - nni_msg *msg; - uint8_t idbuf[4]; - int hops; - int rv; + nni_msgq * urq; + nni_msg * msg; + uint8_t idbuf[4]; + int hops; + int rv; if (nni_aio_result(&ppipe->aio_recv) != 0) { goto error; @@ -322,7 +310,7 @@ nni_resp_recv_cb(void *arg) NNI_PUT32(idbuf, ppipe->id); - msg = ppipe->aio_recv.a_msg; + msg = ppipe->aio_recv.a_msg; ppipe->aio_recv.a_msg = NULL; // Store the pipe id in the header, first thing. @@ -334,7 +322,7 @@ nni_resp_recv_cb(void *arg) // Move backtrace from body to header hops = 0; for (;;) { - int end = 0; + int end = 0; uint8_t *body; if (hops >= psock->ttl) { @@ -346,8 +334,8 @@ nni_resp_recv_cb(void *arg) goto error; } body = nni_msg_body(msg); - end = (body[0] & 0x80) ? 1 : 0; - rv = nni_msg_append_header(msg, body, 4); + end = (body[0] & 0x80) ? 1 : 0; + rv = nni_msg_append_header(msg, body, 4); if (rv != 0) { nni_msg_free(msg); goto error; @@ -367,7 +355,6 @@ error: nni_pipe_stop(ppipe->npipe); } - static void nni_resp_putq_cb(void *arg) { @@ -382,13 +369,12 @@ nni_resp_putq_cb(void *arg) nni_pipe_recv(ppipe->npipe, &ppipe->aio_recv); } - static int nni_resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_resp_sock *psock = arg; - int rv; - int oldraw; + int rv; + int oldraw; switch (opt) { case NNG_OPT_MAXTTL: @@ -396,7 +382,7 @@ nni_resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz) break; case NNG_OPT_RAW: oldraw = psock->raw; - rv = nni_setopt_int(&psock->raw, buf, sz, 0, 1); + rv = nni_setopt_int(&psock->raw, buf, sz, 0, 1); if (oldraw != psock->raw) { if (!psock->raw) { nni_sock_senderr(psock->nsock, 0); @@ -411,12 +397,11 @@ nni_resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_resp_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_resp_sock *psock = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_MAXTTL: @@ -431,7 +416,6 @@ nni_resp_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - static nni_msg * nni_resp_sock_sfilter(void *arg, nni_msg *msg) { @@ -454,38 +438,38 @@ nni_resp_sock_sfilter(void *arg, nni_msg *msg) // drop anything else in the header... nni_msg_trunc_header(msg, nni_msg_header_len(msg)); - if (nni_msg_append_header(msg, psock->btrace, psock->btrace_len) != 0) { + if (nni_msg_append_header(msg, psock->btrace, psock->btrace_len) != + 0) { nni_free(psock->btrace, psock->btrace_len); - psock->btrace = NULL; + psock->btrace = NULL; psock->btrace_len = 0; nni_msg_free(msg); return (NULL); } nni_free(psock->btrace, psock->btrace_len); - psock->btrace = NULL; + psock->btrace = NULL; psock->btrace_len = 0; return (msg); } - static nni_msg * nni_resp_sock_rfilter(void *arg, nni_msg *msg) { nni_resp_sock *psock = arg; - char *header; - size_t len; + char * header; + size_t len; if (psock->raw) { return (msg); } nni_sock_senderr(psock->nsock, 0); - len = nni_msg_header_len(msg); + len = nni_msg_header_len(msg); header = nni_msg_header(msg); if (psock->btrace != NULL) { nni_free(psock->btrace, psock->btrace_len); - psock->btrace = NULL; + psock->btrace = NULL; psock->btrace_len = 0; } if ((psock->btrace = nni_alloc(len)) == NULL) { @@ -498,30 +482,29 @@ nni_resp_sock_rfilter(void *arg, nni_msg *msg) return (msg); } - static nni_proto_pipe_ops nni_resp_pipe_ops = { - .pipe_init = nni_resp_pipe_init, - .pipe_fini = nni_resp_pipe_fini, - .pipe_start = nni_resp_pipe_start, - .pipe_stop = nni_resp_pipe_stop, + .pipe_init = nni_resp_pipe_init, + .pipe_fini = nni_resp_pipe_fini, + .pipe_start = nni_resp_pipe_start, + .pipe_stop = nni_resp_pipe_stop, }; static nni_proto_sock_ops nni_resp_sock_ops = { - .sock_init = nni_resp_sock_init, - .sock_fini = nni_resp_sock_fini, - .sock_open = nni_resp_sock_open, - .sock_close = nni_resp_sock_close, - .sock_setopt = nni_resp_sock_setopt, - .sock_getopt = nni_resp_sock_getopt, - .sock_rfilter = nni_resp_sock_rfilter, - .sock_sfilter = nni_resp_sock_sfilter, + .sock_init = nni_resp_sock_init, + .sock_fini = nni_resp_sock_fini, + .sock_open = nni_resp_sock_open, + .sock_close = nni_resp_sock_close, + .sock_setopt = nni_resp_sock_setopt, + .sock_getopt = nni_resp_sock_getopt, + .sock_rfilter = nni_resp_sock_rfilter, + .sock_sfilter = nni_resp_sock_sfilter, }; nni_proto nni_respondent_proto = { - .proto_self = NNG_PROTO_RESPONDENT, - .proto_peer = NNG_PROTO_SURVEYOR, - .proto_name = "respondent", - .proto_flags = NNI_PROTO_FLAG_SNDRCV, + .proto_self = NNG_PROTO_RESPONDENT, + .proto_peer = NNG_PROTO_SURVEYOR, + .proto_name = "respondent", + .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &nni_resp_sock_ops, .proto_pipe_ops = &nni_resp_pipe_ops, }; diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c index 14d73028..e7d0f3ce 100644 --- a/src/protocol/survey/survey.c +++ b/src/protocol/survey/survey.c @@ -15,8 +15,8 @@ // Surveyor protocol. The SURVEYOR protocol is the "survey" side of the // survey pattern. This is useful for building service discovery, voting, etc. -typedef struct nni_surv_pipe nni_surv_pipe; -typedef struct nni_surv_sock nni_surv_sock; +typedef struct nni_surv_pipe nni_surv_pipe; +typedef struct nni_surv_sock nni_surv_sock; static void nni_surv_sock_getq_cb(void *); static void nni_surv_getq_cb(void *); @@ -27,31 +27,31 @@ static void nni_surv_timeout(void *); // An nni_surv_sock is our per-socket protocol private structure. struct nni_surv_sock { - nni_sock * nsock; - nni_duration survtime; - nni_time expire; - int raw; - int closing; - uint32_t nextid; // next id - uint8_t survid[4]; // outstanding request ID (big endian) - nni_list pipes; - nni_aio aio_getq; - nni_timer_node timer; - nni_msgq * uwq; - nni_msgq * urq; - nni_mtx mtx; + nni_sock * nsock; + nni_duration survtime; + nni_time expire; + int raw; + int closing; + uint32_t nextid; // next id + uint8_t survid[4]; // outstanding request ID (big endian) + nni_list pipes; + nni_aio aio_getq; + nni_timer_node timer; + nni_msgq * uwq; + nni_msgq * urq; + nni_mtx mtx; }; // An nni_surv_pipe is our per-pipe protocol private structure. struct nni_surv_pipe { - nni_pipe * npipe; - nni_surv_sock * psock; - nni_msgq * sendq; - nni_list_node node; - nni_aio aio_getq; - nni_aio aio_putq; - nni_aio aio_send; - nni_aio aio_recv; + nni_pipe * npipe; + nni_surv_sock *psock; + nni_msgq * sendq; + nni_list_node node; + nni_aio aio_getq; + nni_aio aio_putq; + nni_aio aio_send; + nni_aio aio_recv; }; static void @@ -64,12 +64,11 @@ nni_surv_sock_fini(void *arg) NNI_FREE_STRUCT(psock); } - static int nni_surv_sock_init(void **sp, nni_sock *nsock) { nni_surv_sock *psock; - int rv; + int rv; if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) { return (NNG_ENOMEM); @@ -84,13 +83,13 @@ nni_surv_sock_init(void **sp, nni_sock *nsock) NNI_LIST_INIT(&psock->pipes, nni_surv_pipe, node); nni_timer_init(&psock->timer, nni_surv_timeout, psock); - psock->nextid = nni_random(); - psock->nsock = nsock; - psock->raw = 0; + psock->nextid = nni_random(); + psock->nsock = nsock; + psock->raw = 0; psock->survtime = NNI_SECOND * 60; - psock->expire = NNI_TIME_ZERO; - psock->uwq = nni_sock_sendq(nsock); - psock->urq = nni_sock_recvq(nsock); + psock->expire = NNI_TIME_ZERO; + psock->uwq = nni_sock_sendq(nsock); + psock->urq = nni_sock_recvq(nsock); *sp = psock; nni_sock_recverr(nsock, NNG_ESTATE); @@ -101,7 +100,6 @@ fail: return (rv); } - static void nni_surv_sock_open(void *arg) { @@ -110,7 +108,6 @@ nni_surv_sock_open(void *arg) nni_msgq_aio_get(psock->uwq, &psock->aio_getq); } - static void nni_surv_sock_close(void *arg) { @@ -120,7 +117,6 @@ nni_surv_sock_close(void *arg) nni_aio_stop(&psock->aio_getq); } - static void nni_surv_pipe_fini(void *arg) { @@ -134,12 +130,11 @@ nni_surv_pipe_fini(void *arg) NNI_FREE_STRUCT(ppipe); } - static int nni_surv_pipe_init(void **pp, nni_pipe *npipe, void *psock) { nni_surv_pipe *ppipe; - int rv; + int rv; if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) { return (NNG_ENOMEM); @@ -166,7 +161,7 @@ nni_surv_pipe_init(void **pp, nni_pipe *npipe, void *psock) } ppipe->npipe = npipe; ppipe->psock = psock; - *pp = ppipe; + *pp = ppipe; return (0); failed: @@ -174,7 +169,6 @@ failed: return (rv); } - static int nni_surv_pipe_start(void *arg) { @@ -190,7 +184,6 @@ nni_surv_pipe_start(void *arg) return (0); } - static void nni_surv_pipe_stop(void *arg) { @@ -210,7 +203,6 @@ nni_surv_pipe_stop(void *arg) nni_mtx_unlock(&psock->mtx); } - static void nni_surv_getq_cb(void *arg) { @@ -227,7 +219,6 @@ nni_surv_getq_cb(void *arg) nni_pipe_send(ppipe->npipe, &ppipe->aio_send); } - static void nni_surv_send_cb(void *arg) { @@ -243,7 +234,6 @@ nni_surv_send_cb(void *arg) nni_msgq_aio_get(ppipe->psock->uwq, &ppipe->aio_getq); } - static void nni_surv_putq_cb(void *arg) { @@ -259,18 +249,17 @@ nni_surv_putq_cb(void *arg) nni_pipe_recv(ppipe->npipe, &ppipe->aio_recv); } - static void nni_surv_recv_cb(void *arg) { nni_surv_pipe *ppipe = arg; - nni_msg *msg; + nni_msg * msg; if (nni_aio_result(&ppipe->aio_recv) != 0) { goto failed; } - msg = ppipe->aio_recv.a_msg; + msg = ppipe->aio_recv.a_msg; ppipe->aio_recv.a_msg = NULL; // We yank 4 bytes of body, and move them to the header. @@ -297,13 +286,12 @@ failed: nni_pipe_stop(ppipe->npipe); } - static int nni_surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz) { nni_surv_sock *psock = arg; - int rv; - int oldraw; + int rv; + int oldraw; switch (opt) { case NNG_OPT_SURVEYTIME: @@ -311,14 +299,14 @@ nni_surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz) break; case NNG_OPT_RAW: oldraw = psock->raw; - rv = nni_setopt_int(&psock->raw, buf, sz, 0, 1); + rv = nni_setopt_int(&psock->raw, buf, sz, 0, 1); if (oldraw != psock->raw) { if (psock->raw) { nni_sock_recverr(psock->nsock, 0); } else { nni_sock_recverr(psock->nsock, NNG_ESTATE); } - memset(psock->survid, 0, sizeof (psock->survid)); + memset(psock->survid, 0, sizeof(psock->survid)); nni_timer_cancel(&psock->timer); } break; @@ -328,12 +316,11 @@ nni_surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz) return (rv); } - static int nni_surv_sock_getopt(void *arg, int opt, void *buf, size_t *szp) { nni_surv_sock *psock = arg; - int rv; + int rv; switch (opt) { case NNG_OPT_SURVEYTIME: @@ -348,20 +335,19 @@ nni_surv_sock_getopt(void *arg, int opt, void *buf, size_t *szp) return (rv); } - static void nni_surv_sock_getq_cb(void *arg) { nni_surv_sock *psock = arg; nni_surv_pipe *ppipe; nni_surv_pipe *last; - nni_msg *msg, *dup; + nni_msg * msg, *dup; if (nni_aio_result(&psock->aio_getq) != 0) { // Should be NNG_ECLOSED. return; } - msg = psock->aio_getq.a_msg; + msg = psock->aio_getq.a_msg; psock->aio_getq.a_msg = NULL; nni_mtx_lock(&psock->mtx); @@ -386,25 +372,23 @@ nni_surv_sock_getq_cb(void *arg) } } - static void nni_surv_timeout(void *arg) { nni_surv_sock *psock = arg; nni_sock_lock(psock->nsock); - memset(psock->survid, 0, sizeof (psock->survid)); + memset(psock->survid, 0, sizeof(psock->survid)); nni_sock_recverr(psock->nsock, NNG_ESTATE); nni_msgq_set_get_error(psock->urq, NNG_ETIMEDOUT); nni_sock_unlock(psock->nsock); } - static nni_msg * nni_surv_sock_sfilter(void *arg, nni_msg *msg) { nni_surv_sock *psock = arg; - uint32_t id; + uint32_t id; if (psock->raw) { // No automatic retry, and the request ID must @@ -434,12 +418,11 @@ nni_surv_sock_sfilter(void *arg, nni_msg *msg) // Clear the error condition. nni_sock_recverr(psock->nsock, 0); - //nni_msgq_set_get_error(nni_sock_recvq(psock->nsock), 0); + // nni_msgq_set_get_error(nni_sock_recvq(psock->nsock), 0); return (msg); } - static nni_msg * nni_surv_sock_rfilter(void *arg, nni_msg *msg) { @@ -466,32 +449,31 @@ nni_surv_sock_rfilter(void *arg, nni_msg *msg) return (msg); } - static nni_proto_pipe_ops nni_surv_pipe_ops = { - .pipe_init = nni_surv_pipe_init, - .pipe_fini = nni_surv_pipe_fini, - .pipe_start = nni_surv_pipe_start, - .pipe_stop = nni_surv_pipe_stop, + .pipe_init = nni_surv_pipe_init, + .pipe_fini = nni_surv_pipe_fini, + .pipe_start = nni_surv_pipe_start, + .pipe_stop = nni_surv_pipe_stop, }; static nni_proto_sock_ops nni_surv_sock_ops = { - .sock_init = nni_surv_sock_init, - .sock_fini = nni_surv_sock_fini, - .sock_open = nni_surv_sock_open, - .sock_close = nni_surv_sock_close, - .sock_setopt = nni_surv_sock_setopt, - .sock_getopt = nni_surv_sock_getopt, - .sock_rfilter = nni_surv_sock_rfilter, - .sock_sfilter = nni_surv_sock_sfilter, + .sock_init = nni_surv_sock_init, + .sock_fini = nni_surv_sock_fini, + .sock_open = nni_surv_sock_open, + .sock_close = nni_surv_sock_close, + .sock_setopt = nni_surv_sock_setopt, + .sock_getopt = nni_surv_sock_getopt, + .sock_rfilter = nni_surv_sock_rfilter, + .sock_sfilter = nni_surv_sock_sfilter, }; // This is the global protocol structure -- our linkage to the core. // This should be the only global non-static symbol in this file. nni_proto nni_surveyor_proto = { - .proto_self = NNG_PROTO_SURVEYOR, - .proto_peer = NNG_PROTO_RESPONDENT, - .proto_name = "surveyor", - .proto_flags = NNI_PROTO_FLAG_SNDRCV, + .proto_self = NNG_PROTO_SURVEYOR, + .proto_peer = NNG_PROTO_RESPONDENT, + .proto_name = "surveyor", + .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &nni_surv_sock_ops, .proto_pipe_ops = &nni_surv_pipe_ops, }; diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c index 01ffd39a..330c1d13 100644 --- a/src/transport/inproc/inproc.c +++ b/src/transport/inproc/inproc.c @@ -7,9 +7,9 @@ // found online at https://opensource.org/licenses/MIT. // +#include #include #include -#include #include "core/nng_impl.h" @@ -17,50 +17,46 @@ // peer to another. The inproc transport is only valid within the same // process. -typedef struct nni_inproc_pair nni_inproc_pair; -typedef struct nni_inproc_pipe nni_inproc_pipe; -typedef struct nni_inproc_ep nni_inproc_ep; +typedef struct nni_inproc_pair nni_inproc_pair; +typedef struct nni_inproc_pipe nni_inproc_pipe; +typedef struct nni_inproc_ep nni_inproc_ep; typedef struct { - nni_mtx mx; - nni_list servers; + nni_mtx mx; + nni_list servers; } nni_inproc_global; // nni_inproc_pipe represents one half of a connection. struct nni_inproc_pipe { - const char * addr; - nni_inproc_pair * pair; - nni_msgq * rq; - nni_msgq * wq; - uint16_t peer; - uint16_t proto; + const char * addr; + nni_inproc_pair *pair; + nni_msgq * rq; + nni_msgq * wq; + uint16_t peer; + uint16_t proto; }; // nni_inproc_pair represents a pair of pipes. Because we control both // sides of the pipes, we can allocate and free this in one structure. struct nni_inproc_pair { - nni_mtx mx; - int refcnt; - nni_msgq * q[2]; - nni_inproc_pipe * pipes[2]; + nni_mtx mx; + int refcnt; + nni_msgq * q[2]; + nni_inproc_pipe *pipes[2]; }; struct nni_inproc_ep { - char addr[NNG_MAXADDRLEN+1]; - int mode; - int closed; - int started; - nni_list_node node; - uint16_t proto; - nni_cv cv; - nni_list clients; - nni_list aios; + char addr[NNG_MAXADDRLEN + 1]; + int mode; + int closed; + int started; + nni_list_node node; + uint16_t proto; + nni_cv cv; + nni_list clients; + nni_list aios; }; -#define NNI_INPROC_EP_IDLE 0 -#define NNI_INPROC_EP_DIAL 1 -#define NNI_INPROC_EP_LISTEN 2 - // nni_inproc is our global state - this contains the list of active endpoints // which we use for coordinating rendezvous. static nni_inproc_global nni_inproc; @@ -79,14 +75,12 @@ nni_inproc_init(void) return (0); } - static void nni_inproc_fini(void) { nni_mtx_fini(&nni_inproc.mx); } - static void nni_inproc_pipe_close(void *arg) { @@ -100,7 +94,6 @@ nni_inproc_pipe_close(void *arg) } } - // nni_inproc_pair destroy is called when both pipe-ends of the pipe // have been destroyed. static void @@ -112,7 +105,6 @@ nni_inproc_pair_destroy(nni_inproc_pair *pair) NNI_FREE_STRUCT(pair); } - static int nni_inproc_pipe_init(nni_inproc_pipe **pipep, nni_inproc_ep *ep) { @@ -122,12 +114,11 @@ nni_inproc_pipe_init(nni_inproc_pipe **pipep, nni_inproc_ep *ep) return (NNG_ENOMEM); } pipe->proto = ep->proto; - pipe->addr = ep->addr; - *pipep = pipe; + pipe->addr = ep->addr; + *pipep = pipe; return (0); } - static void nni_inproc_pipe_fini(void *arg) { @@ -154,15 +145,14 @@ nni_inproc_pipe_fini(void *arg) NNI_FREE_STRUCT(pipe); } - static void nni_inproc_pipe_send(void *arg, nni_aio *aio) { nni_inproc_pipe *pipe = arg; - nni_msg *msg = aio->a_msg; - char *h; - size_t l; - int rv; + nni_msg * msg = aio->a_msg; + char * h; + size_t l; + int rv; // We need to move any header data to the body, because the other // side won't know what to do otherwise. @@ -176,7 +166,6 @@ nni_inproc_pipe_send(void *arg, nni_aio *aio) nni_msgq_aio_put(pipe->wq, aio); } - static void nni_inproc_pipe_recv(void *arg, nni_aio *aio) { @@ -185,7 +174,6 @@ nni_inproc_pipe_recv(void *arg, nni_aio *aio) nni_msgq_aio_get(pipe->rq, aio); } - static uint16_t nni_inproc_pipe_peer(void *arg) { @@ -194,12 +182,11 @@ nni_inproc_pipe_peer(void *arg) return (pipe->peer); } - static int nni_inproc_pipe_getopt(void *arg, int option, void *buf, size_t *szp) { nni_inproc_pipe *pipe = arg; - size_t len; + size_t len; switch (option) { case NNG_OPT_LOCALADDR: @@ -216,32 +203,30 @@ nni_inproc_pipe_getopt(void *arg, int option, void *buf, size_t *szp) return (NNG_ENOTSUP); } - static int nni_inproc_ep_init(void **epp, const char *url, nni_sock *sock, int mode) { nni_inproc_ep *ep; - if (strlen(url) > NNG_MAXADDRLEN-1) { + if (strlen(url) > NNG_MAXADDRLEN - 1) { return (NNG_EINVAL); } if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NNG_ENOMEM); } - ep->mode = mode; - ep->closed = 0; + ep->mode = mode; + ep->closed = 0; ep->started = 0; - ep->proto = nni_sock_proto(sock); + ep->proto = nni_sock_proto(sock); NNI_LIST_INIT(&ep->clients, nni_inproc_ep, node); nni_aio_list_init(&ep->aios); - (void) snprintf(ep->addr, sizeof (ep->addr), "%s", url); + (void) snprintf(ep->addr, sizeof(ep->addr), "%s", url); *epp = ep; return (0); } - static void nni_inproc_ep_fini(void *arg) { @@ -251,7 +236,6 @@ nni_inproc_ep_fini(void *arg) NNI_FREE_STRUCT(ep); } - static void nni_inproc_conn_finish(nni_aio *aio, int rv) { @@ -265,7 +249,7 @@ nni_inproc_conn_finish(nni_aio *aio, int rv) } nni_aio_list_remove(aio); if (ep != NULL) { - if ((ep->mode != NNI_INPROC_EP_LISTEN) && + if ((ep->mode != NNI_EP_MODE_LISTEN) && nni_list_empty(&ep->aios)) { if (nni_list_active(&ep->clients, ep)) { nni_list_remove(&ep->clients, ep); @@ -275,13 +259,12 @@ nni_inproc_conn_finish(nni_aio *aio, int rv) nni_aio_finish(aio, rv, 0); } - static void nni_inproc_ep_close(void *arg) { nni_inproc_ep *ep = arg; nni_inproc_ep *client; - nni_aio *aio; + nni_aio * aio; nni_mtx_lock(&nni_inproc.mx); ep->closed = 1; @@ -301,7 +284,6 @@ nni_inproc_ep_close(void *arg) nni_mtx_unlock(&nni_inproc.mx); } - static void nni_inproc_connect_abort(nni_aio *aio) { @@ -315,7 +297,7 @@ nni_inproc_connect_abort(nni_aio *aio) } nni_aio_list_remove(aio); if (ep != NULL) { - if ((ep->mode != NNI_INPROC_EP_LISTEN) && + if ((ep->mode != NNI_EP_MODE_LISTEN) && nni_list_empty(&ep->aios)) { if (nni_list_active(&ep->clients, ep)) { nni_list_remove(&ep->clients, ep); @@ -325,14 +307,13 @@ nni_inproc_connect_abort(nni_aio *aio) nni_mtx_unlock(&nni_inproc.mx); } - static void nni_inproc_accept_clients(nni_inproc_ep *server) { - nni_inproc_ep *client, *nclient; - nni_aio *saio, *caio; + nni_inproc_ep * client, *nclient; + nni_aio * saio, *caio; nni_inproc_pair *pair; - int rv; + int rv; nclient = nni_list_first(&server->clients); while ((client = nclient) != NULL) { @@ -358,14 +339,14 @@ nni_inproc_accept_clients(nni_inproc_ep *server) continue; } - pair->pipes[0] = caio->a_pipe; - pair->pipes[1] = saio->a_pipe; + pair->pipes[0] = caio->a_pipe; + pair->pipes[1] = saio->a_pipe; pair->pipes[0]->rq = pair->pipes[1]->wq = pair->q[0]; pair->pipes[1]->rq = pair->pipes[0]->wq = pair->q[1]; pair->pipes[0]->pair = pair->pipes[1]->pair = pair; pair->pipes[1]->peer = pair->pipes[0]->proto; pair->pipes[0]->peer = pair->pipes[1]->proto; - pair->refcnt = 2; + pair->refcnt = 2; nni_inproc_conn_finish(caio, 0); nni_inproc_conn_finish(saio, 0); @@ -381,14 +362,13 @@ nni_inproc_accept_clients(nni_inproc_ep *server) } } - static void nni_inproc_ep_connect(void *arg, nni_aio *aio) { - nni_inproc_ep *ep = arg; + nni_inproc_ep * ep = arg; nni_inproc_pipe *pipe; - nni_inproc_ep *server; - int rv; + nni_inproc_ep * server; + int rv; if (ep->mode != NNI_EP_MODE_DIAL) { nni_aio_finish(aio, NNG_EINVAL, 0); @@ -446,13 +426,12 @@ nni_inproc_ep_connect(void *arg, nni_aio *aio) nni_mtx_unlock(&nni_inproc.mx); } - static int nni_inproc_ep_bind(void *arg) { nni_inproc_ep *ep = arg; nni_inproc_ep *srch; - nni_list *list = &nni_inproc.servers; + nni_list * list = &nni_inproc.servers; if (ep->mode != NNI_EP_MODE_LISTEN) { return (NNG_EINVAL); @@ -467,8 +446,7 @@ nni_inproc_ep_bind(void *arg) return (NNG_ECLOSED); } NNI_LIST_FOREACH (list, srch) { - if ((srch->mode != NNI_EP_MODE_LISTEN) || - (!srch->started)) { + if ((srch->mode != NNI_EP_MODE_LISTEN) || (!srch->started)) { continue; } if (strcmp(srch->addr, ep->addr) == 0) { @@ -482,13 +460,12 @@ nni_inproc_ep_bind(void *arg) return (0); } - static void nni_inproc_ep_accept(void *arg, nni_aio *aio) { - nni_inproc_ep *ep = arg; + nni_inproc_ep * ep = arg; nni_inproc_pipe *pipe; - int rv; + int rv; if (ep->mode != NNI_EP_MODE_LISTEN) { nni_aio_finish(aio, NNG_EINVAL, 0); @@ -520,33 +497,32 @@ nni_inproc_ep_accept(void *arg, nni_aio *aio) nni_mtx_unlock(&nni_inproc.mx); } - static nni_tran_pipe nni_inproc_pipe_ops = { - .p_fini = nni_inproc_pipe_fini, - .p_send = nni_inproc_pipe_send, - .p_recv = nni_inproc_pipe_recv, - .p_close = nni_inproc_pipe_close, - .p_peer = nni_inproc_pipe_peer, - .p_getopt = nni_inproc_pipe_getopt, + .p_fini = nni_inproc_pipe_fini, + .p_send = nni_inproc_pipe_send, + .p_recv = nni_inproc_pipe_recv, + .p_close = nni_inproc_pipe_close, + .p_peer = nni_inproc_pipe_peer, + .p_getopt = nni_inproc_pipe_getopt, }; static nni_tran_ep nni_inproc_ep_ops = { - .ep_init = nni_inproc_ep_init, - .ep_fini = nni_inproc_ep_fini, - .ep_connect = nni_inproc_ep_connect, - .ep_bind = nni_inproc_ep_bind, - .ep_accept = nni_inproc_ep_accept, - .ep_close = nni_inproc_ep_close, - .ep_setopt = NULL, - .ep_getopt = NULL, + .ep_init = nni_inproc_ep_init, + .ep_fini = nni_inproc_ep_fini, + .ep_connect = nni_inproc_ep_connect, + .ep_bind = nni_inproc_ep_bind, + .ep_accept = nni_inproc_ep_accept, + .ep_close = nni_inproc_ep_close, + .ep_setopt = NULL, + .ep_getopt = NULL, }; // This is the inproc transport linkage, and should be the only global // symbol in this entire file. struct nni_tran nni_inproc_tran = { - .tran_scheme = "inproc", - .tran_ep = &nni_inproc_ep_ops, - .tran_pipe = &nni_inproc_pipe_ops, - .tran_init = nni_inproc_init, - .tran_fini = nni_inproc_fini, + .tran_scheme = "inproc", + .tran_ep = &nni_inproc_ep_ops, + .tran_pipe = &nni_inproc_pipe_ops, + .tran_init = nni_inproc_init, + .tran_fini = nni_inproc_fini, }; diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c index fcfe44ea..56ce5ecd 100644 --- a/src/transport/ipc/ipc.c +++ b/src/transport/ipc/ipc.c @@ -7,9 +7,9 @@ // found online at https://opensource.org/licenses/MIT. // +#include #include #include -#include #include "core/nng_impl.h" @@ -17,65 +17,61 @@ // supplied as well. Normally the IPC is UNIX domain sockets or // Windows named pipes. Other platforms could use other mechanisms. -typedef struct nni_ipc_pipe nni_ipc_pipe; -typedef struct nni_ipc_ep nni_ipc_ep; +typedef struct nni_ipc_pipe nni_ipc_pipe; +typedef struct nni_ipc_ep nni_ipc_ep; // nni_ipc_pipe is one end of an IPC connection. struct nni_ipc_pipe { - const char * addr; - nni_plat_ipc_pipe * ipp; - uint16_t peer; - uint16_t proto; - size_t rcvmax; - - uint8_t txhead[1+sizeof (uint64_t)]; - uint8_t rxhead[1+sizeof (uint64_t)]; - int gottxhead; - int gotrxhead; - int wanttxhead; - int wantrxhead; - - nni_aio * user_txaio; - nni_aio * user_rxaio; - nni_aio * user_negaio; - nni_aio txaio; - nni_aio rxaio; - nni_aio negaio; - nni_msg * rxmsg; - nni_mtx mtx; + const char * addr; + nni_plat_ipc_pipe *ipp; + uint16_t peer; + uint16_t proto; + size_t rcvmax; + + uint8_t txhead[1 + sizeof(uint64_t)]; + uint8_t rxhead[1 + sizeof(uint64_t)]; + int gottxhead; + int gotrxhead; + int wanttxhead; + int wantrxhead; + + nni_aio *user_txaio; + nni_aio *user_rxaio; + nni_aio *user_negaio; + nni_aio txaio; + nni_aio rxaio; + nni_aio negaio; + nni_msg *rxmsg; + nni_mtx mtx; }; struct nni_ipc_ep { - char addr[NNG_MAXADDRLEN+1]; - nni_plat_ipc_ep * iep; - int closed; - uint16_t proto; - size_t rcvmax; - nni_aio aio; - nni_aio * user_aio; - nni_mtx mtx; + char addr[NNG_MAXADDRLEN + 1]; + nni_plat_ipc_ep *iep; + int closed; + uint16_t proto; + size_t rcvmax; + nni_aio aio; + nni_aio * user_aio; + nni_mtx mtx; }; - static void nni_ipc_pipe_send_cb(void *); static void nni_ipc_pipe_recv_cb(void *); static void nni_ipc_pipe_nego_cb(void *); static void nni_ipc_ep_cb(void *); - static int nni_ipc_tran_init(void) { return (0); } - static void nni_ipc_tran_fini(void) { } - static void nni_ipc_pipe_close(void *arg) { @@ -84,7 +80,6 @@ nni_ipc_pipe_close(void *arg) nni_plat_ipc_pipe_close(pipe->ipp); } - static void nni_ipc_pipe_fini(void *arg) { @@ -103,12 +98,11 @@ nni_ipc_pipe_fini(void *arg) NNI_FREE_STRUCT(pipe); } - static int nni_ipc_pipe_init(nni_ipc_pipe **pipep, nni_ipc_ep *ep, void *ipp) { nni_ipc_pipe *pipe; - int rv; + int rv; if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) { return (NNG_ENOMEM); @@ -129,10 +123,10 @@ nni_ipc_pipe_init(nni_ipc_pipe **pipep, nni_ipc_ep *ep, void *ipp) goto fail; } - pipe->proto = ep->proto; + pipe->proto = ep->proto; pipe->rcvmax = ep->rcvmax; - pipe->ipp = ipp; - pipe->addr = ep->addr; + pipe->ipp = ipp; + pipe->addr = ep->addr; *pipep = pipe; return (0); @@ -142,7 +136,6 @@ fail: return (rv); } - static void nni_ipc_cancel_start(nni_aio *aio) { @@ -156,13 +149,12 @@ nni_ipc_cancel_start(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_ipc_pipe_nego_cb(void *arg) { nni_ipc_pipe *pipe = arg; - nni_aio *aio = &pipe->negaio; - int rv; + nni_aio * aio = &pipe->negaio; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_result(aio)) != 0) { @@ -177,7 +169,7 @@ nni_ipc_pipe_nego_cb(void *arg) } if (pipe->gottxhead < pipe->wanttxhead) { - aio->a_niov = 1; + aio->a_niov = 1; aio->a_iov[0].iov_len = pipe->wanttxhead - pipe->gottxhead; aio->a_iov[0].iov_buf = &pipe->txhead[pipe->gottxhead]; // send it down... @@ -186,7 +178,7 @@ nni_ipc_pipe_nego_cb(void *arg) return; } if (pipe->gotrxhead < pipe->wantrxhead) { - aio->a_niov = 1; + aio->a_niov = 1; aio->a_iov[0].iov_len = pipe->wantrxhead - pipe->gotrxhead; aio->a_iov[0].iov_buf = &pipe->rxhead[pipe->gotrxhead]; nni_plat_ipc_pipe_recv(pipe->ipp, aio); @@ -195,12 +187,9 @@ nni_ipc_pipe_nego_cb(void *arg) } // We have both sent and received the headers. Lets check the // receive side header. - if ((pipe->rxhead[0] != 0) || - (pipe->rxhead[1] != 'S') || - (pipe->rxhead[2] != 'P') || - (pipe->rxhead[3] != 0) || - (pipe->rxhead[6] != 0) || - (pipe->rxhead[7] != 0)) { + if ((pipe->rxhead[0] != 0) || (pipe->rxhead[1] != 'S') || + (pipe->rxhead[2] != 'P') || (pipe->rxhead[3] != 0) || + (pipe->rxhead[6] != 0) || (pipe->rxhead[7] != 0)) { rv = NNG_EPROTO; goto done; } @@ -215,14 +204,13 @@ done: nni_mtx_unlock(&pipe->mtx); } - static void nni_ipc_pipe_send_cb(void *arg) { nni_ipc_pipe *pipe = arg; - nni_aio *aio; - int rv; - size_t len; + nni_aio * aio; + int rv; + size_t len; nni_mtx_lock(&pipe->mtx); if ((aio = pipe->user_txaio) == NULL) { @@ -241,13 +229,12 @@ nni_ipc_pipe_send_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - static void nni_ipc_pipe_recv_cb(void *arg) { nni_ipc_pipe *pipe = arg; - nni_aio *aio; - int rv; + nni_aio * aio; + int rv; nni_mtx_lock(&pipe->mtx); aio = pipe->user_rxaio; @@ -285,7 +272,7 @@ nni_ipc_pipe_recv_cb(void *arg) } // We should have gotten a message header. - NNI_GET64(pipe->rxhead+1, len); + NNI_GET64(pipe->rxhead + 1, len); // Make sure the message payload is not too big. If it is // the caller will shut down the pipe. @@ -312,7 +299,7 @@ nni_ipc_pipe_recv_cb(void *arg) // read the entire message now. pipe->rxaio.a_iov[0].iov_buf = nni_msg_body(pipe->rxmsg); pipe->rxaio.a_iov[0].iov_len = nni_msg_len(pipe->rxmsg); - pipe->rxaio.a_niov = 1; + pipe->rxaio.a_niov = 1; nni_plat_ipc_pipe_recv(pipe->ipp, &pipe->rxaio); nni_mtx_unlock(&pipe->mtx); @@ -322,13 +309,12 @@ nni_ipc_pipe_recv_cb(void *arg) // Otherwise we got a message read completely. Let the user know the // good news. pipe->user_rxaio = NULL; - aio->a_msg = pipe->rxmsg; - pipe->rxmsg = NULL; + aio->a_msg = pipe->rxmsg; + pipe->rxmsg = NULL; nni_aio_finish(aio, 0, nni_msg_len(aio->a_msg)); nni_mtx_unlock(&pipe->mtx); } - static void nni_ipc_cancel_tx(nni_aio *aio) { @@ -342,13 +328,12 @@ nni_ipc_cancel_tx(nni_aio *aio) nni_aio_stop(&pipe->txaio); } - static void nni_ipc_pipe_send(void *arg, nni_aio *aio) { nni_ipc_pipe *pipe = arg; - nni_msg *msg = aio->a_msg; - uint64_t len; + nni_msg * msg = aio->a_msg; + uint64_t len; len = nni_msg_len(msg) + nni_msg_header_len(msg); @@ -360,22 +345,21 @@ nni_ipc_pipe_send(void *arg, nni_aio *aio) pipe->user_txaio = aio; - pipe->txhead[0] = 1; // message type, 1. + pipe->txhead[0] = 1; // message type, 1. NNI_PUT64(pipe->txhead + 1, len); pipe->txaio.a_iov[0].iov_buf = pipe->txhead; - pipe->txaio.a_iov[0].iov_len = sizeof (pipe->txhead); + pipe->txaio.a_iov[0].iov_len = sizeof(pipe->txhead); pipe->txaio.a_iov[1].iov_buf = nni_msg_header(msg); pipe->txaio.a_iov[1].iov_len = nni_msg_header_len(msg); pipe->txaio.a_iov[2].iov_buf = nni_msg_body(msg); pipe->txaio.a_iov[2].iov_len = nni_msg_len(msg); - pipe->txaio.a_niov = 3; + pipe->txaio.a_niov = 3; nni_plat_ipc_pipe_send(pipe->ipp, &pipe->txaio); nni_mtx_unlock(&pipe->mtx); } - static void nni_ipc_cancel_rx(nni_aio *aio) { @@ -389,7 +373,6 @@ nni_ipc_cancel_rx(nni_aio *aio) nni_aio_stop(&pipe->rxaio); } - static void nni_ipc_pipe_recv(void *arg, nni_aio *aio) { @@ -407,19 +390,18 @@ nni_ipc_pipe_recv(void *arg, nni_aio *aio) // Schedule a read of the IPC header. pipe->rxaio.a_iov[0].iov_buf = pipe->rxhead; - pipe->rxaio.a_iov[0].iov_len = sizeof (pipe->rxhead); - pipe->rxaio.a_niov = 1; + pipe->rxaio.a_iov[0].iov_len = sizeof(pipe->rxhead); + pipe->rxaio.a_niov = 1; nni_plat_ipc_pipe_recv(pipe->ipp, &pipe->rxaio); nni_mtx_unlock(&pipe->mtx); } - static void nni_ipc_pipe_start(void *arg, nni_aio *aio) { nni_ipc_pipe *pipe = arg; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); pipe->txhead[0] = 0; @@ -429,12 +411,12 @@ nni_ipc_pipe_start(void *arg, nni_aio *aio) NNI_PUT16(&pipe->txhead[4], pipe->proto); NNI_PUT16(&pipe->txhead[6], 0); - pipe->user_negaio = aio; - pipe->gotrxhead = 0; - pipe->gottxhead = 0; - pipe->wantrxhead = 8; - pipe->wanttxhead = 8; - pipe->negaio.a_niov = 1; + pipe->user_negaio = aio; + pipe->gotrxhead = 0; + pipe->gottxhead = 0; + pipe->wantrxhead = 8; + pipe->wanttxhead = 8; + pipe->negaio.a_niov = 1; pipe->negaio.a_iov[0].iov_len = 8; pipe->negaio.a_iov[0].iov_buf = &pipe->txhead[0]; rv = nni_aio_start(aio, nni_ipc_cancel_start, pipe); @@ -446,7 +428,6 @@ nni_ipc_pipe_start(void *arg, nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static uint16_t nni_ipc_pipe_peer(void *arg) { @@ -455,7 +436,6 @@ nni_ipc_pipe_peer(void *arg) return (pipe->peer); } - static int nni_ipc_pipe_getopt(void *arg, int option, void *buf, size_t *szp) { @@ -479,7 +459,6 @@ nni_ipc_pipe_getopt(void *arg, int option, void *buf, size_t *szp) return (NNG_ENOTSUP); } - static void nni_ipc_ep_fini(void *arg) { @@ -491,14 +470,13 @@ nni_ipc_ep_fini(void *arg) NNI_FREE_STRUCT(ep); } - static int nni_ipc_ep_init(void **epp, const char *url, nni_sock *sock, int mode) { nni_ipc_ep *ep; - int rv; + int rv; - if ((strlen(url) > NNG_MAXADDRLEN-1) || + if ((strlen(url) > NNG_MAXADDRLEN - 1) || (strncmp(url, "ipc://", strlen("ipc://")) != 0)) { return (NNG_EADDRINVAL); } @@ -513,15 +491,14 @@ nni_ipc_ep_init(void **epp, const char *url, nni_sock *sock, int mode) return (rv); } ep->closed = 0; - ep->proto = nni_sock_proto(sock); + ep->proto = nni_sock_proto(sock); ep->rcvmax = nni_sock_rcvmaxsz(sock); - (void) snprintf(ep->addr, sizeof (ep->addr), "%s", url); + (void) snprintf(ep->addr, sizeof(ep->addr), "%s", url); *epp = ep; return (0); } - static void nni_ipc_ep_close(void *arg) { @@ -530,7 +507,6 @@ nni_ipc_ep_close(void *arg) nni_plat_ipc_ep_close(ep->iep); } - static int nni_ipc_ep_bind(void *arg) { @@ -539,13 +515,12 @@ nni_ipc_ep_bind(void *arg) return (nni_plat_ipc_ep_listen(ep->iep)); } - static void nni_ipc_ep_finish(nni_ipc_ep *ep) { - nni_aio *aio = ep->user_aio; + nni_aio * aio = ep->user_aio; nni_ipc_pipe *pipe; - int rv; + int rv; if ((aio = ep->user_aio) == NULL) { return; @@ -570,7 +545,6 @@ done: nni_aio_finish(aio, rv, 0); } - static void nni_ipc_ep_cb(void *arg) { @@ -581,7 +555,6 @@ nni_ipc_ep_cb(void *arg) nni_mtx_unlock(&ep->mtx); } - static void nni_ipc_cancel_ep(nni_aio *aio) { @@ -595,12 +568,11 @@ nni_ipc_cancel_ep(nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - static void nni_ipc_ep_accept(void *arg, nni_aio *aio) { nni_ipc_ep *ep = arg; - int rv; + int rv; nni_mtx_lock(&ep->mtx); NNI_ASSERT(ep->user_aio == NULL); @@ -617,12 +589,11 @@ nni_ipc_ep_accept(void *arg, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - static void nni_ipc_ep_connect(void *arg, nni_aio *aio) { nni_ipc_ep *ep = arg; - int rv; + int rv; nni_mtx_lock(&ep->mtx); NNI_ASSERT(ep->user_aio == NULL); @@ -639,34 +610,33 @@ nni_ipc_ep_connect(void *arg, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - static nni_tran_pipe nni_ipc_pipe_ops = { - .p_fini = nni_ipc_pipe_fini, - .p_start = nni_ipc_pipe_start, - .p_send = nni_ipc_pipe_send, - .p_recv = nni_ipc_pipe_recv, - .p_close = nni_ipc_pipe_close, - .p_peer = nni_ipc_pipe_peer, - .p_getopt = nni_ipc_pipe_getopt, + .p_fini = nni_ipc_pipe_fini, + .p_start = nni_ipc_pipe_start, + .p_send = nni_ipc_pipe_send, + .p_recv = nni_ipc_pipe_recv, + .p_close = nni_ipc_pipe_close, + .p_peer = nni_ipc_pipe_peer, + .p_getopt = nni_ipc_pipe_getopt, }; static nni_tran_ep nni_ipc_ep_ops = { - .ep_init = nni_ipc_ep_init, - .ep_fini = nni_ipc_ep_fini, - .ep_connect = nni_ipc_ep_connect, - .ep_bind = nni_ipc_ep_bind, - .ep_accept = nni_ipc_ep_accept, - .ep_close = nni_ipc_ep_close, - .ep_setopt = NULL, - .ep_getopt = NULL, + .ep_init = nni_ipc_ep_init, + .ep_fini = nni_ipc_ep_fini, + .ep_connect = nni_ipc_ep_connect, + .ep_bind = nni_ipc_ep_bind, + .ep_accept = nni_ipc_ep_accept, + .ep_close = nni_ipc_ep_close, + .ep_setopt = NULL, + .ep_getopt = NULL, }; // This is the IPC transport linkage, and should be the only global // symbol in this entire file. struct nni_tran nni_ipc_tran = { - .tran_scheme = "ipc", - .tran_ep = &nni_ipc_ep_ops, - .tran_pipe = &nni_ipc_pipe_ops, - .tran_init = nni_ipc_tran_init, - .tran_fini = nni_ipc_tran_fini, + .tran_scheme = "ipc", + .tran_ep = &nni_ipc_ep_ops, + .tran_pipe = &nni_ipc_pipe_ops, + .tran_init = nni_ipc_tran_init, + .tran_fini = nni_ipc_tran_fini, }; diff --git a/src/transport/tcp/tcp.c b/src/transport/tcp/tcp.c index 4b0f77f1..e60a1b1d 100644 --- a/src/transport/tcp/tcp.c +++ b/src/transport/tcp/tcp.c @@ -7,56 +7,55 @@ // found online at https://opensource.org/licenses/MIT. // +#include #include #include -#include #include "core/nng_impl.h" // TCP transport. Platform specific TCP operations must be // supplied as well. -typedef struct nni_tcp_pipe nni_tcp_pipe; -typedef struct nni_tcp_ep nni_tcp_ep; +typedef struct nni_tcp_pipe nni_tcp_pipe; +typedef struct nni_tcp_ep nni_tcp_ep; // nni_tcp_pipe is one end of a TCP connection. struct nni_tcp_pipe { - const char * addr; - nni_plat_tcp_pipe * tpp; - uint16_t peer; - uint16_t proto; - size_t rcvmax; - - nni_aio * user_txaio; - nni_aio * user_rxaio; - nni_aio * user_negaio; - - uint8_t txlen[sizeof (uint64_t)]; - uint8_t rxlen[sizeof (uint64_t)]; - int gottxhead; - int gotrxhead; - int wanttxhead; - int wantrxhead; - nni_aio txaio; - nni_aio rxaio; - nni_aio negaio; - nni_msg * rxmsg; - nni_mtx mtx; + const char * addr; + nni_plat_tcp_pipe *tpp; + uint16_t peer; + uint16_t proto; + size_t rcvmax; + + nni_aio *user_txaio; + nni_aio *user_rxaio; + nni_aio *user_negaio; + + uint8_t txlen[sizeof(uint64_t)]; + uint8_t rxlen[sizeof(uint64_t)]; + int gottxhead; + int gotrxhead; + int wanttxhead; + int wantrxhead; + nni_aio txaio; + nni_aio rxaio; + nni_aio negaio; + nni_msg *rxmsg; + nni_mtx mtx; }; struct nni_tcp_ep { - char addr[NNG_MAXADDRLEN+1]; - nni_plat_tcp_ep * tep; - int closed; - uint16_t proto; - size_t rcvmax; - int ipv4only; - nni_aio aio; - nni_aio * user_aio; - nni_mtx mtx; + char addr[NNG_MAXADDRLEN + 1]; + nni_plat_tcp_ep *tep; + int closed; + uint16_t proto; + size_t rcvmax; + int ipv4only; + nni_aio aio; + nni_aio * user_aio; + nni_mtx mtx; }; - static void nni_tcp_pipe_send_cb(void *); static void nni_tcp_pipe_recv_cb(void *); static void nni_tcp_pipe_nego_cb(void *); @@ -68,13 +67,11 @@ nni_tcp_tran_init(void) return (0); } - static void nni_tcp_tran_fini(void) { } - static void nni_tcp_pipe_close(void *arg) { @@ -83,7 +80,6 @@ nni_tcp_pipe_close(void *arg) nni_plat_tcp_pipe_close(pipe->tpp); } - static void nni_tcp_pipe_fini(void *arg) { @@ -102,12 +98,11 @@ nni_tcp_pipe_fini(void *arg) NNI_FREE_STRUCT(pipe); } - static int nni_tcp_pipe_init(nni_tcp_pipe **pipep, nni_tcp_ep *ep, void *tpp) { nni_tcp_pipe *pipe; - int rv; + int rv; if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) { return (NNG_ENOMEM); @@ -127,10 +122,10 @@ nni_tcp_pipe_init(nni_tcp_pipe **pipep, nni_tcp_ep *ep, void *tpp) if (rv != 0) { goto fail; } - pipe->proto = ep->proto; + pipe->proto = ep->proto; pipe->rcvmax = ep->rcvmax; - pipe->tpp = tpp; - pipe->addr = ep->addr; + pipe->tpp = tpp; + pipe->addr = ep->addr; *pipep = pipe; return (0); @@ -140,7 +135,6 @@ fail: return (rv); } - static void nni_tcp_cancel_nego(nni_aio *aio) { @@ -154,13 +148,12 @@ nni_tcp_cancel_nego(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_tcp_pipe_nego_cb(void *arg) { nni_tcp_pipe *pipe = arg; - nni_aio *aio = &pipe->negaio; - int rv; + nni_aio * aio = &pipe->negaio; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_result(aio)) != 0) { @@ -175,7 +168,7 @@ nni_tcp_pipe_nego_cb(void *arg) } if (pipe->gottxhead < pipe->wanttxhead) { - aio->a_niov = 1; + aio->a_niov = 1; aio->a_iov[0].iov_len = pipe->wanttxhead - pipe->gottxhead; aio->a_iov[0].iov_buf = &pipe->txlen[pipe->gottxhead]; // send it down... @@ -184,7 +177,7 @@ nni_tcp_pipe_nego_cb(void *arg) return; } if (pipe->gotrxhead < pipe->wantrxhead) { - aio->a_niov = 1; + aio->a_niov = 1; aio->a_iov[0].iov_len = pipe->wantrxhead - pipe->gotrxhead; aio->a_iov[0].iov_buf = &pipe->rxlen[pipe->gotrxhead]; nni_plat_tcp_pipe_recv(pipe->tpp, aio); @@ -193,12 +186,9 @@ nni_tcp_pipe_nego_cb(void *arg) } // We have both sent and received the headers. Lets check the // receive side header. - if ((pipe->rxlen[0] != 0) || - (pipe->rxlen[1] != 'S') || - (pipe->rxlen[2] != 'P') || - (pipe->rxlen[3] != 0) || - (pipe->rxlen[6] != 0) || - (pipe->rxlen[7] != 0)) { + if ((pipe->rxlen[0] != 0) || (pipe->rxlen[1] != 'S') || + (pipe->rxlen[2] != 'P') || (pipe->rxlen[3] != 0) || + (pipe->rxlen[6] != 0) || (pipe->rxlen[7] != 0)) { rv = NNG_EPROTO; goto done; } @@ -213,14 +203,13 @@ done: nni_mtx_unlock(&pipe->mtx); } - static void nni_tcp_pipe_send_cb(void *arg) { nni_tcp_pipe *pipe = arg; - int rv; - nni_aio *aio; - size_t len; + int rv; + nni_aio * aio; + size_t len; nni_mtx_lock(&pipe->mtx); if ((aio = pipe->user_txaio) == NULL) { @@ -240,13 +229,12 @@ nni_tcp_pipe_send_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - static void nni_tcp_pipe_recv_cb(void *arg) { nni_tcp_pipe *pipe = arg; - nni_aio *aio; - int rv; + nni_aio * aio; + int rv; nni_mtx_lock(&pipe->mtx); @@ -298,7 +286,7 @@ nni_tcp_pipe_recv_cb(void *arg) // read the entire message now. pipe->rxaio.a_iov[0].iov_buf = nni_msg_body(pipe->rxmsg); pipe->rxaio.a_iov[0].iov_len = nni_msg_len(pipe->rxmsg); - pipe->rxaio.a_niov = 1; + pipe->rxaio.a_niov = 1; nni_plat_tcp_pipe_recv(pipe->tpp, &pipe->rxaio); nni_mtx_unlock(&pipe->mtx); @@ -308,13 +296,12 @@ nni_tcp_pipe_recv_cb(void *arg) // Otherwise we got a message read completely. Let the user know the // good news. pipe->user_rxaio = NULL; - aio->a_msg = pipe->rxmsg; - pipe->rxmsg = NULL; + aio->a_msg = pipe->rxmsg; + pipe->rxmsg = NULL; nni_aio_finish(aio, 0, nni_msg_len(aio->a_msg)); nni_mtx_unlock(&pipe->mtx); } - static void nni_tcp_cancel_tx(nni_aio *aio) { @@ -328,13 +315,12 @@ nni_tcp_cancel_tx(nni_aio *aio) nni_aio_stop(&pipe->txaio); } - static void nni_tcp_pipe_send(void *arg, nni_aio *aio) { nni_tcp_pipe *pipe = arg; - nni_msg *msg = aio->a_msg; - uint64_t len; + nni_msg * msg = aio->a_msg; + uint64_t len; len = nni_msg_len(msg) + nni_msg_header_len(msg); @@ -350,18 +336,17 @@ nni_tcp_pipe_send(void *arg, nni_aio *aio) NNI_PUT64(pipe->txlen, len); pipe->txaio.a_iov[0].iov_buf = pipe->txlen; - pipe->txaio.a_iov[0].iov_len = sizeof (pipe->txlen); + pipe->txaio.a_iov[0].iov_len = sizeof(pipe->txlen); pipe->txaio.a_iov[1].iov_buf = nni_msg_header(msg); pipe->txaio.a_iov[1].iov_len = nni_msg_header_len(msg); pipe->txaio.a_iov[2].iov_buf = nni_msg_body(msg); pipe->txaio.a_iov[2].iov_len = nni_msg_len(msg); - pipe->txaio.a_niov = 3; + pipe->txaio.a_niov = 3; nni_plat_tcp_pipe_send(pipe->tpp, &pipe->txaio); nni_mtx_unlock(&pipe->mtx); } - static void nni_tcp_cancel_rx(nni_aio *aio) { @@ -375,7 +360,6 @@ nni_tcp_cancel_rx(nni_aio *aio) nni_aio_stop(&pipe->rxaio); } - static void nni_tcp_pipe_recv(void *arg, nni_aio *aio) { @@ -393,14 +377,13 @@ nni_tcp_pipe_recv(void *arg, nni_aio *aio) // Schedule a read of the TCP header. pipe->rxaio.a_iov[0].iov_buf = pipe->rxlen; - pipe->rxaio.a_iov[0].iov_len = sizeof (pipe->rxlen); - pipe->rxaio.a_niov = 1; + pipe->rxaio.a_iov[0].iov_len = sizeof(pipe->rxlen); + pipe->rxaio.a_niov = 1; nni_plat_tcp_pipe_recv(pipe->tpp, &pipe->rxaio); nni_mtx_unlock(&pipe->mtx); } - static uint16_t nni_tcp_pipe_peer(void *arg) { @@ -409,7 +392,6 @@ nni_tcp_pipe_peer(void *arg) return (pipe->peer); } - static int nni_tcp_pipe_getopt(void *arg, int option, void *buf, size_t *szp) { @@ -433,14 +415,13 @@ nni_tcp_pipe_getopt(void *arg, int option, void *buf, size_t *szp) return (NNG_ENOTSUP); } - static int nni_tcp_parse_pair(char *pair, char **hostp, char **servp) { char *host, *serv, *end; if (pair[0] == '[') { - host = pair+1; + host = pair + 1; // IP address enclosed ... for IPv6 usually. if ((end = strchr(host, ']')) == NULL) { return (NNG_EADDRINVAL); @@ -478,14 +459,13 @@ nni_tcp_parse_pair(char *pair, char **hostp, char **servp) return (0); } - // Note that the url *must* be in a modifiable buffer. int -nni_tcp_parse_url(char *url, char **host1, char **serv1, char **host2, - char **serv2) +nni_tcp_parse_url( + char *url, char **host1, char **serv1, char **host2, char **serv2) { char *h1; - int rv; + int rv; if (strncmp(url, "tcp://", strlen("tcp://")) != 0) { return (NNG_EADDRINVAL); @@ -516,7 +496,6 @@ nni_tcp_parse_url(char *url, char **host1, char **serv1, char **host2, return (0); } - static void nni_tcp_pipe_start(void *arg, nni_aio *aio) { @@ -530,12 +509,12 @@ nni_tcp_pipe_start(void *arg, nni_aio *aio) NNI_PUT16(&pipe->txlen[4], pipe->proto); NNI_PUT16(&pipe->txlen[6], 0); - pipe->user_negaio = aio; - pipe->gotrxhead = 0; - pipe->gottxhead = 0; - pipe->wantrxhead = 8; - pipe->wanttxhead = 8; - pipe->negaio.a_niov = 1; + pipe->user_negaio = aio; + pipe->gotrxhead = 0; + pipe->gottxhead = 0; + pipe->wantrxhead = 8; + pipe->wanttxhead = 8; + pipe->negaio.a_niov = 1; pipe->negaio.a_iov[0].iov_len = 8; pipe->negaio.a_iov[0].iov_buf = &pipe->txlen[0]; if (nni_aio_start(aio, nni_tcp_cancel_nego, pipe) != 0) { @@ -546,7 +525,6 @@ nni_tcp_pipe_start(void *arg, nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_tcp_ep_fini(void *arg) { @@ -560,12 +538,11 @@ nni_tcp_ep_fini(void *arg) NNI_FREE_STRUCT(ep); } - static int nni_tcp_ep_init(void **epp, const char *url, nni_sock *sock, int mode) { nni_tcp_ep *ep; - int rv; + int rv; if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NNG_ENOMEM); @@ -577,15 +554,14 @@ nni_tcp_ep_init(void **epp, const char *url, nni_sock *sock, int mode) return (rv); } ep->closed = 0; - ep->proto = nni_sock_proto(sock); + ep->proto = nni_sock_proto(sock); ep->rcvmax = nni_sock_rcvmaxsz(sock); - (void) snprintf(ep->addr, sizeof (ep->addr), "%s", url); + (void) snprintf(ep->addr, sizeof(ep->addr), "%s", url); *epp = ep; return (0); } - static void nni_tcp_ep_close(void *arg) { @@ -594,7 +570,6 @@ nni_tcp_ep_close(void *arg) nni_plat_tcp_ep_close(ep->tep); } - static int nni_tcp_ep_bind(void *arg) { @@ -603,13 +578,12 @@ nni_tcp_ep_bind(void *arg) return (nni_plat_tcp_ep_listen(ep->tep)); } - static void nni_tcp_ep_finish(nni_tcp_ep *ep) { - nni_aio *aio = ep->user_aio; + nni_aio * aio = ep->user_aio; nni_tcp_pipe *pipe; - int rv; + int rv; if ((aio = ep->user_aio) == NULL) { return; @@ -634,7 +608,6 @@ done: nni_aio_finish(aio, rv, 0); } - static void nni_tcp_ep_cb(void *arg) { @@ -645,7 +618,6 @@ nni_tcp_ep_cb(void *arg) nni_mtx_unlock(&ep->mtx); } - static void nni_tcp_cancel_ep(nni_aio *aio) { @@ -659,12 +631,11 @@ nni_tcp_cancel_ep(nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - static void nni_tcp_ep_accept(void *arg, nni_aio *aio) { nni_tcp_ep *ep = arg; - int rv; + int rv; nni_mtx_lock(&ep->mtx); NNI_ASSERT(ep->user_aio == NULL); @@ -681,12 +652,11 @@ nni_tcp_ep_accept(void *arg, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - static void nni_tcp_ep_connect(void *arg, nni_aio *aio) { nni_tcp_ep *ep = arg; - int rv; + int rv; nni_mtx_lock(&ep->mtx); NNI_ASSERT(ep->user_aio == NULL); @@ -703,34 +673,33 @@ nni_tcp_ep_connect(void *arg, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - static nni_tran_pipe nni_tcp_pipe_ops = { - .p_fini = nni_tcp_pipe_fini, - .p_start = nni_tcp_pipe_start, - .p_send = nni_tcp_pipe_send, - .p_recv = nni_tcp_pipe_recv, - .p_close = nni_tcp_pipe_close, - .p_peer = nni_tcp_pipe_peer, - .p_getopt = nni_tcp_pipe_getopt, + .p_fini = nni_tcp_pipe_fini, + .p_start = nni_tcp_pipe_start, + .p_send = nni_tcp_pipe_send, + .p_recv = nni_tcp_pipe_recv, + .p_close = nni_tcp_pipe_close, + .p_peer = nni_tcp_pipe_peer, + .p_getopt = nni_tcp_pipe_getopt, }; static nni_tran_ep nni_tcp_ep_ops = { - .ep_init = nni_tcp_ep_init, - .ep_fini = nni_tcp_ep_fini, - .ep_connect = nni_tcp_ep_connect, - .ep_bind = nni_tcp_ep_bind, - .ep_accept = nni_tcp_ep_accept, - .ep_close = nni_tcp_ep_close, - .ep_setopt = NULL, - .ep_getopt = NULL, + .ep_init = nni_tcp_ep_init, + .ep_fini = nni_tcp_ep_fini, + .ep_connect = nni_tcp_ep_connect, + .ep_bind = nni_tcp_ep_bind, + .ep_accept = nni_tcp_ep_accept, + .ep_close = nni_tcp_ep_close, + .ep_setopt = NULL, + .ep_getopt = NULL, }; // This is the TCP transport linkage, and should be the only global // symbol in this entire file. struct nni_tran nni_tcp_tran = { - .tran_scheme = "tcp", - .tran_ep = &nni_tcp_ep_ops, - .tran_pipe = &nni_tcp_pipe_ops, - .tran_init = nni_tcp_tran_init, - .tran_fini = nni_tcp_tran_fini, + .tran_scheme = "tcp", + .tran_ep = &nni_tcp_ep_ops, + .tran_pipe = &nni_tcp_pipe_ops, + .tran_init = nni_tcp_tran_init, + .tran_fini = nni_tcp_tran_fini, }; -- cgit v1.2.3-70-g09d2