1#![deny(
4 dead_code,
5 arithmetic_overflow,
6 invalid_type_param_default,
7 missing_fragment_specifier,
8 mutable_transmutes,
9 no_mangle_const_items,
10 overflowing_literals,
11 patterns_in_fns_without_body,
12 pub_use_of_private_extern_crate,
13 unknown_crate_types,
14 order_dependent_trait_objects,
15 illegal_floating_point_literal_pattern,
16 improper_ctypes,
17 late_bound_lifetime_arguments,
18 non_camel_case_types,
19 non_shorthand_field_patterns,
20 non_snake_case,
21 non_upper_case_globals,
22 no_mangle_generic_items,
23 stable_features,
24 type_alias_bounds,
25 tyvar_behind_raw_pointer,
26 unconditional_recursion,
27 unused_comparisons,
28 unreachable_pub,
29 anonymous_parameters,
30 missing_copy_implementations,
31 missing_debug_implementations,
32 missing_docs,
33 trivial_casts,
34 trivial_numeric_casts,
35 unused_import_braces,
36 unused_qualifications,
37 clippy::all
38)]
39#![forbid(
40 unsafe_code,
41 rustdoc::broken_intra_doc_links,
42 while_true,
43 bare_trait_objects
44)]
45
46use oo_bindgen::model::*;
47
48pub const fn get_impl_file() -> &'static str {
50 include_str!("../runtime.rs")
51}
52
53pub fn define(
55 lib: &mut LibraryBuilder,
56 error_type: ErrorType<Unvalidated>,
57) -> BackTraced<ClassDeclarationHandle> {
58 let runtime = lib.declare_class("runtime")?;
60
61 let config_struct = define_runtime_config(lib)?;
62
63 let constructor = lib
64 .define_constructor(runtime.clone())?
65 .param(
66 "config",
67 config_struct,
68 "Runtime configuration",
69 )?
70 .fails_with(error_type)?
71 .doc(
72 doc("Creates a new runtime for running the protocol stack.")
73 .warning("The runtime should be kept alive for as long as it's needed and it should be released with {class:runtime.[destructor]}")
74 )?
75 .build()?;
76
77 let destructor = lib
78 .define_destructor(
79 runtime.clone(),
80 doc("Destroy a runtime.")
81 .details("This method will gracefully wait for all asynchronous operation to end before returning")
82 )?;
83
84 let set_shutdown_timeout =
85 lib.define_method("set_shutdown_timeout", runtime.clone())?
86 .doc(
87 doc("By default, when the runtime shuts down, it does so without a timeout and waits indefinitely for all spawned tasks to yield.")
88 .details("Setting this value will put a maximum time bound on the eventual shutdown. Threads that have not exited within this timeout will be terminated.")
89 .warning("This can leak memory. This method should only be used if the the entire application is being shut down so that memory can be cleaned up by the OS.")
90 )?
91 .param("timeout", BasicType::Duration(DurationType::Seconds), "Maximum number of seconds to wait for the runtime to shut down")?
92 .build()?;
93
94 let runtime = lib
95 .define_class(&runtime)?
96 .constructor(constructor)?
97 .destructor(destructor)?
98 .method(set_shutdown_timeout)?
99 .custom_destroy("shutdown")?
100 .doc("Handle to the underlying runtime")?
101 .build()?;
102
103 Ok(runtime.declaration())
104}
105
106fn define_runtime_config(lib: &mut LibraryBuilder) -> BackTraced<FunctionArgStructHandle> {
107 let num_core_threads = Name::create("num_core_threads")?;
108
109 let config_struct = lib.declare_function_argument_struct("runtime_config")?;
110 let config_struct= lib
111 .define_function_argument_struct(config_struct)?
112 .add(
113 &num_core_threads,
114 Primitive::U16,
115 doc("Number of runtime threads to spawn. For a guess of the number of CPU cores, use 0.")
116 .details("Even if tons of connections are expected, it is preferred to use a value around the number of CPU cores for better performances. The library uses an efficient thread pool polling mechanism."),
117 )?
118 .doc("Runtime configuration")?
119 .end_fields()?
120 .begin_initializer("init", InitializerType::Normal, "Initialize the configuration to default values")?
121 .default(&num_core_threads, NumberValue::U16(0))?
122 .end_initializer()?
123 .build()?;
124
125 Ok(config_struct)
126}