Skip to content

Commit

Permalink
Some basic setup for the GateServer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Perseus committed Oct 9, 2022
1 parent ac8ca08 commit 47954eb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/rust-clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2


- name: Install Rust toolchain
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af #@v1
with:
Expand All @@ -38,6 +39,9 @@ jobs:
components: clippy
override: true

- name: Install from Cache
uses: Swatinem/rust-cache@v2

- name: Install required cargo
run: cargo install clippy-sarif sarif-fmt

Expand Down
3 changes: 2 additions & 1 deletion gate_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ edition = "2021"
common_utils = { path = "../common_utils" }
serde = { version = "1.0", features = [ "derive" ] }
tokio = { version = "1.21.2", features = [ "full" ] }
colored = { version = "2.0.0" }
colored = { version = "2.0.0" }
anyhow = { version = "1.0.65" }
21 changes: 15 additions & 6 deletions gate_server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use serde::Deserialize;


#[derive(Debug, Deserialize, PartialEq)]
struct GroupConfig {
pub struct GroupConfig {
ip: Ipv4Addr,
port: u16,
ping_duration: u16
}

#[derive(Debug, Deserialize, PartialEq)]
struct ClientConfig {
pub struct ClientConfig {
ip: Ipv4Addr,
port: u16,
ping_duration: u16,
Expand All @@ -23,17 +23,26 @@ struct ClientConfig {
}

#[derive(Debug, Deserialize, PartialEq)]
struct GameServerConfig {
pub struct GameServerConfig {
ip: Ipv4Addr,
port: u16,
ping_duration: u16,
}

#[derive(Debug, Deserialize, PartialEq)]
pub struct GateServerConfig {
group_config: GroupConfig,
client_config: ClientConfig,
game_server_config: GameServerConfig,
pub group_config: GroupConfig,
pub client_config: ClientConfig,
pub game_server_config: GameServerConfig,
}

impl GateServerConfig {
pub fn get_server_ip_and_port(&self) -> (Ipv4Addr, u16) {
(
self.client_config.ip,
self.client_config.port
)
}
}

pub fn parse_config(path: &Path) -> GateServerConfig {
Expand Down
58 changes: 53 additions & 5 deletions gate_server/src/gate_server.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use std::path::Path;
use colored::Colorize;
use tokio::{net::TcpListener, io::AsyncReadExt, io::AsyncWriteExt};

use crate::config::{GateServerConfig, self};

pub struct GateServer {
config: GateServerConfig
config: GateServerConfig,
server: Option<TcpListener>
}

impl GateServer {
pub fn start() -> Self {
pub fn new(path: &str) -> Self {
Self::print_gate_start();

let path = "./config.yaml";
println!("Loading GateServer config from {}", path.blue());
println!("{} {}", "Loading GateServer config from".green(), path.blue().italic());
let config = config::parse_config(Path::new(path));

GateServer {
config
config,
server: None
}
}

Expand All @@ -34,4 +36,50 @@ impl GateServer {
println!("{}", r" by Perseus".red());
println!("{}", r"------------------------------------------------------------------------------------------------------------------------".blue());
}

pub async fn start(&mut self) -> anyhow::Result<()> {
let (ip, port) = self.config.get_server_ip_and_port();
let connect_addr = format!("{}:{}", ip, port);

let server = match TcpListener::bind(&connect_addr).await {
Ok(listener) => listener,
Err(err) => panic!("Unable to start the server at {}, error - {:?}", connect_addr, err),
};

self.server = Some(server);
Ok(())
}

pub async fn start_accepting_connections(self) -> anyhow::Result<()> {
let server = self.server.unwrap();

loop {
let (mut socket, _) = server.accept().await?;
}

}


}


mod tests {
#[tokio::test]
async fn it_should_start_a_tcp_server_at_the_given_port() {
let mut gate_server = super::GateServer::new("./tests/test_gate_config.yaml");
let (gate_server_ip, gate_server_port) = gate_server.config.get_server_ip_and_port();
gate_server.start().await.unwrap();

tokio::spawn(async move {
gate_server.start_accepting_connections().await.unwrap();
});

let connect_addr = format!("{}:{}", gate_server_ip, gate_server_port);

match tokio::net::TcpStream::connect(connect_addr).await {
Ok(conn) => conn,
Err(err) => panic!("Unable to connect to the TCP stream, error - {:?}", err)
};

}
}
8 changes: 6 additions & 2 deletions gate_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use std::path::Path;
use config::parse_config;

#[tokio::main]
async fn main() {
let gate_server = gate_server::GateServer::start();
async fn main() -> anyhow::Result<()> {
let mut gate_server = gate_server::GateServer::new("./config.yaml");

gate_server.start().await?;
gate_server.start_accepting_connections().await?;

Ok(())
}

0 comments on commit 47954eb

Please sign in to comment.