Created
April 13, 2023 11:32
-
-
Save omonien/4f1fb4897651038809e8c83d65742bdf to your computer and use it in GitHub Desktop.
Indy TidTCPServer OnExecute sample that shows the idea how to process an incoming XML file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
procedure TFormMain.ProcessXml(const AXml: string); | |
begin | |
var | |
LXmlDoc := TXMLDocument.Create(nil); | |
try | |
LXmlDoc.DOMVendor := DOMVendors.Find(OmniXML4Factory.Description); | |
LXmlDoc.LoadFromXML(AXml); | |
// if AXml is invalid, then an exception will be thrown | |
// Do further processing here | |
finally | |
FreeAndNil(LXmlDoc); | |
end; | |
end; | |
procedure TFormMain.TCPServerExecute(AContext: TIdContext); | |
begin | |
var | |
LXML := AContext.Connection.IOHandler.Readln; | |
try | |
ProcessXml(LXML); | |
AContext.Connection.IOHandler.WriteLn('ok'); | |
except | |
on e: Exception do | |
begin | |
AContext.Connection.IOHandler.WriteLn('Error: No valid XML received - disconnecting!'); | |
end; | |
end; | |
// Processing done | |
// Disconnect the client, and try to notify it. | |
// If you just "exit" here, then the connection will be keept - which is useful only if you | |
// want to send some feedback to the client at a later point in time, or if you expect the | |
// client to re-use the existing connection for further commands. | |
// This will disconnect us, but there is no way to tell if the client recognizes the disconnect. | |
AContext.Connection.Disconnect; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment