is5 #6

Merged
yukirij merged 4 commits from is5 into main 2024-12-17 16:33:03 -05:00
9 changed files with 290 additions and 313 deletions

View File

@ -12,7 +12,6 @@ hyper-tungstenite = "0.14.0"
rustls = "0.23.5"
rustls-pemfile = "2.1.2"
webpki-roots = "0.26"
opaque-ke = "2.0.0"
hyper = { version = "1.4.1", features = ["full"] }
hyper-util = { version = "0.1.7", features = ["tokio"] }
http-body-util = "0.1.2"
@ -26,7 +25,6 @@ regex = "1.11.0"
game = { path = "../game" }
bus = { git = "https://git.tsukiyo.org/Utility/bus" }
sparse = { git = "https://git.tsukiyo.org/Utility/sparse" }
trie = { git = "https://git.tsukiyo.org/Utility/trie" }
pool = { git = "https://git.tsukiyo.org/Utility/pool" }

View File

@ -14,7 +14,6 @@ type StreamType = Arc<RwLock<SplitSink<WebSocketStream<TokioIo<Upgraded>>, Messa
#[derive(Clone)]
pub struct Connection {
pub bus:u32,
pub stream:StreamType,
pub auth:Option<AuthToken>,

View File

@ -160,7 +160,9 @@ impl App {
use tokio_tungstenite::tungstenite::Message;
use futures::SinkExt;
if match response.data { QRPacketData::None => false, _ => true } {
match response.data {
QRPacketData::None => { }
_ => {
if let Some(conn) = self.connections.get(response.id as usize) {
let mut socket = conn.stream.write().await;
@ -260,6 +262,7 @@ impl App {
}
}
}
}
pub async fn send_session_spectators(&mut self, token:SessionToken)
{

View File

@ -1,8 +1,6 @@
use std::net::SocketAddr;
use std::path::Path;
use bus::Bus;
mod config;
mod util;
mod app;
@ -13,6 +11,7 @@ mod manager;
use app::App;
use hyper::body::Bytes;
use hyper_util::rt::TokioIo;
use tokio::sync::mpsc::{self, Sender};
use system::{
cache::WebCache,
net::{
@ -21,10 +20,11 @@ use system::{
tls::*,
},
};
use protocol::QRPacket;
#[derive(Clone)]
struct HttpServiceArgs {
bus:Bus<protocol::QRPacket>,
tx:Sender<QRPacket>,
cache:WebCache,
}
@ -166,24 +166,16 @@ async fn main()
/*
** Initialize central bus and data serivce.
*/
let b_main :Bus<protocol::QRPacket> = Bus::new_as(bus::Mode::Transmitter);
match b_main.connect() {
Ok(bus) => {
let (data_tx, data_rx) = mpsc::channel::<QRPacket>(64);
tokio::spawn(async move {
manager::thread_system(app, bus).await;
manager::thread_system(app, data_rx).await;
});
}
Err(_) => {
println!("fatal: failed to initialize bus.");
return;
}
}
/*
** Load image assets.
*/
let mut js_asset_data = String::from("const GAME_ASSET = { Image: {");
let mut js_asset_data = String::from("const GAME_ASSET={Image:{");
let asset_path = Path::new("www/asset/");
for name in [
"Promote",
@ -202,10 +194,7 @@ async fn main()
println!("error: failed to load asset: {}", name);
}
}
js_asset_data += "} };";
match b_main.connect() {
Ok(bus) => {
js_asset_data += "}};";
/*
** Cache source files.
@ -276,11 +265,11 @@ async fn main()
let mut tcp_server = TcpServer::new();
match tcp_server.bind("127.0.0.1:38611").await {
Ok(_) => {
let b = bus.connect().unwrap();
let c = cache.clone();
let data_tx = data_tx.clone();
tokio::spawn(async move {
while tcp_server.accept(handle_tcp, HttpServiceArgs {
bus:b.connect().unwrap(),
tx:data_tx.clone(),
cache:c.clone(),
}).await.is_ok() { }
});
@ -301,11 +290,11 @@ async fn main()
}
match tls_server.bind("0.0.0.0:38612").await {
Ok(_) => {
let b = bus.connect().unwrap();
let c = cache.clone();
let data_tx = data_tx.clone();
tokio::spawn(async move {
while tls_server.accept(handle_tls, HttpServiceArgs {
bus:b.connect().unwrap(),
tx:data_tx.clone(),
cache:c.clone(),
}).await.is_ok() { }
});
@ -314,11 +303,6 @@ async fn main()
println!("error: failed to bind TLS port 38612.");
}
}
}
Err(_) => {
println!("error: failed to initialize HTTPS service.");
}
}
loop { tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; }
}

View File

@ -1,4 +1,4 @@
use bus::Bus;
use tokio::sync::mpsc::Receiver;
use game::history::Play;
use crate::{
config,
@ -10,13 +10,12 @@ use crate::{
session::{Session, SessionFilter},
context::{self, Context},
},
protocol,
protocol::*,
};
pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
pub async fn thread_system(mut app:App, mut rx:Receiver<QRPacket>)
{
use futures::SinkExt;
use protocol::*;
use ring::rand::{SecureRandom, SystemRandom};
let rng = SystemRandom::new();
@ -24,8 +23,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
let mut send_user_status = Vec::<u32>::new();
while let Some(packet) = bus.receive_wait() {
let qr = packet.data;
while let Some(qr) = rx.recv().await {
let mut user_id = None;
let mut context = Context::None;
@ -40,10 +38,12 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
}
match match qr.data {
QRPacketData::QConn(request) => {
QRPacketData::QConn {
tx,
stream,
} => {
let id = app.connections.add(Connection {
bus: request.bus_id,
stream: request.stream,
stream,
auth: None,
context:Context::None,
@ -58,10 +58,9 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
app.log.log(&format!("Connect: {}", id));
bus.send(
packet.from,
tx.send(
QRPacket::new(id as u32, QRPacketData::RConn)
).ok();
).await.ok();
Some(QRPacket::new(0, QRPacketData::None))
}
@ -1053,10 +1052,8 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
}
}
fn generate_game_state(app:&App, session:&Session) -> protocol::PacketGameStateResponse
fn generate_game_state(app:&App, session:&Session) -> PacketGameStateResponse
{
use protocol::PacketGameStateResponse;
let mut response = PacketGameStateResponse::new();
response.token = session.token;

View File

@ -1,5 +1,5 @@
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::sync::{RwLock, mpsc};
use hyper::upgrade::Upgraded;
use hyper_util::rt::TokioIo;
use tokio_tungstenite::WebSocketStream;
@ -21,27 +21,28 @@ pub async fn handle_ws(ws:WebSocketStream<TokioIo<Upgraded>>, args:HttpServiceAr
let conn_id :u32;
let (sink, mut stream) = ws.split();
let bus_ds = args.bus.mailbox(1).unwrap_or(1);
let (tx, mut rx) = mpsc::channel::<QRPacket>(1);
// Perform connection handshake with data system.
// - Provide system with connection/bus pairing.
// - Acquire connection id.
//
args.bus.send(bus_ds, QRPacket::new(0, QRPacketData::QConn(LocalPacketConnect {
bus_id:args.bus.id(),
args.tx.send(QRPacket::new(0, QRPacketData::QConn {
tx:tx.clone(),
stream:Arc::new(RwLock::new(sink)),
})))?;
match args.bus.receive_wait() {
Some(resp) => {
let qr = &resp.data;
match qr.data {
})).await.map_err(|_| ())?;
if let Some(qr) = rx.recv().await {
match &qr.data {
QRPacketData::RConn => { conn_id = qr.id; }
_ => { return Err(()); }
}
} else {
return Err(());
}
None => { return Err(()); }
}
drop(tx);
drop(rx);
// Decode client requests from websocket,
// pass requests to data system,
@ -55,171 +56,154 @@ pub async fn handle_ws(ws:WebSocketStream<TokioIo<Upgraded>>, args:HttpServiceAr
match code {
CODE_HELLO => {
args.bus.send(
bus_ds, QRPacket::new(conn_id, QRPacketData::QHello)
).ok();
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QHello)
).await.ok();
}
CODE_REGISTER => match PacketRegister::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QRegister(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_AUTH => match PacketAuth::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QAuth(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_AUTH_RESUME => match PacketAuthResume::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QAuthResume(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_AUTH_REVOKE => {
args.bus.send(
bus_ds, QRPacket::new(conn_id,
QRPacketData::QAuthRevoke)
).ok();
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QAuthRevoke)
).await.ok();
}
CODE_SESSION_LIST => match PacketSessionList::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QSessionList(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
/*CODE_SESSION_JOIN => match PacketSessionCreate::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
args.tx.send(
bus_ds,
QRPacket::new(conn_id, QRPacketData::QSessionCreate(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}*/
CODE_SESSION_VIEW => match PacketSessionView::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QSessionView(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_SESSION_LEAVE => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QSessionLeave)
).ok();
).await.ok();
}
CODE_SESSION_RETIRE => match PacketSessionRetire::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QSessionResign(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_GAME_STATE => match PacketGameState::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QGameState(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_GAME_MESSAGE => match PacketGameMessage::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::GameMessage(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_CHALLENGE => match PacketChallenge::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QChallenge(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_CHALLENGE_ANSWER => match PacketChallengeAnswer::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QChallengeAnswer(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_CHALLENGE_LIST => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QChallengeList)
).ok();
).await.ok();
}
CODE_USER_LIST => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QUserList)
).ok();
).await.ok();
}
CODE_USER_PROFILE => match PacketUserProfile::decode(&data, &mut index) {
Ok(packet) => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QUserProfile(packet))
).ok();
).await.ok();
}
Err(_) => { println!("error: packet decode failed."); }
}
CODE_INVITE_LIST => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QInviteList)
).ok();
).await.ok();
}
CODE_INVITE_ACQUIRE => {
args.bus.send(
bus_ds,
args.tx.send(
QRPacket::new(conn_id, QRPacketData::QInviteAcquire)
).ok();
).await.ok();
}
_ => { }
@ -242,6 +226,6 @@ pub async fn handle_ws(ws:WebSocketStream<TokioIo<Upgraded>>, args:HttpServiceAr
None => false,
} { }
args.bus.send(bus_ds, QRPacket::new(conn_id, QRPacketData::QDisconn)).ok();
args.tx.send(QRPacket::new(conn_id, QRPacketData::QDisconn)).await.ok();
Ok(())
}

