简体   繁体   English

与TIdTCPServer同时发送和接收

[英]Send and receive simultaneously with TIdTCPServer

It is always advised to perform all the sending / receiving tasks in OnExecute event handler of TIdTCPServer , but I do not understand following: 它始终是建议执行所有发送/接收中的任务OnExecute的事件处理程序TIdTCPServer ,但我不明白以下几点:

How to wait for a specific sequence on input and at the same time send some data to the same client? 如何等待输入的特定顺序,同时将一些数据发送到同一客户端? I need not a command-response sequence, but I need to: 我不需要命令响应序列,但是我需要:

  • send live data constantly 不断发送实时数据
  • while receiving edited data back 同时接收回已编辑的数据
  • and receiving commands and provide responses for them. 并接收命令并为其提供响应。

For example, if we are waiting for CR-LF: 例如,如果我们正在等待CR-LF:

procedure TSocketServer._serverExecute(AContext: TIdContext);
var
  msg: string;
begin
  msg := AContext.Connection.IOHandler.ReadLn();
  //Here we are only if CRLF was detected.
  //How to send while we are waiting?
  _log(msg);
end;

It is important that when sending unsolicited data and response data using the same connection, don't overlap the outgoing messages, or else you will corrupt your protocol. 重要的是,当使用同一连接发送未经请求的数据和响应数据时,请勿重叠传出的消息,否则会破坏协议。 It is best to have only 1 thread do all of the sending so that one message gets sent in full before another message is sent. 最好只有一个线程来完成所有发送,以便在发送另一条消息之前,先完整发送一条消息。 Just make sure you design your protocol to allow unsolicited data to be sent after the client sends a command and before it receives a response. 只要确保您设计了协议,就可以在客户端发送命令之后到收到响应之前发送未经请求的数据。 Each message should describe what kind of message it is, in such a way that the client can detect a response and match it to an earlier command, while handling unsolicited data as-is. 每个消息都应描述消息的类型,以便客户端可以检测到响应并将其与更早的命令进行匹配,同时按原样处理未经请求的数据。

There is a few different ways you can handle the sending: 您可以通过几种不同的方式来处理发送:

  • use separate threads for reading and sending. 使用单独的线程进行读取和发送。 For instance, have the OnExecute thread handle all of the reading, and use another worker thread to handle all of the sending. 例如,让OnExecute线程处理所有读取,并使用另一个工作线程处理所有发送。 If OnExecute receives an inbound command that needs to send a response, pass the response data to the sending thread (in a thread-safe manner) so it can send the response when safe to do so in between unsolicited messages. 如果OnExecute接收到需要发送响应的入站命令,则将响应数据(以线程安全的方式)传递给发送线程,以便在安全的情况下可以在未经请求的消息之间发送响应。

  • have the OnExecute thread handle both reading and sending. OnExecute线程处理读取和发送。 Continuously send outgoing unsolicited data as needed, and periodically check for inbound data using the IOHandler.InputBufferIsEmpty() and IOHandler.CheckForSourceOnData() methods to detect when an inbound message needs to be read. 根据需要连续发送未经请求的传出数据,并使用IOHandler.InputBufferIsEmpty()IOHandler.CheckForSourceOnData()方法定期检查入站数据,以检测何时需要读取入站消息。

  • otherwise, like Jerry Dodge suggested in comments, just use separate connections, one for command-response data, and one for unsolicited data. 否则,就像杰里·道奇(Jerry Dodge)在评论中建议的那样,只需使用单独的连接,一个用于命令响应数据,另一个用于非请求数据。

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

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