Skip to content

Fix handling non trivial stream reads in async framer - #32

Open
mmcomando wants to merge 1 commit into
ninjasource:masterfrom
mmcomando:fix_async_framer
Open

Fix handling non trivial stream reads in async framer#32
mmcomando wants to merge 1 commit into
ninjasource:masterfrom
mmcomando:fix_async_framer

Conversation

@mmcomando

Copy link
Copy Markdown

This fixes cases:

  • Stream returns only part of a websocket frame
  • Stream returns only part of websocket frame header
  • There is no way to tell if framer can progress without reading from stream

In my embedded app I have found out that using async framer doesn't work, when a lot of data is sent.
Async framer works only if stream read returns only one complete frame.
If stream returns to little or too much data then I had problems with framer.

There is also a problem when stream returns only part of websocket frame header, in that case framer can't progress further.

I don't have much experience with async rust streams so maybe this can be done better.

Code in my app which works after this patch:

async fn handle_websocket_reads_and_writes<'a>(
    stream: &mut TcpSocketStream<'a>,
    framer: &mut Framer<embedded_websocket::EmptyRng, embedded_websocket::Server>,
    frame_buf: &mut [u8],
) -> Result<(), ()> {
    loop {
        match select(
            // wait_read_ready should be cancel safe, framer is not
            stream.socket.wait_read_ready(),
            SEND_MESSAGES.receive(),
        )
        .await
        {
            Either::First(_) => {
                loop {
                    let read_result = framer.read(stream, frame_buf).await;
                    handle_websocket_incoming_msg(stream, framer, read_result).await?;

                    // Stream socket might be finished but framer might have data left in it.
                    // Read from framer until it doesn't have buffered data.
                    if !framer.read_ready() {
                        break;
                    }
                }
                return Ok(());
            }
            Either::Second(msg) => {
                send_websocket_message(stream, framer, &msg).await?;
            }
        }
    }
}

This fixes cases:
- Stream returns only part of a websocket frame
- Stream returns only part of websocket frame header
- There is no way to tell if framer can progress without reading from
  stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant