72 lines
1.8 KiB
Rust
72 lines
1.8 KiB
Rust
#![allow(unused)]
|
||
use std::default::Default;
|
||
use std::sync::atomic::{AtomicI64, AtomicU32, AtomicU8};
|
||
use std::sync::Mutex;
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
|
||
#[derive(Debug)]
|
||
pub struct Peer {
|
||
pub id: String,
|
||
// 对端阶段的udp信息,包括ipv4地址和子网掩码位数
|
||
pub dev_addr: IpSubnet,
|
||
// 对端对外开放的ip和端口信息
|
||
pub sock: Mutex<SdlanSock>,
|
||
pub pub_key: Mutex<Vec<u8>>,
|
||
pub timeout: isize,
|
||
|
||
// 最近一次遇见
|
||
pub last_seen: AtomicI64,
|
||
// 最近一次p2p消息
|
||
pub last_p2p: AtomicI64,
|
||
// 最近一次发送query
|
||
pub last_send_query: AtomicI64,
|
||
|
||
// 该节点锁属的网络
|
||
pub network_id: Mutex<String>,
|
||
}
|
||
|
||
impl Peer {
|
||
pub fn new(id: &str) -> Self {
|
||
Self {
|
||
id: id.to_string(),
|
||
dev_addr: IpSubnet {
|
||
net_addr: AtomicU32::new(0),
|
||
net_bit_len: AtomicU8::new(0),
|
||
},
|
||
sock: Mutex::new(SdlanSock {
|
||
family: 0,
|
||
port: 0,
|
||
has_v6: false,
|
||
v6_port: 0,
|
||
v4: [0; 4],
|
||
v6: [0; 16],
|
||
}),
|
||
pub_key: Mutex::new(vec![]),
|
||
timeout: 0,
|
||
last_seen: AtomicI64::new(0),
|
||
last_p2p: AtomicI64::new(0),
|
||
last_send_query: AtomicI64::new(0),
|
||
network_id: Mutex::new(String::new()),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// IpSubnet, 对端ipv4信息
|
||
#[derive(Debug, Serialize, Deserialize)]
|
||
pub struct IpSubnet {
|
||
pub net_addr: AtomicU32,
|
||
pub net_bit_len: AtomicU8,
|
||
}
|
||
|
||
/// SdlanSock: 对端对外的ip信息,包括ipv4和ipv6
|
||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||
pub struct SdlanSock {
|
||
pub family: u8,
|
||
pub port: u32,
|
||
pub has_v6: bool,
|
||
pub v6_port: u32,
|
||
pub v4: [u8; 4],
|
||
pub v6: [u8; 16],
|
||
}
|