View File

@ -1,5 +1,12 @@
#![allow(dead_code)]
use std::sync::Arc;
use tokio::sync::{mpsc::Sender, RwLock};
use futures::stream::SplitSink;
use hyper::upgrade::Upgraded;
use hyper_util::rt::TokioIo;
use tokio_tungstenite::{tungstenite::Message, WebSocketStream};
pub mod code; pub use code::*;
pub mod packet; pub use packet::*;
@ -7,7 +14,10 @@ pub mod packet; pub use packet::*;
pub enum QRPacketData {
None,
QConn(LocalPacketConnect),
QConn {
tx:Sender<QRPacket>,
stream:Arc<RwLock<SplitSink<WebSocketStream<TokioIo<Upgraded>>, Message>>>,
},
RConn,
QDisconn,

View File

@ -1,12 +1,14 @@
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::sync::{mpsc::Sender, RwLock};
use futures::stream::SplitSink;
use hyper::upgrade::Upgraded;
use hyper_util::rt::TokioIo;
use tokio_tungstenite::{tungstenite::Message, WebSocketStream};
use crate::protocol::QRPacket;
#[derive(Clone)]
pub struct LocalPacketConnect {
pub bus_id:u32,
pub tx:Sender<QRPacket>,
pub stream:Arc<RwLock<SplitSink<WebSocketStream<TokioIo<Upgraded>>, Message>>>,
}

View File

@ -1,5 +1,5 @@
mod hello; pub use hello::*;
mod connect; pub use connect::*;
//mod connect; pub use connect::*;
mod register; pub use register::*;
mod auth; pub use auth::*;