CurrentReadBuffer 读取缓冲区目前所有数据
OpenWriteBuffer 强制发送缓冲区目前所有数据,并清空缓冲队列
writebuffer true和false同样是指是否立即发送缓冲区数据。
//对writebuffer我也不太理解,代码贴上来大家瞧瞧
procedure TIdTCPConnection.WriteBuffer(const ABuffer; AByteCount: Integer;
const AWriteNow: boolean = false);
var
LBuffer: TIdSimpleBuffer;
nPos, nByteCount: Integer;
begin
if (AByteCount > 0) and (@ABuffer <> nil) then begin
// Check if disconnected
CheckForDisconnect(True, True);
if connected then begin
if (FWriteBuffer = nil) or AWriteNow then begin
LBuffer := TIdSimpleBuffer.Create; try
LBuffer.WriteBuffer(ABuffer, AByteCount);
if Assigned(Intercept) then begin
LBuffer.Position := 0;
Intercept.Send(LBuffer);
AByteCount := LBuffer.Size;
end;
nPos := 1;
repeat
nByteCount := IOHandler.Send(PChar(LBuffer.Memory)[nPos - 1], LBuffer.Size - nPos + 1);
// Write always does someting - never retuns 0
// TODO - Have a AntiFreeze param which allows the send to be split up so that process
// can be called more. Maybe a prop of the connection, MaxSendSize?
TIdAntiFreezeBase.DoProcess(False);
FClosedGracefully := nByteCount = 0;
// Check if other side disconnected
CheckForDisconnect;
// Check to see if the error signifies disconnection
if GStack.CheckForSocketError(nByteCount
, [ID_WSAESHUTDOWN, Id_WSAECONNABORTED, Id_WSAECONNRESET]) then begin
DisconnectSocket;
GStack.RaiseSocketError(GStack.WSGetLastError);
end;
DoWork(wmWrite, nByteCount);
nPos := nPos + nByteCount;
until nPos > AByteCount;
finally FreeAndNil(LBuffer); end;
// Write Buffering is enabled
end else begin
FWriteBuffer.WriteBuffer(ABuffer, AByteCount);
if (FWriteBuffer.Size >= FWriteBufferThreshhold) and (FWriteBufferThreshhold > 0) then begin
// TODO: Maybe? instead of flushing - Write until buffer is smaller than Threshold.
// That is do at least one physical send.
FlushWriteBuffer(FWriteBufferThreshhold);
end;
end;
end
else
begin
Raise EIdNotConnected.Create(RSNotConnected);
end;
end;
end;