|
| 1 | +//! Provides pure Rust equivalent functions for some functions in `networking.c`. |
| 2 | +
|
| 3 | +use crate::{fatal, net::*}; |
| 4 | + |
| 5 | +use std::sync::RwLock; |
| 6 | + |
| 7 | +static TARGET: RwLock<Option<SendTarget>> = RwLock::new(None); |
| 8 | +static SOURCE: RwLock<Option<RecvSource>> = RwLock::new(None); |
| 9 | + |
| 10 | +/// Rust equivalent for `connect_to_srv` function in C. Uses Rust-native types as input and output. |
| 11 | +pub fn connect_to_srv( |
| 12 | + addr: &'static str, |
| 13 | + port: Option<u16>, |
| 14 | + cc_desc: Option<&'static str>, |
| 15 | + pwd: Option<&'static str>, |
| 16 | +) { |
| 17 | + let mut send_target = TARGET.write().expect("Unable to use TARGET"); |
| 18 | + *send_target = Some(SendTarget::new(SendTargetConfig { |
| 19 | + target_addr: addr, |
| 20 | + port, |
| 21 | + password: pwd, |
| 22 | + description: cc_desc, |
| 23 | + })); |
| 24 | +} |
| 25 | + |
| 26 | +/// Rust equivalent for `net_send_header` function in C. Uses Rust-native types as input and output. |
| 27 | +pub fn net_send_header(data: &[u8]) { |
| 28 | + let mut send_target = TARGET.write().unwrap(); |
| 29 | + send_target.as_mut().unwrap().send_header(data); |
| 30 | +} |
| 31 | + |
| 32 | +/// Rust equivalent for `net_send_cc` function in C. Uses Rust-native types as input and output. |
| 33 | +pub fn net_send_cc(data: &[u8]) -> bool { |
| 34 | + let mut send_target = TARGET.write().unwrap(); |
| 35 | + send_target.as_mut().unwrap().send_cc(data) |
| 36 | +} |
| 37 | + |
| 38 | +/// Rust equivalent for `net_check_conn` function in C. Uses Rust-native types as input and output. |
| 39 | +pub fn net_check_conn() { |
| 40 | + let mut send_target = TARGET.write().unwrap(); |
| 41 | + send_target.as_mut().unwrap().check_connection(); |
| 42 | +} |
| 43 | + |
| 44 | +/// Rust equivalent for `net_send_epg` function in C. Uses Rust-native types as input and output. |
| 45 | +pub fn net_send_epg( |
| 46 | + start: &str, |
| 47 | + stop: &str, |
| 48 | + title: Option<&str>, |
| 49 | + desc: Option<&str>, |
| 50 | + lang: Option<&str>, |
| 51 | + category: Option<&str>, |
| 52 | +) { |
| 53 | + let mut send_target = TARGET.write().unwrap(); |
| 54 | + send_target |
| 55 | + .as_mut() |
| 56 | + .unwrap() |
| 57 | + .send_epg_data(start, stop, title, desc, lang, category); |
| 58 | +} |
| 59 | + |
| 60 | +/// Rust equivalent for `net_tcp_read` function in C. Uses Rust-native types as input and output. |
| 61 | +pub fn net_tcp_read(buffer: &mut [u8]) -> Option<usize> { |
| 62 | + let mut recv_source = SOURCE.write().unwrap(); |
| 63 | + if let Ok(b) = recv_source.as_mut().unwrap().recv_header_or_cc() { |
| 64 | + if let Some(block) = b { |
| 65 | + buffer[..block.data().len()].copy_from_slice(block.data()); |
| 66 | + Some(block.data().len()) |
| 67 | + } else { |
| 68 | + Some(0) |
| 69 | + } |
| 70 | + } else { |
| 71 | + None |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Rust equivalent for `net_udp_read` function in C. Uses Rust-native types as input and output. |
| 76 | +pub fn net_udp_read(buffer: &mut [u8]) -> Option<usize> { |
| 77 | + let mut recv_source = SOURCE.write().unwrap(); |
| 78 | + recv_source.as_mut().unwrap().recv(buffer).ok() |
| 79 | +} |
| 80 | + |
| 81 | +/// Rust equivalent for `start_tcp_srv` function in C. Uses Rust-native types as input and output. |
| 82 | +pub fn start_tcp_srv(port: Option<u16>, pwd: Option<&'static str>) { |
| 83 | + let mut recv_source = SOURCE.write().unwrap(); |
| 84 | + *recv_source = Some(RecvSource::new(RecvSourceConfig::Tcp { |
| 85 | + port, |
| 86 | + password: pwd, |
| 87 | + })); |
| 88 | +} |
| 89 | + |
| 90 | +/// Rust equivalent for `start_udp_srv` function in C. Uses Rust-native types as input and output. |
| 91 | +pub fn start_udp_srv(src: Option<&'static str>, addr: Option<&'static str>, port: u16) { |
| 92 | + let mut recv_source = SOURCE.write().unwrap(); |
| 93 | + *recv_source = Some(RecvSource::new(RecvSourceConfig::Udp { |
| 94 | + source: src, |
| 95 | + address: addr, |
| 96 | + port, |
| 97 | + })); |
| 98 | +} |
0 commit comments