aboutsummaryrefslogtreecommitdiff
path: root/tests/perf-server/src/main.rs
blob: f6e7efc595927d7a196cdb3fa70140998f384949 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::thread::spawn;
use std::time::Duration;

use log::info;

fn main() {
    pretty_env_logger::init();
    spawn(|| rx_listen());
    spawn(|| rxtx_listen());
    tx_listen();
}

fn tx_listen() {
    info!("tx: listening on 0.0.0.0:4321");
    let listener = TcpListener::bind("0.0.0.0:4321").unwrap();
    loop {
        let (socket, addr) = listener.accept().unwrap();
        info!("tx: received connection from: {}", addr);
        spawn(|| tx_conn(socket));
    }
}

fn tx_conn(mut socket: TcpStream) {
    socket.set_read_timeout(Some(Duration::from_secs(30))).unwrap();
    socket.set_write_timeout(Some(Duration::from_secs(30))).unwrap();

    let buf = [0; 1024];
    loop {
        if let Err(e) = socket.write_all(&buf) {
            info!("tx: failed to write to socket; err = {:?}", e);
            return;
        }
    }
}

fn rx_listen() {
    info!("rx: listening on 0.0.0.0:4322");
    let listener = TcpListener::bind("0.0.0.0:4322").unwrap();
    loop {
        let (socket, addr) = listener.accept().unwrap();
        info!("rx: received connection from: {}", addr);
        spawn(|| rx_conn(socket));
    }
}

fn rx_conn(mut socket: TcpStream) {
    socket.set_read_timeout(Some(Duration::from_secs(30))).unwrap();
    socket.set_write_timeout(Some(Duration::from_secs(30))).unwrap();

    let mut buf = [0; 1024];
    loop {
        if let Err(e) = socket.read_exact(&mut buf) {
            info!("rx: failed to read from socket; err = {:?}", e);
            return;
        }
    }
}

fn rxtx_listen() {
    info!("rxtx: listening on 0.0.0.0:4323");
    let listener = TcpListener::bind("0.0.0.0:4323").unwrap();
    loop {
        let (socket, addr) = listener.accept().unwrap();
        info!("rxtx: received connection from: {}", addr);
        spawn(|| rxtx_conn(socket));
    }
}

fn rxtx_conn(mut socket: TcpStream) {
    socket.set_read_timeout(Some(Duration::from_secs(30))).unwrap();
    socket.set_write_timeout(Some(Duration::from_secs(30))).unwrap();

    let mut buf = [0; 1024];
    loop {
        match socket.read(&mut buf) {
            Ok(n) => {
                if let Err(e) = socket.write_all(&buf[..n]) {
                    info!("rxtx: failed to write to socket; err = {:?}", e);
                    return;
                }
            }
            Err(e) => {
                info!("rxtx: failed to read from socket; err = {:?}", e);
                return;
            }
        }
    }
}