Is correct using "io.readpartial" instead of "io.read"?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I'm doing a SIP server (listening in TCP) so I receive messages as the=
=20
following:

=2D---------------------------------------------------------
INVITE sip:user@ip SIP/2.0 CRLF
From: xxxxx CRLF
To: xxxxx CRLF
Content-Lenght: 235 CRLF
CRLF
body body body body body body body body body body body body body body body
body body body body body body body body body body body body body body body
body body body body body body=20
body body body body body body body body body

body body body body body body body body body body body body body body bod=
y=20
body body
=2D---------------------------------------------------------

I receive and parse correctly the headers and when I detect an empty line I=
=20
must read the number of bytes indicated in "Content-Length" header (235 the=
=20
example). This is like SMTP or HTTP.

I wonder which method is better in order to receive the body: io.read or=20
io.readlines.

io.read blocks after read the 235 bytes while io.readparti

**** io.read
Reads at most length bytes from the I/O stream (blocks).

**** io.readpartial
Reads at most maxlen bytes from the I/O stream. It blocks only if ios has n=
o=20
data immediately available. It doesn=E2=80=98t block if some data available.

The code I use to read the body is:

begin
request.body =3D io.readpartial(request.hdr_content_length)
rescue EOFError
return
end


=46or now it works perfectly in both ways but there is an obvious differenc=
e:

With io.readpartial my code reads all the bytes indicated in Content-Length=
=20
(235) just if they come "together", but if they come "discontinnuated"=20
io.readpartial return with the read bytes (in the example it can be less th=
an=20
235 bytes). Note that I don't use a loop for io.readpartial.

With io.read the code blocks until read 235 bytes. If the data comes "not=20
together" it waits for the 235 bytes.


=46or now io.readpartial works perfectly and I like it since there could be=
a=20
SIP error (a client sends a body with less bytes than "Content-Length" says=
)=20
and in this way I can detect it inmediately.
But I just do my test in localhost and so, not in real network which can be=
=20
congested, etc.


Any opinion with this please? Thanks a lot.




=2D-=20
I=C3=B1aki Baz Castillo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top