src: create Peer before sending packet

This commit is contained in:
Ujjwal Sharma 2021-04-02 08:08:07 +05:30
parent 5ad54fd523
commit 500198e593
Signed by: ryzokuken
GPG Key ID: 460B292812C67D9F
1 changed files with 25 additions and 1 deletions

View File

@ -23,12 +23,27 @@ struct Address {
handshake: Handshake,
}
impl Address {
fn new(protocol: Protocol, host: IpAddr, port: u16, handshake: Handshake) -> Self {
Self {
protocol,
host,
port,
handshake,
}
}
}
pub struct Peer {
addresses: Vec<Address>,
key: PublicKey,
}
impl Peer {
fn new(addresses: Vec<Address>, key: PublicKey) -> Self {
Self { addresses, key }
}
// TODO: do this properly
pub fn to_discovery_packet(&self) -> String {
self.addresses
@ -120,7 +135,16 @@ pub async fn peer_discovery_recv() {
pub async fn peer_discovery_send(pubkey: Arc<String>) {
let socket = UdpSocket::bind(":::0").await.unwrap();
let msg = format!("net:1.2.3.4:8023~shs:{}", &pubkey);
let msg = Peer::new(
Vec::from([Address::new(
Protocol::Net,
"1.2.3.4".parse().unwrap(),
8023,
Handshake::Shs,
)]),
PublicKey::from_base64(pubkey.as_str()),
)
.to_discovery_packet();
let buf = msg.as_bytes();
socket.set_broadcast(true).unwrap();