🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #254

Re: Indy10 usage

#254
From: Remy Lebeau (TeamB)
Date: Thu, 06 Mar 2008 00:36
65 lines
4678 bytes
"Mike Ruskai" <BUTthannydI@DONTearthlinkLIKE.netSPAM> wrote in message 
news:c65us3ljh7nr89gl6i6sojfkumfuup7qrt@4ax.com...

> Sometimes I forget I have code available to check.  I checked
> the code, and I see exactly how this is happening.
>
> Here's the relevant bit in IdIOHandler.pas (lines numbered for reference):
<snip>

Your analysis, although technically accurate, is misplaced in this 
situation.

ReadFromSource() retreives whatever data is currently pending in the socket 
at that moment.  ReadFromSource() sets the ClosedGracefully property to True 
only if the socket API recv() function returns 0 bytes after Readable() had 
returned True.  If even 1 byte is read, the ClosedGracefully property is not 
set to True.  CheckForDisconnect() can only raise the exception if the 
IOHandler's ClosedGracefully property has been set to True beforehand.

ReadBytes() does not call ReadFromSource() if there are already enough bytes 
stored in the InputBuffer to satisfy the caller.  It only goes to the socket 
when it absolutely needs to.  ReadBytes() is required to return all 
requested bytes, or fail otherwise.  If the socket reports a disconnect 
before all of the necessary bytes have been buffered, an exception is 
raised.  That is by design.  Changing ReadBytes() to pass False instead of 
True to the AIgnoreBuffer of CheckForDisconnect() would break that design, 
which in turn will break every method that relies on that design.  What you 
have described so far does NOT warrant such a change.

Think about it.  The server sends 1024 bytes and then disconnects.  Your 
client calls ReadBytes(..., 1024).  ReadBytes(), seeing that the InputBuffer 
does not hold enough bytes yet, calls ReadFromSource().  1 of 3 things will 
happen:

1) at least 1 byte will be read from the socket.  All of the received bytes 
will be stored in the InputBuffer, ReadFromSource() will exit without 
setting the ClosedGracefully property to True, CheckForDisconnect() will 
exit without error, and ReadBytes() will check the InputBuffer again.  If 
needed, the cycle continues for another iteration.

2) no bytes are received.  This means the socket reported a disconnect 
before all of the expected bytes could be read.  ReadFromSource() will set 
the ClosedGracefully property to True and exit.  CheckForDisconnect() will 
see that the ClosedGracefully property is True and raise an exception. 
ReadBytes() was expecting 1024 bytes, but only received (1024-X) bytes.  The 
socket has already discarded the remaining bytes.  There is nothing Indy can 
do about that.  Raising an exception is the proper behavior.

3) the socket reports an actual error.  An exception will be raised 
accordingly.

So, given the example you have shown earlier, the only way an 
EIdConnClosedGracefully exception can be raised is if the socket itself is 
actually reporting a disconnect before all of the expected bytes could be 
read from it.  If you are seeing ReadBytes() raise an exception too early in 
your code, then that is the only thing that can be happening.  The only 
reason to catch the exception and call ExtractToBytes() manually would be if 
you don't care that a disconnect was reported prematurely and want to 
continue processing whatever data was successfully buffered beforehand. 
Which, in most cases, will fail anyway since the data is incomplete and will 
likely break whatever protocol you are implementing.


Gambit 
Technical Details
Message-ID: <47cf3c78@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <ghsqs35tipni65rjlntjpl7hgi6f1bv1g8@4ax.com> <47cd89bb$1@newsgroups.borland.com> <tefrs3lpnfkk0arr2qa3vospikaj3j491h@4ax.com> <47cdc920@newsgroups.borland.com> <tu0ss39uaq8j35if50r88eflpsf5nvs431@4ax.com> <ch7ss31acm13alsqj2s3utjhnbusnks1a9@4ax.com> <47ce339a$1@newsgroups.borland.com> <c65us3ljh7nr89gl6i6sojfkumfuup7qrt@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,ea086d952e8cb041
X-Google-Attributes: gid11508e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <ghsqs35tipni65rjlntjpl7hgi6f1bv1g8@4ax.com> <47cd89bb$1@newsgroups.borland.com> <tefrs3lpnfkk0arr2qa3vospikaj3j491h@4ax.com> <47cdc920@newsgroups.borland.com> <tu0ss39uaq8j35if50r88eflpsf5nvs431@4ax.com> <ch7ss31acm13alsqj2s3utjhnbusnks1a9@4ax.com> <47ce339a$1@newsgroups.borland.com> <c65us3ljh7nr89gl6i6sojfkumfuup7qrt@4ax.com>
Subject: Re: Indy10 usage
Date: Wed, 5 Mar 2008 16:36:20 -0800
Lines: 67
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 207.178.157.90
Message-ID: <47cf3c78@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1204763768 207.178.157.90 (5 Mar 2008 16:36:08 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail