Skip to content

Commit

Permalink
some tweaks and updates
Browse files Browse the repository at this point in the history
- auto reconnect after timeout.
- update static 7779 port to PORT + 2
  • Loading branch information
Pycckue-Bnepeg committed Sep 2, 2021
1 parent 04f75a2 commit c3f43a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions client/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crossbeam_channel::{Receiver, Sender};

use detour::GenericDetour;

const CEF_SERVER_PORT: u16 = 7779;
const CEF_SERVER_PORT_OFFSET: u16 = 2;
pub const CEF_PLUGIN_VERSION: i32 = 0x00_01_00;

static mut APP: Option<App> = None;
Expand Down Expand Up @@ -207,7 +207,7 @@ impl App {

log::trace!("SAMP: CNetGame address: {}", addr);

addr.set_port(CEF_SERVER_PORT);
addr.set_port(addr.port() + CEF_SERVER_PORT_OFFSET);

log::trace!(
"Event::Connect({}). Elapsed {:?}",
Expand Down
2 changes: 2 additions & 0 deletions client/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ impl Network {
if timeout_addr == addr {
log::trace!("CEF Network: Timeout");
handle_result(self.event_tx.send(Event::Timeout));

self.net_open_connection(timeout_addr);
}
}
}
Expand Down
29 changes: 9 additions & 20 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::utils::{handle_result, IdPool};
use crossbeam_channel::Receiver;

const INIT_TIMEOUT: Duration = Duration::from_secs(10);
const PORT_OFFSET: u16 = 2;

pub enum Event {
EmitEvent(i32, String, String),
Expand All @@ -39,26 +40,14 @@ struct CefPlugin {

impl CefPlugin {
fn new() -> Self {
// открывает UDP сокет на 7779 порту для cef

let ip: IpAddr = std::fs::read_to_string("./server.cfg")
.ok()
.and_then(|inner| {
inner
.lines()
.find(|line| line.starts_with("bind"))
.map(|borrow| borrow.to_string())
.and_then(|bind| {
bind.split(" ")
.skip(1)
.next()
.map(|borrow| borrow.to_string())
})
})
.and_then(|addr| addr.parse().ok())
.unwrap_or_else(|| "0.0.0.0".parse().unwrap());

let server = Server::new(SocketAddr::from((ip, 7779)));
let ip: IpAddr =
crate::utils::parse_config_field("bind").unwrap_or_else(|| "0.0.0.0".parse().unwrap());

let port = crate::utils::parse_config_field("port").unwrap_or_else(|| 7777);
let addr = SocketAddr::from((ip, port + PORT_OFFSET));
let server = Server::new(addr);

info!("Bind CEF server on {:?}", addr);

let event_rx = {
let s = server.lock().unwrap();
Expand Down
20 changes: 19 additions & 1 deletion server/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::error;
use std::collections::VecDeque;
use std::{collections::VecDeque, str::FromStr};

pub struct IdPool {
pool: VecDeque<u32>,
Expand Down Expand Up @@ -32,3 +32,21 @@ pub fn handle_result<T, E: std::fmt::Debug>(result: Result<T, E>) -> Option<T> {

result.ok()
}

pub fn parse_config_field<F: FromStr>(field: &str) -> Option<F> {
std::fs::read_to_string("./server.cfg")
.ok()
.and_then(|inner| {
inner
.lines()
.find(|line| line.starts_with(field))
.map(|borrow| borrow.to_string())
.and_then(|bind| {
bind.split(" ")
.skip(1)
.next()
.map(|borrow| borrow.to_string())
})
})
.and_then(|addr| addr.parse().ok())
}

0 comments on commit c3f43a8

Please sign in to comment.