Git Product home page Git Product logo

Comments (3)

Thomasdezeeuw avatar Thomasdezeeuw commented on September 28, 2024

This is tcp-server example, would_block maybe occur after OK(n),and previously read bytes will be dropped due to would_block break. Is it possible to continue? Is would_block final state of the tcpstream read operation?

I'm not entirely sure what you mean with your first question. But to answer the second one, no a WouldBlock error is not the final state of a TcpStream, it simply means that currently all bytes are read. So for example the peer of the connection send an entire request and is now waiting for a response. If a read call returns Ok(0) it means all bytes are read and the peer is not going to send more.

from mio.

barbedarhl avatar barbedarhl commented on September 28, 2024

I'm not entirely sure what you mean with your first question. But to answer the second one, no a WouldBlock error is not the final state of a TcpStream, it simply means that currently all bytes are read. So for example the peer of the connection send an entire request and is now waiting for a response. If a read call returns Ok(0) it means all bytes are read and the peer is not going to send more.

I have a very simple self-defined protocol just like this:
|magic_code|body_length|body|
Client uses body_length 4bytes to tell the server how many bytes need to read. Some very rarely would_block error occurs while server have not read the entire body bytes, for example, body_length is 4000, but server only read 1500 bytes from tcpStream and meanwhile would_block error returned by tcpStream.read().

Is it supposed that client did not write the entire protocol body bytes?

from mio.

Thomasdezeeuw avatar Thomasdezeeuw commented on September 28, 2024

I have a very simple self-defined protocol just like this: |magic_code|body_length|body| Client uses body_length 4bytes to tell the server how many bytes need to read. Some very rarely would_block error occurs while server have not read the entire body bytes, for example, body_length is 4000, but server only read 1500 bytes from tcpStream and meanwhile would_block error returned by tcpStream.read().

Is it supposed that client did not write the entire protocol body bytes?

It is always possible that not the entire request can be read in a single read call. It may be that the user didn't send the entire request, but it may also be that kernel split up the buffers and only returned one. You should always be ready for short reads and WouldBlock "errors", these are always possible. The solution is pretty simple though, put a (non-blocking) loop around the read to ensure your the entire request (do put in a limit! Don't want to read 1TB of data into your memory).

from mio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.