This commit is contained in:
Daniel Mowitz 2021-08-02 23:26:27 +02:00
parent cb50318a5f
commit 7ae296bf0e
118 changed files with 136 additions and 25 deletions

BIN
lo Normal file

Binary file not shown.

BIN
new.log Normal file

Binary file not shown.

7
rust/Cargo.lock generated
View file

@ -30,12 +30,6 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "protobuf"
version = "2.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12b869b619141004a542c9e585b40c1432e7b0d67a826f68cb5fa4c1ee9aff56"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.3" version = "1.0.3"
@ -89,7 +83,6 @@ name = "tf2p-network"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"flume", "flume",
"protobuf",
"sm-ext", "sm-ext",
] ]

View file

@ -10,4 +10,3 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
sm-ext = "0.3.0" sm-ext = "0.3.0"
flume = "0.5.1" flume = "0.5.1"
protobuf = "2.12.0"

3
rust/build_and_run.sh Normal file
View file

@ -0,0 +1,3 @@
cargo build --all --all-targets --target=i686-unknown-linux-gnu &&
cp target/i686-unknown-linux-gnu/debug/libtf2p_network.so /mnt/disk3/TF2DS/tf/addons/sourcemod/extensions/tf2p-network.ext.so &&
/mnt/disk3/TF2DS/srcds_run +map ctf_2fort

View file

@ -1,4 +1,4 @@
use sm_ext::{cell_t, native, register_natives, IExtension, IExtensionInterface, IShareSys, SMExtension, TryIntoPlugin, HandleError, IHandleSys, HandleType, SMInterfaceApi, IPluginContext, forwards, IForwardManager, ExecType}; use sm_ext::*;
use std::thread; use std::thread;
use std::net::{SocketAddr, UdpSocket}; use std::net::{SocketAddr, UdpSocket};
use std::cell::RefCell; use std::cell::RefCell;
@ -18,6 +18,12 @@ struct Tf2pNetworkingForwards {
on_con_req: fn(ip: &CStr) -> i32, on_con_req: fn(ip: &CStr) -> i32,
} }
fn forward_wrapper(ip: &CStr) -> Result<i32, sm_ext::SPError> {
let result = Tf2pNetworkingForwards::on_con_req(|fwd| fwd.execute(ip))?;
Ok(result)
}
/// The network plugin entrypoint. /// The network plugin entrypoint.
#[native] #[native]
fn start_manager(_ctx: &IPluginContext, ip: &CStr, port1: i32) -> NMSender { fn start_manager(_ctx: &IPluginContext, ip: &CStr, port1: i32) -> NMSender {
@ -52,7 +58,7 @@ fn start_manager(_ctx: &IPluginContext, ip: &CStr, port1: i32) -> NMSender {
// Spawn manager thread // Spawn manager thread
thread::spawn(move || { thread::spawn(move || {
netman::manager(rx_s2m, tx_m2s, serv).unwrap(); netman::manager(rx_s2m, tx_m2s, forward_wrapper, serv).unwrap();
}); });
NMSender::new(tx_c) NMSender::new(tx_c)

View file

@ -4,10 +4,13 @@ use std::collections::VecDeque;
use std::ffi::CStr; use std::ffi::CStr;
// use crate::Tf2pNetworkingForwards;
use flume; use flume;
pub enum NMMessage{ pub enum NMMessage{
Connect(u8, SocketAddr), Connect(u8, SocketAddr),
Ping(SocketAddr),
UDPMessage(Vec<u8>, SocketAddr), UDPMessage(Vec<u8>, SocketAddr),
Kill Kill
} }
@ -32,7 +35,7 @@ fn check_if_in_peer_ips (cl_ip: SocketAddr, peer_ips: &[Option<SocketAddr>]) ->
/// The network manager function. /// The network manager function.
/// It handles the incoming requests and sends the corresponding answers /// It handles the incoming requests and sends the corresponding answers
pub fn manager<'a> (input: flume::Receiver<NMMessage>, output: flume::Sender<(Vec<u8>, SocketAddr)>, serv_addr: SocketAddr) -> Result<(), Box<dyn Error>> { pub fn manager<'a> (input: flume::Receiver<NMMessage>, output: flume::Sender<(Vec<u8>, SocketAddr)>, forward: fn(&CStr) -> Result<i32, sm_ext::SPError>, serv_addr: SocketAddr) -> Result<(), Box<dyn Error>> {
let mut cl_ip: Option<SocketAddr> = None; let mut cl_ip: Option<SocketAddr> = None;
let mut addr_queue = VecDeque::new(); let mut addr_queue = VecDeque::new();
@ -102,6 +105,8 @@ pub fn manager<'a> (input: flume::Receiver<NMMessage>, output: flume::Sender<(Ve
output.send((player_data[0].clone().unwrap(), serv_addr)).unwrap(); output.send((player_data[0].clone().unwrap(), serv_addr)).unwrap();
println!("Got player Data pt 1."); println!("Got player Data pt 1.");
// This loop is activated when a player starts sending theur data and
// breaks the outer loop when the transmission is complete.
'even_more_inner: loop { 'even_more_inner: loop {
match input.recv() { match input.recv() {
Ok(m) => { Ok(m) => {
@ -117,9 +122,9 @@ pub fn manager<'a> (input: flume::Receiver<NMMessage>, output: flume::Sender<(Ve
break 'even_more_inner; break 'even_more_inner;
} }
} }
}, },
NMMessage::Connect(i, addr) => (),
NMMessage::Kill => break 'main, NMMessage::Kill => break 'main,
_ => {},
} }
}, },
Err(e) => panic!(e), Err(e) => panic!(e),
@ -132,14 +137,15 @@ pub fn manager<'a> (input: flume::Receiver<NMMessage>, output: flume::Sender<(Ve
} }
} }
}, },
NMMessage::Connect(i, addr) => (),
NMMessage::Kill => break 'main, NMMessage::Kill => break 'main,
_ => {},
} }
}, },
Err(e) => panic!(e), Err(e) => panic!(e),
} }
} }
// Exchange game data with peers and send updates to client.
'inner2: loop { 'inner2: loop {
match input.recv() { match input.recv() {
@ -149,28 +155,39 @@ pub fn manager<'a> (input: flume::Receiver<NMMessage>, output: flume::Sender<(Ve
if src == serv_addr { // from server? if src == serv_addr { // from server?
output.send((buf, cl_ip.unwrap())); // send to client output.send((buf, cl_ip.unwrap())); // send to client
} else if check_if_in_peer_ips(src, &conns) { // from peer? } else if check_if_in_peer_ips(src, &conns) { // from peer?
// handle request // handle request (quit, peers, gameinfo)
// /send to cliet port (callback) // send to cliet port (forward)
} else { // else } else { // else
if buf == "tf2p?".as_bytes() {// handshake if buf == "TF2P?".as_bytes() {// handshake
output.send((Vec::from("Hey Ho!".as_bytes()), src)); output.send((Vec::from("Hey Ho!".as_bytes()), src));
} }
else if buf == "Connect?".as_bytes() { else if buf == "Connect?".as_bytes() {
match super::Tf2pNetworkingForwards::on_con_req(|fwd| fwd.execute(CStr::from_bytes_with_nul(src.ip()?.to_string()?[..])?))? { // Callback? match forward(CStr::from_bytes_with_nul(src.ip().to_string()[..].as_bytes())?) {
Ok(1) => { Ok(i) => {
// Add to List if i < 3 {
// Send Ack and Ports conns[i as usize] = Some(src);
// 'Ack ServPort CliPort' -> src
output.send((Vec::from(format!("Ack {:05}", serv_addr.port()).as_bytes()), src));
} else {
output.send((Vec::from("No".as_bytes()), src));
}
}, },
_ => (), _ => (),
} }
} }
else if buf == "".as_bytes() { // Ack, SPort
// add to list TODO: Check internal state of place and validity
output.send((Vec::from("QInfo?".as_bytes()), src));
}
} }
}, },
NMMessage::Connect(i, addr) => { NMMessage::Connect(i, addr) => {
// ask for handshawe // TODO: Create internal state to check place and validity
// do it output.send((Vec::from("Connect?".as_bytes()), addr));
// add to list
}, },
NMMessage::Ping(addr) => {
output.send((Vec::from("TF2P?".as_bytes()), addr));
}
NMMessage::Kill => break 'main, NMMessage::Kill => break 'main,
} }
} }

View file

@ -1,7 +1,7 @@
use std::net::{UdpSocket, SocketAddr}; use std::net::{UdpSocket, SocketAddr};
use std::error::Error; use std::error::Error;
use crate::NMMessage; use crate::netman::NMMessage;
use flume; use flume;

6
tests/Cargo.lock generated Normal file
View file

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "tests"
version = "0.1.0"

