1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::net::UdpSocket;
use tracing::{error, warn};

use eyre::Result;

pub(crate) trait SendComplete {
    fn send_complete(&self, x: &[u8]) -> Result<()>;
}

impl SendComplete for UdpSocket {
    fn send_complete(&self, x: &[u8]) -> Result<()> {
        match self.send(&x) {
            Ok(sz) => {
                if sz != x.len() {
                    eyre::bail!("incomplete send");
                }
            }
            Err(err) => match err.kind() {
                std::io::ErrorKind::WouldBlock => {
                    warn!("WouldBlock: dropping socket data");
                }
                std::io::ErrorKind::ConnectionRefused => {
                    warn!("ConnectionRefused: dropping socket data");
                }
                _ => {
                    error!("error sending socket data: {:?}", err);
                    return Err(err.into());
                }
            },
        }
        Ok(())
    }
}