user3047167
user3047167

Reputation: 55

Best pratice ro handle Indy TCP Server execute in Delphi Xe3

When receiving data from a indy TCPServers execute method i generally handle it by reading the data by doing the following:

AContext.Connection.IOHandler.ReadLn;

and then process the data in the execute method. Most of the data that comes through are small JSon strings.

In the future i will need to handle larger data chunks and was wondering what the best pratice is to do so?

Is it a good idea to add the incoming data to a TidContext class and process it using some worker thread? Any thoughts or code samples would be appreciated. I use Indy 10 and Delphi XE3

Upvotes: 0

Views: 1187

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597036

The OnExecute event is already triggered in a worker thread, so it doesn't matter how long it takes to receive the data. As long at the data only has 1 (CR)LF at the end of it, ReadLn() will not care how long the string actually is (subject to the IOHandler's MaxLineAction and MaxLineLength properties, which you can tweak if needed). However, if the data has more than 1 (CR)LF in it, then you will have to either:

  1. transmit the string length before transmitting the actual string, then use ReadLongInt() and ReadString() instead of ReadLn() in the receiving code.

  2. terminate the string with a different delimiter than (CR)LF at the end, and then pass that delimiter to ReadLn() so it knows when to stop reading.

Upvotes: 1

mjn
mjn

Reputation: 36654

If the server has to receive incoming requests at a guaranteed rate, without blocking the consumers by slow request processing, saving the big data chunks to a datastore (file, database) for later processing could be a solution.

This would make the HTTP server thread available for the next request as soon as possible.

It also allows to do the data processing by multiple worker servers, with load balancing.

Upvotes: 0

Related Questions