The thread itself looks like this
while not Terminated do begin
//do something with TXXX object that affects X, Y, Z
vTerminate := X - Y < Z;
if vTerminate then begin
PostLog(0, 'Thread is about to be terminated.');
UpdateThread;
self.Terminate;
end
else begin
PostAlive;
Sleep(FSystemConfig.ThreadSleep)
end
end //while not terminated
Now the TXXX creates and uses TIdHTTP like below
procedure TXXX.CreateHTTP;
begin
FHTTP := TIdHTTP.Create(NIL);
FHTTP.Request.Connection := 'keep-alive';
FHTTP.IOHandler := CreateSSLIOHandler;
FHTTP.ConnectTimeout := 10000;
FHTTP.ReadTimeout := 30000;
end;
// -----------
function TXXX.CreateSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
result := TIdSSLIOHandlerSocketOpenSSL.Create;
result.SSLOptions.Method := sslvTLSv1_2;
result.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
result.SSLOptions.Mode := sslmClient;
result.SSLOptions.VerifyMode := [];
result.SSLOptions.VerifyDepth := 0;
result.OnStatusInfoEx := self.OnStatusInfoEx;
end;
procedure TXXX.OnStatusInfoEx(ASender: TObject; const AsslSocket: PSSL; const AWhere, Aret: TIdC_INT; const AType, AMsg: String);
begin
SSL_set_tlsext_host_name(AsslSocket, FHTTP.Request.Host)
end;
And the actual HTTP processing is the following
vJsonToPost := {....some json goes here}
PostLog(0, vJsonToPost);
vJsonToSend := TStringStream.Create(vJsonToPost);
try
FHTTP.Request.ContentType := 'application/json';
FHTTP.Request.ContentEncoding := 'utf-8';
while TRUE do begin
try
vId := '';
vResult := FHTTP.Post(vUrl, vJsonToSend);
if vResult > '' then begin
///working with vResult
end;
except
on E: Exception do begin
PostLog(1, 'Exception in SubmitIOCBuyOrder: ' + E.Message);
//FGotException := True;
PostLog(100, FHTTP.Response.RawHeaders.Text);
//if Read time out
//if FHTTP.Response.RawHeaders.Text = '' then
if E.Message = 'Read timed out.' then
RecreateHTTP;
end
end;
if vClientOrderId > '' then
if CheckIfOrderIsThere(vClientOrderId) then begin
PostLog(0, 'New order is going to be posted with status: ' + upperCase(vStatus));
PostOrder;
break
end;
end //while TRUE
finally
vJsonToSend.Free
end;
CheckIfOrderIsThere does FHTTP.Get, if that matters.