简体   繁体   English

在Delphi Xe3中执行Indy TCP Server的最佳实践处理

[英]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: 当从Indy TCPServers执行方法接收数据时,我通常通过执行以下操作来读取数据:

AContext.Connection.IOHandler.ReadLn;

and then process the data in the execute method. 然后在execute方法中处理数据。 Most of the data that comes through are small JSon strings. 通过的大多数数据都是小的JSon字符串。

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? 将传入数据添加到TidContext类并使用某些工作线程处理它是一个好主意吗? Any thoughts or code samples would be appreciated. 任何想法或代码示例将不胜感激。 I use Indy 10 and Delphi XE3 我使用Indy 10和Delphi XE3

The OnExecute event is already triggered in a worker thread, so it doesn't matter how long it takes to receive the data. OnExecute事件已在工作线程中触发,因此接收数据所需的时间无关紧要。 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). 只要数据的末尾只有1 (CR)LFReadLn()就不会在乎字符串的实际长度(取决于IOHandler的MaxLineActionMaxLineLength属性,可以根据需要进行调整)。 However, if the data has more than 1 (CR)LF in it, then you will have to either: 但是,如果数据中的数据超过1 (CR)LF ,则您必须:

  1. transmit the string length before transmitting the actual string, then use ReadLongInt() and ReadString() instead of ReadLn() in the receiving code. 在发送实际字符串之前先发送字符串长度,然后在接收代码中使用ReadLongInt()ReadString()代替ReadLn()

  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. 最后使用与(CR)LF不同的定界符终止字符串,然后将该定界符传递给ReadLn()以便它知道何时停止读取。

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. 这将使HTTP服务器线程尽快可用于下一个请求。

It also allows to do the data processing by multiple worker servers, with load balancing. 它还允许在负载平衡的情况下由多个工作服务器进行数据处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM