broadcast_pdus/
broadcast_pdus.rs

1//     dis-rust - A rust implementation of the DIS simulation protocol.
2//     Copyright (C) 2022 Thomas Mann
3// 
4//     This software is dual-licensed. It is available under the conditions of
5//     the GNU Affero General Public License (see the LICENSE file included) 
6//     or under a commercial license (email contact@coffeebreakdevs.com for
7//     details).
8
9use std::{net::UdpSocket};
10
11use bytes::BytesMut;
12
13extern crate dis_rust;
14use dis_rust::{warfare::{fire_pdu::FirePDU, detonation_pdu::DetonationPDU}, entity_information::entity_state_pdu::EntityStatePDU, simulation_management::event_report_pdu::EventReportPDU, common::pdu::PDU};
15
16const SRC_PORT: u16 = 3000;
17
18fn main() {
19    let src_addr = format!("192.168.1.134:{}", SRC_PORT);
20    println!("binding to {} for sending", src_addr.as_str());
21    let socket = UdpSocket::bind(src_addr).expect("bind should succeed");
22
23    socket.set_broadcast(true).expect("set_broadcast to true should succeed");
24
25    // Send FirePDU
26    let data_to_send = FirePDU::default();
27    println!("broadcasting data: {:?}", data_to_send);
28    let mut buffer = BytesMut::new();
29    PDU::serialise(&data_to_send, &mut buffer);
30    socket
31        .send_to(&buffer, format!("255.255.255.255:{}", SRC_PORT))
32        .expect("couldn't send data");
33
34    // Send EventReportPDU
35    let data_to_send = EventReportPDU::default();
36    println!("broadcasting data: {:?}", data_to_send);
37    let mut buffer = BytesMut::new();
38    PDU::serialise(&data_to_send, &mut buffer);
39    socket
40        .send_to(&buffer, format!("255.255.255.255:{}", SRC_PORT))
41        .expect("couldn't send data");
42
43
44    // Send EntityStatePDU
45    let data_to_send = EntityStatePDU::default();
46    println!("broadcasting data: {:?}", data_to_send);
47    let mut buffer = BytesMut::new();
48    PDU::serialise(&data_to_send, &mut buffer);
49    socket
50        .send_to(&buffer, format!("255.255.255.255:{}", SRC_PORT))
51        .expect("couldn't send data");
52
53    // Send DetonationPDU
54    let data_to_send = DetonationPDU::default();
55    println!("broadcasting data: {:?}", data_to_send);
56    let mut buffer = BytesMut::new();
57    PDU::serialise(&data_to_send, &mut buffer);
58    socket
59        .send_to(&buffer, format!("255.255.255.255:{}", SRC_PORT))
60        .expect("couldn't send data");
61}