rust
mod protocol_crate;
use protocol_crate::protocol::IspPacketsInfo;
use std::net::UdpSocket;
use std::thread::sleep;
use std::time::Duration;
use chrono;
fn main() {
let tmp = IspPacketsInfo{
start:0x7E,
ap_layer:0x3E,
vp_layer:0x4E
};
let buffer = [tmp.start,tmp.ap_layer,tmp.vp_layer];
for a in buffer {
print!("{:02x} ",a);
}
let socket = UdpSocket::bind("0.0.0.0:9090").expect("Can't bind ip address or port");
println!();
let mut buffer = [0u8;1500];
let mut recv_size:usize;
let mut src;
let mut slice:&[u8];
let mut ascii_str:String;
let mut snd_buffer:String;
loop {
sleep(Duration::from_secs(1));
(recv_size,src) = socket.recv_from(&mut buffer).expect("recv from buffer error");
slice = &buffer[0 .. recv_size];
ascii_str = String::from_utf8_lossy(slice).parse().unwrap();
println!("[ {}] Recv:{:?} Size:{} RecvBuffer:{:?}",chrono::Local::now().format
("%Y-%m-%d %H:%M:%S"),
src,recv_size,
ascii_str);
snd_buffer = "Rust:".to_owned() + &*ascii_str;
socket.send_to(snd_buffer.as_bytes(), src).expect("send buffer error");
snd_buffer.clear();
buffer.iter_mut().for_each(|x| *x=0);
};
}
升级版本
rust
mod protocol_crate;
use protocol_crate::protocol::IspPacketsInfo;
use std::net::UdpSocket;
use std::process::exit;
use std::thread::sleep;
use std::time::Duration;
use chrono;
fn main() {
let tmp = IspPacketsInfo{
start:0x7E,
ap_layer:0x3E,
vp_layer:0x4E
};
let buffer = [tmp.start,tmp.ap_layer,tmp.vp_layer];
for a in buffer {
print!("{:02x} ",a);
}
let socket = UdpSocket::bind("0.0.0.0:9090");
let _safe_socket:UdpSocket;
if let Ok(_safe_socket) = socket {
let socket = &_safe_socket;
println!();
let mut buffer = [0u8; 1500];
let mut snd_buffer: String = Default::default();
loop {
sleep(Duration::from_secs(1));
if let Err(err) = socket.recv_from(&mut buffer).map(|(recv_size,src)| {
if let Ok(ascii_str) = String::from_utf8_lossy(&buffer[0..recv_size])
.parse::<String>() {
println!("[ {}] Recv:{:?} Size:{} RecvBuffer:{:?}", chrono::Local::now().format
("%Y-%m-%d %H:%M:%S"),
src, recv_size,
ascii_str);
snd_buffer = "Rust:".to_owned() + &*ascii_str;
if let Err(error) = socket.send_to(snd_buffer.as_bytes(), src)
{
print!("error:{}",error);
}else {
snd_buffer.clear();
}}}){
print!("RecvError:{}",err);
}
};
}else {
if let Some(error) = socket.err() {
print!("{}",error);
exit(1);
}
}
}
Rust 安全来自 Result 和 Some
就是写的时候会累一些, 不写原型和特性直接用没啥问题
不适用内存对齐的方式解码也很累,为了安全其实也能忍
减少unsafe 的作用域的使用
rust
use rusqlite::{Connection, Result, params, Error};
use std::collections::HashMap;
use std::fmt;
#[derive(Debug)]
pub struct Table{
pub class:String,
pub name:String,
pub tbl_name:String,
pub root_page:i32,
pub sql:String
}
#[derive(Debug)]
pub struct RepeaterDevTable{
id:u32,
name:String,
}
#[derive(Debug)]
pub struct Sqlite<'a>{
db_path :&'a str,
tb_name :&'a str,
connection: Connection,
tb_list :Vec<Table>,
hash_map: HashMap<u32,RepeaterDevTable>
}
impl<'a> Sqlite<'a> {
pub fn new(db_name:&'a str, table_name:&'a str) -> Result<Box<Self>> {
match Connection::open(db_name) {
Ok(con) => {
Ok(Box::new(Sqlite{db_path:db_name,tb_name:table_name,connection:con,
tb_list:Default::default(),hash_map:HashMap::new()}))
},
Err(err) => {
Err(err)
}
} }
pub fn setup(&mut self) -> Result<()> {
if let Err(err) = self.connection.prepare("SELECT * FROM sqlite_master").map(|mut stmt|{
let query_table = stmt.query_map([],|row|{
Ok(Table{
class: row.get(0).unwrap(),
name: row.get(1).unwrap(),
tbl_name: row.get(2).unwrap(),
root_page: row.get(3).unwrap(),
sql:row.get(4).unwrap()
})
});
if let Ok(table_iter) = query_table {
for it in table_iter {
if let Ok(_it) = it {
self.tb_list.push(_it);
}
}
}
if let Err(err) = self.connection.prepare(format!("SELECT * FROM {}",self
.tb_name).as_str()).map(|mut stmt|{
let _query_table = stmt.query_map([],|row|{
Ok(RepeaterDevTable{
id: row.get(0).unwrap(),
name: row.get(1).unwrap(),
})
});
if let Ok(table_iter) = _query_table {
for it in table_iter {
if let Ok(_it) = it {
self.hash_map.insert(_it.id,_it);
}
}
}
}) {
}
}){
return Err(err);
}
Ok(())
}
pub fn list_table(&self) -> &Vec<Table>{
&self.tb_list
}
pub fn query(&'a self, index:u32) -> Option<&'a RepeaterDevTable> {
match self.hash_map.get(&index) {
None => {
None
}
Some(v) => {
Option::from(v)
}
}
}
}
简易开发还是okay的,生命周期加所有权,还是得多留意
这些足够开发一个可玩性高的GUI 服务的后端的上位机了,告一段落,越复杂的特性,后期带来的回报越大
Rust 版本的OMC 程序 有搞头的,Python 版本和 C++ 版本 都不太如意,Rust 目前还可以
JetBrains 编码不太好用,但是提示修改,一级棒 ,我都熟练CTR-P CTR-N 来代替TAB了,还是没neovim 好用,neovim rust 的提示没有JetBrains 全面