What is the proper way to handle TWSocket.DataAvailable?
I found this way in demo:
function TBinaryWSocket.TriggerDataAvailable(ErrCode: Word): Boolean;
var
Rcvd: Integer;
begin
Result := TRUE;
Rcvd := Receive(@FRcvBuf[FWriteOffs], (SizeOf(FRcvBuf) - FWriteOffs));
if Rcvd > 0 then
begin
Inc(FRemainingBytes, Rcvd);
Inc(FWriteOffs, Rcvd);
end;
if FRemainingBytes > 0 then
ParseReceiveBuffer;
end;
while I previously used such way:
procedure SocketDataAvailable(Sender: TObject; ErrCode: Word);
begin
if TWSocket(Sender).RcvdCount = 0 then Exit;
if ErrCode <> 0 then ; // do something
if FState in [csConnected, csRequesting, csReceivingInfo, csReceivingData]
then
if FEstablished
then SetState(csReceivingData)
else SetState(csReceivingInfo)
else try FSocket.Receive(@ShitBuf, SizeOf(ShitBuf)) except end;
end;
end;
to check if there are data really available and to read everything from the
socket in case of closing state or something else.
Today I decided to get rid of RcvdCount checking like this:
procedure DoDataAvailable(Sender: TObject; ErrCode: Word);
begin
if ErrCode <> 0 then
Error(GetWinsockErr(ErrCode));
res := TWSocket(Sender).Receive(FCurrPtr, FDataNeeded);
if res < 0 then
begin
Log('Error reading data: '+GetWinsockErr(WSocket_WSAGetLastError));
Exit;
end;
Dec(FDataNeeded, res);
Inc(FCurrPtr, res);
if FDataNeeded > 0 then Exit;
....
end
but I found that Receive returns -1 with WSAEWOULDBLOCK error when
DoDataAvailable is called with RcvdCount = 0.
So, the first question is: is it necessary to call method when no data
available in fact, and the second: how to implement DataAvailable code in
proper way? Is it possible for async Receive to returb some error besides
WSAEWOULDBLOCK (i.e. do I need to check the error and report about it)?
Thanks in advance.
--
Anton
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be