mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Update for event system in (failed) attempt to get Singleplayer to spawn robots
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use polariton::packet::{Packet, Message, StandardMessage, Data, Cryptographer};
|
||||
use polariton::packet::{Packet, Message, StandardMessage, Data};
|
||||
use polariton::operation::{Typed, ParameterTable, OperationResponse};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -153,7 +153,7 @@ impl <T: AuthProvider<E>, E> Handshake<Auth<T, E>> {
|
||||
const AUTH_REQUEST_CODE: u8 = 230;
|
||||
const USER_ID_KEY: u8 = 225;
|
||||
const NICKNAME_KEY: u8 = 225;
|
||||
pub fn authenticate<'a>(mut self, packet: &'a Packet, crypto: &dyn Cryptographer) -> Result<Packet, HandshakeAnd<Auth<T, E>, AuthError<E>>> {
|
||||
pub fn authenticate<'a>(mut self, packet: &'a Packet, serdes_ctx: &polariton::packet::SerdesContext<(), polariton::serdes::NoCustomSerdes>) -> Result<Packet, HandshakeAnd<Auth<T, E>, AuthError<E>>> {
|
||||
if let Packet::Packet(packet) = &packet {
|
||||
if let Message::Standard(conn) = &packet.message {
|
||||
if let Data::OpReq(req) = &conn.data {
|
||||
@@ -172,8 +172,6 @@ impl <T: AuthProvider<E>, E> Handshake<Auth<T, E>> {
|
||||
params_resp.insert(Self::USER_ID_KEY, user_id.to_owned());
|
||||
params_resp.insert(Self::NICKNAME_KEY, user_id.to_owned());
|
||||
}
|
||||
let serdes_ctx = Default::default();
|
||||
let serdes_ctx = polariton::packet::SerdesContext::new(&serdes_ctx, crypto);
|
||||
return Ok(Packet::from_message(
|
||||
Message::Standard(
|
||||
StandardMessage {
|
||||
@@ -185,7 +183,7 @@ impl <T: AuthProvider<E>, E> Handshake<Auth<T, E>> {
|
||||
params: params_resp.into(),
|
||||
})
|
||||
}.encrypt(conn.is_encrypted())
|
||||
), packet.header.channel, true, &serdes_ctx).unwrap());
|
||||
), packet.header.channel, true, serdes_ctx).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
use polariton_auth::Handshake;
|
||||
use tokio::net;
|
||||
@@ -44,11 +43,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let sock_state = state::State::new(enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &sock_state.serdes_ctx()).await {
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &ctx).await {
|
||||
match packet {
|
||||
Packet::Ping(ping) => {
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &sock_state.serdes_ctx()).await.unwrap_or_default();
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &ctx).await.unwrap_or_default();
|
||||
},
|
||||
Packet::Packet(packet) => log::warn!("Not handling packet {:?}", packet),
|
||||
}
|
||||
@@ -107,7 +106,7 @@ async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
game_server_name: &str,
|
||||
game_server_url: &str,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -168,7 +167,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -188,7 +187,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -257,5 +256,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
const SERDES_SERDES_CTX_REF: &'static polariton::serdes::SerdesContext<(), polariton::serdes::NoCustomSerdes> = &polariton::serdes::SerdesContext::default_const();
|
||||
|
||||
pub struct State {
|
||||
pub crypto: polariton_auth::CryptoImpl,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(c: polariton_auth::CryptoImpl) -> Self {
|
||||
Self {
|
||||
crypto: c,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serdes_ctx(&self) -> polariton::packet::SerdesContext<'_, (), polariton::serdes::NoCustomSerdes> {
|
||||
polariton::packet::SerdesContext::new(SERDES_SERDES_CTX_REF, &self.crypto)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let args = cli::CliArgs::get();
|
||||
log::debug!("Got cli args {:?}", args);
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler()));
|
||||
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,14 +27,16 @@ 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).await;
|
||||
Ok(())
|
||||
process_socket(socket, address, server.clone()).await;
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone()));
|
||||
}
|
||||
}
|
||||
server.join();
|
||||
server.join_async().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>) {
|
||||
@@ -47,7 +49,8 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
}
|
||||
};
|
||||
let user_state = state::UserState::new();
|
||||
server.handle_async(socket, user_state, enc, Default::default()).await;
|
||||
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;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
@@ -100,7 +103,7 @@ impl polariton_auth::AuthProvider<AuthError> for AuthImpl {
|
||||
|
||||
async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -161,7 +164,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -181,7 +184,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -252,5 +255,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
use polariton_auth::Handshake;
|
||||
use tokio::net;
|
||||
@@ -43,11 +42,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let sock_state = state::State::new(enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &sock_state.serdes_ctx()).await {
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &ctx).await {
|
||||
match packet {
|
||||
Packet::Ping(ping) => {
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &sock_state.serdes_ctx()).await.unwrap_or_default();
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &ctx).await.unwrap_or_default();
|
||||
},
|
||||
Packet::Packet(packet) => log::warn!("Not handling packet {:?}", packet),
|
||||
}
|
||||
@@ -106,7 +105,7 @@ async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
game_server_url: &str,
|
||||
game_server_name: &str,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -167,7 +166,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -187,7 +186,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -271,5 +270,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
const SERDES_SERDES_CTX_REF: &'static polariton::serdes::SerdesContext<(), polariton::serdes::NoCustomSerdes> = &polariton::serdes::SerdesContext::default_const();
|
||||
|
||||
pub struct State {
|
||||
pub crypto: polariton_auth::CryptoImpl,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(c: polariton_auth::CryptoImpl) -> Self {
|
||||
Self {
|
||||
crypto: c,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serdes_ctx(&self) -> polariton::packet::SerdesContext<'_, (), polariton::serdes::NoCustomSerdes> {
|
||||
polariton::packet::SerdesContext::new(SERDES_SERDES_CTX_REF, &self.crypto)
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ async fn main() -> std::io::Result<()> {
|
||||
users,
|
||||
});
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(&init_ctx)));
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(&init_ctx), polariton_server::events::EventsHandler::new()));
|
||||
|
||||
let ip_addr: std::net::IpAddr = args.ip.parse().expect("Invalid IP address");
|
||||
|
||||
@@ -41,14 +41,16 @@ 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, init_ctx).await;
|
||||
Ok(())
|
||||
process_socket(socket, address, server.clone(), init_ctx).await;
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone(), init_ctx.clone()));
|
||||
}
|
||||
}
|
||||
server.join();
|
||||
server.join_async().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>, init_ctx: std::sync::Arc<InitConfig>) {
|
||||
@@ -60,8 +62,10 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let (socket_r, socket_w) = socket.into_split();
|
||||
let user_state = std::sync::RwLock::new(state::UserState::<()>::new(init_ctx.users.clone()));
|
||||
server.handle_async(socket, user_state, enc, Default::default()).await;
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
server.handle_async(socket_r, socket_w, user_state, ctx).await;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
@@ -114,7 +118,7 @@ impl polariton_auth::AuthProvider<AuthError> for AuthImpl {
|
||||
|
||||
async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -175,7 +179,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -195,7 +199,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -266,5 +270,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
use polariton_auth::Handshake;
|
||||
use tokio::net;
|
||||
@@ -44,11 +43,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let sock_state = state::State::new(enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &sock_state.serdes_ctx()).await {
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &ctx).await {
|
||||
match packet {
|
||||
Packet::Ping(ping) => {
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &sock_state.serdes_ctx()).await.unwrap_or_default();
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &ctx).await.unwrap_or_default();
|
||||
},
|
||||
Packet::Packet(packet) => log::warn!("Not handling packet {:?}", packet),
|
||||
}
|
||||
@@ -107,7 +106,7 @@ async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
game_server_name: &str,
|
||||
game_server_url: &str,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -168,7 +167,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -188,7 +187,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -259,5 +258,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
const SERDES_SERDES_CTX_REF: &'static polariton::serdes::SerdesContext<(), polariton::serdes::NoCustomSerdes> = &polariton::serdes::SerdesContext::default_const();
|
||||
|
||||
pub struct State {
|
||||
pub crypto: polariton_auth::CryptoImpl,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(c: polariton_auth::CryptoImpl) -> Self {
|
||||
Self {
|
||||
crypto: c,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serdes_ctx(&self) -> polariton::packet::SerdesContext<'_, (), polariton::serdes::NoCustomSerdes> {
|
||||
polariton::packet::SerdesContext::new(SERDES_SERDES_CTX_REF, &self.crypto)
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
log.workspace = true
|
||||
env_logger.workspace = true
|
||||
tokio = { version = "1.43", features = [ "net", "macros", "rt-multi-thread", "io-util" ] }
|
||||
tokio = { version = "1.43", features = [ "net", "macros", "rt-multi-thread", "io-util", "time"] }
|
||||
clap.workspace = true
|
||||
polariton.workspace = true
|
||||
polariton_auth = { version = "*", path = "../polariton_auth" }
|
||||
|
||||
@@ -10,7 +10,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 = state::UserState;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let args = cli::CliArgs::get();
|
||||
log::debug!("Got cli args {:?}", args);
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler()));
|
||||
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,14 +27,16 @@ 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).await;
|
||||
Ok(())
|
||||
process_socket(socket, address, server.clone()).await;
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone()));
|
||||
}
|
||||
}
|
||||
server.join();
|
||||
server.join_async().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>) {
|
||||
@@ -46,8 +48,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let user_state = state::UserState::new();
|
||||
server.handle_async(socket, user_state, enc, Default::default()).await;
|
||||
let (socket_r, socket_w) = socket.into_split();
|
||||
let (chann_tx, chann_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let user_state = state::UserState::new(chann_tx.clone());
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
server.handle_async_with_channel(socket_r, socket_w, user_state, ctx, chann_tx, chann_rx).await;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
@@ -100,7 +105,7 @@ impl polariton_auth::AuthProvider<AuthError> for AuthImpl {
|
||||
|
||||
async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -161,7 +166,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -181,7 +186,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -252,5 +257,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use polariton_server::operations::SimpleFunc;
|
||||
use polariton::operation::ParameterTable;
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
use crate::data::player_data::*;
|
||||
|
||||
@@ -785,7 +785,7 @@ const VALID_COLOUR: &[u8] = &[64,
|
||||
|
||||
pub(super) fn tdm_machines_provider() -> SimpleFunc<1, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
|
||||
SimpleFunc::new(|params, user: &crate::UserTy| {
|
||||
let ulock = user.read().unwrap();
|
||||
let ulock = user.auth.read().unwrap();
|
||||
let mut params = params.to_dict();
|
||||
params.insert(PARAM_KEY, PlayerDatas {
|
||||
players: vec![
|
||||
@@ -838,7 +838,7 @@ pub(super) fn tdm_machines_provider() -> SimpleFunc<1, crate::UserTy, impl (Fn(P
|
||||
robot_map: VALID_ROBOT.iter().map(|x| *x).collect(),
|
||||
team: 1,
|
||||
has_premium: false,
|
||||
robot_uuid: "1_1".to_owned(),
|
||||
robot_uuid: "12_12".to_owned(),
|
||||
cpu: 0,
|
||||
weapon_order: vec![20000200, 0, 0],
|
||||
colour_map: VALID_COLOUR.iter().map(|x| *x).collect(),
|
||||
@@ -850,6 +850,30 @@ pub(super) fn tdm_machines_provider() -> SimpleFunc<1, crate::UserTy, impl (Fn(P
|
||||
},*/
|
||||
]
|
||||
}.as_transmissible());
|
||||
let event_tx = user.event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||
log::debug!("Sending singleplayer events");
|
||||
let mut spawn_params = std::collections::HashMap::with_capacity(4);
|
||||
spawn_params.insert(2 /* robot GUID */, Typed::Str("12_12".into()));
|
||||
spawn_params.insert(3 /* machine model */, Typed::Bytes(VALID_ROBOT.iter().map(|x| *x).collect::<Vec<_>>().into()));
|
||||
spawn_params.insert(4 /* robot name */, Typed::Str("RE_robot_spawn_name0".into()));
|
||||
spawn_params.insert(7 /* color model */, Typed::Bytes(VALID_COLOUR.iter().map(|x| *x).collect::<Vec<_>>().into()));
|
||||
event_tx.send(polariton_server::ToSend::Data {
|
||||
data: polariton::packet::Data::Event(polariton::operation::Event { code: 3, params: spawn_params.into() }),
|
||||
encrypt: true,
|
||||
channel: 0,
|
||||
reliable: true,
|
||||
}).unwrap();
|
||||
let mut update_params = std::collections::HashMap::with_capacity(1);
|
||||
update_params.insert(6 /* ??? */, Typed::Int(5));
|
||||
event_tx.send(polariton_server::ToSend::Data {
|
||||
data: polariton::packet::Data::Event(polariton::operation::Event { code: 5, params: update_params.into() }),
|
||||
encrypt: true,
|
||||
channel: 0,
|
||||
reliable: true,
|
||||
}).unwrap();
|
||||
});
|
||||
Ok(params.into())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ 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) {
|
||||
//let mut write_lock = user.write().unwrap();
|
||||
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 +1,40 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct UserState {
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use polariton_server::ToSend;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct UserAuthInfo {
|
||||
pub uuid: String,
|
||||
pub token: String,
|
||||
pub refresh_token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UserState {
|
||||
pub auth: RwLock<UserAuthInfo>,
|
||||
pub event_tx: UnboundedSender<ToSend>,
|
||||
}
|
||||
|
||||
impl UserState {
|
||||
pub fn update_with_auth(&mut self, auth_str: &str) -> bool {
|
||||
pub fn update_with_auth(&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();
|
||||
let mut lock = self.auth.write().unwrap();
|
||||
lock.uuid = splits[0].to_owned();
|
||||
lock.token = splits[1].to_owned();
|
||||
lock.refresh_token = splits[2].to_owned();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new() -> crate::UserTy {
|
||||
RwLock::new(UserState::default())
|
||||
pub fn new(event_tx: UnboundedSender<ToSend>) -> crate::UserTy {
|
||||
UserState {
|
||||
auth: RwLock::new(Default::default()),
|
||||
event_tx,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
use polariton_auth::Handshake;
|
||||
use tokio::net;
|
||||
@@ -44,11 +43,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let sock_state = state::State::new(enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &sock_state.serdes_ctx()).await {
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
while let Ok(packet) = polariton_server::utils::receive_packet_async(&mut socket, &ctx).await {
|
||||
match packet {
|
||||
Packet::Ping(ping) => {
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &sock_state.serdes_ctx()).await.unwrap_or_default();
|
||||
polariton_server::utils::handle_ping_async(ping, &mut socket, &ctx).await.unwrap_or_default();
|
||||
},
|
||||
Packet::Packet(packet) => log::warn!("Not handling packet {:?}", packet),
|
||||
}
|
||||
@@ -107,7 +106,7 @@ async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
game_server_name: &str,
|
||||
game_server_url: &str,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -168,7 +167,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -188,7 +187,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -257,5 +256,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
const SERDES_SERDES_CTX_REF: &'static polariton::serdes::SerdesContext<(), polariton::serdes::NoCustomSerdes> = &polariton::serdes::SerdesContext::default_const();
|
||||
|
||||
pub struct State {
|
||||
pub crypto: polariton_auth::CryptoImpl,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(c: polariton_auth::CryptoImpl) -> Self {
|
||||
Self {
|
||||
crypto: c,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serdes_ctx(&self) -> polariton::packet::SerdesContext<'_, (), polariton::serdes::NoCustomSerdes> {
|
||||
polariton::packet::SerdesContext::new(SERDES_SERDES_CTX_REF, &self.crypto)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let args = cli::CliArgs::get();
|
||||
log::debug!("Got cli args {:?}", args);
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler()));
|
||||
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,14 +27,16 @@ 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).await;
|
||||
Ok(())
|
||||
process_socket(socket, address, server.clone()).await;
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone()));
|
||||
}
|
||||
}
|
||||
server.join();
|
||||
server.join_async().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy, crate::data::custom::CustomType>>) {
|
||||
@@ -46,9 +48,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let (socket_r, socket_w) = socket.into_split();
|
||||
let user_state = state::UserState::new();
|
||||
let op_ctx = polariton::serdes::SerdesContext::<crate::data::custom::CustomType, crate::data::custom::CustomTypeSerdes>::default_const();
|
||||
server.handle_async(socket, user_state, enc, op_ctx).await;
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(op_ctx, enc);
|
||||
server.handle_async(socket_r, socket_w, user_state, ctx).await;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
@@ -101,7 +105,7 @@ impl polariton_auth::AuthProvider<AuthError> for AuthImpl {
|
||||
|
||||
async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -162,7 +166,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -182,7 +186,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -253,5 +257,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user