adding option on server to define ip:port listen

This commit is contained in:
AleaJactaEst 2023-10-23 22:43:58 +02:00
parent c35345bce2
commit 70f59e12af
4 changed files with 201 additions and 35 deletions

113
server/Cargo.lock generated
View file

@ -26,6 +26,54 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anstream"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.75" version = "1.0.75"
@ -134,6 +182,46 @@ dependencies = [
"libloading", "libloading",
] ]
[[package]]
name = "clap"
version = "4.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
[[package]] [[package]]
name = "cmake" name = "cmake"
version = "0.1.50" version = "0.1.50"
@ -143,6 +231,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.4" version = "0.8.4"
@ -191,6 +285,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "home" name = "home"
version = "0.5.5" version = "0.5.5"
@ -398,6 +498,7 @@ dependencies = [
"anyhow", "anyhow",
"byteorder", "byteorder",
"chrono", "chrono",
"clap",
"enet", "enet",
"log", "log",
] ]
@ -408,6 +509,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.38" version = "2.0.38"
@ -445,6 +552,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.87" version = "0.2.87"

View file

@ -11,3 +11,4 @@ anyhow = "1.0.56"
log = "0.4.20" log = "0.4.20"
chrono = "0.4.31" chrono = "0.4.31"
byteorder = "1.5.0" byteorder = "1.5.0"
clap = { version = "4.4.6", features = [ "derive" ]}

View file

@ -14,9 +14,9 @@ extern crate enet;
target/debug/server-rust target/debug/server-rust
*/ */
use std::net::Ipv4Addr; use std::net::{Ipv4Addr};
use log::{debug, error, info, trace, warn, Level, LevelFilter, Metadata, Record, SetLoggerError}; use log::{debug, error, info, trace, warn, Level, LevelFilter, Metadata, Record, SetLoggerError};
use clap::Parser;
use anyhow::Context; use anyhow::Context;
use enet::*; use enet::*;
@ -101,13 +101,13 @@ enum StateUsers {
Error, Error,
Done, Done,
} }
/*
struct Position { struct Position {
x: f64, x: f64,
y: f64, y: f64,
z: f64, z: f64,
} }
*/
/* /*
* User * User
*/ */
@ -169,10 +169,12 @@ impl User {
self.position_updated = true; self.position_updated = true;
} }
} }
/*
pub fn get_position(&mut self) -> Position { pub fn get_position(&mut self) -> Position {
let pos:Position = Position{x: self.x, y: self.y, z: self.z}; let pos:Position = Position{x: self.x, y: self.y, z: self.z};
return pos; return pos;
} }
*/
pub fn push_packet(&self, data:&mut Vec<u8>) { pub fn push_packet(&self, data:&mut Vec<u8>) {
push_u64(data, self.id); push_u64(data, self.id);
push_f64(data, self.x); push_f64(data, self.x);
@ -360,7 +362,7 @@ impl Users {
pub fn get_user(&mut self, address: Address) -> Result<&User, &'static str> { pub fn get_user(&mut self, address: Address) -> Result<&User, &'static str> {
for user in &self.users { for user in &self.users {
if user.address == address { if user.address == address {
return Ok(user.clone()); return Ok(user);
} }
} }
error!("invalid address {}:{}", address.ip(), address.port()); error!("invalid address {}:{}", address.ip(), address.port());
@ -440,20 +442,6 @@ fn send_message(sender:Peer<()>, data: &[u8] ) -> Result<(), Error> {
1, 1,
) )
} }
/*
fn send_message_connect_ok(sender:Peer<()>, id:u64, x:f64, y:f64, z:f64) -> Result<(), Error> {
let mut data:Vec<u8> = Vec::new();
data.push(1); // return connexion request
data.push(0); // return ok
// get_packet
push_u64(&mut data, id);
push_f64(&mut data, x);
push_f64(&mut data, y);
push_f64(&mut data, z);
let c: &[u8] = &data;
send_message(sender, c)
}
*/
fn send_message_connect_ok(sender:Peer<()>, user: &User) -> Result<(), Error> { fn send_message_connect_ok(sender:Peer<()>, user: &User) -> Result<(), Error> {
let mut data:Vec<u8> = Vec::new(); let mut data:Vec<u8> = Vec::new();
@ -471,30 +459,63 @@ fn send_message_connect_ko(sender:Peer<()>) -> Result<(), Error> {
let c: &[u8] = &data; let c: &[u8] = &data;
send_message(sender, c) send_message(sender, c)
} }
/*
fn show(data: &[u8]) -> String { fn show(data: &[u8]) -> String {
data.iter().map(|b| format!("{:02X}", b)).collect::<Vec<_>>().join(", ") data.iter().map(|b| format!("{:02X}", b)).collect::<Vec<_>>().join(", ")
} }
*/
/*
* Argument
#[arg(short, long, default_value_t = "127.0.0.1")]
*/
#[derive(Parser)]
#[command(name = "server")]
#[command(author = "AleaJactaEst <jean.sorgemoel.liber@free.fr>")]
#[command(version = "1.0")]
#[command(about = "Alea's Bazar Server", long_about = None)]
struct Cli {
/// Ip where server listen
#[arg(short, long, default_value = "127.0.0.1")]
listen: String,
/// Port listen
#[arg(short, long, default_value_t = 33333)]
port: u16,
/// Show trace message
#[arg(short, long)]
trace: bool,
/// Show debug message
#[arg(short, long)]
debug: bool,
/// Show info message
#[arg(short, long)]
verbose: bool,
}
/* /*
* Main * Main
*/ */
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let enet = Enet::new().context("could not initialize ENet")?; let enet = Enet::new().context("could not initialize ENet")?;
//env_logger::init(); if cli.trace {
loginit(LevelFilter::Trace).unwrap(); loginit(LevelFilter::Trace).unwrap();
} else if cli.debug {
loginit(LevelFilter::Debug).unwrap();
} else if cli.verbose {
loginit(LevelFilter::Info).unwrap();
} else {
loginit(LevelFilter::Warn).unwrap();
}
// simple_logging::log_to_stderr(LevelFilter::Info); debug!("Listen: {}", cli.listen);
/* debug!("Port: {}", cli.port);
debug!("Debug");
trace!("Trace"); let addr:Ipv4Addr = cli.listen.as_str().parse::<Ipv4Addr>().unwrap();
info!("Enet initialized"); let local_addr = Address::new(addr, cli.port);
warn!("Enet initialized");
error!("Enet initialized");
*/
let local_addr = Address::new(Ipv4Addr::LOCALHOST, 33333);
let mut host = enet let mut host = enet
.create_host::<()>( .create_host::<()>(
@ -631,7 +652,7 @@ fn main() -> anyhow::Result<()> {
for peer in host.peers() { for peer in host.peers() {
if peer.state() == PeerState::Connected { if peer.state() == PeerState::Connected {
trace!("peer: {}:{}", peer.address().ip(), peer.address().port()); trace!("peer: {}:{}", peer.address().ip(), peer.address().port());
send_message(peer, c); let _ = send_message(peer, c);
} }
} }
users.clear_position_updated(); users.clear_position_updated();

View file

@ -17,12 +17,18 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
# #
# Example : Start server with trace level (more debug message), and listen all network card (0.0.0.0) and port : 9876
# ./start-bazar-server.sh -b -o ' -t -l "0.0.0.0" -p 9876'
declare DEBUG=0 declare DEBUG=0
declare VERBOSE=0
declare HELP=0 declare HELP=0
declare BUILD=0 declare BUILD=0
declare WORKDIR="$(dirname $(readlink -f $0))" declare WORKDIR="$(dirname $(readlink -f $0))"
declare RUSTDIR="$WORKDIR/.rust" declare RUSTDIR="$WORKDIR/.rust"
declare SERVERDIR="$WORKDIR/server" declare SERVERDIR="$WORKDIR/server"
declare OPTIONS=""
declare LISTEN=""
declare PORT=""
function msg_debug() function msg_debug()
{ {
@ -54,12 +60,16 @@ function byebye()
exit $CODE exit $CODE
} }
while getopts hdb flag while getopts hdvbo:l:p: flag
do do
case "${flag}" in case "${flag}" in
h) HELP=1;; h) HELP=1;;
d) DEBUG=1;; d) DEBUG=1;;
v) VERBOSE=1;;
b) BUILD=1;; b) BUILD=1;;
o) OPTIONS=${OPTARG};;
l) LISTEN=${OPTARG};;
p) PORT=${OPTARG};;
*) HELP=1;; *) HELP=1;;
esac esac
done done
@ -71,7 +81,11 @@ $(basename $0) [Option] : Donwload Launch Godot
Option: Option:
-h : Show help -h : Show help
-d : Show debug message -d : Show debug message
-v : Show verbose message
-b : (re)build program -b : (re)build program
-o : option send to server (like --help)
-l : <IP> network listen
-p : <PORT> port listen
EOF EOF
exit 1 exit 1
fi fi
@ -81,6 +95,22 @@ trap byebye EXIT
msg_info "Start" msg_info "Start"
msg_debug "WORKDIR:$WORKDIR" msg_debug "WORKDIR:$WORKDIR"
if [ $DEBUG -ne 0 ]
then
OPTIONS="$OPTIONS -d"
fi
if [ $VERBOSE -ne 0 ]
then
OPTIONS="$OPTIONS -v"
fi
if [ -n "$LISTEN" ]
then
OPTIONS="$OPTIONS -l $LISTEN"
fi
if [ -n "$PORT" ]
then
OPTIONS="$OPTIONS -p $PORT"
fi
if [[ ($BUILD -ne 0) || (! -x $SERVERDIR/target/debug/server) ]] if [[ ($BUILD -ne 0) || (! -x $SERVERDIR/target/debug/server) ]]
then then
@ -101,13 +131,14 @@ then
then then
source $RUSTDIR/.cargo/env source $RUSTDIR/.cargo/env
cd $SERVERDIR cd $SERVERDIR
cargo build cargo build || exit 2
else else
msg_error "Error to load envi rust" msg_error "Error to load envi rust"
exit 2 exit 2
fi fi
fi fi
$SERVERDIR/target/debug/server #echo "OPTIONS:$OPTIONS"
$SERVERDIR/target/debug/server $OPTIONS
# END (call function byebye) # END (call function byebye)