!RxaQWWMYJTsVdHEKmL:matrix.org

smoltcp

165 Members
smoltcp discussion and help - https://github.com/smoltcp-rs/smoltcp13 Servers

Load older messages


SenderMessageTime
16 Feb 2024
@talizkahn:matrix.orgtalizkahn

this is the code where I create a TCP socket and attemt a basic data reverse service (copied from the server.rs example). It doesnt actually get past the iface.poll:

fn tcp_service(device: &mut MyDevice) {
    // Create interface
    let mut config = match device.capabilities().medium {
        Medium::Ethernet => {
            // 52:54:00:ca:d0:df
            Config::new(EthernetAddress([0x52, 0x54, 0x00, 0xca, 0xd0, 0xdf]).into())
        }
        _ => return,
    };

    config.random_seed = rand::random();

    let mut iface = Interface::new(config, device, Instant::now());
    iface.update_ip_addrs(|ip_addrs| {
        ip_addrs
            .push(IpCidr::new(IpAddress::v4(192, 168, 122, 129), 24))
            .unwrap();
    });
    iface
        .routes_mut()
        .add_default_ipv4_route(Ipv4Address::new(192, 168, 122, 1))
        .unwrap();

    let mut sockets = SocketSet::new(vec![]);
    let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
    let tcp1_tx_buffer = tcp::SocketBuffer::new(vec![0; 128]);
    let tcp1_socket = tcp::Socket::new(tcp1_rx_buffer, tcp1_tx_buffer);
    let tcp1_handle = sockets.add(tcp1_socket);

    let mut connected = false;
    let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
    if !socket.is_open() {
        info!("listening");
        socket.listen(8080).unwrap();
    }

    loop {
        let timestamp = Instant::now();
        debug!("polling...");
        iface.poll(timestamp, device, &mut sockets);
        debug!("polled...");

        let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
        if socket.is_active() && !connected {
            info!("connected");
        }
        connected = socket.is_active();

        if socket.may_recv() {
            let data = socket
                .recv(|buffer| {
                    let recvd_len = buffer.len();
                    let mut data = buffer.to_owned();
                    if !data.is_empty() {
                        data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
                        data.reverse();
                        data.extend(b"\n");
                    }
                    (recvd_len, data)
                })
                .unwrap();
            if socket.can_send() && !data.is_empty() {
                socket.send_slice(&data[..]).unwrap();
            }
        }
    }
}
20:02:35
@dirbaio:matrix.orgdirbaioseems your Device implementation is blocking on receive, instead of returning None20:04:10
@talizkahn:matrix.orgtalizkahnshould the Device::recieve() function actually poll the underlying subsystem for data or should that be done in the consume for the token?20:07:21
@dirbaio:matrix.orgdirbaioread the Device trait docs20:07:36
@talizkahn:matrix.orgtalizkahn

"* The receive and transmit functions only construct such tokens, the real sending/receiving operation are performed when the tokens are consumed.*"

This indicates that the actual work should be done in the token consume, which is what im doing... under what circumstances would you return None from receive()?

20:10:05
@dirbaio:matrix.orgdirbaio receive must return None if there's no packet received right now, Some if there is. it must not block
and then RxToken::consume must consume that received packet immediately. it must not block either.
20:11:10
@dirbaio:matrix.orgdirbaio * receive must return None if there's no packet received right now, Some if there is. it must not block.
and then RxToken::consume must consume that received packet immediately. it must not block either.
20:11:19
@talizkahn:matrix.orgtalizkahnRight, so the receive will read the data into the token if it exists, and the consume will process it?20:12:06
@dirbaio:matrix.orgdirbaioit's up to you when you read the data20:12:30
@dirbaio:matrix.orgdirbaiobut you must not block20:12:32
@talizkahn:matrix.orgtalizkahnok cool. I think my problem is that the receive() wont block, but returns tokens no matter what. The consume wont block, but there may not be anything to do20:13:50
@talizkahn:matrix.orgtalizkahnI'll refactor a bit and see how I go20:14:01
@talizkahn:matrix.orgtalizkahnthanks for your help20:14:07
@talizkahn:matrix.orgtalizkahnits looking a lot healthier now :)20:28:39
@talizkahn:matrix.orgtalizkahn I see that tcp::Socket is Sync and Send, is it a common/done pattern to send ESTABLISHED sockets to another thread to process and add a new LISTENING socket to the SockerSet? 22:24:37
@dirbaio:matrix.orgdirbaioyou can't send it because it must be inside the SocketSet to get processed22:25:38
@dirbaio:matrix.orgdirbaioyou can share the SocketSet behind a mutex22:26:02
@dirbaio:matrix.orgdirbaiobut it's probably not worth it, it won't increase perf much22:26:54
@talizkahn:matrix.orgtalizkahn Just looking for a pattern to handle multiple connections 22:27:32
@dirbaio:matrix.orgdirbaioa big main loop polling everything22:27:49
@dirbaio:matrix.orgdirbaio if that's too annoying, there's async wrappers, for example embassy-net. where you can spawn one task per connection 22:28:25
@dirbaio:matrix.orgdirbaio * if that's too annoying, there's async wrappers, for example embassy-net. where you can spawn one async task per connection 22:28:31
@dirbaio:matrix.orgdirbaioall tasks still running in a single-thread executor22:28:34
@dirbaio:matrix.orgdirbaioso no mutex overheads22:28:37
@talizkahn:matrix.orgtalizkahn Cool, thanks for the pointers 22:42:06
18 Feb 2024
@embassy-bot:matrix.orgsmoltcp-bot

New PR: feat(congestion control): add CongestionControler tairt and example impl

11:15:20
19 Feb 2024
@oddstr13:matrix.orgOddstr13 changed their profile picture.23:59:04
24 Feb 2024
@lovesabugopeludo:matrix.org@lovesabugopeludo:matrix.org changed their display name from menino putinha de 14 anos to Love pedomoms (14).21:47:43
25 Feb 2024
@lovesabugopeludo:matrix.org@lovesabugopeludo:matrix.org left the room.17:52:22
@whitequark:matrix.orgCatherine banned @lovesabugopeludo:matrix.org@lovesabugopeludo:matrix.org.17:52:26

There are no newer messages yet.


Back to Room ListRoom Version: 6