9
tests/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "tests"
version = "0.1.0"
authors = ["TheLie0 <wf7za0XoHyYXVEkOeNu8>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

41
tests/src/main.rs Normal file
View file

@ -0,0 +1,41 @@
use std::env;
use std::net::{UdpSocket, SocketAddr};
fn main() {
println!("Hello, world!");
let args: Vec<String> = env::args().collect();
let mut server_adress = None;
for i in 0..args.len()-1 {
if args[i] == "-s" {
server_adress = Some(args[i + 1].parse::<SocketAddr>()
.expect("Unable to parse output socket address"));
}
}
let server_message = [0xff, 0xff, 0xff, 0xff, 0x41, 0x33, 0x49, 0x4f, 0x5a,
0x7c, 0x71, 0x34, 0x2d, 0xf9, 0x23, 0x30, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x08, 0xc0, 0x56, 0x25, 0x42, 0x38, 0x40, 0x01, 0x01, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x00];
let server_socket = UdpSocket::bind("127.0.0.1:27123")
.expect("Could not bind to 127.0.0.1:27123.");
// Connect to client port
match server_adress {
Some(a) => server_socket.connect(a).expect("Couldn't connect to server."),
None => panic!("No Server adress provided!")
}
server_socket.send(& server_message).expect("Send failed.");
// print all traffic from cl
loop{
let mut output = [0; 128];
server_socket.recv(&mut output).unwrap();
}
}

View file

@ -0,0 +1 @@
{"rustc_fingerprint":1683364760131821644,"outputs":{"1164083562126845933":["rustc 1.44.0 (49cae5576 2020-06-01)\nbinary: rustc\ncommit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4\ncommit-date: 2020-06-01\nhost: x86_64-unknown-linux-gnu\nrelease: 1.44.0\nLLVM version: 9.0\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/dannieboy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n",""]},"successes":{}}

View file

View file

@ -0,0 +1 @@
657008ade131bb46

View file

@ -0,0 +1 @@
{"rustc":5807094999684781751,"features":"[]","target":8443017735982037405,"profile":14891217944882224483,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tests-c066b971b0bbd15a/dep-bin-tests-c066b971b0bbd15a"}}],"rustflags":[],"metadata":7884812644471753209}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
aee87f802deaab67

View file

@ -0,0 +1 @@
{"rustc":5807094999684781751,"features":"[]","target":8443017735982037405,"profile":14996655781355331481,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tests-e7efc60f064cff81/dep-bin-tests-e7efc60f064cff81"}}],"rustflags":[],"metadata":7884812644471753209}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1,5 @@
/mnt/disk1/projects/TF2P/tests/target/debug/deps/tests-c066b971b0bbd15a.rmeta: src/main.rs
/mnt/disk1/projects/TF2P/tests/target/debug/deps/tests-c066b971b0bbd15a.d: src/main.rs
src/main.rs:

Binary file not shown.

View file

@ -0,0 +1,5 @@
/mnt/disk1/projects/TF2P/tests/target/debug/deps/tests-e7efc60f064cff81: src/main.rs
/mnt/disk1/projects/TF2P/tests/target/debug/deps/tests-e7efc60f064cff81.d: src/main.rs
src/main.rs:

BIN
tests/target/debug/tests Normal file

Binary file not shown.

View file

@ -0,0 +1 @@
/mnt/disk1/projects/TF2P/tests/target/debug/tests: /mnt/disk1/projects/TF2P/tests/src/main.rs

View file

@ -0,0 +1 @@
{"rustc_fingerprint":4567211532567258067,"outputs":{"1138116330425514636":["___\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/dannieboy/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/rls\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"mmx\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"cas\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"unknown\"\nunix\n",""],"1164083562126845933":["rustc 1.39.0-nightly (c6e9c76c5 2019-09-04)\nbinary: rustc\ncommit-hash: c6e9c76c59e3c10acd63ca9ec157a8894ea1a068\ncommit-date: 2019-09-04\nhost: x86_64-unknown-linux-gnu\nrelease: 1.39.0-nightly\nLLVM version: 9.0\n",""],"2196823701345282402":["___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/dannieboy/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/rls\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"mmx\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"cas\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"unknown\"\nunix\n",""]},"successes":{}}

View file

View file

@ -0,0 +1 @@
ec4873444cbdaeef

View file

@ -0,0 +1 @@
{"rustc":14587545897571530554,"features":"[]","target":8443017735982037405,"profile":14891217944882224483,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tests-2d2ee19b1caaa0fc/dep-bin-tests-2d2ee19b1caaa0fc"}}],"rustflags":[],"metadata":7884812644471753209}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

Some files were not shown because too many files have changed in this diff Show more