intuicio_frontend_simpleton/library/
net.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use super::bytes::Bytes;
use crate::{Boolean, Integer, Reference, Text};
use intuicio_core::{registry::Registry, IntuicioStruct};
use intuicio_derive::{intuicio_method, intuicio_methods, IntuicioStruct};
use std::{
    io::{Read, Write},
    net::{TcpListener, TcpStream},
};

#[derive(IntuicioStruct, Default)]
#[intuicio(name = "Server", module_name = "net_server")]
pub struct Server {
    #[intuicio(ignore)]
    listener: Option<TcpListener>,
}

#[intuicio_methods(module_name = "net_server")]
impl Server {
    #[allow(clippy::new_ret_no_self)]
    #[intuicio_method(use_registry)]
    pub fn new(registry: &Registry, address: Reference) -> Reference {
        let address = address.read::<Text>().unwrap();
        Reference::new(
            Server {
                listener: Some(TcpListener::bind(address.as_str()).unwrap()),
            },
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn set_nonblocking(registry: &Registry, server: Reference, mode: Reference) -> Reference {
        let server = server.read::<Server>().unwrap();
        let mode = *mode.read::<Boolean>().unwrap();
        Reference::new_boolean(
            server
                .listener
                .as_ref()
                .unwrap()
                .set_nonblocking(mode)
                .is_ok(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn accept(registry: &Registry, server: Reference) -> Reference {
        let server = server.read::<Server>().unwrap();
        if let Ok((stream, _)) = server.listener.as_ref().unwrap().accept() {
            Reference::new(
                Channel {
                    stream: Some(stream),
                },
                registry,
            )
        } else {
            Reference::null()
        }
    }
}

#[derive(IntuicioStruct, Default)]
#[intuicio(name = "Channel", module_name = "net_channel")]
pub struct Channel {
    #[intuicio(ignore)]
    stream: Option<TcpStream>,
}

#[intuicio_methods(module_name = "net_channel")]
impl Channel {
    #[intuicio_method(use_registry)]
    pub fn connect(registry: &Registry, address: Reference) -> Reference {
        let address = address.read::<Text>().unwrap();
        let stream = TcpStream::connect(address.as_str()).unwrap();
        Reference::new(
            Channel {
                stream: Some(stream),
            },
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn set_nonblocking(registry: &Registry, channel: Reference, mode: Reference) -> Reference {
        let channel = channel.read::<Channel>().unwrap();
        let mode = *mode.read::<Boolean>().unwrap();
        Reference::new_boolean(
            channel
                .stream
                .as_ref()
                .unwrap()
                .set_nonblocking(mode)
                .is_ok(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn set_no_delay(registry: &Registry, channel: Reference, mode: Reference) -> Reference {
        let channel = channel.read::<Channel>().unwrap();
        let mode = *mode.read::<Boolean>().unwrap();
        Reference::new_boolean(
            channel.stream.as_ref().unwrap().set_nodelay(mode).is_ok(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn read(registry: &Registry, mut channel: Reference, size: Reference) -> Reference {
        let mut channel = channel.write::<Channel>().unwrap();
        let size = *size.read::<Integer>().unwrap() as usize;
        let mut result = vec![0; size];
        if channel.stream.as_mut().unwrap().read(&mut result).is_ok() {
            Reference::new(Bytes::new_raw(result), registry)
        } else {
            Reference::null()
        }
    }

    #[intuicio_method(use_registry)]
    pub fn write(registry: &Registry, mut channel: Reference, buffer: Reference) -> Reference {
        let mut channel = channel.write::<Channel>().unwrap();
        let buffer = buffer.read::<Bytes>().unwrap();
        Reference::new_boolean(
            channel
                .stream
                .as_mut()
                .unwrap()
                .write(buffer.get_ref())
                .is_ok(),
            registry,
        )
    }
}

pub fn install(registry: &mut Registry) {
    registry.add_type(Server::define_struct(registry));
    registry.add_function(Server::new__define_function(registry));
    registry.add_function(Server::set_nonblocking__define_function(registry));
    registry.add_function(Server::accept__define_function(registry));
    registry.add_type(Channel::define_struct(registry));
    registry.add_function(Channel::connect__define_function(registry));
    registry.add_function(Channel::set_nonblocking__define_function(registry));
    registry.add_function(Channel::set_no_delay__define_function(registry));
    registry.add_function(Channel::read__define_function(registry));
    registry.add_function(Channel::write__define_function(registry));
}