Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Async chat server example connection_writer_loop() might not be necessary #1068

Open
@fvacek

Description

@fvacek

Documentation

Async chat server example is usingconnection_writer_loop() task to ensure, that client messages will not be interleaved.https://book.async.rs/tutorial/sending_messages

What ifbroker_loop() will send client messages back toconnection_loop() instead of creatingconnection_writer_loop() and passingAcr<TcpStram> all around? This can IMO lead to cleaner design, what is always good in tutorial. AlsoAcr<TcpStram> is not necessary then, because TcpStream never escapesconnection_loop(). We can also uselet (socket_reader, mut socket_writer) = (&stream, &stream); pattern here.

We can then write something like this:

asyncfnconnection_loop(client_id:i32,broker:Sender<ClientEvent>,stream:TcpStream) ->Result<()>{let(socket_reader,mut socket_writer) =(&stream,&stream);let reader =BufReader::new(socket_reader);letmut lines = reader.lines();let name =match lines.next().await{None =>Err("peer disconnected immediately")?,Some(line) => line?,};    broker.send(Event::NewPeer{name: name.clone(),stream:Arc::clone(&stream)}).await// 3.unwrap();let(peer_sender, peer_receiver) = channel::unbounded::<String>();    broker.send(ClientEvent::NewPeer{            client_id,sender: peer_sender,}).await.unwrap();loop{select!{            line = lines.next().fuse() =>{let line = line?;let(dest, msg) =match line.find(':'){None =>continue,Some(idx) =>(&line[..idx], line[idx +1 ..].trim()),};let dest:Vec<String> = dest.split(',').map(|name| name.trim().to_string()).collect();let msg:String = msg.to_string();                broker.send(Event::Message{// 4                    from: name.clone(),                    to: dest,                    msg,}).await.unwrap();},            message = peer_receiver.next().fuse() =>match message{None =>{break;}Some(message) =>{                    socket_writer.send(msg.as_bytes()).await?;}}}}    broker.send(ClientEvent::PeerGone{ client_id}).await.unwrap();Ok(())}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp