From 83a0f4986143efb195007bb89fa21c48a9cf3df5 Mon Sep 17 00:00:00 2001 From: AlexKlo <65765845+AlexKlo@users.noreply.github.com> Date: Thu, 16 Jan 2025 13:09:22 +0300 Subject: [PATCH] Refactor types for improved library stability (#4) Co-authored-by: Stanislav Gryumov <33372067+gryumov@users.noreply.github.com> --- Project.toml | 2 +- README.md | 4 +-- docs/src/index.md | 4 +-- docs/src/pages/manual.md | 4 +-- src/functions.jl | 17 ++++++++- src/types.jl | 77 +++++++++++++++++++++++++++++----------- 6 files changed, 79 insertions(+), 29 deletions(-) diff --git a/Project.toml b/Project.toml index b0b5986..ebffc74 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Libwebsockets" uuid = "30a1cee6-c274-443c-b274-d8ea34e8f87d" -version = "1.0.0" +version = "1.0.1" [deps] libwebsockets_jll = "b6b84edf-6aef-5ff6-9b66-656961a84a09" diff --git a/README.md b/README.md index 5a7b7d7..140bb37 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ function ws_open(callback::Function, addr::String, port::Int, path::String) callback_ptr = @cfunction(ws_callback, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t)) protocols = [ LwsProtocols(pointer("ws"), callback_ptr, 0, 0, 0, C_NULL, 0), - LwsProtocols() + LwsProtocols(C_NULL, C_NULL, 0, 0, 0, C_NULL, 0) ] user = UserData(callback) @@ -48,7 +48,7 @@ function ws_open(callback::Function, addr::String, port::Int, path::String) ctx_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT ctx_info.port = CONTEXT_PORT_NO_LISTEN ctx_info.user = Base.unsafe_convert(Ptr{UserData}, Ref(user)) - ctx_info.protocols = Base.unsafe_convert(Ptr{LwsProtocols}, Ref(protocols[1])) + ctx_info.protocols = pointer(protocols) ws_ctx = lws_create_context(Ref(ctx_info)) conn_info = LwsClientConnectInfo() diff --git a/docs/src/index.md b/docs/src/index.md index 90be535..9fedd41 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -34,7 +34,7 @@ function ws_open(callback::Function, addr::String, port::Int, path::String) callback_ptr = @cfunction(ws_callback, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t)) protocols = [ LwsProtocols(pointer("ws"), callback_ptr, 0, 0, 0, C_NULL, 0), - LwsProtocols() + LwsProtocols(C_NULL, C_NULL, 0, 0, 0, C_NULL, 0) ] user = UserData(callback) @@ -42,7 +42,7 @@ function ws_open(callback::Function, addr::String, port::Int, path::String) ctx_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT ctx_info.port = CONTEXT_PORT_NO_LISTEN ctx_info.user = Base.unsafe_convert(Ptr{UserData}, Ref(user)) - ctx_info.protocols = Base.unsafe_convert(Ptr{LwsProtocols}, Ref(protocols[1])) + ctx_info.protocols = pointer(protocols) ws_ctx = lws_create_context(Ref(ctx_info)) conn_info = LwsClientConnectInfo() diff --git a/docs/src/pages/manual.md b/docs/src/pages/manual.md index 1f45ce0..1b96953 100644 --- a/docs/src/pages/manual.md +++ b/docs/src/pages/manual.md @@ -55,7 +55,7 @@ function ws_open(callback::Function, addr::String, port::Int, path::String) callback_ptr = @cfunction(ws_callback, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t)) protocols = [ LwsProtocols(pointer("ws"), callback_ptr, 0, 0, 0, C_NULL, 0), - LwsProtocols() + LwsProtocols(C_NULL, C_NULL, 0, 0, 0, C_NULL, 0) ] user = UserData(callback) @@ -63,7 +63,7 @@ function ws_open(callback::Function, addr::String, port::Int, path::String) ctx_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT ctx_info.port = CONTEXT_PORT_NO_LISTEN ctx_info.user = Base.unsafe_convert(Ptr{UserData}, Ref(user)) - ctx_info.protocols = Base.unsafe_convert(Ptr{LwsProtocols}, Ref(protocols[1])) + ctx_info.protocols = pointer(protocols) ws_ctx = lws_create_context(Ref(ctx_info)) conn_info = LwsClientConnectInfo() diff --git a/src/functions.jl b/src/functions.jl index 67009cb..2265368 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -154,7 +154,10 @@ export lws_add_http_common_headers, lwsl_visible, lwsl_wsi_get_cx, lwsws_get_config_globals, - lwsws_get_config_vhosts + lwsws_get_config_vhosts, + lws_sul_schedule, + lws_retry_sul_schedule, + lws_retry_sul_schedule_retry_wsi function lwsl_context_get_cx(cx) ccall((:lwsl_context_get_cx, libwebsockets), Ptr{LwsLogCx}, (Ptr{LwsContext},), cx) @@ -775,3 +778,15 @@ end function lws_tls_session_dump_load(vh, host, port, cb_load, opq) ccall((:lws_tls_session_dump_load, libwebsockets), Cint, (Ptr{LwsVhost}, Ptr{Cchar}, UInt16, Ptr{Cvoid}, Ptr{Cvoid}), vh, host, port, cb_load, opq) end + +function lws_sul_schedule(ctx, tsi, sul, _cb, _us) + ccall((:lws_sul_schedule, libwebsockets), Cvoid, (Ptr{LwsContext}, Cint, Ptr{LwsSortedUsecList}, Ptr{Cvoid}, Cuint), ctx, tsi, sul, _cb, _us) +end + +function lws_retry_sul_schedule(context, tid, sul, retry, cb, ctry) + ccall((:lws_retry_sul_schedule, libwebsockets), Cint, (Ptr{LwsContext}, Cint, Ptr{LwsSortedUsecList}, Ptr{LwsRetryBo}, Ptr{Cvoid}, Ptr{Cushort}), context, tid, sul, retry, cb, ctry) +end + +function lws_retry_sul_schedule_retry_wsi(wsi, sul, cb, ctry) + ccall((:lws_retry_sul_schedule_retry_wsi, libwebsockets), Cint, (Ptr{Lws}, Ptr{LwsSortedUsecList}, Ptr{Cvoid}, Ptr{Cushort}), wsi, sul, cb, ctry) +end \ No newline at end of file diff --git a/src/types.jl b/src/types.jl index 71dfe96..5040b0a 100644 --- a/src/types.jl +++ b/src/types.jl @@ -39,7 +39,13 @@ export Lws, LwsTokens, LwsVhost, LwsWritePassthru, - LwsX509Cert + LwsX509Cert, + LwsSslCtxSt, + LwsSystemOpts, + LwsRetryBo, + LwsDll2, + LwsStateNotifyLink, + LwsSortedUsecList struct LwsContext end struct LwsVhost end @@ -54,6 +60,7 @@ struct LwsSequencer end struct LwsSpa end struct LwsAc end struct LwsX509Cert end +struct LwsSslCtxSt end Base.@kwdef mutable struct LwsLogCxUnion emit::Ptr{Cvoid} = C_NULL @@ -105,7 +112,7 @@ Base.@kwdef mutable struct LwsExtension client_offer::Ptr{Cchar} = C_NULL end -Base.@kwdef mutable struct LwsProtocols +Base.@kwdef struct LwsProtocols name::Ptr{Cchar} = C_NULL callback::Ptr{Cvoid} = C_NULL per_session_data_size::Csize_t = 0 @@ -176,6 +183,44 @@ Base.@kwdef mutable struct LwsHttpMount basic_auth_login_file::Ptr{Cchar} = C_NULL end +struct LwsSystemOpts + reboot::Ptr{Cvoid} + set_clock::Ptr{Cvoid} + attach::Ptr{Cvoid} + captive_portal_detect_request::Ptr{Cvoid} + metric_report::Ptr{Cvoid} + jit_trust_query::Ptr{Cvoid} + wake_latency_us::UInt32 +end + +mutable struct LwsRetryBo + retry_ms_table::Ptr{UInt32} + retry_ms_table_count::UInt16 + conceal_count::UInt16 + secs_since_valid_ping::UInt16 + secs_since_valid_hangup::UInt16 + jitter_percent::UInt8 +end + +Base.@kwdef struct LwsDll2 + prev::Ptr{LwsDll2} = C_NULL + next::Ptr{LwsDll2} = C_NULL + owner::Ptr{Cvoid} = C_NULL +end + +struct LwsStateNotifyLink + list::LwsDll2 + notify_cb::Ptr{Cvoid} + name::Ptr{Cchar} +end + +Base.@kwdef mutable struct LwsSortedUsecList + list::LwsDll2 = LwsDll2() + us::Int64 = 0 + cb::Ptr{Cvoid} = C_NULL + latency_us::UInt32 = UInt32(0) +end + Base.@kwdef mutable struct LwsContextCreationInfo iface::Ptr{Cchar} = C_NULL protocols::Ptr{LwsProtocols} = C_NULL @@ -194,13 +239,7 @@ Base.@kwdef mutable struct LwsContextCreationInfo max_http_header_data2::Cuint = 0 max_http_header_pool2::Cuint = 0 keepalive_timeout::Cint = 0 - http2_settings1::Cuint = 0 - http2_settings2::Cuint = 0 - http2_settings3::Cuint = 0 - http2_settings4::Cuint = 0 - http2_settings5::Cuint = 0 - http2_settings6::Cuint = 0 - http2_settings7::Cuint = 0 + http2_settings::NTuple{7,UInt32} = ntuple(i -> UInt32(0), 7) max_http_header_data::Cushort = 0 max_http_header_pool::Cushort = 0 ssl_private_key_password::Ptr{Cchar} = C_NULL @@ -236,7 +275,7 @@ Base.@kwdef mutable struct LwsContextCreationInfo ssl_client_options_clear::Clong = 0 client_ssl_ca_mem_len::Cuint = 0 client_ssl_key_mem_len::Cuint = 0 - provided_client_ssl_ctx::Ptr{Cint} = C_NULL + provided_client_ssl_ctx::Ptr{LwsSslCtxSt} = Ptr{LwsSslCtxSt}(C_NULL) ka_time::Cint = 0 ka_probes::Cint = 0 ka_interval::Cint = 0 @@ -267,11 +306,11 @@ Base.@kwdef mutable struct LwsContextCreationInfo username::Ptr{Cchar} = C_NULL groupname::Ptr{Cchar} = C_NULL unix_socket_perms::Ptr{Cchar} = C_NULL - system_ops::Ptr{Cint} = C_NULL - retry_and_idle_policy::Ptr{Cint} = C_NULL - register_notifier_list::Ptr{Ptr{Cint}} = C_NULL + system_ops::Ptr{LwsSystemOpts} = Ptr{LwsSystemOpts}(C_NULL) + retry_and_idle_policy::Ptr{LwsRetryBo} = Ptr{LwsRetryBo}(C_NULL) + register_notifier_list::Ptr{Ptr{LwsStateNotifyLink}} = Ptr{Ptr{LwsStateNotifyLink}}(C_NULL) rlimit_nofile::Cint = 0 - early_smd_cb::Cint = 0 + early_smd_cb::Ptr{Cvoid} = C_NULL early_smd_opaque::Ptr{Cvoid} = C_NULL early_smd_class_filter::Cint = 0 smd_ttl_us::Cintmax_t = 0 @@ -283,8 +322,7 @@ Base.@kwdef mutable struct LwsContextCreationInfo http_nsc_heap_max_footprint::Csize_t = 0 http_nsc_heap_max_items::Csize_t = 0 http_nsc_heap_max_payload::Csize_t = 0 - _unused1::Ptr{Cvoid} = C_NULL - _unused2::Ptr{Cvoid} = C_NULL + _unused::NTuple{2,Ptr{Cvoid}} = (Ptr{Nothing}(C_NULL), Ptr{Nothing}(C_NULL)) end Base.@kwdef mutable struct LwsClientConnectInfo @@ -310,7 +348,7 @@ Base.@kwdef mutable struct LwsClientConnectInfo alpn::Ptr{Cchar} = C_NULL seq::Ptr{LwsSequencer} = C_NULL opaque_user_data::Ptr{Cvoid} = C_NULL - retry_and_idle_policy::Ptr{Cint} = C_NULL + retry_and_idle_policy::Ptr{LwsRetryBo} = C_NULL manual_initial_tx_credit::Cint = 0 sys_tls_client_cert::Cuchar = 0 priority::Cuchar = 0 @@ -318,10 +356,7 @@ Base.@kwdef mutable struct LwsClientConnectInfo fi_wsi_name::Ptr{Cchar} = C_NULL keep_warm_secs::Cushort = 0 log_cx::Ptr{LwsLogCx} = C_NULL - _unused1::Ptr{Cvoid} = C_NULL - _unused2::Ptr{Cvoid} = C_NULL - _unused3::Ptr{Cvoid} = C_NULL - _unused4::Ptr{Cvoid} = C_NULL + _unused::NTuple{4,Ptr{Cvoid}} = (Ptr{Nothing}(C_NULL), Ptr{Nothing}(C_NULL), Ptr{Nothing}(C_NULL), Ptr{Nothing}(C_NULL)) end Base.@kwdef mutable struct LwsProcessHtmlArgs