rust-nex/src/protocols/mod.rs

61 lines
2.3 KiB
Rust
Raw Normal View History

2025-02-04 16:31:56 +01:00
use crate::prudp::socket::ConnectionData;
pub mod auth;
pub mod server;
2025-02-04 16:31:56 +01:00
pub mod secure;
pub mod matchmake_extension;
pub mod matchmake_common;
2025-02-04 16:31:56 +01:00
#[macro_export]
macro_rules! define_protocol {
($id:literal ($($varname:ident : $ty:ty),*) => {$($func_id:literal => $func:path),*} ) => {
#[allow(unused_parens)]
2025-02-04 16:31:56 +01:00
async fn protocol (rmcmessage: &crate::RMCMessage, connection: &mut crate::protocols::ConnectionData, $($varname : $ty),*) -> Option<crate::rmc::response::RMCResponse>{
if rmcmessage.protocol_id != $id{
return None;
}
let self_data: ( $( $ty ),* ) = ($( $varname ),*);
let response_result = match rmcmessage.method_id{
$(
2025-02-04 16:31:56 +01:00
$func_id => $func ( rmcmessage, connection, self_data).await,
2025-02-01 20:59:21 +01:00
)*
_ => {
2025-02-04 16:31:56 +01:00
log::error!("invalid method id sent to protocol {}: {:?}", $id, rmcmessage.method_id);
2025-02-01 20:59:21 +01:00
return Some(
2025-02-04 16:31:56 +01:00
crate::rmc::response::RMCResponse{
2025-02-01 20:59:21 +01:00
protocol_id: $id,
2025-02-04 16:31:56 +01:00
response_result: rmcmessage.error_result_with_code(crate::rmc::response::ErrorCode::Core_NotImplemented)
2025-02-01 20:59:21 +01:00
}
);
}
};
2025-02-04 16:31:56 +01:00
Some(crate::rmc::response::RMCResponse{
protocol_id: $id,
response_result
})
}
#[allow(unused_parens)]
2025-02-04 16:31:56 +01:00
pub fn bound_protocol($($varname : $ty,)*) -> Box<dyn for<'message_lifetime> Fn(&'message_lifetime crate::RMCMessage, &'message_lifetime mut crate::protocols::ConnectionData)
-> ::std::pin::Pin<Box<dyn ::std::future::Future<Output = Option<crate::rmc::response::RMCResponse>> + Send + 'message_lifetime>> + Send + Sync>{
Box::new(
2025-02-04 16:31:56 +01:00
move |v, cd| {
Box::pin({
$(
let $varname = $varname.clone();
)*
async move {
$(
let $varname = $varname.clone();
)*
protocol(v, cd, $($varname,)*).await
}
})
}
)
}
};
}