On Fri, 25 Apr 2014, silvioprog wrote:
2014-04-25 3:39 GMT-03:00 Michael Van Canneyt <[email protected]>:
[...]
Damn windows returning 0 on read... Can you please test with the
following change:
Procedure FillBuffer;
Var
R : Integer;
begin
SetLength(FBuffer,ReadBufLen);
Repeat
r:=FSocket.Read(FBuffer[1],ReadBufLen);
If r<0 then
Raise EHTTPServer.Create(SErrReadingSocket);
if R=0 then
Sleep(10);
Until (R<>0);
if (R<ReadBuflen) then
SetLength(FBuffer,r);
end;
After apply this change, now when read returns 0, it freezes the app., because the
"sleep(10)" command is infinitely called. :/
OK, what we can do is add a timeout:
Procedure FillBuffer;
Const
MaxReadRetries = 150;
Var
R,C : Integer;
begin
C:=0;
SetLength(FBuffer,ReadBufLen);
Repeat
r:=FSocket.Read(FBuffer[1],ReadBufLen);
If r<0 then
Raise EHTTPServer.Create(SErrReadingSocket);
if R=0 then
begin
Sleep(10);
Inc(C);
end;
Until (R<>0) or (C>MaxReadRetries);
if (R<ReadBuflen) then
SetLength(FBuffer,r);
end;
It will then try 150 times, and then error out.
BUT, reading this:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121%28v=vs.85%29.aspx
"If the connection has been gracefully closed, the return value is zero."
So, this means then thay the original implementation is correct.
You just need to catch the error using the OnRequestError handler...
Michael.
_______________________________________________
fpc-pascal maillist - [email protected]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal