|
|
@ -10,7 +10,14 @@ use tokio::{ |
|
|
|
|
|
|
|
|
use std::{collections::HashMap, net::SocketAddr, sync::Arc}; |
|
|
use std::{collections::HashMap, net::SocketAddr, sync::Arc}; |
|
|
|
|
|
|
|
|
use crate::{db, games::RunningGame, server_properties::ServerProperties}; |
|
|
use crate::{ |
|
|
|
|
|
db, |
|
|
|
|
|
games::RunningGame, |
|
|
|
|
|
server::connection::{ |
|
|
|
|
|
connect, create_lobby, disconnect, get_public_lobbies, join_lobby_with_code, name, |
|
|
|
|
|
}, |
|
|
|
|
|
server_properties::ServerProperties, |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
use prost::Message; |
|
|
use prost::Message; |
|
|
|
|
|
|
|
|
@ -31,7 +38,9 @@ pub async fn start( |
|
|
games: Vec<crate::games::Game>, |
|
|
games: Vec<crate::games::Game>, |
|
|
properties: crate::server_properties::ServerProperties, |
|
|
properties: crate::server_properties::ServerProperties, |
|
|
) { |
|
|
) { |
|
|
serve(pool, games, properties).await.expect("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") |
|
|
serve(pool, games, properties) |
|
|
|
|
|
.await |
|
|
|
|
|
.expect("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") |
|
|
// let connection = ConnectionService {
|
|
|
// let connection = ConnectionService {
|
|
|
// conn: RwLock::new(pool.clone().await),
|
|
|
// conn: RwLock::new(pool.clone().await),
|
|
|
// properties: properties.clone(),
|
|
|
// properties: properties.clone(),
|
|
|
@ -111,7 +120,7 @@ pub async fn serve( |
|
|
running_games: running_games.clone(), |
|
|
running_games: running_games.clone(), |
|
|
voting: voting.clone(), |
|
|
voting: voting.clone(), |
|
|
addr, |
|
|
addr, |
|
|
db: pool.clone().await |
|
|
db: pool.clone().await, |
|
|
}; |
|
|
}; |
|
|
tokio::spawn(async move { |
|
|
tokio::spawn(async move { |
|
|
loop { |
|
|
loop { |
|
|
@ -130,18 +139,30 @@ pub async fn serve( |
|
|
.data |
|
|
.data |
|
|
.expect("Unexpected data in packet recieved"); |
|
|
.expect("Unexpected data in packet recieved"); |
|
|
use protos::protocol::client_server_packet::Data; |
|
|
use protos::protocol::client_server_packet::Data; |
|
|
|
|
|
|
|
|
match packet { |
|
|
match packet { |
|
|
// CONNECTION
|
|
|
// CONNECTION
|
|
|
Data::QueryName(()) => {} |
|
|
Data::QueryName(()) => name(&mut service_data) |
|
|
Data::Connect(name) => { |
|
|
.await |
|
|
connection::connect(&mut service_data, name).await.expect("Error handling connect") |
|
|
.expect("Error handling name query"), |
|
|
} |
|
|
Data::Connect(name) => connect(&mut service_data, name) |
|
|
Data::Disconnect(()) => {} |
|
|
.await |
|
|
Data::JoinLobby(code) => {} |
|
|
.expect("Error handling connect"), |
|
|
Data::CreateLobby(config) => {} |
|
|
Data::Disconnect(()) => disconnect(&mut service_data) |
|
|
Data::QueryGames(()) => {} |
|
|
.await |
|
|
Data::QueryPublicLobbies(()) => {} |
|
|
.expect("Error handling disconnect"), |
|
|
|
|
|
Data::JoinLobby(code) => join_lobby_with_code(&mut service_data, code) |
|
|
|
|
|
.await |
|
|
|
|
|
.expect("Error handling join with code"), |
|
|
|
|
|
Data::CreateLobby(config) => create_lobby(&mut service_data, config) |
|
|
|
|
|
.await |
|
|
|
|
|
.expect("Error handling create lobby"), |
|
|
|
|
|
Data::QueryGames(()) => connection::games(&mut service_data) |
|
|
|
|
|
.await |
|
|
|
|
|
.expect("Error handling games query"), |
|
|
|
|
|
Data::QueryPublicLobbies(()) => get_public_lobbies(&mut service_data) |
|
|
|
|
|
.await |
|
|
|
|
|
.expect("Error handling public lobbies query"), |
|
|
|
|
|
|
|
|
// LOBBY
|
|
|
// LOBBY
|
|
|
Data::QueryUsers(()) => {} |
|
|
Data::QueryUsers(()) => {} |
|
|
@ -163,13 +184,14 @@ struct MessageWriter { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl MessageWriter { |
|
|
impl MessageWriter { |
|
|
async fn write(&mut self, packet: protos::protocol::ServerClientPacket) -> Result<()> { |
|
|
async fn write(&mut self, packet: protos::protocol::server_client_packet::Data) -> Result<()> { |
|
|
let length = packet.encoded_len(); |
|
|
let length = packet.encoded_len(); |
|
|
if length > u32::MAX as usize { |
|
|
if length > u32::MAX as usize { |
|
|
panic!("Can't send a message larger than {} bytes", u32::MAX); |
|
|
panic!("Can't send a message larger than {} bytes", u32::MAX); |
|
|
} |
|
|
} |
|
|
let mut data = vec![0u8; length]; |
|
|
let mut data = vec![0u8; length]; |
|
|
packet.encode(&mut data.as_mut_slice())?; |
|
|
protos::protocol::ServerClientPacket { data: Some(packet) } |
|
|
|
|
|
.encode(&mut data.as_mut_slice())?; |
|
|
|
|
|
|
|
|
self.writer |
|
|
self.writer |
|
|
.write_all(&(length as u32).to_le_bytes()) |
|
|
.write_all(&(length as u32).to_le_bytes()) |
|
|
|