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",
]
[[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]]
name = "anyhow"
version = "1.0.75"
@ -134,6 +182,46 @@ dependencies = [
"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]]
name = "cmake"
version = "0.1.50"
@ -143,6 +231,12 @@ dependencies = [
"cc",
]
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
@ -191,6 +285,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "home"
version = "0.5.5"
@ -398,6 +498,7 @@ dependencies = [
"anyhow",
"byteorder",
"chrono",
"clap",
"enet",
"log",
]
@ -408,6 +509,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.38"
@ -445,6 +552,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "wasm-bindgen"
version = "0.2.87"

View file

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

View file

@ -14,9 +14,9 @@ extern crate enet;
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 clap::Parser;
use anyhow::Context;
use enet::*;
@ -101,13 +101,13 @@ enum StateUsers {
Error,
Done,
}
/*
struct Position {
x: f64,
y: f64,
z: f64,
}
*/
/*
* User
*/
@ -169,10 +169,12 @@ impl User {
self.position_updated = true;
}
}
/*
pub fn get_position(&mut self) -> Position {
let pos:Position = Position{x: self.x, y: self.y, z: self.z};
return pos;
}
*/
pub fn push_packet(&self, data:&mut Vec<u8>) {
push_u64(data, self.id);
push_f64(data, self.x);
@ -360,7 +362,7 @@ impl Users {
pub fn get_user(&mut self, address: Address) -> Result<&User, &'static str> {
for user in &self.users {
if user.address == address {
return Ok(user.clone());
return Ok(user);
}
}
error!("invalid address {}:{}", address.ip(), address.port());
@ -440,20 +442,6 @@ fn send_message(sender:Peer<()>, data: &[u8] ) -> Result<(), Error> {
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> {
let mut data:Vec<u8> = Vec::new();
@ -471,30 +459,63 @@ fn send_message_connect_ko(sender:Peer<()>) -> Result<(), Error> {
let c: &[u8] = &data;
send_message(sender, c)
}
/*
fn show(data: &[u8]) -> String {
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
*/
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let enet = Enet::new().context("could not initialize ENet")?;
//env_logger::init();
loginit(LevelFilter::Trace).unwrap();
if cli.trace {
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!("Debug");
trace!("Trace");
info!("Enet initialized");
warn!("Enet initialized");
error!("Enet initialized");
*/
let local_addr = Address::new(Ipv4Addr::LOCALHOST, 33333);
debug!("Listen: {}", cli.listen);
debug!("Port: {}", cli.port);
let addr:Ipv4Addr = cli.listen.as_str().parse::<Ipv4Addr>().unwrap();
let local_addr = Address::new(addr, cli.port);
let mut host = enet
.create_host::<()>(
@ -631,7 +652,7 @@ fn main() -> anyhow::Result<()> {
for peer in host.peers() {
if peer.state() == PeerState::Connected {
trace!("peer: {}:{}", peer.address().ip(), peer.address().port());
send_message(peer, c);
let _ = send_message(peer, c);
}
}
users.clear_position_updated();

View file

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