1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00

Add status reporting to auth server

This commit is contained in:
NG (Graham)
2025-11-03 19:24:20 -05:00
parent 7579963893
commit 2583429af8
28 changed files with 216 additions and 18 deletions

View File

@@ -10,7 +10,10 @@ use tokio::net;
use polariton::packet::{Data, Message, Packet, StandardMessage};
use polariton::operation::{OperationResponse, Typed};
pub type UserTy = oj_rc_core::UserState<crate::data::custom::CustomType>;
pub type UserTy = std::sync::Arc<oj_rc_core::UserState<crate::data::custom::CustomType>>;
pub static START_TIMESTAMP_S: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0);
pub static ONLINE_USERS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
#[tokio::main]
async fn main() -> std::io::Result<()> {
@@ -27,6 +30,9 @@ async fn main() -> std::io::Result<()> {
let listener = net::TcpListener::bind(std::net::SocketAddr::new(ip_addr, args.port)).await?;
let start_time = chrono::Utc::now();
START_TIMESTAMP_S.store(start_time.timestamp(), std::sync::atomic::Ordering::Relaxed);
if args.once {
log::warn!("Handling first connection and then exiting");
let (socket, address) = listener.accept().await?;
@@ -51,13 +57,18 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
return;
}
};
ONLINE_USERS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let (socket_r, socket_w) = socket.into_split();
let (chann_tx, chann_rx) = tokio::sync::mpsc::unbounded_channel();
let user_state = oj_rc_core::UserState::<crate::data::custom::CustomType>::new(users, chann_tx.clone());
let user_state = std::sync::Arc::new(oj_rc_core::UserState::<crate::data::custom::CustomType>::new(users, chann_tx.clone()));
let op_ctx = polariton::serdes::SerdesContext::<crate::data::custom::CustomType, crate::data::custom::CustomTypeSerdes>::default_const();
let ctx = polariton::packet::SerdesContext::from_boxed(op_ctx, enc);
server.handle_async_with_channel(socket_r, socket_w, user_state, ctx, chann_tx, chann_rx).await;
server.handle_async_with_channel_join(socket_r, socket_w, user_state.clone(), ctx, chann_tx, chann_rx).await;
log::debug!("Goodbye connection from address {}", address);
ONLINE_USERS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
if let Ok(user_info) = user_state.user() {
update_status(user_info.as_ref().as_ref()).await;
}
}
const APP_ID: &str = "SocialServer";
@@ -263,3 +274,14 @@ async fn do_connect_handshake(
Some(ctx.into_crypto())
}
pub async fn update_status(user_info: &dyn oj_rc_core::persist::user::IntercomUser) {
user_info.update_status(
env!("CARGO_PKG_NAME"),
oj_serdes::ServerStatus {
uptime_s: (chrono::Utc::now().timestamp() - crate::START_TIMESTAMP_S.load(std::sync::atomic::Ordering::Relaxed)).try_into().unwrap_or_default(),
players: ONLINE_USERS.load(std::sync::atomic::Ordering::SeqCst),
version: env!("CARGO_PKG_VERSION").to_owned(),
},
).await;
}

View File

@@ -15,6 +15,7 @@ impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
let params_dict = params.to_dict();
if let Some(Typed::Str(auth_payload)) = params_dict.get(&Self::AUTH_PAYLOAD_KEY) {
if user.update_with_auth(&auth_payload.string).await {
crate::update_status(user.user().unwrap().as_ref().as_ref()).await;
let mut resp_params = std::collections::HashMap::with_capacity(1);
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
return polariton::operation::OperationResponse {