mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Move common components to shared lib
This commit is contained in:
@@ -11,6 +11,14 @@ pub struct CliArgs {
|
||||
#[arg(long, default_value_t = {"127.0.0.1".to_string()})]
|
||||
pub ip: String,
|
||||
|
||||
/// Assets root
|
||||
#[arg(long, default_value_t = {"../assets/robocraft".to_string()})]
|
||||
pub assets: String,
|
||||
|
||||
/// User data root
|
||||
#[arg(long, default_value_t = {"../data/robocraft".to_string()})]
|
||||
pub data: String,
|
||||
|
||||
/// Handle one connection and then exit
|
||||
#[arg(short = '1', long)]
|
||||
pub once: bool,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
mod data;
|
||||
mod operations;
|
||||
@@ -10,7 +9,7 @@ use tokio::net;
|
||||
use polariton::packet::{Data, Message, Packet, StandardMessage};
|
||||
use polariton::operation::{OperationResponse, Typed};
|
||||
|
||||
pub type UserTy = std::sync::RwLock<state::UserState>;
|
||||
pub type UserTy = rc_core::UserState;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@@ -18,6 +17,9 @@ async fn main() -> std::io::Result<()> {
|
||||
let args = cli::CliArgs::get();
|
||||
log::debug!("Got cli args {:?}", args);
|
||||
|
||||
let cubes = rc_core::persist::config::ConfigImpl::load(&args.assets).expect("Bad config data");
|
||||
let users = std::sync::Arc::new(rc_core::persist::user::UserImpl::load(&args.data, &cubes).expect("Bad user data"));
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(), polariton_server::events::EventsHandler::new()));
|
||||
|
||||
let ip_addr: std::net::IpAddr = args.ip.parse().expect("Invalid IP address");
|
||||
@@ -27,11 +29,11 @@ async fn main() -> std::io::Result<()> {
|
||||
if args.once {
|
||||
log::warn!("Handling first connection and then exiting");
|
||||
let (socket, address) = listener.accept().await?;
|
||||
process_socket(socket, address, server.clone()).await;
|
||||
process_socket(socket, address, server.clone(), users.clone()).await;
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone()));
|
||||
tokio::spawn(process_socket(socket, address, server.clone(), users.clone()));
|
||||
}
|
||||
}
|
||||
server.join();
|
||||
@@ -39,7 +41,7 @@ async fn main() -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>) {
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>, users: std::sync::Arc<rc_core::persist::user::UserImpl>) {
|
||||
log::debug!("Accepting connection from address {}", address);
|
||||
let enc = match do_connect_handshake(&mut socket).await {
|
||||
Some(x) => x,
|
||||
@@ -48,9 +50,10 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let user_state = state::UserState::new();
|
||||
let (chann_tx, chann_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let user_state = rc_core::UserState::<()>::new(users, chann_tx.clone());
|
||||
let (socket_r, socket_w) = socket.into_split();
|
||||
let _packet_chann = server.handle_async(socket_r, socket_w, user_state, polariton::packet::SerdesContext::from_boxed(Default::default(), enc)).await;
|
||||
server.handle_async_with_channel(socket_r, socket_w, user_state, polariton::packet::SerdesContext::from_boxed(Default::default(), enc), chann_tx, chann_rx).await;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,7 @@ impl <C> Operation<C> for MoreLobbyAuth {
|
||||
fn handle(&self, params: polariton::operation::ParameterTable<C>, _: &mut Self::State, user: &Self::User) -> polariton::operation::OperationResponse<C> {
|
||||
let params_dict = params.to_dict();
|
||||
if let Some(Typed::Str(auth_payload)) = params_dict.get(&Self::AUTH_PAYLOAD_KEY) {
|
||||
let mut write_lock = user.write().unwrap();
|
||||
if write_lock.update_with_auth(&auth_payload.string) {
|
||||
if user.update_with_auth(&auth_payload.string) {
|
||||
let mut resp_params = std::collections::HashMap::new();
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
return polariton::operation::OperationResponse {
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct UserState {
|
||||
pub uuid: String,
|
||||
pub token: String,
|
||||
pub refresh_token: String,
|
||||
}
|
||||
|
||||
impl UserState {
|
||||
pub fn update_with_auth(&mut self, auth_str: &str) -> bool {
|
||||
let splits: Vec<&str> = auth_str.split(';').collect();
|
||||
if splits.len() != 3 {
|
||||
log::warn!("Invalid auth payload: {}", auth_str);
|
||||
false
|
||||
} else {
|
||||
self.uuid = splits[0].to_owned();
|
||||
self.token = splits[1].to_owned();
|
||||
self.refresh_token = splits[2].to_owned();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new() -> crate::UserTy {
|
||||
RwLock::new(UserState::default())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user